import { test, expect, describe } from "bun:test"; import { serverBlock, parsePsms } from "@bbbug/shared/server_prompt"; import { DEFAULT_BUG_RUN_TEMPLATE, DEFAULT_FEATURE_RUN_FIRST_TEMPLATE, DEFAULT_FEATURE_RUN_SECOND_TEMPLATE, renderRunPrompt, } from "@bbbug/shared/run_prompt"; /** * 服务端块的**零影响契约** —— 这组测试存在的唯一理由。 * * Streamline 线上绝大多数需求是纯客户端的。服务端能力接进来不能让它们的行为发生任何 * 变化,而「不会有变化」必须是可断言的,不能是承诺。 * * 契约与 `device_prompt.ts` 完全一致:占位符紧贴其后的标题(`{{serverBlock}}## 步骤 …`), * 所以**空块 = 模板逐字节不变**。`run_prompt_parity.test.ts` 的 50 份夹具是最终验收; * 这里钉的是它的前提——`serverBlock` 在纯客户端任务上必须返回严格的空字符串。 */ describe("服务端块 · 纯客户端任务零影响", () => { test("psms 为空时返回严格空字符串(不是空格、不是换行)", () => { for (const empty of [[], null, undefined]) { const got = serverBlock(empty as string[] | null | undefined); expect(got).toBe(""); expect(got.length).toBe(0); } }); test("platform 不影响纯客户端任务的输出", () => { for (const p of ["android", "ios", null, undefined, "server"]) { expect(serverBlock([], p as string | null | undefined)).toBe(""); } }); test("空块拼进模板后与不拼逐字节相同", () => { // 这正是 device_prompt.ts 的占位符契约:块自带尾部空行,模板里紧贴标题。 const template = (block: string) => `前文\n\n${block}## 步骤 3 — 实现\n正文`; expect(template(serverBlock([]))).toBe(template("")); }); test("声明了 PSM 时才产出内容,且以两个换行收尾(与占位符契约一致)", () => { const got = serverBlock(["tiktok.profile.api"]); expect(got.length).toBeGreaterThan(200); expect(got.endsWith("\n\n")).toBe(true); expect(got).toContain("tiktok.profile.api"); }); }); /** * psms 的来源必须是「人确认过的方案」,所以解析要挡住散文和哨兵 —— 一旦让 * NEEDS_INPUT 之类的东西混进来,纯客户端任务就会被误挂上服务端流程。 */ describe("psms 解析", () => { test("认三段式 PSM", () => { expect(parsePsms(["tiktok.profile.api", "tiktok.profile.admin"])) .toEqual(["tiktok.profile.api", "tiktok.profile.admin"]); }); test("挡住散文、哨兵和空值 —— 它们不该让任务变成服务端任务", () => { expect(parsePsms(["<>", "待确认", "", " ", null, 42])).toEqual([]); }); test("挡住两段式(不是合法 PSM)", () => { expect(parsePsms(["tiktok.profile"])).toEqual([]); }); test("去重且保序", () => { expect(parsePsms(["a.b.c", "a.b.c", "d.e.f"])).toEqual(["a.b.c", "d.e.f"]); }); test("非数组一律视为纯客户端", () => { for (const v of [null, undefined, "a.b.c", {}]) expect(parsePsms(v)).toEqual([]); }); }); /** * 块的内容要点 —— 钉住的是试点里真实犯过的错误,防止有人后来"精简"掉它们。 */ describe("服务端块内容", () => { const b = serverBlock(["tiktok.profile.api"]); test("先探测环境:卫星上没有 bytedcli/bits,照抄文档必然失败", () => { expect(b).toContain("serverline env probe"); expect(b).toContain("没有"); }); test("点名「不要等价改写命令」这个犯了三次的错误模式", () => { expect(b).toContain("翻译"); expect(b).toContain("gcflags"); }); test("带误导性报错对照表 —— Go 版本那条最要紧", () => { expect(b).toContain("invalid recursive type"); expect(b).toContain("GOTOOLCHAIN"); }); test("说清 blocked 不是 fail", () => { expect(b).toContain("blocked"); expect(b).toContain("不存在的代码问题"); }); test("给出时间尺度,否决客户端那套多次尝试", () => { expect(b).toContain("40 分钟"); }); }); /** * 模板层面的零影响 —— 上面钉的是 serverBlock 返回空串,这里钉的是「空串进了模板之后 * 模板也没变」。两条都要有:前者保证块本身不产出内容,后者保证占位符的位置没把 * 相邻文本挤开。run_prompt_parity 的 50 份夹具是最终验收,这组是它的可读版本。 */ describe("模板层面 · 纯客户端任务逐字节不变", () => { const templates: [string, string][] = [ ["bug.run", DEFAULT_BUG_RUN_TEMPLATE], ["feature.run.first", DEFAULT_FEATURE_RUN_FIRST_TEMPLATE], ["feature.run.second", DEFAULT_FEATURE_RUN_SECOND_TEMPLATE], ]; test("三份模板都带上了 {{serverBlock}} 占位符", () => { for (const [name, t] of templates) { expect(t.includes("{{serverBlock}}"), `${name} 缺占位符`).toBe(true); } }); test("占位符紧贴 {{iosDeviceBlock}} —— 两块都空时不会留下空行", () => { for (const [name, t] of templates) { expect(t.includes("{{serverBlock}}{{iosDeviceBlock}}"), `${name} 占位符之间被插入了字符`).toBe(true); } }); test("纯客户端任务:渲染结果与不存在该占位符时逐字节相同", () => { for (const [name, t] of templates) { const withBlock = renderRunPrompt(t, { serverBlock: serverBlock([]), iosDeviceBlock: "" }); const withoutPlaceholder = renderRunPrompt(t.replaceAll("{{serverBlock}}", ""), { iosDeviceBlock: "" }); expect(withBlock, `${name} 渲染结果被改变了`).toBe(withoutPlaceholder); } }); test("声明了 PSM 时才出现在渲染结果里", () => { const out = renderRunPrompt(DEFAULT_BUG_RUN_TEMPLATE, { serverBlock: serverBlock(["tiktok.profile.api"]), iosDeviceBlock: "", }); expect(out).toContain("serverline env probe"); expect(out).toContain("tiktok.profile.api"); }); }); /** * 从已确认的方案里提取 PSM —— 「人确认 → psms」那条链的实现。 */ describe("extractPsmsFromSpec", () => { const spec = (body: string) => `## 目标\n做点什么\n\n## 服务端范围\n${body}\n\n## 验收标准\n- 一\n`; test("从「服务端范围」一节里提取", async () => { const { extractPsmsFromSpec } = await import("@bbbug/shared/server_prompt"); expect(extractPsmsFromSpec(spec("- 涉及 PSM: tiktok.profile.api"), "feature")) .toEqual(["tiktok.profile.api"]); }); test("只认这一节 —— 别处随口提到的服务名不算声明", async () => { const { extractPsmsFromSpec } = await import("@bbbug/shared/server_prompt"); // 描述里一句「这个数据是 X 给的」不该让任务变成服务端任务。 const text = `## 目标\n这个数据是 tiktok.other.svc 给的\n\n## 验收标准\n- 一\n`; expect(extractPsmsFromSpec(text, "feature")).toEqual([]); }); test("哨兵在场 = 还没定,不替人决定", async () => { const { extractPsmsFromSpec } = await import("@bbbug/shared/server_prompt"); expect(extractPsmsFromSpec(spec("<> tiktok.profile.api"), "feature")) .toEqual([]); }); test("没有这一节时返回空 —— 纯客户端任务的正常形态", async () => { const { extractPsmsFromSpec } = await import("@bbbug/shared/server_prompt"); expect(extractPsmsFromSpec("## 目标\n只改客户端\n", "feature")).toEqual([]); }); }); /** * 术语表零影响 —— 这一条是被 run_prompt_parity 逮出来之后补的。 * * 新增 spec 标签会被「告诉模型有哪些小节」的术语表自动收进去,而那张表拼在**三处** * (eval_prompt / feature_evaluator / security_evaluator)。第一次加服务端标签时 * 50 份夹具当场红了 11 处:纯客户端任务的英文提示词里多了一串用不到的服务端术语。 * * 修法是 `serverOnly` 标记 + 共用的 `promptFacingLabels`。这组测试钉住的就是 * 「以后再加服务端标签时,它不会重新漏进去」。 */ describe("术语表 · 不把服务端术语塞给纯客户端任务", () => { test("promptFacingLabels 过滤掉 serverOnly", async () => { const { promptFacingLabels, specLabels } = await import("@bbbug/shared/spec_sections"); for (const track of ["bug", "feature"] as const) { const facing = promptFacingLabels(track).map((d) => d.id); expect(facing).not.toContain("serverScope"); expect(facing).not.toContain("serverPsm"); // 但标签本身仍在表里 —— 匹配路径不能被削弱 expect(specLabels(track).map((d) => d.id)).toContain("serverScope"); } }); test("英文术语表里不出现服务端小节名", async () => { const { specGlossaryBlock } = await import("@bbbug/shared/spec_sections"); for (const track of ["bug", "feature"] as const) { const g = specGlossaryBlock(track, "en"); expect(g).not.toContain("Server scope"); expect(g).not.toContain("服务端范围"); } }); test("模型真写了这一节时仍然认得 —— 过滤的只是「主动告知」", async () => { const { hasSpecSection } = await import("@bbbug/shared/spec_sections"); expect(hasSpecSection("## 服务端范围\n- 涉及 PSM: a.b.c\n", "feature", "serverScope")).toBe(true); expect(hasSpecSection("## Server scope\n- PSM: a.b.c\n", "feature", "serverScope")).toBe(true); }); });