Files
streamline/satellite/test/run_prompt_parity.test.ts
2026-11-16 17:26:44 -08:00

49 lines
2.5 KiB
TypeScript

import { test, expect, describe } from "bun:test";
import { existsSync, readdirSync } from "node:fs";
import path from "node:path";
import { FIXTURE_DIR } from "./prompt_fixture_cases";
/**
* 执行期提示词的逐字节一致性。
*
* 这些提示词的正文被从卫星代码里抽成了 `{{placeholder}}` 模板(shared/src/run_prompt.ts),
* 目的是让它们能被版本化、能随任务下发、能拿两版跑同一个任务再比。这类改造唯一真正的
* 风险不是崩,是**悄悄改了措辞**:少一个空格、把某个条件分支的边界挪一格,提示词照样
* 跑,只是行为变了,而且要几十次真机执行之后才可能有人察觉。
*
* 所以夹具是在重构**之前**从当时的构造函数里抓的,不是照着新实现反推的 —— 这是它能
* 当基准的唯一理由。
*
* 比对跑在**独立进程**里(scripts/verify_prompt_fixtures.ts)。被比对的构造函数读模块
* 作用域常量,`claude_launch.ts` 的 `RECORD_EVIDENCE_CMD` 在 import 那一刻就被环境变量
* 定死;只要同进程里有别的测试文件先 import 过它,这套断言就会随「今天跑了哪些测试
* 文件」时红时绿。这个 bug 真的发生过 —— 单独跑绿、全量跑红。
*
* 措辞是有意改的 → bun scripts/capture_prompt_fixtures.ts 重抓夹具
*/
const REPO = path.resolve(import.meta.dir, "../..");
describe("执行期提示词 · 与重构前逐字节一致", () => {
test("夹具目录存在且非空", () => {
const dir = path.join(REPO, FIXTURE_DIR);
expect(existsSync(dir), `缺少 ${FIXTURE_DIR} — 先跑 bun scripts/capture_prompt_fixtures.ts`).toBe(true);
expect(readdirSync(dir).filter((f) => f.endsWith(".txt")).length).toBeGreaterThan(20);
});
test("每个用例都渲染出与夹具完全相同的字节", async () => {
const proc = Bun.spawn(["bun", "scripts/verify_prompt_fixtures.ts"], {
cwd: REPO, stdout: "pipe", stderr: "pipe",
// A clean env for the bits the prompts interpolate — the script sets the
// pinned values itself, but an inherited AI_WORKSPACE would otherwise win
// whenever the script's own assignment happens to run later.
env: { ...process.env, AI_WORKSPACE: "", SATELLITE_ENTRY_PATH: "", RECORD_EVIDENCE_SCRIPT: "" },
});
const [out, err, code] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited,
]);
expect(code === 0 ? "" : `${err}${out}`.trim(), "提示词措辞与重构前不一致").toBe("");
}, 30_000);
});