From 15d58d5c15ffd7ca57d86aa5429e9b91a4ab35aa Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 5 Nov 2026 10:32:04 +0000 Subject: [PATCH] test(accounting): cover tenant e-invoice opt-in --- .../tests/outgoingEInvoiceSettings.test.ts | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 backend/tests/outgoingEInvoiceSettings.test.ts diff --git a/backend/tests/outgoingEInvoiceSettings.test.ts b/backend/tests/outgoingEInvoiceSettings.test.ts new file mode 100644 index 00000000..055d6c5a --- /dev/null +++ b/backend/tests/outgoingEInvoiceSettings.test.ts @@ -0,0 +1,56 @@ +import assert from "node:assert/strict" +import test from "node:test" +import { tenants } from "../db/schema" +import { PgDialect } from "drizzle-orm/pg-core" +import { assertOutgoingEInvoiceRequirements, attachZugferdToPdf, checkOutgoingEInvoice, EInvoiceRequirementsError } from "../src/modules/einvoice/outgoingZugferd.service" + +const invoice = { type: "invoices", documentNumber: "RE-1", payment_type: "sepa", rows: [] } +function serverFor(businessInfo: any) { + const tenantIds: number[] = [] + let otherReads = 0 + const server = { db: { select: () => ({ from: (table: any) => { + const rows = table === tenants ? [{ businessInfo }] : [] + if (table !== tenants) otherReads++ + const query: any = { + where: (condition: any) => { + if (table === tenants) tenantIds.push(new PgDialect().sqlToQuery(condition).params[0] as number) + return query + }, + limit: async () => rows, + then: (resolve: any, reject: any) => Promise.resolve(rows).then(resolve, reject), + } + return query + } }) } } as any + return { server, tenantIds, otherReads: () => otherReads } +} + +for (const businessInfo of [undefined, {}, { outgoingEInvoiceEnabled: false }, { outgoingEInvoiceEnabled: "true" }]) { + test(`disabled/legacy tenant skips e-invoice requirements: ${JSON.stringify(businessInfo)}`, async () => { + const { server, tenantIds, otherReads } = serverFor(businessInfo) + assert.deepEqual(await checkOutgoingEInvoice(server, 8, invoice), []) + await assertOutgoingEInvoiceRequirements(server, 8, invoice) + const pdf = Buffer.from("original PDF bytes") + const result = await attachZugferdToPdf(server, 8, invoice, pdf) + assert.equal(result.pdf, pdf) + assert.equal(result.einvoice, false) + assert.deepEqual(result.issues, []) + assert.deepEqual(tenantIds, [8, 8, 8]) + assert.equal(otherReads(), 0) + }) +} + +test("enabled tenant still blocks incomplete invoices before finalization and embedding", async () => { + const { server, tenantIds } = serverFor({ outgoingEInvoiceEnabled: true }) + const issues = await checkOutgoingEInvoice(server, 9, invoice) + assert.ok(issues.some(issue => issue.type === "breaking")) + await assert.rejects(assertOutgoingEInvoiceRequirements(server, 9, invoice), EInvoiceRequirementsError) + await assert.rejects(attachZugferdToPdf(server, 9, invoice, Buffer.from("pdf")), EInvoiceRequirementsError) + assert.ok(tenantIds.every(id => id === 9)) +}) + +test("document payload cannot disable the tenant setting", async () => { + const { server } = serverFor({ outgoingEInvoiceEnabled: true }) + await assert.rejects(assertOutgoingEInvoiceRequirements(server, 8, { + ...invoice, businessInfo: { outgoingEInvoiceEnabled: false }, outgoingEInvoiceEnabled: false, + }), EInvoiceRequirementsError) +})