import { test, expect } from "bun:test"; import { createHealthConvergence, freshSecretsAfterSeal, } from "../scripts/health-convergence"; test("all three native Agent renders must be fresh after seal, including worker", () => { const now = Date.parse("2026-11-16T12:00:00Z"), fresh = new Date(now - 1000).toISOString(); expect( freshSecretsAfterSeal([fresh, fresh, fresh], now - 10000, now, 120000), ).toBe(true); expect( freshSecretsAfterSeal( [fresh, fresh, new Date(now - 11000).toISOString()], now - 10000, now, 120000, ), ).toBe(false); }); test.each([ "invalid", new Date(1794830400000 + 1).toISOString(), "x".repeat(129), ])("malformed, future or oversized render fails closed: %s", (value) => { const now = 1794830400000, fresh = new Date(now - 1000).toISOString(); expect( freshSecretsAfterSeal([fresh, fresh, value], now - 10000, now, 120000), ).toBe(false); }); test("bounded recovery waits for the actual worker render, not only API readiness", async () => { let now = Date.parse("2026-11-16T12:00:00Z"); const sealedAt = now - 10000; let reads = 0; const result = await createHealthConvergence( { now: () => now, sleep: async (ms) => { now += ms; }, read: async () => { reads++; const fresh = new Date(now - 1000).toISOString(), worker = new Date( reads === 1 ? sealedAt - 1000 : now - 1000, ).toISOString(); return freshSecretsAfterSeal( [fresh, fresh, worker], sealedAt, now, 120000, ) ? "healthy" : "unhealthy"; }, }, 5000, 1000, )(); expect(result.reads).toBe(2); }); function fixture(states: string[]) { let now = 1000, reads = 0; const slept: number[] = []; return { slept, ports: { read: async () => states[Math.min(reads++, states.length - 1)], now: () => now, sleep: async (ms: number) => { slept.push(ms); now += ms; }, }, }; } test("wait for the next healthy tick after unseal instead of trusting stale unhealthy", async () => { const f = fixture(["unhealthy", "starting", "healthy"]); expect(await createHealthConvergence(f.ports, 5000, 1000)()).toEqual({ elapsedMilliseconds: 2000, reads: 3, }); expect(f.slept).toEqual([1000, 1000]); }); test("healthy container returns immediately without delaying recovery", async () => { const f = fixture(["healthy"]); expect(await createHealthConvergence(f.ports, 5000, 1000)()).toEqual({ elapsedMilliseconds: 0, reads: 1, }); expect(f.slept).toEqual([]); }); test("never healthy fails at the bounded deadline", async () => { const f = fixture(["unhealthy"]); await expect(createHealthConvergence(f.ports, 2000, 1000)()).rejects.toThrow( "BAO_HEALTH_NOT_CONVERGED", ); expect(f.slept).toEqual([1000, 1000]); }); test("native read failure preserves its original identity", async () => { const f = fixture(["healthy"]), error = Error("FIXTURE_READ"); f.ports.read = async () => { throw error; }; let actual; try { await createHealthConvergence(f.ports, 5000, 1000)(); } catch (e) { actual = e; } expect(actual).toBe(error); }); test("unknown state fails closed", async () => { const f = fixture(["unknown"]); await expect(createHealthConvergence(f.ports, 5000, 1000)()).rejects.toThrow( "BAO_HEALTH_INVALID", ); }); test("backward clock is rejected", async () => { const f = fixture(["unhealthy"]); let ticks = 0; f.ports.now = () => (ticks++ === 0 ? 1000 : 999); await expect(createHealthConvergence(f.ports, 5000, 1000)()).rejects.toThrow( "HEALTH_CLOCK_INVALID", ); }); test("explicit ports are required", () => expect(() => createHealthConvergence({} as never, 5000, 1000)).toThrow( "HEALTH_PORTS_REQUIRED", ));