You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
2.8 KiB
TypeScript
50 lines
2.8 KiB
TypeScript
// apps/hq/test/preview-fidelity.test.ts
|
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
import express from 'express'
|
|
import { openDb } from '../src/db'
|
|
import { seedIfEmpty } from '../src/seed'
|
|
import { createStaff } from '../src/auth'
|
|
import { createClient, getClient } from '../src/repos-clients'
|
|
import { createModule, setPrice } from '../src/repos-modules'
|
|
import { createDraft } from '../src/repos-documents'
|
|
import { documentHtml } from '../src/templates'
|
|
import { apiRouter } from '../src/api'
|
|
|
|
async function companyMap(db: any): Promise<Record<string, string>> {
|
|
const rows = await db.all(`SELECT key, value FROM setting WHERE key LIKE 'company.%'`) as { key: string; value: string }[]
|
|
return Object.fromEntries(rows.map((r) => [r.key, r.value]))
|
|
}
|
|
|
|
async function setup() {
|
|
const db = openDb(':memory:'); await seedIfEmpty(db)
|
|
await createStaff(db, { email: 'owner@test.in', displayName: 'Owner', role: 'owner', password: 'owner-password' })
|
|
const c = await createClient(db, 'u1', { name: 'Acme', code: 'ACME', stateCode: '32', address: 'Kochi', gstin: '32ABCDE1234F1Z9' })
|
|
const m = await createModule(db, 'u1', { code: 'POS', name: 'POS Billing', quoteContent: ['Cloud POS', 'GST filing'] })
|
|
await setPrice(db, 'u1', { moduleId: m.id, kind: 'yearly', pricePaise: 10_000_00, effectiveFrom: '2026-04-01' })
|
|
const app = express(); app.use(express.json()); app.locals['db'] = db; app.use('/api', apiRouter(db))
|
|
const server = app.listen(0)
|
|
const base = `http://localhost:${(server.address() as { port: number }).port}/api`
|
|
return { db, c, m, server, base }
|
|
}
|
|
|
|
describe('preview fidelity (string identity with the PDF path)', () => {
|
|
let ctx: Awaited<ReturnType<typeof setup>>
|
|
beforeAll(async () => { ctx = await setup() })
|
|
afterAll(() => ctx.server.close())
|
|
|
|
it('preview.html === documentHtml the /pdf route would feed renderPdf', async () => {
|
|
const { db, c, m, base } = ctx
|
|
const input = { docType: 'INVOICE', clientId: c.id, terms: 'Net 15',
|
|
lines: [{ moduleId: m.id, qty: 2, kind: 'yearly' }] }
|
|
// The fixture draft: exactly what /documents/:id/pdf renders (un-issued draft, docNo=null).
|
|
const fixture = await createDraft(db, 'u1', input)
|
|
const expected = documentHtml(fixture, (await getClient(db, c.id))!, await companyMap(db))
|
|
|
|
const token = (await (await fetch(`${base}/auth/login`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ email: 'owner@test.in', password: 'owner-password' }) })).json() as any).token
|
|
const res = await fetch(`${base}/documents/preview`, { method: 'POST', headers: { 'content-type': 'application/json', authorization: `Bearer ${token}` }, body: JSON.stringify(input) })
|
|
const out = await res.json() as any
|
|
|
|
expect(out.html).toBe(expected) // one renderer — no drift possible
|
|
})
|
|
})
|