import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test from "node:test"; const verifier = new URL("../../scripts/verify-generated-runtime.mjs", import.meta.url); async function runtimeFixture() { const root = await mkdtemp(join(tmpdir(), "codex-chief-of-staff-runtime-test-")); await Promise.all([ mkdir(join(root, "dist", "src"), { recursive: true }), mkdir(join(root, "scripts"), { recursive: true }), mkdir(join(root, "src"), { recursive: true }), ]); await writeFile(join(root, "package.json"), JSON.stringify({ private: true, scripts: { compile: "node scripts/compile.mjs" }, })); await writeFile( join(root, "scripts", "compile.mjs"), "import { copyFile } from 'node:fs/promises';\nawait copyFile('src/runtime.js', 'dist/src/runtime.js');\n", ); await writeFile(join(root, "src", "runtime.js"), "export const version = 1;\n"); await writeFile(join(root, "dist", "src", "runtime.js"), "export const version = 1;\n"); execFileSync("git", ["init", "--quiet"], { cwd: root }); execFileSync("git", ["add", "."], { cwd: root }); return root; } test("generated-runtime verifier accepts a fresh staged baseline", async () => { const root = await runtimeFixture(); try { const result = spawnSync(process.execPath, [verifier.pathname], { cwd: root, encoding: "utf8", }); assert.equal(result.status, 0, result.stderr); assert.match(result.stdout, /Generated runtime is fresh/); } finally { await rm(root, { recursive: true, force: true }); } }); test("generated-runtime verifier rejects source and runtime drift", async () => { const root = await runtimeFixture(); try { await writeFile(join(root, "src", "runtime.js"), "export const version = 2;\n"); const result = spawnSync(process.execPath, [verifier.pathname], { cwd: root, encoding: "utf8", }); assert.notEqual(result.status, 0); assert.match(result.stderr, /generated runtime drift detected in dist\/src/i); assert.match(result.stderr, /runtime\.js/); } finally { await rm(root, { recursive: true, force: true }); } });