import { describe, expect, test } from "bun:test"; import type { InvoiceTemplateConfig } from "../db/schema"; import type { InvoicePDFInvoiceData } from "./app-renderer"; import { renderInvoicePDF } from "./app-renderer"; const invoice: InvoicePDFInvoiceData = { invoiceNumber: "INV-1", issueDate: new Date("2026-06-01"), dueDate: null, subtotal: 10000, taxRate: 0, taxAmount: 0, total: 10000, currency: "USD", lineItems: [{ description: "Work", quantity: 1, rate: 10000, amount: 10000 }], }; const baseTemplate: InvoiceTemplateConfig = { companyName: "Co", primaryColor: "#000000", accentColor: "#22d3ee", textColor: "#374151", headerStyle: "classic", showLogo: false, showCompanyDetails: true, showHours: true, showRate: true, showProjectBreakdown: true, }; function pdfIsValid(bytes: Uint8Array): boolean { return ( bytes.length > 0 && Buffer.from(bytes.subarray(0, 5)).toString() === "%PDF-" ); } describe("renderInvoicePDF — structured payment methods", () => { test("renders a valid PDF when structured payment methods are present", async () => { const bytes = await renderInvoicePDF({ invoice, template: { ...baseTemplate, paymentMethods: [ { type: "wire", label: "Bank Wire", details: { account: "1234567890" }, }, ], }, }); expect(pdfIsValid(bytes)).toBe(true); }); test("payment methods add rendered content beyond the base invoice", async () => { const without = await renderInvoicePDF({ invoice, template: baseTemplate }); const withPM = await renderInvoicePDF({ invoice, template: { ...baseTemplate, paymentMethods: [ { type: "wire", label: "Bank Wire", details: { account: "1234567890", routing: "021000021" }, }, { type: "usdc", label: "USDC", details: { address: "0xabc123" } }, ], }, }); // Drawing the structured methods deterministically grows the content stream. expect(withPM.length).toBeGreaterThan(without.length + 100); }); test("more payment methods render more content (each method is drawn)", async () => { const one = await renderInvoicePDF({ invoice, template: { ...baseTemplate, paymentMethods: [ { type: "wire", label: "Bank Wire", details: { account: "1" } }, ], }, }); const three = await renderInvoicePDF({ invoice, template: { ...baseTemplate, paymentMethods: [ { type: "wire", label: "Bank Wire", details: { account: "1" } }, { type: "usdc", label: "USDC", details: { address: "0xabc" } }, { type: "paypal", label: "PayPal", details: { email: "a@b.co" } }, ], }, }); expect(three.length).toBeGreaterThan(one.length); }); test("empty payment methods array does not add a footer section", async () => { const without = await renderInvoicePDF({ invoice, template: baseTemplate }); const withEmpty = await renderInvoicePDF({ invoice, template: { ...baseTemplate, paymentMethods: [] }, }); expect(withEmpty.length).toBe(without.length); }); });