/** * Unit Tests — graph-seed-service.js verifyCounts() (T4) * Runs the real seed functions against the actual repo fs (system/agents/, * system/templates/, data/companies/) into a throwaway GRAPH_DB_PATH, then * asserts verifyCounts() reports ok:true against that freshly-seeded data, * and that it actually catches a mismatch when the DB drifts from the fs. */ import { describe, it, expect, beforeAll, afterAll } from 'vitest'; import fs from 'fs'; describe('graph-seed-service verifyCounts', () => { let seedService; let graphDb; const TEST_DB_PATH = '/tmp/test-graph-seed-service.sqlite'; beforeAll(() => { if (fs.existsSync(TEST_DB_PATH)) fs.unlinkSync(TEST_DB_PATH); process.env.GRAPH_DB_PATH = TEST_DB_PATH; delete require.cache[require.resolve('../../services/graph-db')]; delete require.cache[require.resolve('../../services/graph-seed-service')]; graphDb = require('../../services/graph-db'); seedService = require('../../services/graph-seed-service'); }); afterAll(() => { try { fs.unlinkSync(TEST_DB_PATH); } catch {} try { fs.unlinkSync(`${TEST_DB_PATH}-shm`); } catch {} try { fs.unlinkSync(`${TEST_DB_PATH}-wal`); } catch {} delete process.env.GRAPH_DB_PATH; }); it('reports ok:true against a freshly-seeded DB (no drift)', () => { seedService.seedGlobalAgents(); seedService.seedTemplates(); seedService.seedCompanyAgents(); const v = seedService.verifyCounts(); expect(v.mismatches).toEqual([]); expect(v.ok).toBe(true); }); it('catches a mismatch when the DB has a row the filesystem does not back', () => { const db = graphDb.getDb(); const now = new Date().toISOString(); db.prepare(` INSERT INTO system_agents (id, name, scope, owner_company_id, source_kind, legacy_path, status, benchmark_score, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `).run('bogus-agent-no-fs-backing', 'Bogus', 'public', null, 'global_agent', 'system/agents/bogus-agent-no-fs-backing', 'active', null, now, now); const v = seedService.verifyCounts(); expect(v.ok).toBe(false); expect(v.mismatches.some(m => m.check.includes("source_kind='global_agent'"))).toBe(true); db.prepare('DELETE FROM system_agents WHERE id = ?').run('bogus-agent-no-fs-backing'); }); });