import { describe, it, expect } from 'vitest' import { openDb } from '../src/db' import { seedIfEmpty } from '../src/seed' describe('hq2 schema', () => { it('creates every HQ-2 table', async () => { const db = openDb(':memory:') const names = (await db.all<{ name: string }>(`SELECT name FROM sqlite_master WHERE type='table'`)).map((r) => r.name) for (const t of ['recurring_plan', 'amc_contract', 'interaction', 'interaction_type', 'reminder']) expect(names, `missing table ${t}`).toContain(t) }) it('adds the bounced column to email_log', async () => { const db = openDb(':memory:') const cols = (await db.all<{ name: string }>(`PRAGMA table_info(email_log)`)).map((c) => c.name) expect(cols).toContain('bounced') }) it('enforces the reminder idempotency key', async () => { const db = openDb(':memory:') const ins = `INSERT OR IGNORE INTO reminder (id, rule_kind, subject_id, due_period, client_id, status, policy_applied, created_at) VALUES (?, 'invoice_overdue', 's1', '2026-07', 'c1', 'queued', 'manual', '2026-07-10T00:00:00Z')` expect((await db.run(ins, 'r1')).changes).toBe(1) expect((await db.run(ins, 'r2')).changes).toBe(0) // same (rule_kind, subject_id, due_period) → ignored }) it('seeds interaction types, the AMC module and reminder settings', async () => { const db = openDb(':memory:'); await seedIfEmpty(db) const types = (await db.all<{ code: string }>(`SELECT code FROM interaction_type`)).map((r) => r.code) expect(types).toEqual(expect.arrayContaining(['call', 'site_visit', 'training', 'complaint'])) expect(await db.get(`SELECT COUNT(*) AS n FROM module WHERE code='AMC'`)).toMatchObject({ n: 1 }) const s = await db.get<{ value: string }>(`SELECT value FROM setting WHERE key='reminders.overdue_days'`) expect(s?.value).toBe('7') }) })