import { test, expect, describe } from "bun:test"; import { inProject, inProjectHanded, WATCH_SCHEDULE_DEFAULT, nextWatchScanAt, parseWatchSchedule, validateWatchSchedule, watchScanDue, watchScheduleText, afterHandoverOf, buildWatchStats, handoverKindOf, inStock, isHandoverStatus, isRecordedOnly, notOursToFix, parseWatchExtraStates, recordMark, recordReasonOf, watchBriefOf, watchHealthy, watchStateOf, type WatchRow, type WatchState, } from "@bbbug/shared/batch_watch"; const H = 3_600_000; const NOW = Date.UTC(2026, 9, 21, 20); let seq = 0; const row = (state: WatchState, p: Partial = {}): WatchRow => ({ id: `w${++seq}`, title: "t", platform: "ios", meegoId: "1", meegoUrl: "u", assignee: null, seenAt: NOW - 10 * H, status: "fixing", meegoState: null, state, handedOverAt: null, handoverKind: null, after: null, resolvedAt: null, withdrawnReason: null, recordReason: null, reopened: false, failure: null, ...p, }); describe("Ready 与移交", () => { test("修好待复核和复现不了都算 Ready;修不了不算", () => { expect(isHandoverStatus("manual_review")).toBe(true); expect(isHandoverStatus("unreproducible")).toBe(true); expect(isHandoverStatus("fix_failed")).toBe(false); expect(isHandoverStatus("info_required")).toBe(false); expect(handoverKindOf("manual_review")).toBe("fixed"); expect(handoverKindOf("unreproducible")).toBe("unrepro"); expect(handoverKindOf("dispatched")).toBeNull(); }); test("状态:移交过就一直是已移交;撤回 / 归档 / 取消是已撤回;修不了和环境故障待定夺;其余托管中", () => { const s = (status: string, handedOverAt: number | null = null, withdrawnReason: "meego_closed" | null = null) => watchStateOf({ status, handedOverAt, withdrawnReason }); expect(s("fixing")).toBe("hosting"); expect(s("awaiting_fix")).toBe("hosting"); expect(s("fix_failed")).toBe("decide"); expect(s("info_required")).toBe("decide"); expect(s("cancelled")).toBe("withdrawn"); expect(s("archived")).toBe("withdrawn"); expect(s("fixing", null, "meego_closed")).toBe("withdrawn"); // 移交之后打回重跑、甚至修不了,也不会回到待定夺——之后是普通单。 expect(s("fix_failed", NOW)).toBe("handed"); expect(s("awaiting_fix", NOW)).toBe("handed"); }); test("移交之后:复核通过和确认复现不了算接受", () => { expect(afterHandoverOf({ status: "fix_succeeded" })).toBe("accepted"); expect(afterHandoverOf({ status: "unreproducible", meegoState: "RESOLVED" })).toBe("accepted"); expect(afterHandoverOf({ status: "unreproducible", meegoState: "OPEN" })).toBe("waiting"); // 工单被删后点了「确认无法复现」(routes.ts 的 /review/wont-fix 记下 deleted)也算接受; // 没点之前 meegoState 是空的,仍在等。 expect(afterHandoverOf({ status: "unreproducible", meegoState: "deleted" })).toBe("accepted"); expect(afterHandoverOf({ status: "unreproducible", meegoState: null })).toBe("waiting"); expect(afterHandoverOf({ status: "manual_review" })).toBe("waiting"); expect(afterHandoverOf({ status: "awaiting_fix" })).toBe("other"); // QA 打回的不算解决。 expect(afterHandoverOf({ status: "fix_succeeded", meegoState: "REOPENED" })).toBe("other"); }); test("只建记录:record: 前缀,不管任务是什么状态都是 recorded;不认识的原因不算", () => { expect(recordReasonOf(recordMark("mr"))).toBe("mr"); expect(recordReasonOf("record:bogus")).toBeNull(); expect(recordReasonOf("meego_closed")).toBeNull(); expect(recordReasonOf(null)).toBeNull(); expect(watchStateOf({ status: "cancelled", handedOverAt: null, withdrawnReason: recordMark("duplicate") })).toBe("recorded"); expect(watchStateOf({ status: "cancelled", handedOverAt: null, withdrawnReason: "mr_bound" })).toBe("withdrawn"); }); test("照样导入要真跑的:取消状态 + 只建记录;托管撤回的、没取消的都不算", () => { expect(isRecordedOnly({ status: "cancelled", watchWithdrawn: "record:closed" })).toBe(true); expect(isRecordedOnly({ status: "cancelled", watchWithdrawn: "record:mr" })).toBe(true); expect(isRecordedOnly({ status: "cancelled", watchWithdrawn: "meego_closed" })).toBe(false); expect(isRecordedOnly({ status: "cancelled", watchWithdrawn: null })).toBe(false); expect(isRecordedOnly({ status: "initializing", watchWithdrawn: "record:closed" })).toBe(false); }); test("巡检策略:只认列出来的状态;存的是 JSON,坏数据当没勾", () => { expect(parseWatchExtraStates('["FIX IN FUTURE"]')).toEqual(["FIX IN FUTURE"]); expect(parseWatchExtraStates(["FIX IN FUTURE", "CLOSED"])).toEqual(["FIX IN FUTURE"]); expect(parseWatchExtraStates(null)).toEqual([]); expect(parseWatchExtraStates("not json")).toEqual([]); }); test("移交后复核通过、QA 重新打开:回到待定夺;没重新打开就还是已移交", () => { expect(watchStateOf({ status: "fix_succeeded", handedOverAt: NOW, withdrawnReason: null, meegoState: "REOPENED" })).toBe("decide"); expect(watchStateOf({ status: "fix_succeeded", handedOverAt: NOW, withdrawnReason: null, meegoState: "RESOLVED" })).toBe("handed"); // 没移交过的不会因为 Meego 状态进待定夺。 expect(watchStateOf({ status: "fix_succeeded", handedOverAt: null, withdrawnReason: null, meegoState: "REOPENED" })).toBe("hosting"); }); test("效率不计平台没机会修的:撤回(关单 / 绑 MR)和只建记录;负责人归档照算", () => { expect(notOursToFix("meego_closed")).toBe(true); expect(notOursToFix("mr_bound")).toBe(true); expect(notOursToFix(recordMark("closed"))).toBe(true); expect(notOursToFix("owner_archived")).toBe(false); expect(notOursToFix(null)).toBe(false); }); }); describe("解决数量与概况", () => { test("解决 = 移交后经办人接受(复核通过 / 确认复现不了);近 7 天按解决时刻算", () => { const rows = [ row("handed", { handedOverAt: NOW - 5 * H, handoverKind: "fixed", after: "accepted", resolvedAt: NOW - 2 * H }), row("handed", { handedOverAt: NOW - 9 * 24 * H, handoverKind: "unrepro", after: "accepted", resolvedAt: NOW - 8 * 24 * H, seenAt: NOW - 10 * 24 * H }), row("handed", { handedOverAt: NOW - 8 * H, handoverKind: "unrepro", after: "waiting" }), row("handed", { handedOverAt: NOW - 9 * H, handoverKind: "fixed", after: "other" }), row("decide"), row("withdrawn", { withdrawnReason: "owner_archived" }), row("withdrawn", { withdrawnReason: "meego_closed" }), row("hosting"), ]; const s = buildWatchStats(rows, NOW); expect(s.counts).toEqual({ hosting: 1, handed: 4, decide: 1, withdrawn: 2, recorded: 0 }); expect(s.resolved).toBe(2); expect(s.resolved7d).toBe(1); expect(s.handedFixed).toBe(2); expect(s.handedUnrepro).toBe(2); expect(s.after).toEqual({ accepted: 2, waiting: 1, other: 1 }); expect(s.incoming).toBe(8); expect(s.incoming7d).toBe(7); const b = watchBriefOf({ enabled: true, auth: "ok", lastScanError: null }, rows, NOW); expect(b).toEqual({ enabled: true, healthy: true, hosting: 1, decide: 1, incoming7d: 7, resolved: 2, resolved7d: 1 }); }); test("只建记录的不算进来的增量单", () => { const s = buildWatchStats([row("hosting"), row("recorded", { recordReason: "mr" }), row("recorded", { recordReason: "closed", seenAt: NOW - 20 * 24 * H })], NOW); expect(s.counts.recorded).toBe(2); expect(s.incoming).toBe(1); expect(s.incoming7d).toBe(1); }); test("还没有移交时解决为 0", () => { const s = buildWatchStats([row("hosting")], NOW); expect(s.resolved).toBe(0); expect(s.resolved7d).toBe(0); }); test("巡检健康:关着永远健康;开着要授权正常且上次没出错", () => { expect(watchHealthy({ enabled: false, auth: "expired", lastScanError: "x" })).toBe(true); expect(watchHealthy({ enabled: true, auth: "ok", lastScanError: null })).toBe(true); expect(watchHealthy({ enabled: true, auth: "expired", lastScanError: null })).toBe(false); expect(watchHealthy({ enabled: true, auth: "ok", lastScanError: "Meego 500" })).toBe(false); }); }); describe("存量只算存量", () => { test("这一批托管接住的单不算它的存量;别的批次托管的单照常算", () => { expect(inStock({ tags: ["social"], watchTag: null }, "social")).toBe(true); expect(inStock({ tags: ["social"] }, "social")).toBe(true); expect(inStock({ tags: ["social"], watchTag: "social" }, "social")).toBe(false); expect(inStock({ tags: ["social", "im"], watchTag: "im" }, "social")).toBe(true); expect(inStock({ tags: ["im"], watchTag: null }, "social")).toBe(false); }); }); describe("日报里的增量托管", () => { const w = { healthy: true, new24h: 3, resolved24h: 2, resolved: 9, hosting: 4, decide: 1 }; test("插在末尾「查看详情」的分隔线之前", async () => { const { withWatchSection } = await import("@bbbug/shared/batch_watch"); const card = { header: {}, body: { elements: [{ tag: "markdown", content: "stock" }, { tag: "hr" }, { tag: "column_set" }] } }; const out = withWatchSection(card, w, "zh") as { body: { elements: { tag: string; content?: string }[] } }; expect(out.body.elements.map((e) => e.tag)).toEqual(["markdown", "hr", "markdown", "hr", "column_set"]); expect(out.body.elements[2].content).toContain("接住新单 3 条"); expect(out.body.elements[2].content).toContain("累计解决 9 条(过去 24 小时 +2)"); expect(out.body.elements[2].content).toContain("1 条待你定夺"); // 原卡片不被改动。 expect(card.body.elements.length).toBe(3); }); test("只有增量的那一封:巡检停了要说出来", async () => { const { buildWatchDigestCard } = await import("@bbbug/shared/batch_watch"); const card = buildWatchDigestCard({ name: "社交关系链", tag: "social", now: NOW }, { ...w, healthy: false, decide: 0 }, { lang: "zh", panelBase: "https://x", test: false, tz: "America/Los_Angeles" }) as { header: { title: { content: string } }; body: { elements: { tag: string; content?: string }[] }; }; expect(card.header.title.content).toBe("Bug 治理日报 · 社交关系链"); const text = card.body.elements.map((e) => e.content ?? "").join("\n"); expect(text).toContain("巡检已停"); expect(text).not.toContain("待你定夺"); }); }); describe("项目口径:平台没机会修的接进来的单不进总数", () => { test("标了 notOurs 的单落到 excluded,不管它是什么状态;效率里也不计", async () => { const { buildEfficiency, buildProgress, fixBucketOf } = await import("@bbbug/shared/batch_center"); expect(fixBucketOf({ status: "cancelled", meegoState: "CLOSED", mrUrl: null, notOurs: true })).toBe("excluded"); expect(fixBucketOf({ status: "fix_succeeded", meegoState: null, mrUrl: null, notOurs: true })).toBe("excluded"); // 没标的照旧:取消算不再修。 expect(fixBucketOf({ status: "cancelled", meegoState: null, mrUrl: null })).toBe("closed"); const tasks = [ { id: "a", status: "fix_succeeded", meegoState: null, mrUrl: null }, { id: "b", status: "manual_review", meegoState: null, mrUrl: null }, { id: "c", status: "cancelled", meegoState: "CLOSED", mrUrl: null, notOurs: true }, ]; const p = buildProgress(tasks as never, new Map(), Date.now()); expect(p.total).toBe(2); expect(p.excluded).toBe(1); const e = buildEfficiency(tasks.map((t) => ({ bucket: fixBucketOf(t), sendbacks: 0 })), 1); expect(e.targeted).toBe(2); expect(e.excluded).toBe(1); }); }); describe("巡检时间(每 30 分钟 / 每周选几天)", () => { // 2026-11 太平洋是 PST(UTC-8):周五 2026-11-06 09:00 PST = 17:00Z。 const Z = (iso: string) => Date.parse(iso); const FSM = { kind: "weekly" as const, days: [5, 0, 1], hour: 9, tz: "America/Los_Angeles" as const }; const synced = Z("2026-10-01T00:00:00Z"); test("校验:去重排序;没选天、钟点越界、时区不认识都拒绝", () => { expect(validateWatchSchedule({ kind: "weekly", days: [5, 0, 1, 5], hour: 9, tz: "America/Los_Angeles" })).toEqual({ kind: "weekly", days: [0, 1, 5], hour: 9, tz: "America/Los_Angeles" }); expect(typeof validateWatchSchedule({ kind: "weekly", days: [], hour: 9, tz: "America/Los_Angeles" })).toBe("string"); expect(typeof validateWatchSchedule({ kind: "weekly", days: [1], hour: 24, tz: "America/Los_Angeles" })).toBe("string"); expect(typeof validateWatchSchedule({ kind: "weekly", days: [7], hour: 9, tz: "America/Los_Angeles" })).toBe("string"); expect(typeof validateWatchSchedule({ kind: "weekly", days: [1], hour: 9, tz: "Asia/Tokyo" })).toBe("string"); expect(validateWatchSchedule({ kind: "interval", extra: 1 })).toEqual({ kind: "interval" }); }); test("库里的坏值、旧数据当默认间隔", () => { expect(parseWatchSchedule(null)).toEqual(WATCH_SCHEDULE_DEFAULT); expect(parseWatchSchedule("{oops")).toEqual(WATCH_SCHEDULE_DEFAULT); expect(parseWatchSchedule(JSON.stringify(FSM))).toEqual({ ...FSM, days: [0, 1, 5] }); }); test("默认间隔:满 30 分钟才跑", () => { const last = Z("2026-11-06T17:00:00Z"); expect(watchScanDue(WATCH_SCHEDULE_DEFAULT, last, synced, last + 29 * 60_000)).toBe(false); expect(watchScanDue(WATCH_SCHEDULE_DEFAULT, last, synced, last + 30 * 60_000)).toBe(true); expect(nextWatchScanAt(WATCH_SCHEDULE_DEFAULT, last, synced, last + 60_000)).toBe(last + 30 * 60_000); }); test("每周五、日、一 09:00:到点跑一次,当天不再跑,周六不跑", () => { const thu = Z("2026-11-05T17:05:00Z"); expect(watchScanDue(FSM, thu, synced, Z("2026-11-06T16:59:00Z"))).toBe(false); expect(watchScanDue(FSM, thu, synced, Z("2026-11-06T17:00:00Z"))).toBe(true); // 控制面重启错过了 9 点:之后补跑,不等下一个选定的日子。 expect(watchScanDue(FSM, thu, synced, Z("2026-11-06T22:00:00Z"))).toBe(true); const fri = Z("2026-11-06T17:01:00Z"); expect(watchScanDue(FSM, fri, synced, Z("2026-11-06T23:00:00Z"))).toBe(false); expect(watchScanDue(FSM, fri, synced, Z("2026-11-07T17:30:00Z"))).toBe(false); expect(watchScanDue(FSM, fri, synced, Z("2026-11-08T17:00:00Z"))).toBe(true); expect(nextWatchScanAt(FSM, fri, synced, Z("2026-11-06T18:00:00Z"))).toBe(Z("2026-11-08T17:00:00Z")); expect(nextWatchScanAt(FSM, Z("2026-11-09T17:02:00Z"), synced, Z("2026-11-09T18:00:00Z"))).toBe(Z("2026-11-13T17:00:00Z")); }); test("新加坡时区按新加坡的日历和整点", () => { const sg = { kind: "weekly" as const, days: [1], hour: 9, tz: "Asia/Singapore" as const }; // 周一 2026-11-02 09:00 SGT = 01:00Z(太平洋那边还是周日)。 expect(watchScanDue(sg, Z("2026-10-27T00:00:00Z"), synced, Z("2026-11-02T00:59:00Z"))).toBe(false); expect(watchScanDue(sg, Z("2026-10-27T00:00:00Z"), synced, Z("2026-11-02T01:00:00Z"))).toBe(true); }); test("开启导入还没跑完时按默认间隔重试,不等选定的日子", () => { const last = Z("2026-11-07T17:00:00Z"); // 周六 expect(watchScanDue(FSM, last, null, last + 30 * 60_000)).toBe(true); expect(watchScanDue(FSM, null, null, last)).toBe(true); }); test("一句话描述按周一在前排", () => { expect(watchScheduleText(FSM, "zh")).toBe("每周一、五、日 09:00(太平洋时间)"); expect(watchScheduleText({ ...FSM, days: [0, 1, 2, 3, 4, 5, 6] }, "en")).toBe("daily at 09:00 (Pacific)"); expect(watchScheduleText(WATCH_SCHEDULE_DEFAULT, "zh")).toBe("每 30 分钟"); }); }); describe("项目页的统计范围(存量 + 增量合成一页)", () => { const T = (p: Partial<{ tags: string[]; watchTag: string | null; handedOverAt: number | null; status: string; watchWithdrawn: string | null }>) => ({ tags: ["p"], watchTag: null, handedOverAt: null, status: "fixing", watchWithdrawn: null, ...p }); test("存量两边都算", () => { expect(inProject(T({}), "p")).toBe(true); expect(inProjectHanded(T({}), "p")).toBe(true); }); test("还在托管的增量单:负责人看得到,经办人看不到", () => { const t = T({ watchTag: "p" }); expect(inProject(t, "p")).toBe(true); expect(inProjectHanded(t, "p")).toBe(false); }); test("交给经办人之后两边都算", () => { const t = T({ watchTag: "p", handedOverAt: 1, status: "manual_review" }); expect(inProjectHanded(t, "p")).toBe(true); }); test("只建记录、从没跑过的增量单哪边都不算", () => { const t = T({ watchTag: "p", status: "cancelled", watchWithdrawn: "record:mr" }); expect(inProject(t, "p")).toBe(false); expect(inProjectHanded(t, "p")).toBe(false); }); test("别的项目的单不算", () => { expect(inProject(T({ tags: ["q"] }), "p")).toBe(false); }); });