Files
2026-11-16 17:26:44 -08:00

324 lines
14 KiB
TypeScript

import { test, expect, describe } from "bun:test";
import {
parseSentinels, applyAnswers, normalizeSentinels, nextSentinelIndex, rehypePlanMarks,
type Answer, type Comment,
} from "../src/lib/planDecisions";
/**
* 执行计划确认页把每个 NEEDS_INPUT 哨兵变成一张可操作的卡片。出错的方式不是抛异常,
* 而是安静地少一张卡 —— 用户看不到那处待补充,以为自己已经处理完了,然后带着缺失
* 信息把任务放进修复队列。所以这里盯死两件事:
*
* 1. 渲染出来的卡片数量与顺序,必须和源文本里解析出的哨兵一一对应;
* 2. 「填入计划」写回文本时,必须只替换用户答的那一个哨兵。
*
* 用例形状取自生产上真实的 TestSpec:哨兵嵌在列表项里、提示里引代码(反引号)、
* 同一句提示重复出现、以及夹在代码块里的那种。
*/
const NI = (h: string) => `<<<<<<< NEEDS_INPUT: ${h} >>>>>>>`;
const PLAN = `## 复现条件 // Reproduct condition
- 账号/环境: 泰国测试账号 tt_qa_th_04;设备 ${NI("具体设备型号与系统版本")}
- UI 入口指引:
- ${NI("从首页到拍摄页的入口路径")}
- 其他特殊要求: 无特殊要求
## 复现步骤 // Operations
1. 进入拍摄页,把滤镜强度拖到 80。
2. 切后台:**${NI("切后台的具体操作")}**
3. 回前台观察滤镜强度。
![](images/plan-11111111-2222-3333-4444-555555555555.png)
## 期望与实际
- 期望: ${NI("期望对齐到 `CameraFilterStore.restoreOnResume()` 的哪个分支")}
- 实际: 强度回落到默认值 50。
## 回归风险点
- ${NI("具体设备型号与系统版本")}
- 不得影响既有的相机资源释放降级逻辑。
\`\`\`
${NI("这条在代码块里,渲染扫描看不到")}
\`\`\`
`;
const sentinels = () => parseSentinels(PLAN, "(未分节)", "(未注明)");
describe("parseSentinels", () => {
test("按文档顺序找全哨兵,并记住各自所属章节", () => {
const s = sentinels();
expect(s).toHaveLength(6);
expect(s.map((x) => x.section)).toEqual([
"复现条件 // Reproduct condition",
"复现条件 // Reproduct condition",
"复现步骤 // Operations",
"期望与实际",
"回归风险点",
"回归风险点",
]);
});
test("提示文字剥掉哨兵外壳", () => {
expect(sentinels()[0].hint).toBe("具体设备型号与系统版本");
});
test("同一句提示出现两次时,两处拿到不同的 key", () => {
// 否则答了「复现条件」那处,「回归风险点」那处会跟着一起被判定为已处理。
const s = sentinels();
expect(s[0].raw).toBe(s[4].raw);
expect(s[0].key).not.toBe(s[4].key);
});
});
describe("parseSentinels · @OPTION 候选", () => {
const withOpts = `## 复现条件 // Reproduct condition
- 设备: <<<<<<< NEEDS_INPUT: 具体设备型号与系统版本 @OPTION iPhone 15 Pro · iOS 17.5 | 设备池默认机 | 设备:iPhone 15 Pro / iOS 17.5 @OPTION Pixel 8 · Android 14 | 工单截图状态栏 | 设备:Pixel 8 / Android 14 >>>>>>>
- 门控: <<<<<<< NEEDS_INPUT: 受哪个实验位控制 @OPTION studio_effects_panel_discover | 本地新建(未找到可复用的既有实验位) | 门控 Key:studio_effects_panel_discover(AB Class: StudioEffectsPanelDiscoverExp) >>>>>>>`;
test("候选被解析出来,且不混进 hint", () => {
const s = parseSentinels(withOpts, "-", "-");
expect(s).toHaveLength(2);
expect(s[0].hint).toBe("具体设备型号与系统版本");
expect(s[0].options.map((o) => o.label)).toEqual(["iPhone 15 Pro · iOS 17.5", "Pixel 8 · Android 14"]);
expect(s[0].options[0].source).toBe("设备池默认机");
expect(s[0].options[0].fill).toBe("设备:iPhone 15 Pro / iOS 17.5");
expect(s[1].hint).toBe("受哪个实验位控制");
expect(s[1].options[0].source).toBe("本地新建(未找到可复用的既有实验位)");
});
test("fill 里的竖线不会被当成分隔符", () => {
// 只有前两个分隔符是结构性的,成品文本里出现 "|" 属于正常。
const s = parseSentinels(`<<<<<<< NEEDS_INPUT: h @OPTION L | src | a | b | c >>>>>>>`, "-", "-");
expect(s[0].options[0].fill).toBe("a | b | c");
});
test("缺出处的候选被丢弃,而不是半渲染", () => {
// 没有出处的候选正是绝对不能出现在按钮上的东西 —— 宁可当它不存在。
const s = parseSentinels(
`<<<<<<< NEEDS_INPUT: h @OPTION 只有标签 @OPTION 标签 | | 有 fill 没出处 >>>>>>>`, "-", "-");
expect(s[0].options).toHaveLength(0);
expect(s[0].hint).toBe("h");
});
test("没有候选时 options 为空数组(存量计划走这条路)", () => {
const s = parseSentinels(`<<<<<<< NEEDS_INPUT: 就一句提示 >>>>>>>`, "-", "-");
expect(s[0].options).toEqual([]);
expect(s[0].hint).toBe("就一句提示");
});
test("选中候选后整个哨兵(连同候选)被替换掉", () => {
const s = parseSentinels(withOpts, "-", "-");
const out = applyAnswers(withOpts, { [s[0].key]: { mode: "fill", text: s[0].options[0].fill } });
expect(out).toContain("- 设备: 设备:iPhone 15 Pro / iOS 17.5");
expect(out).not.toContain("@OPTION iPhone");
expect(out).not.toContain("@OPTION Pixel");
// 另一个哨兵原样留着
expect(out).toContain("@OPTION studio_effects_panel_discover");
});
});
describe("normalizeSentinels", () => {
// 行首的 `>>>>>>>` 会被 markdown 解析成七层引用块,哨兵在渲染文本里就永远闭合
// 不了 —— 卡片配不上、退化成不可点的高亮、候选全部消失,而且屏幕上没有任何
// 迹象。模型偶尔会换行,所以加载计划时先把它拍回一行。
test("把跨行的哨兵拍回一行", () => {
const bad = `- 设备: <<<<<<< NEEDS_INPUT: 具体设备型号
@OPTION iPhone 15 Pro | 设备池默认机 | 设备:iPhone 15 Pro / iOS 17.5
>>>>>>>`;
const fixed = normalizeSentinels(bad);
expect(fixed.split("\n")).toHaveLength(1);
expect(parseSentinels(fixed, "-", "-")[0].options).toHaveLength(1);
});
test("哨兵之外的换行不动", () => {
const t = `## 标题\n- 一\n- 二`;
expect(normalizeSentinels(t)).toBe(t);
});
});
describe("applyAnswers", () => {
const answered = (): Record<string, Answer> => {
const s = sentinels();
return {
[s[0].key]: { mode: "fill", text: "iPhone 15 Pro / iOS 17.5" },
[s[2].key]: { mode: "agent", text: "去查主拍页是怎么做的" },
[s[4].key]: { mode: "fill", text: "仅拍摄页滤镜档位" },
};
};
test("fill 就地替换,agent 的哨兵原样留着", () => {
// agent 那条要留下来:下一轮 regen 的 comment 就是锚在这段原文上的,
// 提前抹掉的话 Agent 就不知道这条要求指的是哪儿。
const out = applyAnswers(PLAN, answered());
expect(out).toContain("设备 iPhone 15 Pro / iOS 17.5");
expect(out).toContain(NI("切后台的具体操作"));
expect(out).toContain(NI("从首页到拍摄页的入口路径"));
});
test("ignore 拆掉哨兵,不写回任何文字,也不留给 Agent", () => {
const s = sentinels();
const out = applyAnswers(PLAN, { [s[1].key]: { mode: "ignore", text: "" } });
expect(out).not.toContain(NI("从首页到拍摄页的入口路径"));
expect(out).toContain("UI 入口指引:");
expect(out).toContain(NI("具体设备型号与系统版本"));
});
test("答重复哨兵时只命中用户答的那一处", () => {
const out = applyAnswers(PLAN, answered());
expect(out).toContain("- 仅拍摄页滤镜档位");
expect(out).not.toContain("设备 仅拍摄页滤镜档位");
});
test("计划里的图片引用不受影响", () => {
expect(applyAnswers(PLAN, answered()))
.toContain("images/plan-11111111-2222-3333-4444-555555555555.png");
});
test("没有答案时原样返回", () => {
expect(applyAnswers(PLAN, {})).toBe(PLAN);
});
});
/**
* 渲染层。跑真实的 react-markdown 管线,而不是复刻一遍插件逻辑 —— 这里要验的正是
* 「源文本」与「渲染后的文本」之间那道缝。
*/
async function renderCards(plan: string, comments: Comment[] = []) {
const [{ renderToStaticMarkup }, React, { default: ReactMarkdown }, { default: remarkGfm }] =
await Promise.all([
import("react-dom/server"),
import("react"),
import("react-markdown"),
import("remark-gfm"),
]);
const s = parseSentinels(plan, "(未分节)", "(未注明)");
const html = renderToStaticMarkup(
React.createElement(ReactMarkdown, {
remarkPlugins: [remarkGfm],
rehypePlugins: [rehypePlanMarks(comments, s)],
components: {
// 与 PlanEditor 的 mark 覆写走同一条分支:head 出卡片,其余切片丢掉。
// eslint-disable-next-line @typescript-eslint/no-explicit-any
mark: ({ className, children }: any) => {
const cls = Array.isArray(className) ? className.join(" ") : String(className || "");
const m = cls.match(/snt-(\d+)/);
if (m) {
if (!cls.includes("snt-head")) return null;
return React.createElement("span", { "data-idx": m[1] });
}
return React.createElement("mark", { className: cls }, children);
},
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any, plan),
);
return { html, sentinels: s, idx: [...html.matchAll(/data-idx="(\d+)"/g)].map((x) => Number(x[1])) };
}
describe("rehypePlanMarks", () => {
test("每个哨兵出且只出一张卡片,索引与源顺序一致", async () => {
const { idx } = await renderCards(PLAN);
// 代码块里那条渲染扫描看不到,所以是 5 张而不是 6 张。
expect(idx).toEqual([0, 1, 2, 3, 4]);
});
test("提示里带反引号的哨兵照样出卡片", async () => {
// 这是生产上真实踩到的:评估器很爱在提示里引代码符号,markdown 渲染会把反引号
// 吃掉,源文本里却还在。当初按原文逐字配对,31 份含哨兵的线上计划里有 7 份
// 因此丢卡 —— 退化成不可点的高亮,而用户看不出区别。
const { idx } = await renderCards(PLAN);
expect(idx).toContain(3);
});
test("被 ** 包住的哨兵不会因为跨节点而漏掉", async () => {
const { html } = await renderCards(PLAN);
expect(html).toMatch(/<strong[^>]*><span data-idx="2"><\/span><\/strong>/);
});
test("哨兵原文不会漏到渲染结果里(代码块除外)", async () => {
const { html } = await renderCards(PLAN);
// 代码块是明确的降级路径:扫描跳过 <pre>,那里保留旧的纯文本高亮。
expect(html.replace(/<pre[\s\S]*?<\/pre>/g, "")).not.toContain("NEEDS_INPUT");
expect(html).toContain("这条在代码块里");
});
test("配不上的哨兵不会被错配到相邻那条身上", async () => {
// 代码块里那条(源索引 5)永远进不了渲染文本,前向指针不能因此把后面的卡片
// 整体错位 —— 最后一张卡必须还是 4。
const { idx, sentinels: s } = await renderCards(PLAN);
expect(s).toHaveLength(6);
expect(idx[idx.length - 1]).toBe(4);
});
test("带候选的哨兵仍然只出一张卡", async () => {
// 候选让哨兵变长但仍是一行。真正的陷阱是换行:行首的 `>>>>>>>` 会被 markdown
// 吃成引用块,哨兵在渲染文本里永远闭合不了,卡片直接消失且无任何迹象。
// normalizeSentinels 负责在加载时消除这种情况,这里验证归一化之后确实能配上。
const plan = normalizeSentinels(`## 复现条件
- 设备: <<<<<<< NEEDS_INPUT: 具体设备型号
@OPTION iPhone 15 Pro | 设备池默认机 | 设备:iPhone 15 Pro / iOS 17.5
@OPTION Pixel 8 | 工单截图 | 设备:Pixel 8 / Android 14
>>>>>>>
- 其他: 无`);
const { idx, html, sentinels: s } = await renderCards(plan);
expect(s).toHaveLength(1);
expect(s[0].options).toHaveLength(2);
expect(idx).toEqual([0]);
// 候选行属于哨兵的一部分,必须随卡片一起被替换掉,不能漏到正文里。
expect(html).not.toContain("@OPTION");
expect(html).not.toContain("NEEDS_INPUT");
});
test("哨兵提示里的反引号不会留下空 code 盒子", async () => {
// Evaluators cite symbols with backticks. Markdown turns each into <code>,
// and the card only replaces the head slice — leftover empty <code> tags
// inherit .fix-report-doc code chrome (dark fill + border + radius) and
// show up as a row of blank boxes under the input.
const plan = `- 入口: ${NI("看 `FooAssem` 的 `bar()` 以及 `BazExp` `QuxKey` `Zzz`")}`;
const { html, idx } = await renderCards(plan);
expect(idx).toEqual([0]);
expect(html).not.toMatch(/<code[^>]*>\s*<\/code>/);
expect(html).not.toContain("FooAssem");
expect(html).not.toContain("NEEDS_INPUT");
});
test("批注下划线与卡片共存", async () => {
const { html } = await renderCards(PLAN, [
{ id: 7, start: 0, end: 0, quote: "强度回落到默认值 50", comment: "要写成机制级命题" },
]);
expect(html).toContain("aid-7");
expect(html).toContain("data-idx=");
});
});
describe("nextSentinelIndex", () => {
// 走查按钮是导航,不是任务队列:第二次点击必须移动,哪怕当前卡片还是空的。
// 之前按「第一个未处理项」实现,连点会一直停在同一张卡上。
test("从未点过时落在第一项", () => {
expect(nextSentinelIndex(-1, 3)).toBe(0);
});
test("逐个前进", () => {
expect(nextSentinelIndex(0, 3)).toBe(1);
expect(nextSentinelIndex(1, 3)).toBe(2);
});
test("最后一个绕回第一个", () => {
expect(nextSentinelIndex(2, 3)).toBe(0);
});
test("只有一项时停在原地而不是报错", () => {
expect(nextSentinelIndex(0, 1)).toBe(0);
});
test("没有哨兵时返回 -1", () => {
expect(nextSentinelIndex(-1, 0)).toBe(-1);
});
test("光标越界(计划被编辑短了)时从头开始", () => {
expect(nextSentinelIndex(9, 3)).toBe(0);
});
});