// apps/hq/test/template-render.test.ts import { describe, it, expect } from 'vitest' import { documentHtml } from '../src/templates' const invoice = { id: 'd1', docType: 'INVOICE', docNo: 'INV/26-27-0001', fy: '2026-27', clientId: 'c1', docDate: '2026-07-13', status: 'draft', refDocId: null, taxablePaise: 10_000_00, cgstPaise: 900_00, sgstPaise: 900_00, igstPaise: 0, roundOffPaise: 0, payablePaise: 11_800_00, payload: { lines: [{ itemId: 'm1', name: 'POS', hsn: '998313', qty: 1, unitCode: 'NOS', unitPricePaise: 10_000_00, grossPaise: 10_000_00, discountPaise: 0, taxablePaise: 10_000_00, taxRateBp: 1800, cgstPaise: 900_00, sgstPaise: 900_00, igstPaise: 0, cessPaise: 0, lineTotalPaise: 11_800_00 }], totals: {} }, } as never const client = { id: 'c1', code: 'CL1', name: 'Acme', stateCode: '32', address: 'Kochi', contacts: [], status: 'active', notes: '' } as never describe('template.* rendering (default-fallback)', () => { it('renders built-in defaults when template.* is unset (nothing breaks pre-config)', () => { const html = documentHtml(invoice, client, { 'company.name': 'SiMS' }) expect(html).toContain('TAX INVOICE') // built-in title expect(html).toContain('Authorised Signatory') // built-in signatory label expect(html).not.toContain('class="declaration"') expect(html).not.toContain('class="jurisdiction"') expect(html).not.toContain('class="footer-note"') expect(html).not.toContain('class="logo"') }) it('applies each template.* override where set', () => { const html = documentHtml(invoice, client, { 'company.name': 'SiMS', 'template.title_INVOICE': 'GST INVOICE', 'template.declaration': 'We declare the particulars are true.', 'template.jurisdiction': 'Subject to Kochi jurisdiction', 'template.footer_note': 'Thank you for your business.', 'template.signatory_label': 'Proprietor', 'template.logo': 'data:image/png;base64,AAAABBBB', }) expect(html).toContain('GST INVOICE') expect(html).not.toContain('TAX INVOICE') expect(html).toContain('We declare the particulars are true.') expect(html).toContain('Subject to Kochi jurisdiction') expect(html).toContain('Thank you for your business.') expect(html).toContain('Proprietor') expect(html).not.toContain('Authorised Signatory') expect(html).toContain('data:image/png;base64,AAAABBBB') }) it('template.terms is the editable default; a per-doc terms overrides it', () => { const def = documentHtml(invoice, client, { 'company.name': 'X', 'template.terms': 'Net 30 default' }) expect(def).toContain('Net 30 default') const perDoc = { ...(invoice as object), payload: { ...(invoice as never as { payload: object }).payload, terms: 'Per-doc terms' } } as never const html = documentHtml(perDoc, client, { 'company.name': 'X', 'template.terms': 'Net 30 default' }) expect(html).toContain('Per-doc terms') expect(html).not.toContain('Net 30 default') }) })