// Byte-snapshot harness for product wire formats: assert an emitted artifact // (workflow YAML, manifest JSON, compose file) matches a committed fixture // byte-for-byte, so unintended emission changes show up as a reviewable diff. // Refresh deliberately: UPDATE_SNAPSHOTS=1 bun test rewrites fixtures; // commit the diff with the change that caused it. Framework-free (plain throw), // so it works under bun:test, vitest, or bare scripts alike. import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { dirname } from "node:path"; const UPDATE_HINT = "run UPDATE_SNAPSHOTS=1 bun test to (re)write it, then commit the fixture"; function updating(): boolean { const v = process.env.UPDATE_SNAPSHOTS; return v !== undefined && v !== "" && v !== "0" && v.toLowerCase() !== "false"; } function firstDiffLine(actual: string, expected: string): string { const a = actual.split("\n"); const e = expected.split("\n"); for (let i = 0; i < Math.max(a.length, e.length); i++) { if (a[i] !== e[i]) { return `line ${i + 1}: snapshot ${JSON.stringify(e[i] ?? "")}, actual ${JSON.stringify(a[i] ?? "")}`; } } return "content differs"; } export function expectByteSnapshot(actual: string, fixturePath: string): void { if (updating()) { mkdirSync(dirname(fixturePath), { recursive: true }); writeFileSync(fixturePath, actual); return; } if (!existsSync(fixturePath)) { throw new Error(`no committed snapshot at ${fixturePath} — ${UPDATE_HINT}.`); } const expected = readFileSync(fixturePath, "utf8"); if (actual !== expected) { throw new Error( `emitted bytes differ from the committed snapshot ${fixturePath} (${firstDiffLine(actual, expected)}). If the change is intended, ${UPDATE_HINT}.`, ); } }