import { test, expect } from "bun:test"; import { createGitProofProducer, createGitProofReader, proofHash, type SourceHashes, } from "../scripts/git-tracking-proof"; const files: SourceHashes = { "scripts/example.ts": "a".repeat(64) }; async function produced() { return createGitProofProducer({ query: async () => ({ code: 0, stdout: "" }), head: async () => "b".repeat(40), snapshot: async () => ({ ...files }), })("/fixture/canonical"); } const bytes = (p: unknown) => Buffer.from(JSON.stringify(p)); test("actual producer output is accepted only for matching pinned bytes and current source", async () => { const p = bytes(await produced()); const r = await createGitProofReader({ read: async () => p, snapshot: async () => ({ ...files }), })(proofHash(p)); expect(r.tracked_private_files).toBe(0); expect(r.proof_sha256).toBe(proofHash(p)); }); test("changed proof bytes are rejected before source inspection", async () => { const p = bytes(await produced()); let calls = 0; await expect( createGitProofReader({ read: async () => Buffer.concat([p, Buffer.from(" ")]), snapshot: async () => { calls++; return files; }, })(proofHash(p)), ).rejects.toThrow("GIT_PROOF_SHA_MISMATCH"); expect(calls).toBe(0); }); test("missing proof preserves the primary read failure", async () => { const error = new Error("FIXTURE_MISSING_PROOF"); let actual: unknown; try { await createGitProofReader({ read: async () => { throw error; }, snapshot: async () => files, })("c".repeat(64)); } catch (e) { actual = e; } expect(actual).toBe(error); }); const sourceChanges: { name: string; current: SourceHashes }[] = [ { name: "changed source bytes", current: { "scripts/example.ts": "c".repeat(64) }, }, { name: "added source entry", current: { ...files, "scripts/new.ts": "d".repeat(64) }, }, ]; test.each(sourceChanges)( "$name invalidates canonical Git evidence", async ({ current }) => { const p = bytes(await produced()); await expect( createGitProofReader({ read: async () => p, snapshot: async () => current, })(proofHash(p)), ).rejects.toThrow("GIT_PROOF_SOURCE_MISMATCH"); }, ); test("nonempty private tracking query result never becomes a zero-count proof", async () => { const p = await produced(); p.result.stdout_base64 = Buffer.from(".env\0").toString("base64"); const b = bytes(p); await expect( createGitProofReader({ read: async () => b, snapshot: async () => files })( proofHash(b), ), ).rejects.toThrow("GIT_PROOF_INVALID"); }); test("a weakened query cannot prove the original private-file patterns", async () => { const p = await produced(); p.query.pop(); const b = bytes(p); await expect( createGitProofReader({ read: async () => b, snapshot: async () => files })( proofHash(b), ), ).rejects.toThrow("GIT_PROOF_INVALID"); }); test("producer rejects a changing HEAD during capture", async () => { let reads = 0; await expect( createGitProofProducer({ query: async () => ({ code: 0, stdout: "" }), head: async () => (++reads === 1 ? "b".repeat(40) : "c".repeat(40)), snapshot: async () => files, })("/fixture/canonical"), ).rejects.toThrow("GIT_CAPTURE_CHANGED"); }); test("producer rejects a changing source snapshot", async () => { let reads = 0; await expect( createGitProofProducer({ query: async () => ({ code: 0, stdout: "" }), head: async () => "b".repeat(40), snapshot: async () => ++reads === 1 ? files : { "scripts/example.ts": "c".repeat(64) }, })("/fixture/canonical"), ).rejects.toThrow("GIT_CAPTURE_CHANGED"); });