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

370 lines
21 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test, expect, describe } from "bun:test";
import { batchCreateRefusal } from "@bbbug/shared/batch_center";
import {
batchStartOf, buildDigestCard, buildEfficiency, effBucketOf, deltaPts, peopleText, buildFixSeries, buildProgress, digestDue, execRate, fixBucketOf, nextDigestAt, pacificParts, stageOf,
buildLanding, isListedBatch, landingGroupOf, mrLandingOf, qaStateOf, reviewedRate,
type BatchMeta, type ProgressTask,
} from "@bbbug/shared/batch_center";
const H = 3_600_000;
const NOW = Date.UTC(2026, 8, 29, 22); // 2026-09-29 15:00 PDT
let seq = 0;
const t = (p: Partial<ProgressTask>): ProgressTask =>
({ id: `t${++seq}`, status: "fixing", meegoState: null, mrState: null, mrUrl: null, ...p } as ProgressTask);
describe("fix buckets", () => {
test("every status lands in exactly one bucket; closed-ticket unrepro is fixed unless an MR is attached", () => {
expect(fixBucketOf({ status: "fix_succeeded", meegoState: null, mrUrl: null })).toBe("fixed");
expect(fixBucketOf({ status: "manual_review", meegoState: null, mrUrl: null })).toBe("review");
expect(fixBucketOf({ status: "info_required", meegoState: null, mrUrl: null })).toBe("confirm");
expect(fixBucketOf({ status: "unreproducible", meegoState: null, mrUrl: null })).toBe("recall");
expect(fixBucketOf({ status: "unreproducible", meegoState: "OPEN", mrUrl: null })).toBe("recall");
for (const m of ["RESOLVED", "CLOSED", "done"]) {
expect(fixBucketOf({ status: "unreproducible", meegoState: m, mrUrl: null })).toBe("fixed");
expect(fixBucketOf({ status: "unreproducible", meegoState: m, mrUrl: "https://bits/x" })).toBe("closed");
}
expect(fixBucketOf({ status: "archived", meegoState: null, mrUrl: null })).toBe("closed");
// 归档为服务端 bug、不是问题的:平台本来就不该修,摘到 excluded;其他类别和没分类的仍是不再修。
expect(fixBucketOf({ status: "archived", meegoState: null, mrUrl: null, archiveReason: "server_bug" })).toBe("excluded");
expect(fixBucketOf({ status: "archived", meegoState: null, mrUrl: null, archiveReason: "not_a_bug" })).toBe("excluded");
expect(fixBucketOf({ status: "archived", meegoState: null, mrUrl: null, archiveReason: "ai_fix_wrong" })).toBe("closed");
expect(fixBucketOf({ status: "archived", meegoState: null, mrUrl: null, archiveReason: "fixed_elsewhere" })).toBe("closed");
expect(fixBucketOf({ status: "awaiting_fix", meegoState: null, mrUrl: null })).toBe("running");
});
});
describe("buildProgress", () => {
test("out-of-scope archives leave the denominator but are still counted", () => {
const tasks = [
t({ status: "fix_succeeded" }), t({ status: "fix_succeeded" }), t({ status: "manual_review" }),
t({ status: "archived", archiveReason: "ai_fix_wrong" }),
t({ status: "archived", archiveReason: "server_bug" }), t({ status: "archived", archiveReason: "not_a_bug" }),
];
const p = buildProgress(tasks, new Map(), NOW);
expect(p).toMatchObject({ total: 4, excluded: 2, fixed: 2, settled: 3, remaining: 1 });
expect(p.fixRate).toBeCloseTo(2 / 4);
expect(p.buckets.excluded).toBe(2);
const e = buildEfficiency(tasks.map((x) => ({ bucket: fixBucketOf(x), sendbacks: 0 })), 1);
expect(e).toMatchObject({ targeted: 4, excluded: 2 });
expect(e.failRate).toBeCloseTo(1 / 4);
});
test("fix rate keeps everything in the denominator; remaining excludes fixed and closed", () => {
const a = t({ status: "fix_succeeded", mrUrl: "x", mrState: "merged" });
const b = t({ status: "fix_succeeded" });
const tasks = [a, b, t({ status: "manual_review" }), t({ status: "fix_failed" }), t({ status: "cancelled" })];
const p = buildProgress(tasks, new Map([[a.id, NOW - 2 * H], [b.id, NOW - 48 * H]]), NOW);
expect(p.fixRate).toBeCloseTo(2 / 5);
expect(p.merged).toBe(1);
expect(p.remaining).toBe(2);
expect(p.remainingRatio).toBeCloseTo(2 / 5);
expect(p.fixedLast24h).toBe(1);
expect(p.fixRate24hAgo).toBeCloseTo(1 / 5);
expect(Object.values(p.buckets).reduce((x, y) => x + y, 0)).toBe(5);
});
test("progress counts archived / cancelled as settled; fix rate does not", () => {
const a = t({ status: "fix_succeeded" });
const arch = t({ status: "archived" });
const old = t({ status: "cancelled" });
const tasks = [a, arch, old, t({ status: "manual_review" })];
const p = buildProgress(tasks, new Map([[a.id, NOW - 48 * H], [arch.id, NOW - H], [old.id, NOW - 48 * H]]), NOW);
expect(p.fixRate).toBeCloseTo(1 / 4);
expect(p.settled).toBe(3);
expect(p.settledRate).toBeCloseTo(3 / 4);
expect(p.settledRate).toBeCloseTo(1 - p.remainingRatio);
expect(p.archived).toBe(1);
expect(p.fixedLast24h).toBe(0);
expect(p.settledLast24h).toBe(1);
expect(p.settledRate24hAgo).toBeCloseTo(2 / 4);
});
test("empty batch is 0, not NaN", () => {
const p = buildProgress([], new Map(), NOW);
expect(p.fixRate).toBe(0);
expect(p.settledRate).toBe(0);
});
});
test("buildFixSeries accumulates per Shanghai day", () => {
const d0 = Date.UTC(2026, 8, 26, 16); // 09-27 00:00 CST
const s = buildFixSeries([d0 + H, d0 + 25 * H, d0 + 26 * H], d0 + 10 * 60_000, d0 + 50 * H);
expect(s.map((p) => p.day)).toEqual(["2026-09-27", "2026-09-28", "2026-09-29"]);
expect(s.map((p) => p.fixed)).toEqual([1, 3, 3]);
expect(s.map((p) => p.added)).toEqual([1, 2, 0]);
});
describe("digest schedule (14:00 America/Los_Angeles)", () => {
test("pacificParts follows DST", () => {
expect(pacificParts(Date.UTC(2026, 8, 29, 21))).toEqual({ day: "2026-09-29", hour: 14 }); // PDT = UTC-7
expect(pacificParts(Date.UTC(2026, 11, 1, 22))).toEqual({ day: "2026-12-01", hour: 14 }); // PST = UTC-8
});
test("due after 14:00 once per Pacific day", () => {
const before = Date.UTC(2026, 8, 29, 20, 59);
const after = Date.UTC(2026, 8, 29, 21, 5);
expect(digestDue(before, null)).toBe(false);
expect(digestDue(after, null)).toBe(true);
expect(digestDue(after, after - 60_000)).toBe(false);
expect(digestDue(after, Date.UTC(2026, 8, 28, 21, 5))).toBe(true);
});
test("next digest is the upcoming 14:00 when today's is done", () => {
const sent = Date.UTC(2026, 8, 29, 21, 5);
expect(nextDigestAt(sent + H, sent)).toBe(Date.UTC(2026, 8, 30, 21));
expect(nextDigestAt(Date.UTC(2026, 8, 29, 18, 30), null)).toBe(Date.UTC(2026, 8, 29, 21));
});
test("Singapore: 14:00 SGT = 06:00 UTC, days split on Singapore midnight", () => {
const sg = "Asia/Singapore" as const;
expect(digestDue(Date.UTC(2026, 8, 29, 5, 59), null, sg)).toBe(false);
expect(digestDue(Date.UTC(2026, 8, 29, 6, 5), null, sg)).toBe(true);
// 太平洋 9-28 14:05 发过(= 新加坡 9-29 05:05),随后改成新加坡:9-29 已经发过,不会一天两封。
expect(digestDue(Date.UTC(2026, 8, 29, 6, 5), Date.UTC(2026, 8, 28, 21, 5), sg)).toBe(false);
expect(digestDue(Date.UTC(2026, 8, 30, 6, 5), Date.UTC(2026, 8, 28, 21, 5), sg)).toBe(true);
expect(nextDigestAt(Date.UTC(2026, 8, 29, 7), Date.UTC(2026, 8, 29, 6, 5), sg)).toBe(Date.UTC(2026, 8, 30, 6));
});
});
test("digest card carries rate, last-24h, remaining and the ranking, in one language", () => {
const meta: BatchMeta = { tag: "social_relation", name: "社交关系存量", owners: ["boss@x.com"], description: null, createdBy: null, createdAt: null, digestEnabled: true, lastDigestAt: null, digestTz: "America/Los_Angeles", remindDaily: false, lastRemindAt: null, saved: true };
const a = t({ status: "fix_succeeded" });
const progress = buildProgress([a, t({ status: "manual_review" }), t({})], new Map([[a.id, NOW - H]]), NOW);
const counts = { review_nomr: 2, review_mr: 0, merge: 0, recall: 0, confirm: 0 };
const card = buildDigestCard({
meta, progress, now: NOW, last24h: { steps: 7, people: 3, unrecorded: 0 },
leaderboard: { generatedAt: NOW, unassigned: 1, rows: [{ prefix: "lisi", total: 2, counts }, { prefix: "wangwu", total: 1, counts }] },
}, { lang: "zh", panelBase: "https://p" });
const text = JSON.stringify(card);
expect(text).toContain("进度 33%");
expect(text).toContain("修复率 33%");
expect(text).toContain("推进 7 步 · 3 人参与");
expect(text).toContain("剩余 2 条(67%)");
expect(text).toContain("1. lisi · **2**");
expect(text).toContain("1 条待指定经办人");
expect(text).toContain("https://p/batches?tag=social_relation");
expect(text).not.toMatch(/Fix rate/);
});
describe("batchStartOf", () => {
// 北京时间某天 10:00 的时刻。
const cn = (m: number, d: number) => Date.UTC(2026, m - 1, d, 2);
const socialRelation = [cn(7, 16), cn(7, 17), cn(9, 23), ...Array.from({ length: 67 }, () => cn(9, 24)), cn(9, 30)];
test("retagged old bugs don't drag the start back; the trial ticket the day before the bulk import stays", () => {
expect(batchStartOf(socialRelation, null)).toBe(cn(9, 23));
});
test("a batches row saved later from Settings doesn't move the start forward", () => {
expect(batchStartOf(socialRelation, cn(9, 29))).toBe(cn(9, 23));
});
test("a wizard-created batch starts at its import time", () => {
expect(batchStartOf([cn(9, 30), cn(9, 30)], cn(9, 30) - 60_000)).toBe(cn(9, 30) - 60_000);
});
test("empty batch falls back to the row", () => {
expect(batchStartOf([], null)).toBeNull();
expect(batchStartOf([], 5)).toBe(5);
});
});
test("peopleText never reads as 'nobody did anything' when operators just weren't recorded", () => {
expect(peopleText({ steps: 8, people: 0, unrecorded: 8 }, "zh")).toBe("8 步未记录操作人");
expect(peopleText({ steps: 8, people: 2, unrecorded: 3 }, "zh")).toBe("2 人参与,3 步未记录操作人");
expect(peopleText({ steps: 0, people: 0, unrecorded: 0 }, "zh")).toBe("0 人参与");
});
test("the delta is in percentage points and matches the rounded rates people see", () => {
// 21/71 = 29.6% → 30%,24 小时前 18/71 = 25.4% → 25%:差 5 个点,不是 29.6 − 25.4 ≈ 4。
expect(deltaPts(21 / 71, 18 / 71)).toBe(5);
expect(deltaPts(0.5, 0.5)).toBe(0);
});
test("digest states the delta in points and carries the definitions", () => {
const meta: BatchMeta = { tag: "t", name: "n", owners: [], description: null, createdBy: null, createdAt: null, digestEnabled: true, lastDigestAt: null, digestTz: "America/Los_Angeles", remindDaily: false, lastRemindAt: null, saved: true };
const a = t({ status: "fix_succeeded" });
const progress = buildProgress([a, t({ status: "manual_review" })], new Map([[a.id, NOW - H]]), NOW);
const text = JSON.stringify(buildDigestCard({ meta, progress, now: NOW, last24h: { steps: 1, people: 0, unrecorded: 1 }, leaderboard: { generatedAt: NOW, rows: [], unassigned: 0 } }, { lang: "zh", panelBase: "https://p" }));
expect(text).toContain("比 24 小时前 +50 个百分点");
expect(text).toContain("口径:进度 = (已修复 + 不再修) ÷ 这一批的总数");
expect(text).toContain("归档不算修复");
expect(text).toContain("1 步未记录操作人");
});
describe("efficiency", () => {
test("R is counted from human send-backs; failed = won't fix; everything else unfinished is pending", () => {
expect(effBucketOf("fixed", 0)).toBe("r0");
expect(effBucketOf("fixed", 2)).toBe("r1_2");
expect(effBucketOf("fixed", 3)).toBe("r3p");
expect(effBucketOf("closed", 0)).toBe("fail");
for (const b of ["review", "confirm", "recall", "running"] as const) expect(effBucketOf(b, 5)).toBe("pending");
});
test("social_relation as of 10/05: 32 R0, 9 R1–R2, 2 R3+, 20 failed, 4 pending → 34.9 PD at 1 PD per bug", () => {
const rows = [
...Array(32).fill({ bucket: "fixed", sendbacks: 0 }),
...Array(9).fill({ bucket: "fixed", sendbacks: 1 }),
...Array(2).fill({ bucket: "fixed", sendbacks: 4 }),
...Array(20).fill({ bucket: "closed", sendbacks: 0 }),
...Array(4).fill({ bucket: "review", sendbacks: 0 }),
];
const e = buildEfficiency(rows, 1);
expect(e.targeted).toBe(67);
expect(e.pdSaved).toBe(34.9);
expect(e.r0Rate).toBeCloseTo(32 / 67);
expect(e.rnRate).toBeCloseTo(11 / 67);
expect(e.failRate).toBeCloseTo(20 / 67);
expect(buildEfficiency(rows, 0.5).pdSaved).toBe(17.5);
expect(buildEfficiency([], 1)).toMatchObject({ targeted: 0, r0Rate: 0, pdSaved: 0 });
});
});
describe("stage", () => {
const many = (n: number, p: Partial<ProgressTask>) => Array.from({ length: n }, () => t(p));
const stage = (tasks: ProgressTask[]) => stageOf(buildProgress(tasks, new Map(), NOW));
test("just imported: machines still hold the batch → exec", () => {
expect(stage([])).toBe("exec");
expect(stage(many(10, {}))).toBe("exec");
// 机器跑完 7 / 10:平台执行 70% < 80%
expect(stage([...many(3, {}), ...many(7, { status: "manual_review" })])).toBe("exec");
});
test("machines mostly done, reviewed under 80% → review; reviewed 80% → backlog done (landing follow-up)", () => {
const tasks = [...many(2, {}), ...many(8, { status: "manual_review" })];
expect(execRate(buildProgress(tasks, new Map(), NOW))).toBeCloseTo(0.8);
expect(stage(tasks)).toBe("review");
expect(stage([...many(7, { status: "fix_succeeded" }), ...many(3, { status: "manual_review" })])).toBe("review");
// 复核完成 80%:存量完成,进入落地跟进(2026-10-23 起;之前要 95%)
expect(stage([...many(8, { status: "fix_succeeded", meegoState: "RESOLVED" }), ...many(2, { status: "manual_review" })])).toBe("landing");
});
test("thresholds compare the rounded percentages people see", () => {
// 159 / 200 复核完成 = 79.5% → 显示 80%,存量完成
expect(stage([...many(159, { status: "fix_succeeded", meegoState: "RESOLVED" }), ...many(41, { status: "manual_review" })])).toBe("landing");
expect(stage([...many(157, { status: "fix_succeeded", meegoState: "RESOLVED" }), ...many(43, { status: "manual_review" })])).toBe("review");
// 159 / 200 跑完 = 79.5% → 显示 80%,进入人工复核
expect(stage([...many(41, {}), ...many(159, { status: "manual_review" })])).toBe("review");
});
});
describe("landing → settlement", () => {
const many = (n: number, p: Partial<ProgressTask>) => Array.from({ length: n }, () => t(p));
const prog = (tasks: ProgressTask[]) => buildProgress(tasks, new Map(), NOW);
const landed = { status: "fix_succeeded", meegoState: "CLOSED", mrUrl: "u", mrState: "merged" } as const;
const awaitingQa = { status: "fix_succeeded", meegoState: "RESOLVED", mrUrl: "u", mrState: "opened" } as const;
test("reviewed ≥ 80% → landing follow-up until 90% of approved bugs have landed, then landed", () => {
expect(stageOf(prog([...many(19, awaitingQa), t({ status: "manual_review" })]))).toBe("landing");
expect(stageOf(prog([...many(18, landed), ...many(2, awaitingQa)]))).toBe("wrapup");
expect(stageOf(prog([...many(17, landed), ...many(3, awaitingQa)]))).toBe("landing");
// 复核还没到 80%,落地再高也不算完成
expect(stageOf(prog([...many(15, landed), ...many(5, { status: "manual_review" })]))).toBe("review");
});
test("QA reopens don't push it back to review; enough of them push settlement back to landing", () => {
const p = prog([...many(15, landed), ...many(5, { status: "fix_succeeded", meegoState: "REOPENED" })]);
expect(p.settledRate).toBeCloseTo(0.75);
expect(reviewedRate(p)).toBe(1);
expect(p).toMatchObject({ approved: 20, landed: 15 });
expect(stageOf(p)).toBe("landing");
});
test("nothing approved (all archived) settles once reviewed", () => {
expect(stageOf(prog(many(4, { status: "archived" })))).toBe("wrapup");
expect(stageOf(prog([]))).toBe("exec");
});
});
describe("QA reopened", () => {
test("approved bugs whose Meego is REOPENED are reopened, not fixed; OPEN / IN PROGRESS still await QA", async () => {
const { isQaReopened } = await import("@bbbug/shared/types");
const { remindKindOf } = await import("@bbbug/shared/batch_remind");
for (const m of ["RESOLVED", "resolved", "OPEN", "IN PROGRESS", "CLOSED", "closed", "done"]) {
expect(isQaReopened({ status: "fix_succeeded", meegoState: m })).toBe(false);
expect(fixBucketOf({ status: "fix_succeeded", meegoState: m, mrUrl: null })).toBe("fixed");
}
for (const m of ["OPEN", "IN PROGRESS", "RESOLVED"]) expect(qaStateOf(m)).toBe("pending");
for (const m of ["REOPENED", "reopened"]) {
expect(isQaReopened({ status: "fix_succeeded", meegoState: m })).toBe(true);
expect(qaStateOf(m)).toBe("reopened");
expect(fixBucketOf({ status: "fix_succeeded", meegoState: m, mrUrl: "https://bits/x" })).toBe("reopened");
expect(remindKindOf({ status: "fix_succeeded", meegoState: m, mrUrl: null, mrState: null, mergedIntoBaseAt: null } as never)).toBe("qa_reopened");
}
// 没有工单(meegoState 为空)的不算;别的状态也不算。
expect(isQaReopened({ status: "fix_succeeded", meegoState: null })).toBe(false);
expect(isQaReopened({ status: "manual_review", meegoState: "REOPENED" })).toBe(false);
});
test("reopened bugs leave the fixed count and stay in remaining", () => {
const p = buildProgress([
t({ status: "fix_succeeded", meegoState: "RESOLVED" }),
t({ status: "fix_succeeded", meegoState: "REOPENED" }),
t({ status: "manual_review" }),
t({ status: "archived" }),
], new Map(), NOW);
expect(p.fixed).toBe(1);
expect(p.buckets.reopened).toBe(1);
expect(p.remaining).toBe(2);
expect(p.settledRate).toBeCloseTo(0.5);
});
});
describe("landing digest", () => {
test("landing stage sends a post-approval card: landed, deltas since last digest, reopens with review state", async () => {
const { buildLanding, landingGroupOf, DIGEST_STAGES } = await import("@bbbug/shared/batch_center");
expect(DIGEST_STAGES).toEqual(["review", "landing"]);
const row = (id: string, qa: "confirmed" | "pending" | "reopened", mr: "merged" | "open", extra: Record<string, unknown> = {}) => ({
id, title: `bug ${id}`, platform: "ios", meegoId: null, meegoUrl: null, meegoState: qa === "reopened" ? "REOPENED" : qa === "pending" ? "RESOLVED" : "CLOSED",
mrUrl: "u", mrState: mr === "merged" ? "merged" : "opened", mergedAt: mr === "merged" ? NOW - 3_600_000 : null,
approvedAt: NOW - 10 * 86_400_000, approvedBy: null, qa, mr, group: landingGroupOf(qa, mr), triage: null, ...extra,
});
const rows = [
row("a", "confirmed", "merged"), row("b", "confirmed", "merged"), row("c", "confirmed", "open"), row("d", "pending", "open"),
row("e", "reopened", "open"), row("f", "reopened", "open", { triage: { id: "x", taskId: "f", verdict: "fix_wrong", reason: "incomplete", note: null, action: null, meegoState: "REOPENED", by: null, at: NOW } }),
];
const meta: BatchMeta = { tag: "t", name: "n", owners: [], description: null, createdBy: null, createdAt: null, digestEnabled: true, lastDigestAt: null, digestTz: "America/Los_Angeles", remindDaily: false, lastRemindAt: null, saved: true };
const progress = buildProgress([], new Map(), NOW);
const text = JSON.stringify(buildDigestCard({
meta, progress, now: NOW, last24h: { steps: 0, people: 0, unrecorded: 0 }, leaderboard: { generatedAt: NOW, rows: [], unassigned: 0, importer: null },
landing: { x: buildLanding(rows), rows, since: { at: NOW - 86_400_000, snap: { approved: 6, landed: 1, qaConfirmed: 3, merged: 1, reopened: 1 } }, mergedLast24h: 2, falseAlarms: 1 },
}, { lang: "zh", panelBase: "https://p" }));
expect(text).toContain("真正落地 33%");
expect(text).toContain("QA 验过且 MR 已合入 2(+1)");
expect(text).toContain("MR 已合入 2(+1)");
expect(text).toContain("MR 新合入 2 条");
expect(text).toContain("QA 打回 2 条");
expect(text).toContain("1 条待核实");
expect(text).toContain("核实过误报 1 次");
expect(text).toContain("bug e");
expect(text).toContain("待 QA 验证 1(复核通过超过 7 天的 1)");
expect(text).not.toContain("待跟进排行");
});
});
describe("isListedBatch:Bug 治理列表只收真项目", () => {
test("从 Meego 视图建过单的项目进列表", () => {
expect(isListedBatch("fcc_mobile", true, true)).toBe(true);
});
test("建单时随手打的标签不进列表,不管有多少单带着它", () => {
expect(isListedBatch("android", false, false)).toBe(false);
});
test("在标签页上保存过设置、但没从视图建过单的也不算", () => {
expect(isListedBatch("android", true, false)).toBe(false);
});
test("没有元数据行的标签,哪怕有同名导入留痕也不算", () => {
expect(isListedBatch("android", false, true)).toBe(false);
});
test("走查批次不靠视图,照常列出(前端收进次级标签页)", () => {
expect(isListedBatch("audit-b2e6bdb9", true, false)).toBe(true);
expect(isListedBatch("audit-b2e6bdb9", false, false)).toBe(true);
});
});
describe("batchCreateRefusal(非管理员新建治理项目)", () => {
test("没人用过的新标签放行", () => {
expect(batchCreateRefusal(["my_team"], false)).toBeNull();
});
test("已有的标签不行:会覆盖别人项目的负责人", () => {
expect(batchCreateRefusal(["social_relation"], true)).toContain("social_relation");
});
test("走查批次的标签不行,哪怕还没人用", () => {
expect(batchCreateRefusal(["audit-e1e28a58"], false)).toContain("走查");
});
test("必须正好一个标签:没标签进不了项目,多标签能把单塞进别人的项目", () => {
expect(batchCreateRefusal([], false)).not.toBeNull();
expect(batchCreateRefusal(["a", "b"], false)).not.toBeNull();
});
});