import { describe, it, expect } from 'vitest' import { openDb } from '../src/db' import { writeAudit, listAudit } from '../src/audit' describe('hq db', () => { it('creates every HQ-1 table', () => { const db = openDb(':memory:') const names = (db.prepare(`SELECT name FROM sqlite_master WHERE type='table'`).all() as { name: string }[]).map(r => r.name) for (const t of ['staff_user','session','client','module','module_price_book','client_module', 'tax_class','doc_series','document','payment','payment_allocation','document_event', 'email_account','email_log','setting','audit_log','stg_client','stg_invoice']) expect(names, `missing table ${t}`).toContain(t) }) it('audit writes and lists newest-first', () => { const db = openDb(':memory:') writeAudit(db, 'u1', 'create', 'client', 'c1', undefined, { name: 'Acme' }) writeAudit(db, 'u1', 'update', 'client', 'c1', { name: 'Acme' }, { name: 'Acme Ltd' }) const rows = listAudit(db) expect(rows).toHaveLength(2) expect(rows[0]!.action).toBe('update') expect(JSON.parse(rows[0]!.after_json!)).toEqual({ name: 'Acme Ltd' }) }) })