Files
streamline/scripts/verify_prompt_fixtures.ts
2026-11-16 17:26:44 -08:00

63 lines
2.8 KiB
TypeScript

/**
* 校验执行期提示词与夹具逐字节相同。
*
* bun scripts/verify_prompt_fixtures.ts # 退出码 0 = 一致
*
* 为什么它是一个独立进程,而不是直接写在测试里:被比对的构造函数读的是**模块作用域**
* 常量(`claude_launch.ts` 的 `RECORD_EVIDENCE_CMD` 在 import 那一刻就由环境变量定死
* 了)。只要同一个 bun 进程里有任何别的测试文件先 import 过这些模块,常量就已经用别的
* 环境算完了 —— 于是这套断言会随「今天跑了哪些测试文件」时红时绿,而那种测试比没有
* 测试更糟。
*
* 独占一个进程,环境在任何 import 之前设好,结果就只取决于代码本身。
*/
import { existsSync, readFileSync, readdirSync } from "node:fs";
import path from "node:path";
import {
PROMPT_FIXTURE_ENV, PROMPT_FIXTURE_EXEC_PATH, promptFixtureCases, FIXTURE_DIR,
} from "../satellite/test/prompt_fixture_cases";
for (const [k, v] of Object.entries(PROMPT_FIXTURE_ENV)) process.env[k] = v;
Object.defineProperty(process, "execPath", { value: PROMPT_FIXTURE_EXEC_PATH, configurable: true });
const dir = path.resolve(import.meta.dir, "..", FIXTURE_DIR);
if (!existsSync(dir)) {
console.error(`缺少夹具目录 ${FIXTURE_DIR} — 先跑 bun scripts/capture_prompt_fixtures.ts`);
process.exit(2);
}
const cases = await promptFixtureCases();
const problems: string[] = [];
const rendered = new Map<string, string>();
for (const c of cases) {
const file = path.join(dir, `${c.name}.txt`);
const got = c.render();
const prev = rendered.get(got);
// Two cases rendering the same text means that branch is not actually covered;
// the fixture count would then overstate what is pinned.
if (prev) problems.push(`用例未区分分支:${prev} 与 ${c.name} 渲染结果相同`);
else rendered.set(got, c.name);
if (!existsSync(file)) { problems.push(`${c.name}: 没有对应夹具`); continue; }
const want = readFileSync(file, "utf8");
if (got === want) continue;
let i = 0;
while (i < Math.min(got.length, want.length) && got[i] === want[i]) i++;
const around = (s: string) => JSON.stringify(s.slice(Math.max(0, i - 60), i + 60));
problems.push(`${c.name}: 第 ${i} 字符起不同\n 夹具: ${around(want)}\n 当前: ${around(got)}`);
}
const names = new Set(cases.map((c) => c.name));
for (const f of readdirSync(dir).filter((x) => x.endsWith(".txt"))) {
const n = f.replace(/\.txt$/, "");
if (!names.has(n)) problems.push(`孤儿夹具 ${n}:没有对应用例,请删掉或补回用例`);
}
if (problems.length) {
console.error(`[parity] ${problems.length} 处不一致:\n\n${problems.join("\n\n")}`);
console.error(`\n[parity] 若这些措辞改动是有意的:bun scripts/capture_prompt_fixtures.ts 重抓夹具。`);
process.exit(1);
}
console.log(`[parity] ${cases.length} 个用例与夹具逐字节一致 ✓`);