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

209 lines
11 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
attribute, validLabel, gateCandidate, statsOf, candidateIdentity, insertPersonalPrefs, noteKey, MINING, peopleOfItem,
type PracticeRow,
} from "@bbbug/shared/review_mining";
import { codeConventionsBlock, replaceConventionsBlock, CODE_CONVENTIONS_HEADING } from "@bbbug/shared/code_conventions";
import { resolveLines } from "@bbbug/shared/prompt_platform";
import { DEFAULT_BUG_RUN_TEMPLATE, DEFAULT_FEATURE_RUN_FIRST_TEMPLATE, DEFAULT_FEATURE_RUN_SECOND_TEMPLATE } from "@bbbug/shared/run_prompt";
import { REVIEW_LABEL_PROMPT_TEMPLATE, CONVENTION_CLUSTER_PROMPT_TEMPLATE, fillMiningPrompt } from "@bbbug/shared/review_mining_prompt";
// 只测纯逻辑:control-server/src/review_mining.ts 一 import 就要连库。
const row = (over: Partial<PracticeRow> & { key: string }): PracticeRow => ({
kind: "feature", targetId: over.key, platform: "ios", reviewer: null, reviewerVia: null,
scope: "repo", topic: "ab_switch_wrap", rule: "改动用 AB 包裹", excerpt: "", at: 1, ...over,
});
describe("归属:谁写的这条意见", () => {
test("note.reviewer 优先,邮箱归一成前缀", () => {
expect(attribute({ at: 1000, reviewer: "Hazel.Li@bytedance.com" }, [{ ts: 1000, actor: "other" }]))
.toEqual({ reviewer: "hazel.li", via: "note" });
});
test("没有 reviewer 时取 60 秒内最近的打回流转", () => {
const actors = [{ ts: 1000 + 50_000, actor: "far@x.com" }, { ts: 1000 - 2_000, actor: "near@x.com" }];
expect(attribute({ at: 1000 }, actors)).toEqual({ reviewer: "near", via: "transition" });
});
test("超出窗口的流转不认,也不拿建单人顶替", () => {
expect(attribute({ at: 0 }, [{ ts: 61_000, actor: "a" }])).toEqual({ reviewer: null, via: null });
});
});
describe("标签校验", () => {
const keys = new Set(["bug:t1:1:human"]);
test("不认识的 key / 类别丢掉", () => {
expect(validLabel({ key: "bug:x:1:human", cat: "defect" }, keys)).toBeNull();
expect(validLabel({ key: "bug:t1:1:human", cat: "nonsense" }, keys)).toBeNull();
});
test("非 practice 类别清空 scope / topic / rule", () => {
const l = validLabel({ key: "bug:t1:1:human", cat: "defect", scope: "global", topic: "x", rule: "y", preventable: true }, keys)!;
expect([l.scope, l.topic, l.rule]).toEqual([null, null, null]);
});
test("practice 抽不出 rule 时降成 other", () => {
expect(validLabel({ key: "bug:t1:1:human", cat: "code_practice", scope: "global", rule: "" }, keys)!.cat).toBe("other");
});
test("topic 归一成 snake_case", () => {
expect(validLabel({ key: "bug:t1:1:human", cat: "code_practice", scope: "repo", topic: "AB Switch-Wrap", rule: "r" }, keys)!.topic)
.toBe("ab_switch_wrap");
});
});
describe("门槛按证据重算,不信模型", () => {
const rows = [
row({ key: "a", targetId: "t1", reviewer: "p1", reviewerVia: "note" }),
row({ key: "b", targetId: "t2", reviewer: "p2", reviewerVia: "transition" }),
row({ key: "c", targetId: "t3", reviewer: null }),
row({ key: "d", targetId: "t3", reviewer: "p1", reviewerVia: "note" }),
];
const byKey = new Map(rows.map((r) => [r.key, r]));
test("全局 / 仓库级:≥3 单、≥2 人才过;认不出的人不算人数", () => {
const g = gateCandidate({ scope: "repo", platform: "ios", family: "ab_switch_wrap", rule: "r", evidence: ["a", "b", "c"] }, byKey);
expect(g.ok).toBe(true);
if (g.ok) expect(g.stats).toMatchObject({ notes: 3, targets: 3, people: 2, unattributed: 1 });
const two = gateCandidate({ scope: "global", family: "x", rule: "r", evidence: ["a", "b"] }, byKey);
expect(two.ok).toBe(false);
});
test("模型编的证据 key 直接忽略", () => {
const g = gateCandidate({ scope: "global", family: "x", rule: "r", evidence: ["a", "ghost1", "ghost2", "ghost3"] }, byKey);
expect(g.ok).toBe(false);
});
test("repo 候选必须指明哪一端", () => {
const g = gateCandidate({ scope: "repo", platform: null, family: "x", rule: "r", evidence: ["a", "b", "c"] }, byKey);
expect(g.ok).toBe(false);
});
test("个人候选只数本人、来源可靠的证据", () => {
const mine = [
row({ key: "m1", targetId: "t1", reviewer: "p1", reviewerVia: "note" }),
row({ key: "m2", targetId: "t2", reviewer: "p1", reviewerVia: "transition" }),
row({ key: "m3", targetId: "t2", reviewer: "p1", reviewerVia: "note" }),
row({ key: "o1", targetId: "t4", reviewer: "p2", reviewerVia: "note" }),
];
const m = new Map(mine.map((r) => [r.key, r]));
const ok = gateCandidate({ scope: "personal", owner: "P1@x.com", family: "concise_comments", rule: "r", evidence: ["m1", "m2", "m3", "o1"] }, m);
expect(ok.ok).toBe(true);
if (ok.ok) { expect(ok.draft.owner).toBe("p1"); expect(ok.draft.evidence).toEqual(["m1", "m2", "m3"]); }
const thin = gateCandidate({ scope: "personal", owner: "p1", family: "x", rule: "r", evidence: ["m1", "m2"] }, m);
expect(thin.ok).toBe(false);
});
test("同一题目的身份", () => {
expect(candidateIdentity({ scope: "repo", platform: "ios", owner: null, family: "ab" })).toBe("repo|ios|-|ab");
});
test("statsOf 按平台计数", () => {
expect(statsOf(rows).platforms).toEqual({ ios: 4 });
});
});
describe("代码规范块", () => {
const templates = { bug: DEFAULT_BUG_RUN_TEMPLATE, f1: DEFAULT_FEATURE_RUN_FIRST_TEMPLATE, f2: DEFAULT_FEATURE_RUN_SECOND_TEMPLATE };
test("三份执行模板都带上了,且按平台只展开本端的仓库约定", () => {
for (const [name, t] of Object.entries(templates)) {
expect(t, name).toContain(CODE_CONVENTIONS_HEADING);
const ios = resolveLines(t, "ios").lines.join("\n");
const android = resolveLines(t, "android").lines.join("\n");
expect(ios, name).toContain("iOS 仓约定");
expect(ios, name).not.toContain("Android 仓约定");
expect(android, name).toContain("Android 仓约定");
expect(android, name).not.toContain("iOS 仓约定");
}
});
test("feature 线有 AB 门控默认值规则,bug 线只保留两端共有的 AB 开关写法", () => {
expect(codeConventionsBlock("feature", "")).toContain("AB 门控(功能改动默认要有)");
expect(codeConventionsBlock("bug", "")).not.toContain("AB 门控(功能改动默认要有)");
expect(codeConventionsBlock("bug", "")).toContain("AB 开关的写法(两条线都适用)");
});
test("模板里抄的原文与 code_conventions.ts 一致(不一致跑 bun scripts/sync-code-conventions.ts)", () => {
for (const [name, t] of Object.entries(templates)) expect(replaceConventionsBlock(t), name).toBe(t);
});
test("同步能把过期的块换成现行版本", () => {
const stale = DEFAULT_FEATURE_RUN_FIRST_TEMPLATE.replace("**最小改动**", "**旧的最小改动**");
expect(replaceConventionsBlock(stale)).toBe(DEFAULT_FEATURE_RUN_FIRST_TEMPLATE);
});
test("规范块不引入新占位符(老卫星不会填)", () => {
expect(codeConventionsBlock("feature", " ")).not.toMatch(/\{\{[a-zA-Z]+\}\}/);
});
});
describe("个人口味插入", () => {
const base = resolveLines(DEFAULT_FEATURE_RUN_FIRST_TEMPLATE, "ios").lines.join("\n");
test("插在规范块末尾,缩进与规范块一致", () => {
const out = insertPersonalPrefs(base, CODE_CONVENTIONS_HEADING, [{ owner: "janice.wang", rules: ["不加大段注释"] }]);
const lines = out.split("\n");
const h = lines.findIndex((l) => l.includes(CODE_CONVENTIONS_HEADING));
const p = lines.findIndex((l) => l.includes("个人代码口味"));
expect(p).toBeGreaterThan(h);
expect(lines[p].startsWith(" - ")).toBe(true);
expect(lines[p + 1]).toBe(" - janice.wang:不加大段注释");
// 规范块的子项全部在口味块之前
expect(lines.slice(h + 1, p).every((l) => l.startsWith(" - "))).toBe(true);
expect(lines[p - 1]).toContain("iOS 仓约定");
});
test("没有口味 / 找不到规范块时原样返回", () => {
expect(insertPersonalPrefs(base, CODE_CONVENTIONS_HEADING, [])).toBe(base);
expect(insertPersonalPrefs("无规范块", CODE_CONVENTIONS_HEADING, [{ owner: "a", rules: ["r"] }])).toBe("无规范块");
});
test("最多注入的人数和条数有上限", () => {
const many = Array.from({ length: 5 }, (_, i) => ({ owner: `p${i}`, rules: Array.from({ length: 9 }, (_, j) => `r${j}`) }));
const out = insertPersonalPrefs(base, CODE_CONVENTIONS_HEADING, many);
expect((out.match(/ - p\d:/g) ?? []).length).toBe(MINING.prefsPeople * MINING.prefsPerPerson);
});
});
describe("写法偏好随口味块注入", () => {
const base = resolveLines(DEFAULT_BUG_RUN_TEMPLATE, "android").lines.join("\n");
test("多行偏好整段缩进到本人名下,不留空行,也不变成规范块的同级条目", () => {
const taste = "- 所有改动都加 AB 开关\n\n- 补单测";
const out = insertPersonalPrefs(base, CODE_CONVENTIONS_HEADING, [{ owner: "rd.a", rules: [], taste }]);
const lines = out.split("\n");
const p = lines.findIndex((l) => l.includes("个人代码口味"));
const indent = lines[p].slice(0, lines[p].indexOf("- "));
expect(lines[p + 1]).toBe(`${indent} - rd.a 的写法偏好(本人填写):`);
expect(lines[p + 2]).toBe(`${indent} - 所有改动都加 AB 开关`);
expect(lines[p + 3]).toBe(`${indent} - 补单测`);
// 偏好之后紧接着的是模板原有的下一条,而不是空行
expect(lines[p + 4].trim()).not.toBe("");
expect(lines[p + 4].startsWith(`${indent} `)).toBe(false);
});
test("挖掘规则在前、偏好在后;只有偏好、没有规则的人也会注入", () => {
const out = insertPersonalPrefs(base, CODE_CONVENTIONS_HEADING, [
{ owner: "rev.b", rules: ["不加大段注释"], taste: "补单测" },
{ owner: "imp.c", rules: [], taste: " " },
{ owner: "rd.a", rules: [], taste: "AB 开关" },
]);
const block = out.slice(out.indexOf("个人代码口味"));
expect(block.indexOf("rev.b:不加大段注释")).toBeLessThan(block.indexOf("rev.b 的写法偏好"));
expect(block).toContain("rd.a 的写法偏好");
expect(block).not.toContain("imp.c"); // 空白偏好等于没写
});
});
describe("派单相关人", () => {
const history = [
{ at: 1, source: "human", reviewer: "Old.Rev@bytedance.com", text: "x" },
{ at: 5, source: "human", reviewer: "new.rev", text: "y" },
{ at: 9, source: "critic", reviewer: "bot", text: "z" },
];
test("复核人(新的在前)→ Meego 经办人 → 建单人,去重", () => {
expect(peopleOfItem("importer@bytedance.com", history, ["rd.a@bytedance.com", "new.rev@bytedance.com"]))
.toEqual(["new.rev", "old.rev", "rd.a", "importer"]);
});
test("批量导入还没人复核时,经办人排在导入人前面", () => {
expect(peopleOfItem("importer@bytedance.com", [], ["rd.a@bytedance.com"])).toEqual(["rd.a", "importer"]);
});
test("不传经办人时与原来一致", () => {
expect(peopleOfItem("c@bytedance.com", history)).toEqual(["new.rev", "old.rev", "c"]);
});
});
describe("挖掘提示词", () => {
test("占位符都能填上", () => {
const a = fillMiningPrompt(REVIEW_LABEL_PROMPT_TEMPLATE, { notes: "x" });
const b = fillMiningPrompt(CONVENTION_CLUSTER_PROMPT_TEMPLATE, { conventions: "c", existing: "e", rows: "r" });
expect(a).not.toMatch(/\{\{\w+\}\}/);
expect(b).not.toMatch(/\{\{\w+\}\}/);
});
test("key 形状", () => {
expect(noteKey("bug", "abc-1", 123, "human")).toBe("bug:abc-1:123:human");
});
});