|
| 1 | +/** |
| 2 | + * Integration test: the HMAC integrity chain must survive process restart. |
| 3 | + * |
| 4 | + * Simulates the restart by discarding the original createGovernance() |
| 5 | + * instance (closures gone, chain state lost) and creating a fresh one |
| 6 | + * against the same storage. The second instance must resume the chain |
| 7 | + * and produce events whose sequence continues from where the first |
| 8 | + * instance left off, with verifyAuditIntegrity passing end-to-end. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, it } from "node:test"; |
| 12 | +import assert from "node:assert/strict"; |
| 13 | +import { createGovernance, createMemoryStorage } from "./index.js"; |
| 14 | +import { verifyAuditIntegrity } from "./audit-integrity-verify.js"; |
| 15 | +import type { GovernanceStorage } from "./storage.js"; |
| 16 | + |
| 17 | +const KEY = "test-signing-key-0.12-restart"; |
| 18 | + |
| 19 | +async function writeSomeEvents(gov: Awaited<ReturnType<typeof createGovernance>>, count: number) { |
| 20 | + for (let i = 0; i < count; i++) { |
| 21 | + await gov.audit.log({ |
| 22 | + agentId: "restart-agent", |
| 23 | + eventType: "test_event", |
| 24 | + outcome: "success", |
| 25 | + severity: "info", |
| 26 | + detail: { iteration: i }, |
| 27 | + }); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +describe("integrity chain restart durability (0.12)", () => { |
| 32 | + it("resumes sequence from storage on fresh createGovernance() call", async () => { |
| 33 | + const storage: GovernanceStorage = createMemoryStorage(); |
| 34 | + |
| 35 | + const gov1 = createGovernance({ storage, integrityAudit: { signingKey: KEY } }); |
| 36 | + await writeSomeEvents(gov1, 5); |
| 37 | + const stats1 = gov1.integrityChain!.stats(); |
| 38 | + assert.equal(stats1.latestSequence, 5, "first instance wrote sequences 1..5"); |
| 39 | + const firstHash = stats1.latestHash; |
| 40 | + |
| 41 | + // Simulate restart: drop gov1 entirely, keep storage. |
| 42 | + const gov2 = createGovernance({ storage, integrityAudit: { signingKey: KEY } }); |
| 43 | + // Write one more event — must pick up at sequence 6 and chain to firstHash. |
| 44 | + await writeSomeEvents(gov2, 1); |
| 45 | + const stats2 = gov2.integrityChain!.stats(); |
| 46 | + assert.equal(stats2.latestSequence, 6, "second instance resumed at sequence 6"); |
| 47 | + assert.notEqual(stats2.latestHash, firstHash, "new event produced new head"); |
| 48 | + |
| 49 | + // Full chain must verify end-to-end across the boundary. |
| 50 | + const chain = await gov2.integrityChain!.export(); |
| 51 | + assert.equal(chain.length, 6, "export includes all 6 events"); |
| 52 | + assert.deepEqual( |
| 53 | + chain.map((e) => e.integrity.sequence), |
| 54 | + [1, 2, 3, 4, 5, 6], |
| 55 | + "sequences are contiguous across restart", |
| 56 | + ); |
| 57 | + |
| 58 | + const verification = await verifyAuditIntegrity(chain, KEY); |
| 59 | + assert.equal(verification.valid, true, verification.breakDetail ?? "chain should verify"); |
| 60 | + assert.equal(verification.eventsVerified, 6); |
| 61 | + }); |
| 62 | + |
| 63 | + it("getChainHead returns null for empty storage (cold start)", async () => { |
| 64 | + const storage = createMemoryStorage(); |
| 65 | + const head = await storage.getChainHead!(); |
| 66 | + assert.equal(head, null); |
| 67 | + }); |
| 68 | + |
| 69 | + it("legacy storage adapter (no createAuditEventWithIntegrity) still works but warns", async () => { |
| 70 | + // Build an adapter that implements the core interface but omits the |
| 71 | + // new integrity methods, emulating a third-party 0.11.x adapter. |
| 72 | + const base = createMemoryStorage(); |
| 73 | + const legacy: GovernanceStorage = { |
| 74 | + createAgent: base.createAgent, |
| 75 | + getAgent: base.getAgent, |
| 76 | + getAgentByName: base.getAgentByName, |
| 77 | + listAgents: base.listAgents, |
| 78 | + updateAgent: base.updateAgent, |
| 79 | + deleteAgent: base.deleteAgent, |
| 80 | + createAuditEvent: base.createAuditEvent, |
| 81 | + queryAuditEvents: base.queryAuditEvents, |
| 82 | + countAuditEvents: base.countAuditEvents, |
| 83 | + // intentionally omit: createAuditEventWithIntegrity, getChainHead, getAuditIntegrity |
| 84 | + }; |
| 85 | + |
| 86 | + const warnings: unknown[] = []; |
| 87 | + const gov = createGovernance({ |
| 88 | + storage: legacy, |
| 89 | + integrityAudit: { signingKey: KEY }, |
| 90 | + onAuditError: (e) => warnings.push(e), |
| 91 | + }); |
| 92 | + |
| 93 | + await writeSomeEvents(gov, 2); |
| 94 | + const chain = await gov.integrityChain!.export(); |
| 95 | + assert.equal(chain.length, 2, "chain export still works via in-memory fallback"); |
| 96 | + const verification = await verifyAuditIntegrity(chain, KEY); |
| 97 | + assert.equal(verification.valid, true); |
| 98 | + assert.ok( |
| 99 | + warnings.length >= 2, |
| 100 | + "onAuditError fired at least once per write on legacy adapter", |
| 101 | + ); |
| 102 | + assert.ok( |
| 103 | + warnings.every((w) => w instanceof Error && /chain is session-local/.test(w.message)), |
| 104 | + "warning explains the session-local downgrade", |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments