import { test, expect, describe } from "bun:test"; import { buildRemindCard, formatDeadline, remindKindOf, resolveRemindLang, type RemindRecipient } from "@bbbug/shared/batch_remind"; import { assembleRecipients, batchImporterOf, decisionsByTask, lastSentByPrefix, platformOwnerOf, remindOwnerOf } from "../src/batch_remind"; import type { Task } from "../src/types"; const H = 3_600_000; const NOW = 1_800_000_000_000; let seq = 0; function task(p: Partial): Task { seq++; return { id: `t${seq}`, title: `bug ${seq}`, meegoId: String(7000000000 + seq), meegoUrl: null, platform: "ios", status: "manual_review", failureReason: null, attempts: 1, tags: ["b"], createdAt: NOW - 10 * H, statusChangedAt: NOW - 24 * H, mrUrl: null, mrState: null, meegoState: null, mergedIntoBaseAt: null, watchers: [], ...p, } as Task; } describe("remindKindOf", () => { test("review splits on MR presence", () => { expect(remindKindOf(task({ status: "manual_review" }))).toBe("review_nomr"); expect(remindKindOf(task({ status: "manual_review", mrUrl: "https://bits/x" }))).toBe("review_mr"); }); test("approved bugs waiting for an MR merge need no click on the platform, so they're never pending", () => { expect(remindKindOf(task({ status: "fix_succeeded", meegoState: "closed", mrState: "opened" }))).toBeNull(); expect(remindKindOf(task({ status: "fix_succeeded", meegoState: "resolved", mrState: "opened" }))).toBeNull(); expect(remindKindOf(task({ status: "fix_succeeded", meegoState: "closed", mrState: "merged" }))).toBeNull(); }); test("unconfirmed unreproducible and fix_failed are recall; confirmed / cancelled / machine states are not", () => { expect(remindKindOf(task({ status: "unreproducible" }))).toBe("recall"); expect(remindKindOf(task({ status: "unreproducible", meegoState: "RESOLVED" }))).toBeNull(); expect(remindKindOf(task({ status: "fix_failed" }))).toBe("recall"); expect(remindKindOf(task({ status: "cancelled" }))).toBeNull(); for (const s of ["initializing", "awaiting_fix", "fixing", "archived"] as const) { expect(remindKindOf(task({ status: s }))).toBeNull(); } expect(remindKindOf(task({ status: "pending_confirmation" }))).toBe("confirm"); }); }); describe("decisions and history", () => { test("only manual_review → approve / reject count", () => { const m = decisionsByTask([ { targetId: "a", fromStatus: "manual_review", toStatus: "fix_succeeded" }, { targetId: "a", fromStatus: "manual_review", toStatus: "awaiting_fix" }, { targetId: "a", fromStatus: "unreproducible", toStatus: "awaiting_fix" }, { targetId: "b", fromStatus: "manual_review", toStatus: "pending_confirmation" }, ]); expect(m.get("a")).toEqual({ approve: 1, reject: 1 }); expect(m.has("b")).toBe(false); }); test("test sends and failures never count as 'already reminded'", () => { const m = lastSentByPrefix([ { at: 30, by: "x", test: true, results: [{ prefix: "p", ok: true, lang: "zh", items: 1 }] }, { at: 20, by: "x", test: false, results: [{ prefix: "p", ok: true, lang: "zh", items: 1 }, { prefix: "q", ok: false, lang: "zh", items: 1 }] }, ]); expect(m.get("p")).toBe(20); expect(m.has("q")).toBe(false); }); }); describe("assembleRecipients", () => { test("groups by assignee, credits decisions on settled tasks, lists unassigned", () => { const a1 = task({ status: "manual_review", attempts: 3 }); const a2 = task({ status: "unreproducible" }); const aDone = task({ status: "fix_succeeded", meegoState: "resolved" }); // settled, but a had decided on it const b1 = task({ status: "manual_review", mrUrl: "https://bits/1" }); const orphan = task({ status: "manual_review" }); const cOnlyDone = task({ status: "fix_succeeded", meegoState: "resolved" }); const owners = new Map([ [a1.id, "Alice@bytedance.com"], [a2.id, "alice@bytedance.com"], [aDone.id, "alice@bytedance.com"], [b1.id, "bob@bytedance.com"], [orphan.id, null], [cOnlyDone.id, "carol@bytedance.com"], ]); const decisions = new Map([[aDone.id, { approve: 1, reject: 0 }], [a1.id, { approve: 0, reject: 2 }], [cOnlyDone.id, { approve: 1, reject: 0 }]]); const { recipients, unassigned } = assembleRecipients({ tasks: [aDone, a1, a2, b1, orphan, cOnlyDone], owners, decisions, hasToken: (p) => p === "bob", savedLang: (p) => (p === "bob" ? "en" : null), lastSent: new Map([["bob", 5]]), }); expect(recipients.map((r) => r.prefix)).toEqual(["alice", "bob"]); // carol has nothing pending const alice = recipients[0]; expect(alice.items.length).toBe(2); expect(alice.counts.review_nomr).toBe(1); expect(alice.counts.recall).toBe(1); expect(alice.late).toBe(1); expect(alice.decisions).toEqual({ approve: 1, reject: 2 }); expect(alice.hasBitsToken).toBe(false); expect(recipients[1]).toMatchObject({ savedLang: "en", hasBitsToken: true, lastSentAt: 5 }); expect(unassigned.map((u) => u.id)).toEqual([orphan.id]); }); }); function recipient(p: Partial = {}): RemindRecipient { return { prefix: "alice", email: "alice@bytedance.com", items: [ { id: "i1", title: "[iOS] a [b] title", platform: "ios", kind: "review_nomr", round: 3, status: "manual_review", since: NOW - 48 * H }, { id: "i2", title: "recall one", platform: "android", kind: "recall", round: 1, status: "unreproducible", since: NOW - 24 * H }, ], counts: { review_nomr: 1, review_mr: 0, merge: 0, recall: 1, confirm: 0 }, late: 1, hasBitsToken: false, decisions: { approve: 0, reject: 1 }, savedLang: null, lastSentAt: null, ...p, }; } const base = { tag: "social_relation", panelBase: "https://sl", bitsTokenUrl: "https://bits/token", now: NOW }; const text = (card: unknown) => JSON.stringify(card); describe("buildRemindCard", () => { test("says what to do, never why the platform failed", () => { for (const lang of ["zh", "en"] as const) { const s = text(buildRemindCard(recipient(), { ...base, lang })); expect(s).not.toMatch(/过期|服务账号|Invalid token|expired|service account|不是你的问题|not your fault/i); } }); test("token step and Bits button only for people without a token who have MR-less reviews", () => { expect(text(buildRemindCard(recipient(), { ...base, lang: "zh" }))).toContain("上传 Bits Token"); const withToken = text(buildRemindCard(recipient({ hasBitsToken: true }), { ...base, lang: "zh" })); expect(withToken).not.toContain("上传 Bits Token"); expect(withToken).not.toContain("https://bits/token"); }); test("round tag, links, deadline and sections", () => { const s = text(buildRemindCard(recipient(), { ...base, lang: "zh", deadline: "2026-10-01" })); expect(s).toContain("第 3 轮结果"); expect(s).toContain("轮次原则"); expect(s).toContain("https://sl/bugfix?id=i1"); expect(s).toContain("10 月 1 日(周四)"); expect(s).toContain("[【iOS】 a 【b】 title]("); // brackets can't break the markdown link expect(s).toContain("谢谢你已经在平台上做过 1 次决策"); }); test("languages", () => { const en = text(buildRemindCard(recipient(), { ...base, lang: "en" })); expect(en).toContain("waiting for you"); expect(en).not.toMatch(/待处理|复核/); const zh = text(buildRemindCard(recipient(), { ...base, lang: "zh" })); expect(zh).toContain("待处理"); expect(zh).not.toMatch(/waiting|Approve|review the report/i); // one language per card, never mixed }); test("a test send never @-mentions the real recipient", () => { const live = text(buildRemindCard(recipient(), { ...base, lang: "zh", recipientOpenId: "ou_alice" })); expect(live).toContain(""); const t = text(buildRemindCard(recipient(), { ...base, lang: "zh", recipientOpenId: "ou_alice", testFor: "alice" })); expect(t).not.toContain("ou_alice"); expect(t).toContain("测试预览"); }); }); describe("small helpers", () => { test("auto language: saved preference, else Chinese", () => { expect(resolveRemindLang("auto", "en")).toBe("en"); expect(resolveRemindLang("auto", null)).toBe("zh"); expect(resolveRemindLang("zh", "en")).toBe("zh"); }); test("deadline formatting rejects junk", () => { expect(formatDeadline("2026-10-01")).toEqual({ zh: "10 月 1 日(周四)", en: "Thu, Oct 1" }); expect(formatDeadline("2026-02-30")).toBeNull(); expect(formatDeadline("tomorrow")).toBeNull(); expect(formatDeadline(null)).toBeNull(); }); }); test("「已等 N 天」 counts from the batch start, not from months before the bug joined", () => { const t = task({ status: "manual_review", statusChangedAt: NOW - 60 * 24 * H }); const base = { tasks: [t], owners: new Map([[t.id, "a@x.com"]]), decisions: new Map(), hasToken: () => true, savedLang: () => null, lastSent: new Map() }; expect(assembleRecipients(base).recipients[0].items[0].since).toBe(NOW - 60 * 24 * H); expect(assembleRecipients({ ...base, startedAt: NOW - 5 * 24 * H }).recipients[0].items[0].since).toBe(NOW - 5 * 24 * H); }); describe("batchImporterOf", () => { test("whoever created at least half the batch is the importer; retagged old bugs keep their own creators", () => { const creators = [...Array(68).fill("Jingyuan.Yi@bytedance.com"), "a@x.com", "b@x.com", null]; expect(batchImporterOf(creators)).toBe("jingyuan.yi@bytedance.com"); }); test("no majority → no importer, nobody gets excluded", () => { expect(batchImporterOf(["a@x.com", "b@x.com", "c@x.com"])).toBeNull(); expect(batchImporterOf([])).toBeNull(); }); }); describe("platformOwnerOf", () => { const IMP = "jingyuan.yi@bytedance.com"; const GROUP = "oc_7228e81a8c1ae1d97983e1fa8198bb7b"; test("imported bug: skips the importer as creator and watcher, lands on the import-time Meego assignee", () => { expect(platformOwnerOf([IMP, "Bellay.Shen@bytedance.com", GROUP], IMP, IMP)).toBe("bellay.shen@bytedance.com"); }); test("a real creator wins over watchers", () => { expect(platformOwnerOf(["a@x.com"], "Creator@x.com", IMP)).toBe("creator@x.com"); }); test("only the importer and groups → nobody", () => { expect(platformOwnerOf([IMP, GROUP, "ou_123"], IMP, IMP)).toBeNull(); expect(platformOwnerOf(null, null, null)).toBeNull(); }); test("no importer → nobody is excluded", () => { expect(platformOwnerOf([IMP], IMP, null)).toBe(IMP); }); }); describe("remindOwnerOf", () => { const IMP = "jingyuan.yi@bytedance.com"; const base = { watchers: [IMP, "rd.a@bytedance.com"], creatorEmail: IMP, importer: IMP }; test("open ticket follows the live Meego assignee, including a reassignment", () => { expect(remindOwnerOf({ ...base, meego: { state: "IN PROGRESS", assignees: ["RD.B@bytedance.com"] } })).toBe("rd.b@bytedance.com"); }); test("reopened ticket follows Meego again", () => { expect(remindOwnerOf({ ...base, meego: { state: "REOPENED", assignees: ["rd.b@bytedance.com"] } })).toBe("rd.b@bytedance.com"); }); test("open ticket explicitly assigned to the importer still counts", () => { expect(remindOwnerOf({ ...base, meego: { state: "OPEN", assignees: [IMP] } })).toBe(IMP); }); test("resolved / done / closed ticket ignores Meego (by then it is the QA) and uses the platform owner", () => { for (const state of ["RESOLVED", "resolved", "CLOSED", "closed", "done", "close"]) { expect(remindOwnerOf({ ...base, meego: { state, assignees: ["qa.x@bytedance.com"] } })).toBe("rd.a@bytedance.com"); } }); test("no ticket, failed lookup, or nobody on Meego → platform owner", () => { expect(remindOwnerOf({ ...base, meego: null })).toBe("rd.a@bytedance.com"); expect(remindOwnerOf({ ...base, meego: { state: "OPEN", assignees: [] } })).toBe("rd.a@bytedance.com"); }); });