import { test, expect, describe } from "bun:test"; import { NUDGE_EVERY_MS, NUDGE_FIRST_AFTER_MS, NUDGE_MAX, PENDING_REWORK_TTL_MS, ballOf, canRework, inWorkHours, meegoCommentForRework, nextNudgeState, nudgeDue, nudgeText, parseGroupMessage, parseLarkContent, pendingStillValid, reworkCheck, reworkNoteText, roleOf, stripMentions, type NudgeState, type PendingRework, actorLabel, bjTime, readonlyReply, statusDigest, } from "@bbbug/shared/review_group"; // 2026-11-10 周二 11:00 北京时间 = 03:00 UTC。 const WORK = Date.UTC(2026, 10, 10, 3, 0); const H = 3600_000; describe("群消息 → 意图", () => { test("@ 占位符去掉;空消息和「帮助」都是 help", () => { expect(stripMentions("@_user_1 打回 @_user_2 xx")).toBe("打回 xx"); expect(parseGroupMessage("@_user_1")).toEqual({ kind: "help" }); expect(parseGroupMessage("@_user_1 帮助")).toEqual({ kind: "help" }); }); test("状态 / 确认 / 取消", () => { expect(parseGroupMessage("@_user_1 状态")).toEqual({ kind: "status" }); expect(parseGroupMessage("@_user_1 进展?")).toEqual({ kind: "status" }); expect(parseGroupMessage("@_user_1 确认")).toEqual({ kind: "confirm" }); expect(parseGroupMessage("@_user_1 取消")).toEqual({ kind: "cancel" }); }); test("打回 / 重修带原因;关键字后的分隔符都认", () => { expect(parseGroupMessage("@_user_1 打回:头像还是没对齐")).toEqual({ kind: "rework", text: "头像还是没对齐" }); expect(parseGroupMessage("@_user_1 重修 iPad 横屏也要修")).toEqual({ kind: "rework", text: "iPad 横屏也要修" }); expect(parseGroupMessage("@_user_1 打回")).toEqual({ kind: "rework", text: "" }); }); test("「打回」得是开头的词:句中提到不算,免得一句讨论就触发重修", () => { expect(parseGroupMessage("@_user_1 这个是不是要打回")).toEqual({ kind: "note", text: "这个是不是要打回" }); expect(parseGroupMessage("@_user_1 打回车键没反应")).toEqual({ kind: "note", text: "打回车键没反应" }); }); }); describe("飞书消息内容", () => { test("text / image", () => { expect(parseLarkContent("text", JSON.stringify({ text: "@_user_1 打回 xx" }))).toEqual({ text: "@_user_1 打回 xx", imageKeys: [], unsupported: null }); expect(parseLarkContent("image", JSON.stringify({ image_key: "img_1" }))).toEqual({ text: "", imageKeys: ["img_1"], unsupported: null }); }); test("post:拼文字、收图片,两种形状都认", () => { const flat = { title: "", content: [[{ tag: "at", user_id: "@_user_1", user_name: "Streamline" }, { tag: "text", text: " 打回 还是错位" }], [{ tag: "img", image_key: "img_a" }]] }; const parsed = parseLarkContent("post", JSON.stringify(flat)); expect(parsed).toEqual({ text: "@_user_1 打回 还是错位", imageKeys: ["img_a"], unsupported: null }); expect(parseGroupMessage(parsed.text)).toEqual({ kind: "rework", text: "还是错位" }); const byLang = { zh_cn: { title: "标题", content: [[{ tag: "text", text: "正文" }, { tag: "img", image_key: "img_b" }]] } }; expect(parseLarkContent("post", JSON.stringify(byLang))).toEqual({ text: "标题\n正文", imageKeys: ["img_b"], unsupported: null }); }); test("视频 / 文件只报类型", () => { expect(parseLarkContent("media", JSON.stringify({ file_key: "f" })).unsupported).toBe("media"); }); }); describe("角色", () => { const people = { assignees: ["rd@bytedance.com"], reporter: "qa@bytedance.com", watchers: ["owner@bytedance.com"] }; test("经办人 RD、创建人 QA、watchers 按 QA 一侧、管理员兜底、其他人不能打回", () => { expect(roleOf("rd@bytedance.com", people, false)).toBe("rd"); expect(roleOf("QA@ByteDance.com", people, false)).toBe("qa"); expect(roleOf("owner", people, false)).toBe("qa"); expect(roleOf("boss@bytedance.com", people, true)).toBe("admin"); expect(roleOf("passerby@bytedance.com", people, false)).toBe("other"); expect(roleOf(null, people, false)).toBe("other"); expect(canRework("other")).toBe(false); expect(canRework("qa")).toBe(true); }); }); describe("能不能从群里打回", () => { const t = (status: string, extra: Partial<{ mrState: string | null; meegoState: string | null }> = {}) => ({ status, mrState: null, meegoState: null, ...extra }); test("待复核 / 复现不了走面板同一个打回", () => { expect(reworkCheck(t("manual_review"), false)).toEqual({ ok: true, path: "review_reject" }); expect(reworkCheck(t("unreproducible"), false)).toEqual({ ok: true, path: "review_reject" }); }); test("已修复:QA 先验收的项目里是 QA 打回;默认策略下要去 Meego 重开;合入了就不行", () => { expect(reworkCheck(t("fix_succeeded", { meegoState: "resolved" }), true)).toEqual({ ok: true, path: "qa_reject" }); expect(reworkCheck(t("fix_succeeded", { meegoState: "resolved" }), false).ok).toBe(false); expect(reworkCheck(t("fix_succeeded", { mrState: "merged", meegoState: "resolved" }), true).ok).toBe(false); }); test("正在修 / 还在评估都不行,并说清楚为什么", () => { const r = reworkCheck(t("fixing"), true); expect(r.ok).toBe(false); if (!r.ok) expect(r.reason).toContain("正在修"); expect(reworkCheck(t("pending_confirmation"), true).ok).toBe(false); }); }); describe("球在谁手里", () => { const t = (status: string, meegoState: string | null = null, mrState: string | null = null) => ({ status, meegoState, mrState }); test("待复核 → RD;QA 验收 → QA;QA 关单没合入 → RD 合码", () => { expect(ballOf(t("manual_review"), { handsToQa: false })).toMatchObject({ stage: "rd_review", who: "rd" }); expect(ballOf(t("fix_succeeded", "resolved", "opened"), { handsToQa: true })).toMatchObject({ stage: "qa_verify", who: "qa" }); expect(ballOf(t("fix_succeeded", "closed", "opened"), { handsToQa: false })).toMatchObject({ stage: "merge", who: "rd" }); expect(ballOf(t("fix_succeeded", "closed", "merged"), { handsToQa: false })).toBeNull(); }); test("复现不了:没交 QA 时等 RD 确认;交了 QA 就等 QA 判断;RD 已确认则不等人", () => { expect(ballOf(t("unreproducible", "open"), { handsToQa: false })).toMatchObject({ stage: "rd_unrepro", who: "rd" }); expect(ballOf(t("unreproducible", "resolved"), { handsToQa: false })).toBeNull(); expect(ballOf(t("unreproducible", "resolved"), { handsToQa: true, qaHandedUnrepro: true })).toMatchObject({ stage: "qa_unrepro", who: "qa" }); }); test("平台在跑时不等人", () => { expect(ballOf(t("fixing"), { handsToQa: true })).toBeNull(); expect(ballOf(t("awaiting_fix"), { handsToQa: true })).toBeNull(); }); }); describe("催办节奏", () => { const ball = ballOf({ status: "manual_review", meegoState: null, mrState: null }, { handsToQa: false })!; test("只在北京时间工作日白天", () => { expect(inWorkHours(WORK)).toBe(true); expect(inWorkHours(Date.UTC(2026, 10, 10, 13, 0))).toBe(false); // 21:00 expect(inWorkHours(Date.UTC(2026, 10, 14, 3, 0))).toBe(false); // 周六 }); test("等满 24 小时才第一次催;只播报过(count 0)也算还没催", () => { const since = WORK - NUDGE_FIRST_AFTER_MS + H; expect(nudgeDue(ball, since, WORK, null)).toBe(false); const since2 = WORK - NUDGE_FIRST_AFTER_MS - H; expect(nudgeDue(ball, since2, WORK, null)).toBe(true); const announced: NudgeState = { stage: ball.stage, since: since2, count: 0, lastAt: since2 }; expect(nudgeDue(ball, since2, WORK, announced)).toBe(true); }); test("之后每天一次,最多 3 次;换了一段等待重新计数", () => { const since = WORK - 5 * 24 * H; let s: NudgeState | null = null; s = nextNudgeState(ball, since, WORK, s); expect(s.count).toBe(1); expect(nudgeDue(ball, since, WORK + H, s)).toBe(false); expect(nudgeDue(ball, since, WORK + NUDGE_EVERY_MS, s)).toBe(true); s = { ...s, count: NUDGE_MAX }; expect(nudgeDue(ball, since, WORK + 10 * NUDGE_EVERY_MS, s)).toBe(false); expect(nudgeDue(ball, since + 1, WORK, s)).toBe(true); // 新的一段 expect(nextNudgeState(ball, since + 1, WORK, s).count).toBe(1); }); test("催办的话只说要做什么、等了多久", () => { expect(nudgeText(ball, 30 * H)).toMatch(/^这张单已经等了 30 小时。复核这一轮的修复/); expect(nudgeText(ball, 72 * H)).toContain("等了 3 天"); }); }); describe("打回意见", () => { const p: PendingRework = { id: "abc", by: "qa", byName: "QA", text: "还是错位", imageRefs: [], status: "manual_review", round: 2, at: WORK }; test("过期 / 单子变了就作废", () => { expect(pendingStillValid(p, { status: "manual_review", attempts: 2 }, WORK + H)).toBeNull(); expect(pendingStillValid(p, { status: "manual_review", attempts: 2 }, WORK + PENDING_REWORK_TTL_MS + 1)).toContain("过期"); expect(pendingStillValid(p, { status: "awaiting_fix", attempts: 2 }, WORK + H)).toContain("作废"); expect(pendingStillValid(p, { status: "manual_review", attempts: 3 }, WORK + H)).toContain("作废"); }); test("写到 Meego 的评论标明来源;复核意见带截图引用", () => { expect(meegoCommentForRework("QA(RD 确认)", "还是错位", ["https://x/1.png"])).toBe("【复核群打回 · QA(RD 确认)】\n还是错位\n截图:\nhttps://x/1.png"); expect(reworkNoteText("qa", "还是错位", ["images/plan-1.png"])).toBe("【复核群里的打回意见】\n@qa:\n还是错位\n![](images/plan-1.png)"); }); }); describe("只读阶段", () => { test("「轮到你」只指向面板和 Meego 上的现有操作,不引导在群里打回", () => { for (const t of [ { status: "manual_review", meegoState: null, mrState: null }, { status: "unreproducible", meegoState: "open", mrState: null }, { status: "fix_succeeded", meegoState: "resolved", mrState: "opened" }, ]) { const b = ballOf(t, { handsToQa: true, qaHandedUnrepro: false }); expect(b?.todo).not.toContain("@我"); } }); test("状态回复:现在、轮到谁、最近 5 次流转(北京时间)、链接", () => { const ball = ballOf({ status: "manual_review", meegoState: null, mrState: null }, { handsToQa: false }); const rows = Array.from({ length: 7 }, (_, i) => ({ from: i ? "fixing" : null, to: i % 2 ? "manual_review" : "fixing", ts: WORK + i * H, actor: i === 6 ? "herui.ryon@bytedance.com" : "system:runner" })); const text = statusDigest({ status: "manual_review", attempts: 2, ball, whoName: "herui.ryon", running: false, transitions: rows, links: { panel: "https://p/?task_id=1", mr: "https://bits/mr/1", mrState: "opened", meego: "https://meego/1" }, }); expect(text.split("\n")[0]).toBe("现在:待复核 · 第 2 轮"); expect(text).toContain("轮到 herui.ryon:复核这一轮的修复"); expect(text.match(/^· /gm)?.length).toBe(5); expect(text).toContain("· 11-10 17:00 修复中 → 修复中(herui.ryon)"); expect(text).toContain("· 11-10 16:00 修复中 → 待复核(平台)"); expect(text).toContain("MR:https://bits/mr/1(opened)"); expect(text).toContain("Meego:https://meego/1"); }); test("操作人和时间", () => { expect(actorLabel("system:qa-first")).toBe("平台(交 QA)"); expect(actorLabel("system:mr-sync")).toBe("平台"); expect(actorLabel(null)).toBe(""); expect(bjTime(WORK)).toBe("11-10 11:00"); }); test("状态 / 帮助以外的话:说清楚去哪操作", () => { const t = readonlyReply({ panel: "https://p", meego: "https://m" }); expect(t).toContain("https://p"); expect(t).toContain("https://m"); }); }); import { buildGroupCard, type GroupCardKind } from "@bbbug/shared/review_group_cards"; describe("复核群卡片", () => { const base = { title: "标题", meegoId: "7387858159", platform: "android", round: 2, links: { panel: "https://p", mr: "https://mr", mrState: "opened", meego: "https://m" } }; const ME = [{ openId: "ou_me", name: "me" }]; const rdBall = ballOf({ status: "manual_review", meegoState: null, mrState: null }, { handsToQa: false }); const KINDS: GroupCardKind[] = ["welcome", "plan_ready", "info_required", "fixing", "refixing", "rd_review", "qa_verify", "awaiting_merge", "merged", "unrepro", "fix_failed", "nudge", "status"]; // notify_cards.ts 里核实过能渲染的图标;未知 token 飞书静默渲染成破图。 const OK_ICONS = new Set(["warning_outlined", "info_outlined", "search_outlined", "done_outlined", "time_outlined", "approval_outlined", "bell_outlined", "edit_outlined", "code_outlined", "myai_colorful", "todo_colorful"]); test("每种都是 JSON 2.0、图标在核实过的集合里", () => { for (const kind of KINDS) { const c = buildGroupCard({ ...base, kind, ball: rdBall, who: ME }) as any; expect(c.schema).toBe("2.0"); expect(OK_ICONS.has(c.header.icon.token)).toBe(true); } }); test("等人的卡里直接 @ 那个人;没在等人的卡不 @", () => { const review = JSON.stringify(buildGroupCard({ ...base, kind: "rd_review", ball: rdBall, who: ME })); expect(review).toContain(""); expect(review).toContain("第 2 轮 · 修好了,待 RD 复核"); expect(JSON.stringify(buildGroupCard({ ...base, kind: "fixing" }))).not.toContain(" { const qa = JSON.stringify(buildGroupCard({ ...base, kind: "qa_verify", rdSkipped: true, ball: rdBall, who: ME })); expect(qa).toContain("~~RD 复核~~"); expect(qa).toContain("● QA 验收"); }); test("重修卡写明按谁的意见;催办最后一次标红并说明不再提醒", () => { const re = JSON.stringify(buildGroupCard({ ...base, kind: "refixing", feedback: { who: "larry.guo", role: "QA", text: "还是错位" } })); expect(re).toContain("这一轮按 QA larry.guo 的意见修"); const last = buildGroupCard({ ...base, kind: "nudge", ball: rdBall, who: ME, waitedMs: 72 * H, nudgeCount: 3, nudgeMax: 3 }) as any; expect(last.header.template).toBe("red"); expect(JSON.stringify(last)).toContain("之后不再在群里提醒"); }); test("状态卡:等人处理用黄色;MR 状态是纯文字,不在灰字小标题里嵌 text_tag", () => { const waiting = buildGroupCard({ ...base, kind: "status", ball: rdBall, who: ME, transitions: [] }) as any; expect(waiting.header.template).toBe("yellow"); expect(waiting.header.title.content).toContain("等人处理"); const text = JSON.stringify(waiting); expect(text).toContain("MR 未合入"); expect(text).not.toMatch(/]*>[^<]*