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', () => { 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 ['recurring_plan', 'amc_contract', 'interaction', 'interaction_type', 'reminder']) expect(names, `missing table ${t}`).toContain(t) }) it('adds the bounced column to email_log', () => { const db = openDb(':memory:') const cols = (db.prepare(`PRAGMA table_info(email_log)`).all() as { name: string }[]).map((c) => c.name) expect(cols).toContain('bounced') }) it('enforces the reminder idempotency key', () => { const db = openDb(':memory:') const ins = db.prepare( `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(ins.run('r1').changes).toBe(1) expect(ins.run('r2').changes).toBe(0) // same (rule_kind, subject_id, due_period) → ignored }) it('seeds interaction types, the AMC module and reminder settings', () => { const db = openDb(':memory:'); seedIfEmpty(db) const types = (db.prepare(`SELECT code FROM interaction_type`).all() as { code: string }[]).map((r) => r.code) expect(types).toEqual(expect.arrayContaining(['call', 'site_visit', 'training', 'complaint'])) expect(db.prepare(`SELECT COUNT(*) AS n FROM module WHERE code='AMC'`).get()).toMatchObject({ n: 1 }) const s = (db.prepare(`SELECT value FROM setting WHERE key='reminders.overdue_days'`).get() as { value: string } | undefined) expect(s?.value).toBe('7') }) })