import { describe, expect, test } from 'bun:test'; import type { DbClient } from '../../db/client'; import { runAudit } from './index'; import { unusedPublicDnsProbe } from './public-dns'; const fakeDb = {} as DbClient; const emptyDeps = { cliVersion: { installedVersion: '0.1.5', fetcher: async () => '0.1.5', }, schema: { // A READABLE journal with nothing pending. It used to be `() => null`, // which now (correctly) reports `unmeasured` — an unreadable journal is not // the same as a schema with no pending migrations, and this fixture means // the latter. journal: () => ({ version: '6', dialect: 'sqlite', entries: [] }), applied: () => [], db: fakeDb, }, browserPin: { consumers: [], provisioned: null }, capabilityAbi: { modules: [] }, terraformPlan: { modules: [], run: async () => ({ exitCode: 0, stdout: '', stderr: '' }), }, moduleVersions: { installed: [], fetcher: async () => ({ latest: null }), }, moduleConfigs: { modules: [] }, moduleIntegrity: { results: [] }, detectWithoutConverge: { modules: [] }, jailExemptions: { exemptions: [] }, health: { results: [] }, backups: { modules: [] }, abandonedOperations: { records: [] }, undeployedModules: { modules: [] }, unconfiguredModules: { modules: [] }, servicesCredentials: { results: [] }, secretsDecryptable: { results: [] }, servicesReachable: { results: [] }, machinesReachable: { results: [] }, diskSpace: { results: [] }, transportReads: { statuses: [], now: new Date(), staleAfterMs: 30 * 60_000 }, trustedSources: { firewalls: [], unreachableFirewalls: [] }, publicDns: { records: [], probe: unusedPublicDnsProbe }, }; describe('runAudit', () => { test('READY when no checks produce findings', async () => { const report = await runAudit({ ...emptyDeps, now: () => new Date('2026-04-25T00:00:00Z') }); expect(report.version).toBe(1); expect(report.verdict).toBe('READY'); expect(report.findings).toEqual([]); expect(report.generatedAt).toBe('2026-04-25T00:00:00.000Z'); }); test('DRIFT when only drift-severity findings', async () => { const report = await runAudit({ ...emptyDeps, cliVersion: { installedVersion: '0.1.5', fetcher: async () => '0.1.7', }, }); expect(report.verdict).toBe('DRIFT'); expect(report.findings).toHaveLength(1); expect(report.findings[0].category).toBe('cli_version'); }); test('BLOCKED when any blocked finding is present', async () => { const report = await runAudit({ ...emptyDeps, schema: { journal: () => ({ version: '5', dialect: 'sqlite', entries: [{ idx: 0, tag: '0001_pending', when: 1 }], }), applied: () => [], db: fakeDb, }, }); expect(report.verdict).toBe('BLOCKED'); expect(report.findings.some((f) => f.category === 'schema')).toBe(true); }); test('disk_space is wired into the report (celilo#1336)', async () => { const events: string[] = []; const report = await runAudit( { ...emptyDeps, diskSpace: { results: [{ hostname: 'celilo-mgr', ipAddress: '10.0.0.5', usedPercent: 96 }], }, }, (event) => events.push(`${event.category}:${event.phase}`), ); // 96% is at the blocked threshold, so the wired category alone flips // the fleet verdict — which is the point of wiring it. expect(report.verdict).toBe('BLOCKED'); expect(report.findings.map((f) => f.category)).toContain('disk_space'); expect(events).toContain('disk_space:start'); expect(events).toContain('disk_space:end'); }); test('aggregates findings across all categories', async () => { const report = await runAudit({ ...emptyDeps, cliVersion: { installedVersion: '0.1.5', fetcher: async () => '0.1.7', }, moduleVersions: { installed: [{ id: 'caddy', version: '1.0.0' }], fetcher: async () => ({ latest: '1.1.0' }), }, }); expect(report.verdict).toBe('DRIFT'); expect(report.findings).toHaveLength(2); expect(report.findings.map((f) => f.category).sort()).toEqual([ 'cli_version', 'module_versions', ]); }); });