Files
streamline/control-server/test/audit_fix_plan.test.ts
T
2026-11-16 17:26:44 -08:00

279 lines
16 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
AUDIT_FIX_GROUP_CAP, AUDIT_PARK_MS, AUDIT_REFILE_VERDICTS, AUDIT_VERDICTS, TRUSTED_REPRO_MARK, auditBatchTag, auditChecksFor, auditIssueIds, auditIssueNum, parseAuditVerdicts, isTrustedAuditSpec, planAuditFixGroups,
planExplicitAuditFixGroups, planAuditFixMerge, renderAuditFixDescription, renderAuditFixSpec, saturatedAuditTags, standsAlone, type AuditFixPick,
} from "@bbbug/shared/audit_fix_plan";
import { auditParkedUntil, parkAuditTask, parkedAuditTaskIds } from "../src/audit_park";
import { DEFAULT_BUG_RUN_TEMPLATE } from "@bbbug/shared/run_prompt";
import { isValidTag } from "@bbbug/shared/types";
import { specLabel } from "@bbbug/shared/spec_sections";
import type { AuditIssue, AuditPage, AuditReport, AuditShot } from "@bbbug/shared/audit_report";
const iss = (n: number, sev: AuditIssue["sev"] = "P2", cat = "热区"): AuditIssue => ({
n, sev, cat, langs: ["en"], text: `第 ${n} 条问题的现象。`, code: "—",
});
const page = (id: string, surface: string, issues: AuditIssue[]): AuditPage => ({ id, surface, coverage: ["issue"], issues });
const report: Pick<AuditReport, "pages"> & AuditReport = {
title: "走查", meta: [{ label: "截图所用包", value: "TikTokM 47.3.0" }, { label: "账号", value: "@b236874(验证码登录);后来又切过号" }],
langs: ["en"], blocked: [], totals: { issues: 0, instances: 0, bySev: {}, byCat: {} }, coverage: { ok: 0, issue: 0, missing: 0 },
constructed: [{ page: "s1-profile/02-b、s1-profile/03-c", how: "Frida 让列表返回空", script: "hooks/s1-empty.js" }],
routes: [{ page: "s1-profile/01-a", title: "首页", how: "我 → 个人主页" }],
pages: [
page("s1-profile/01-a", "面 1 · Profile", [iss(1), iss(2), iss(3, "P1")]),
page("s1-profile/02-b", "面 1 · Profile", [iss(1), iss(2), iss(3)]),
page("s1-profile/03-c", "面 1 · Profile", [iss(1), iss(2)]),
page("s2-dm/01-inbox", "面 2 · DM", [iss(1, "P3", "功能缺陷 / 持久化同步"), iss(2, "P3", "文案截断")]),
page("s2-dm/02-big", "面 2 · DM", Array.from({ length: 8 }, (_, i) => iss(i + 1, "P3", "布局"))),
],
};
const all = (): AuditFixPick[] => report.pages.flatMap((p) => p.issues.map((i) => ({ pageId: p.id, n: i.n, lang: "en" })));
const keys = (ps: AuditFixPick[]) => ps.map((p) => `${p.pageId.split("/")[1]}#${p.n}`);
describe("planAuditFixGroups", () => {
const groups = planAuditFixGroups(all(), report);
test("P0/P1 和功能类单独成单、不进合并单", () => {
expect(standsAlone(iss(1, "P1"))).toBe(true);
expect(standsAlone(iss(1, "P0", "布局"))).toBe(true);
expect(standsAlone(iss(1, "P3", "功能缺陷 / 持久化同步"))).toBe(true);
expect(standsAlone(iss(1, "P2", "热区"))).toBe(false);
const singles = groups.filter((g) => !g.trusted);
expect(singles.map((g) => keys(g.picks))).toEqual([["01-a#3"], ["01-inbox#1"]]);
});
test("同一个面装箱,页面不拆,满上限换下一张", () => {
const merged = groups.filter((g) => g.trusted && g.surface === "面 1 · Profile").map((g) => keys(g.picks));
// 01-a 剩 2 条 + 02-b 3 条 = 5;再放 03-c 的 2 条会超过 6,换下一张。
expect(merged).toEqual([["01-a#1", "01-a#2", "02-b#1", "02-b#2", "02-b#3"], ["03-c#1", "03-c#2"]]);
});
test("一个页面本身超过上限时才切开这一页", () => {
const dm = groups.filter((g) => g.trusted && g.surface === "面 2 · DM").map((g) => g.picks.length);
expect(dm).toEqual([1, AUDIT_FIX_GROUP_CAP, 2]);
for (const g of groups) expect(g.picks.length).toBeLessThanOrEqual(AUDIT_FIX_GROUP_CAP);
});
test("每条问题恰好进一张单,按报告顺序输出", () => {
const flat = groups.flatMap((g) => keys(g.picks));
expect(new Set(flat).size).toBe(all().length);
expect(flat.length).toBe(all().length);
const ids = auditIssueIds(report);
const firsts = groups.map((g) => Number(ids.get(`${g.picks[0].pageId}#${g.picks[0].n}`)!.slice(2)));
expect([...firsts].sort((a, b) => a - b)).toEqual(firsts);
});
test("报告里找不到的、重复勾的丢掉", () => {
const gs = planAuditFixGroups([{ pageId: "s1-profile/01-a", n: 1, lang: "en" }, { pageId: "s1-profile/01-a", n: 1, lang: "en" }, { pageId: "nope/x", n: 1, lang: "en" }], report);
expect(gs.map((g) => keys(g.picks))).toEqual([["01-a#1"]]);
});
test("合并单标题带条数和面名,单条不带", () => {
const merged = groups.find((g) => g.trusted && g.picks.length === 5)!;
expect(merged.title).toMatch(/^\[UI\] .+ 等 5 个问题 · Profile$/);
expect(groups.find((g) => g.picks.length === 1)!.title).not.toContain("等");
});
});
describe("renderAuditFixSpec", () => {
const g = planAuditFixGroups(all(), report).find((x) => x.trusted && x.picks.length === 5)!;
const ctx = {
data: { report, shots: [{ pageId: "s1-profile/01-a", lang: "en", file: "flow-r2-en-s1-profile-01-a.png", boxes: { 1: [{ x: 10, y: 20, w: 30, h: 4 }] } } as AuditShot] },
source: { id: "e3c30b3d-4825", title: "社交走查" },
notes: { "s1-profile/01-a#1": { note: "热区扩到 44pt" } },
files: { "flow-r2-en-s1-profile-01-a.png": "images/plan-11111111-1111-1111-1111-111111111111.png", "flow-r2-en-s1-profile-01-a.ui.txt": "images/plan-22222222-2222-2222-2222-222222222222.txt" },
};
const spec = renderAuditFixSpec(g, ctx);
test("第一行是复现已确认的声明,bug.run 认得这几个字", () => {
expect(spec.split("\n")[0]).toContain(TRUSTED_REPRO_MARK);
expect(DEFAULT_BUG_RUN_TEMPLATE).toContain(`「${TRUSTED_REPRO_MARK}」`);
});
test("骨架标签齐全、不留 NEEDS_INPUT", () => {
for (const id of ["reproConditions", "condAccount", "condEntry", "condOther", "reproSteps", "postFixVerification", "verifyNegative", "verifyPositive", "verifyMechanism", "verifyAdversarial", "expectedActual", "expected", "actual", "regressionRisks"]) {
expect(spec).toContain(specLabel("bug", id, "zh"));
}
expect(spec).not.toContain("NEEDS_INPUT");
});
test("带上现场:截图、标注框、结构树、期望、构造方式、路由;原因标成线索", () => {
expect(spec).toContain("![F-01](images/plan-11111111-1111-1111-1111-111111111111.png)");
expect(spec).toContain("x 10% · y 20% · 宽 30% · 高 4%");
expect(spec).toContain("images/plan-22222222-2222-2222-2222-222222222222.txt");
expect(spec).toContain("期望:热区扩到 44pt");
expect(spec).toContain("Frida 让列表返回空");
expect(spec).toContain("我 → 个人主页");
expect(spec).toContain("逐条结论");
// 部分修好也交:至少一条已修复就 FIXED,不再「一条未修复整单失败」。
expect(spec).toContain("至少一条「已修复」就提交、推送");
expect(spec).toContain("「不成立」");
expect(spec).not.toContain("所有问题都是「已修复」或「不在本仓」");
// 账号一格只取第一句。
expect(spec).toContain("@b236874(验证码登录)");
expect(spec).not.toContain("后来又切过号");
});
test("描述第一行是标题,截图转存失败时退回文件名", () => {
const desc = renderAuditFixDescription(g, { ...ctx, files: {} });
expect(desc.split("\n")[0]).toBe(g.title);
expect(desc).toContain("来源:验证任务「社交走查」(e3c30b3d-4825)");
expect(desc).toContain("`flow-r2-en-s1-profile-01-a.png`(留在走查任务的证据里");
});
});
describe("批次", () => {
test("标签合 Task.tags 的规则", () => {
expect(auditBatchTag("E3C30B3D-4825-47c5-957e-dece098e0722")).toBe("audit-e3c30b3d");
expect(isValidTag(auditBatchTag("e3c30b3d-4825-47c5-957e-dece098e0722"))).toBe(true);
});
test("跑满的走查批次", () => {
expect(saturatedAuditTags([["audit-aaaa"], ["audit-aaaa", "x"], ["audit-bbbb"], ["social"]], 2)).toEqual(["audit-aaaa"]);
expect(saturatedAuditTags([["social"], ["social"]], 2)).toEqual([]);
});
});
describe("停泊合并", () => {
const P = (id: string, n: number): AuditFixPick => ({ pageId: id, n, lang: "en" });
const parked = [
{ taskId: "t-profile", title: "x", surface: "面 1 · Profile", picks: [P("s1-profile/02-b", 1), P("s1-profile/02-b", 2)] },
{ taskId: "t-dm-full", title: "y", surface: "面 2 · DM", picks: Array.from({ length: 6 }, (_, i) => P("s2-dm/02-big", i + 1)) },
];
test("同一个面的并进停泊单,同页面优先,并满为止", () => {
const { appends, groups } = planAuditFixMerge([P("s1-profile/03-c", 1), P("s1-profile/02-b", 3), P("s1-profile/01-a", 1)], parked, report);
expect(appends).toHaveLength(1);
expect(appends[0].taskId).toBe("t-profile");
expect(keys(appends[0].added)).toEqual(["01-a#1", "02-b#3", "03-c#1"]);
// 并完按报告顺序排、标题按全部问题重算。
expect(keys(appends[0].picks)).toEqual(["01-a#1", "02-b#1", "02-b#2", "02-b#3", "03-c#1"]);
expect(appends[0].title).toContain("等 5 个问题 · Profile");
expect(groups).toEqual([]);
});
test("满了的不并、别的面不并、单独成单的不并", () => {
const { appends, groups } = planAuditFixMerge([P("s2-dm/02-big", 7), P("s2-dm/01-inbox", 1), P("s1-profile/01-a", 3)], parked, report);
expect(appends).toEqual([]);
expect(groups.map((g) => [g.trusted, keys(g.picks)])).toEqual([
[false, ["01-a#3"]], [false, ["01-inbox#1"]], [true, ["02-big#7"]],
]);
});
test("停泊单上已经有的问题不重复并", () => {
expect(planAuditFixMerge([P("s1-profile/02-b", 1)], parked, report)).toEqual({ appends: [], groups: [] });
});
test("认得出控制面写的合并单 TestSpec", () => {
const g = planAuditFixGroups(all(), report).find((x) => x.trusted)!;
expect(isTrustedAuditSpec(renderAuditFixSpec(g, { data: { report, shots: [] }, source: { id: "v", title: "t" }, notes: {} }))).toBe(true);
expect(isTrustedAuditSpec("## 复现条件\n- 账号")).toBe(false);
});
test("停泊期 15 分钟,过期自动清掉", () => {
expect(AUDIT_PARK_MS).toBe(15 * 60_000);
const t0 = 1_000_000;
parkAuditTask("park-a", t0);
expect(parkedAuditTaskIds(t0 + 60_000)).toContain("park-a");
expect(auditParkedUntil("park-a", t0)).toBe(t0 + AUDIT_PARK_MS);
expect(parkedAuditTaskIds(t0 + AUDIT_PARK_MS + 1)).not.toContain("park-a");
expect(auditParkedUntil("park-a", t0 + AUDIT_PARK_MS + 1)).toBeNull();
});
});
describe("逐条结论", () => {
test("解析 FixReport 的逐条结论表;编号、加粗、反引号都认", () => {
const md = [
"## 逐条结论", "", "| 编号 | 结论 | 证据 |", "| --- | --- | --- |",
"| F-60 | 已修复 | `evidence/after-1-f-60.png` |",
"| **F-62** | 未修复 | 实测 12dp,`mech-1-f-62.txt` |",
"| F-07 | 不成立 | 量的是屏幕边 |",
"| F-08 | 不在本仓 | Lynx 页面 |",
"| F-09 | 部分修复 | 不认识的结论不算 |",
"| F-11 | 不成立(走查误判) | 点击探测 probe.txt |",
"| 编号 | 已修复 | 表头不算 |",
"正文里的 F-10 已修复 不是表格行",
].join("\n");
const v = parseAuditVerdicts(md);
expect([...v.keys()].sort((a, b) => a - b)).toEqual([7, 8, 11, 60, 62]);
expect(v.get(11)).toEqual({ verdict: "不成立", evidence: "走查误判:点击探测 probe.txt" });
expect(v.get(62)).toEqual({ verdict: "未修复", evidence: "实测 12dp,mech-1-f-62.txt" });
expect(v.get(7)?.verdict).toBe("不成立");
expect(parseAuditVerdicts(null).size).toBe(0);
});
test("编号和 auditIssueIds 配得上", () => {
const ids = auditIssueIds(report);
expect(auditIssueNum(ids.get("s1-profile/02-b#1")!)).toBe(4);
});
test("TestSpec 规定的结论词和解析器同一套", () => {
const g = planAuditFixGroups(all(), report).find((x) => x.trusted)!;
const spec = renderAuditFixSpec(g, { data: { report, shots: [] }, source: { id: "v", title: "走查" }, notes: {} });
expect(spec).toContain(AUDIT_VERDICTS.map((x) => `「${x}」`).join(""));
expect([...AUDIT_REFILE_VERDICTS].sort()).toEqual(["不成立", "未修复"]);
});
test("再转的单带上上一张的结论", () => {
const g = planAuditFixGroups(all(), report).find((x) => x.trusted)!;
const k = `${g.picks[0].pageId}#${g.picks[0].n}`;
const spec = renderAuditFixSpec(g, {
data: { report, shots: [] }, source: { id: "v", title: "走查" },
notes: { [k]: { prior: { taskId: "old-task", verdict: "不成立", evidence: "量的是屏幕边" } } },
});
expect(spec).toContain("之前转过一张单(old-task),它的逐条结论是「不成立」:量的是屏幕边");
expect(spec).toContain("先对齐双方量的是不是同一个东西");
});
});
describe("走查用的判定规则随单走", () => {
const checks = ["【热区】热区规则原文", "【布局】间距规则原文\n第二行", "【文案与多语言】截断规则原文", "没有类别的老格式"];
test("只挑和问题同类的检查项;类别按面板口径归一", () => {
expect(auditChecksFor(checks, ["热区"])).toEqual(["【热区】热区规则原文"]);
expect(auditChecksFor(checks, ["点击区域", "布局"])).toEqual(["【热区】热区规则原文", "【布局】间距规则原文\n第二行"]);
expect(auditChecksFor(undefined, ["热区"])).toEqual([]);
});
test("有规则时:写进声明、要求同一规则复算、期望不再让修复侧自定标准", () => {
const g = planAuditFixGroups(all(), report).find((x) => x.trusted)!;
const spec = renderAuditFixSpec(g, { data: { report, shots: [] }, source: { id: "v", title: "走查" }, notes: {}, checks });
expect(spec).toContain("**走查用的判定规则**");
expect(spec).toContain("1. 【热区】热区规则原文");
expect(spec).not.toContain("截断规则原文");
expect(spec).toContain("动手前先按同一条规则、同一个参照系复算");
expect(spec).toContain("规则不适用");
expect(spec).toContain("不另定标准");
expect(spec).not.toContain("写明你采用的标准");
});
test("没有规则时退回原来的说法", () => {
const g = planAuditFixGroups(all(), report).find((x) => x.trusted)!;
const spec = renderAuditFixSpec(g, { data: { report, shots: [] }, source: { id: "v", title: "走查" }, notes: {} });
expect(spec).not.toContain("**走查用的判定规则**");
expect(spec).toContain("写明你采用的标准");
expect(spec).toContain("并排写出走查量的和你量的");
});
});
describe("explicit user task groups", () => {
const picks = [all()[0], all()[1], all()[2], all()[8]];
test("preserves cross-page groups and never promotes functional/priority issues to trusted", () => {
const result = planExplicitAuditFixGroups([[picks[0], picks[3]], [picks[1], picks[2]]], picks, report);
expect(result.map((g) => g.picks)).toEqual([[picks[0], picks[3]], [picks[1], picks[2]]]);
expect(result.map((g) => g.trusted)).toEqual([false, false]);
});
test("rejects duplicated, omitted, unselected and over-capacity picks", () => {
expect(() => planExplicitAuditFixGroups([[picks[0]], [picks[0]]], picks, report)).toThrow();
expect(() => planExplicitAuditFixGroups([[picks[0]]], picks, report)).toThrow();
expect(() => planExplicitAuditFixGroups([[all()[4]]], [picks[0]], report)).toThrow();
expect(() => planExplicitAuditFixGroups([all().slice(0, 7)], all().slice(0, 7), report)).toThrow();
});
test("separate user groups stay separate even on the same page", () => {
const result = planExplicitAuditFixGroups([[picks[0]], [picks[1]]], picks.slice(0, 2), report);
expect(result).toHaveLength(2);
expect(result.every((g) => g.trusted)).toBe(true);
});
});