|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createHash } from "node:crypto"; |
| 3 | +import { readFile } from "node:fs/promises"; |
| 4 | +import { dirname, join, resolve } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { loadEvalCases, resolveFixturePath } from "./lib.js"; |
| 7 | + |
| 8 | +const capabilityDir = dirname(fileURLToPath(import.meta.url)); |
| 9 | + |
| 10 | +function parseLockedContract( |
| 11 | + verifySrc: string, |
| 12 | + caseId: string, |
| 13 | +): { sha: string; path: string } | null { |
| 14 | + const sha = verifySrc.match(/^TEST_SHA="([0-9a-f]{64})"$/m)?.[1]; |
| 15 | + if (sha === undefined) return null; |
| 16 | + const hashed = verifySrc.match(/shasum -a 256 (\S+)/)?.[1]; |
| 17 | + const guarded = [...verifySrc.matchAll(/\[\[ -f (\S+) \]\]/g)].map( |
| 18 | + (m) => m[1], |
| 19 | + ); |
| 20 | + if (hashed === undefined || !guarded.includes(hashed)) { |
| 21 | + throw new Error( |
| 22 | + `case ${caseId}: cannot determine the locked contract file from verify.sh`, |
| 23 | + ); |
| 24 | + } |
| 25 | + return { sha, path: hashed }; |
| 26 | +} |
| 27 | + |
| 28 | +describe("locked fixture hashes", () => { |
| 29 | + test("every TEST_SHA literal matches its fixture file", async () => { |
| 30 | + const repoRoot = resolve(capabilityDir, "..", ".."); |
| 31 | + const cases = await loadEvalCases(join(capabilityDir, "cases")); |
| 32 | + const failures: string[] = []; |
| 33 | + let checked = 0; |
| 34 | + for (const c of cases) { |
| 35 | + const verifySrc = await readFile(join(c.caseDir, c.verify), "utf8"); |
| 36 | + const locked = parseLockedContract(verifySrc, c.id); |
| 37 | + if (locked === null) continue; |
| 38 | + checked += 1; |
| 39 | + const bytes = await readFile( |
| 40 | + join(resolveFixturePath(repoRoot, c.fixture), locked.path), |
| 41 | + ); |
| 42 | + const actual = createHash("sha256").update(bytes).digest("hex"); |
| 43 | + if (actual !== locked.sha) { |
| 44 | + failures.push( |
| 45 | + `case ${c.id}: ${locked.path} hashes to ${actual} but verify.sh pins ${locked.sha}`, |
| 46 | + ); |
| 47 | + } |
| 48 | + } |
| 49 | + expect(checked).toBeGreaterThan(0); |
| 50 | + if (failures.length > 0) { |
| 51 | + throw new Error(`stale locked hashes:\n${failures.join("\n")}`); |
| 52 | + } |
| 53 | + }); |
| 54 | +}); |
0 commit comments