import { describe, expect, test } from "vitest"; import { CHAIN_FIELDS, canonicalisePluginCapabilityUseRow, computeRowHash, stampPluginCapabilityUseChain, verifyPluginCapabilityUseChain, type PluginCapabilityUseChainRow, type StampedRow, } from "./audit-chain.js"; function row(overrides: Partial = {}): PluginCapabilityUseChainRow { return { id: "row-1", pluginInvocationId: "inv-1", tenantId: "t-1", pluginId: "com.acme.demo", capability: "log", payloadHash: null, outcome: "success", errorCode: null, durationMs: 12, timestamp: "2026-05-19T18:00:00.000Z", ...overrides, }; } describe("canonicalisePluginCapabilityUseRow", () => { test("is deterministic and order-stable", () => { const a = canonicalisePluginCapabilityUseRow(row()); const b = canonicalisePluginCapabilityUseRow(row()); expect(a).toBe(b); expect(a.length).toBeGreaterThan(0); }); test("CHAIN_FIELDS has not been silently reordered", () => { // Guard against accidentally moving fields around — chain-breaking. expect(CHAIN_FIELDS).toEqual([ "id", "pluginInvocationId", "tenantId", "pluginId", "capability", "payloadHash", "outcome", "errorCode", "durationMs", "timestamp", ]); }); test("differentiates null vs empty string", () => { const withNull = canonicalisePluginCapabilityUseRow(row({ errorCode: null })); const withEmpty = canonicalisePluginCapabilityUseRow(row({ errorCode: "" })); expect(withNull).not.toBe(withEmpty); }); }); describe("computeRowHash", () => { test("returns a 64-char hex string", () => { const h = computeRowHash(null, row()); expect(h).toMatch(/^[0-9a-f]{64}$/); }); test("changes when prevHash changes", () => { const a = computeRowHash(null, row()); const b = computeRowHash("abc", row()); expect(a).not.toBe(b); }); test("changes when any field changes", () => { const base = computeRowHash(null, row()); expect(computeRowHash(null, row({ pluginId: "other" }))).not.toBe(base); expect(computeRowHash(null, row({ outcome: "error" }))).not.toBe(base); expect(computeRowHash(null, row({ durationMs: 999 }))).not.toBe(base); }); }); describe("stampPluginCapabilityUseChain", () => { test("stamps an empty batch as a no-op", () => { const out = stampPluginCapabilityUseChain({ seedPrevHash: null, rows: [] }); expect(out.stamped).toEqual([]); expect(out.chainHead).toBeNull(); }); test("preserves the seed prev hash for a non-empty batch", () => { const out = stampPluginCapabilityUseChain({ seedPrevHash: "seed", rows: [row({ id: "r1" })], }); expect(out.stamped[0]?.prevHash).toBe("seed"); }); test("threads the rowHash forward through the batch", () => { const out = stampPluginCapabilityUseChain({ seedPrevHash: null, rows: [row({ id: "r1" }), row({ id: "r2" }), row({ id: "r3" })], }); expect(out.stamped[0]!.prevHash).toBeNull(); expect(out.stamped[1]!.prevHash).toBe(out.stamped[0]!.rowHash); expect(out.stamped[2]!.prevHash).toBe(out.stamped[1]!.rowHash); expect(out.chainHead).toBe(out.stamped[2]!.rowHash); }); test("produces deterministic output for the same input", () => { const a = stampPluginCapabilityUseChain({ seedPrevHash: "x", rows: [row({ id: "r1" }), row({ id: "r2" })], }); const b = stampPluginCapabilityUseChain({ seedPrevHash: "x", rows: [row({ id: "r1" }), row({ id: "r2" })], }); expect(a.stamped.map((r) => r.rowHash)).toEqual(b.stamped.map((r) => r.rowHash)); }); }); describe("verifyPluginCapabilityUseChain", () => { function stamped(seed: string | null, rs: PluginCapabilityUseChainRow[]): StampedRow[] { return [...stampPluginCapabilityUseChain({ seedPrevHash: seed, rows: rs }).stamped]; } test("returns ok for an empty input", () => { expect(verifyPluginCapabilityUseChain([])).toEqual({ ok: true, rowsVerified: 0 }); }); test("returns ok for a freshly-stamped chain", () => { const chain = stamped(null, [row({ id: "r1" }), row({ id: "r2" }), row({ id: "r3" })]); const result = verifyPluginCapabilityUseChain(chain); expect(result).toEqual({ ok: true, rowsVerified: 3 }); }); test("detects a tampered field via row_hash_mismatch", () => { const chain = stamped(null, [row({ id: "r1" }), row({ id: "r2" })]); // Tamper with the second row's outcome — its stored rowHash no // longer matches the recomputed one. const tampered = [chain[0]!, { ...chain[1]!, outcome: "error" }]; const result = verifyPluginCapabilityUseChain(tampered); expect(result.ok).toBe(false); if (!result.ok) { expect(result.reason).toBe("row_hash_mismatch"); expect(result.brokenRowIndex).toBe(1); expect(result.brokenRowId).toBe("r2"); } }); test("detects a deletion via prev_mismatch on the next row", () => { const chain = stamped(null, [row({ id: "r1" }), row({ id: "r2" }), row({ id: "r3" })]); // Delete the middle row — r3's prevHash references r2's rowHash but // r2 is gone, so the next-row check against the running tail fails. const missingR2 = [chain[0]!, chain[2]!]; const result = verifyPluginCapabilityUseChain(missingR2); expect(result.ok).toBe(false); if (!result.ok) { expect(result.reason).toBe("prev_mismatch"); expect(result.brokenRowId).toBe("r3"); } }); test("skips pre-F-003 rows (null hashes) without flagging them as breaks", () => { const preF003 = row({ id: "pre1" }); // no prevHash / rowHash → pre-chain const chain = stamped(null, [row({ id: "post1" }), row({ id: "post2" })]); const mixed = [ preF003 as PluginCapabilityUseChainRow & { prevHash?: null; rowHash?: null }, ...chain, ]; const result = verifyPluginCapabilityUseChain(mixed); expect(result.ok).toBe(true); if (result.ok) { // pre-F-003 row is skipped; post-chain rows count. expect(result.rowsVerified).toBe(2); } }); test("flags a missing rowHash on a row whose prevHash exists", () => { const chain = stamped(null, [row({ id: "r1" })]); // Build a "broken" row where prevHash is set but rowHash is wrong. const broken: StampedRow & { rowHash: string } = { ...chain[0]!, rowHash: "deadbeef".repeat(8), // wrong hash }; const result = verifyPluginCapabilityUseChain([broken]); expect(result.ok).toBe(false); if (!result.ok) expect(result.reason).toBe("row_hash_mismatch"); }); });