import { test, expect, describe } from "bun:test"; import { AUTO_ADVANCE_MS, buildFlowReport, type FlowTask, type FlowTransition } from "../src/batch_flow"; const H = 3_600_000; // 2026-09-28 00:00 Asia/Shanghai const D0 = Date.UTC(2026, 8, 27, 16); const NOW = D0 + 2 * 24 * H + 12 * H; // 09-30 12:00 let seq = 0; function task(p: Partial = {}): FlowTask { seq++; return { id: `t${seq}`, title: `bug ${seq}`, status: "manual_review", unattended: false, mrUrl: null, mrState: null, mrMergedAt: null, meegoState: null, mergedIntoBaseAt: null, ...p, }; } const tr = (t: FlowTask, from: string | null, to: string, ts: number, actor: string | null = null): FlowTransition => ({ targetId: t.id, fromStatus: from, toStatus: to, ts, actor }); const build = (tasks: FlowTask[], transitions: FlowTransition[]) => buildFlowReport({ tag: "b", tasks, transitions, actorSince: null, now: NOW }); describe("buildFlowReport", () => { test("approve and reject are both steps; actor recorded by prefix, missing actor stays unrecorded", () => { const a = task({ status: "fix_succeeded" }); const b = task({ status: "awaiting_fix" }); const r = build([a, b], [ tr(a, "fixing", "manual_review", D0 + 1 * H), tr(a, "manual_review", "fix_succeeded", D0 + 5 * H, "Alice@bytedance.com"), tr(b, "fixing", "manual_review", D0 + 2 * H), tr(b, "manual_review", "awaiting_fix", D0 + 26 * H), ]); expect(r.totals.steps).toBe(2); expect(r.totals.byAction.approve).toBe(1); expect(r.totals.byAction.reject).toBe(1); expect(r.totals.unrecorded).toBe(1); expect(r.people.map((p) => p.prefix)).toEqual(["alice"]); expect(r.steps[0]).toMatchObject({ taskId: b.id, action: "reject", actor: null }); expect(r.response.review.n).toBe(2); }); test("unattended auto-advance out of 待确认 is automated, not a step and not an entry; a slow one is a human step", () => { const fast = task({ unattended: true, status: "awaiting_fix" }); const slow = task({ unattended: true, status: "awaiting_fix" }); const attended = task({ unattended: false, status: "awaiting_fix" }); const r = build([fast, slow, attended], [ tr(fast, "evaluating", "pending_confirmation", D0 + H), tr(fast, "pending_confirmation", "awaiting_fix", D0 + H + 2000), tr(slow, "evaluating", "pending_confirmation", D0 + H), tr(slow, "pending_confirmation", "awaiting_fix", D0 + H + AUTO_ADVANCE_MS + 1), tr(attended, "evaluating", "pending_confirmation", D0 + H), tr(attended, "pending_confirmation", "awaiting_fix", D0 + H + 1000), ]); expect(r.totals.automated).toBe(1); expect(r.totals.entered).toBe(2); expect(r.totals.byAction.confirm).toBe(2); }); test("an explicit system: actor is automated even when slow", () => { const t = task({ status: "awaiting_fix", unattended: true }); const r = build([t], [ tr(t, "evaluating", "info_required", D0 + H), tr(t, "info_required", "awaiting_fix", D0 + 5 * H, "system:unattended"), ]); expect(r.totals.automated).toBe(1); expect(r.totals.steps).toBe(0); }); test("auto-approve after the MR merged is recorded as the merge, at the Bits merge time", () => { const t = task({ status: "fix_succeeded", mrUrl: "https://bits/x", mrState: "merged", mrMergedAt: D0 + 10 * H }); const r = build([t], [ tr(t, "fixing", "manual_review", D0 + H), tr(t, "manual_review", "fix_succeeded", D0 + 30 * H, "system:mr-merged"), ]); expect(r.totals.steps).toBe(1); expect(r.steps[0]).toMatchObject({ action: "merge", at: D0 + 10 * H, actor: null, wait: "review" }); // 通过后进入的「待合入」不再算一段:MR 早就合了。 expect(r.totals.entered).toBe(1); expect(r.totals.backlog).toBe(0); }); test("after approval, waiting for the MR to merge is not waiting on a person: no entry, no step, no backlog", () => { const merged = task({ status: "fix_succeeded", mrUrl: "https://bits/1", mrState: "merged", mrMergedAt: D0 + 30 * H }); const pending = task({ status: "fix_succeeded", mrUrl: "https://bits/2", mrState: "opened", meegoState: "closed" }); const r = build([merged, pending], [ tr(merged, "fixing", "manual_review", D0 + H), tr(merged, "manual_review", "fix_succeeded", D0 + 2 * H, "bob@x.com"), tr(pending, "fixing", "manual_review", D0 + H), tr(pending, "manual_review", "fix_succeeded", D0 + 3 * H, "bob@x.com"), ]); expect(r.totals.entered).toBe(2); // 只有两段复核 expect(r.totals.byAction.approve).toBe(2); expect(r.totals.byAction.merge).toBe(0); expect(r.totals.backlog).toBe(0); }); test("a wait that ended off-ledger (confirmed unrepro in Meego) is neither step nor backlog", () => { const t = task({ status: "unreproducible", meegoState: "RESOLVED" }); const r = build([t], [tr(t, "fixing", "unreproducible", D0 + H)]); expect(r.totals.entered).toBe(0); expect(r.totals.backlog).toBe(0); }); test("daily series: Shanghai day buckets, entered, backlog at end of day, person-days", () => { const a = task({ status: "fix_succeeded" }); const b = task({ status: "fix_succeeded" }); const c = task({ status: "manual_review" }); const r = build([a, b, c], [ tr(a, "fixing", "manual_review", D0 + H), tr(a, "manual_review", "fix_succeeded", D0 + 25 * H, "alice@x.com"), tr(b, "fixing", "manual_review", D0 + 2 * H), tr(b, "manual_review", "fix_succeeded", D0 + 26 * H, "bob@x.com"), tr(c, "fixing", "manual_review", D0 + 3 * H), ]); expect(r.days.map((d) => d.day)).toEqual(["2026-09-28", "2026-09-29", "2026-09-30"]); expect(r.days.map((d) => d.entered)).toEqual([3, 0, 0]); expect(r.days.map((d) => d.total)).toEqual([0, 2, 0]); expect(r.days.map((d) => d.backlog)).toEqual([3, 1, 1]); expect(r.days[1].people).toEqual(["alice", "bob"]); expect(r.totals.personDays).toBe(2); expect(r.totals.backlog).toBe(1); }); test("closing from a wait is a close step", () => { const t = task({ status: "archived" }); const r = build([t], [ tr(t, "fixing", "fix_failed", D0 + H), tr(t, "fix_failed", "archived", D0 + 2 * H, "carol@x.com"), ]); expect(r.steps[0]).toMatchObject({ wait: "recall", action: "close", actor: "carol" }); }); }); describe("batch start boundary", () => { const start = D0 + 24 * H; // 09-29 00:00 CST test("waits that ended before the batch are dropped; waits spanning it count from the start; axis starts at the start", () => { const old = task({ status: "fix_succeeded" }); const spanning = task({ status: "fix_succeeded" }); const r = buildFlowReport({ tag: "b", tasks: [old, spanning], actorSince: null, startedAt: start, now: NOW, transitions: [ tr(old, "fixing", "manual_review", D0 - 40 * 24 * H), tr(old, "manual_review", "awaiting_fix", D0 - 39 * 24 * H), tr(old, "fixing", "manual_review", D0 - 30 * 24 * H), tr(spanning, "fixing", "manual_review", D0 - 5 * 24 * H), tr(spanning, "manual_review", "fix_succeeded", start + 2 * H, "alice@x.com"), ], }); expect(r.days[0].day).toBe("2026-09-29"); // old 的第二段一直等到现在(它现在是 fix_succeeded 但账本没有出口,且不需要人),不计;第一段在开始前结束,不计。 expect(r.totals.steps).toBe(1); expect(r.steps[0]).toMatchObject({ taskId: spanning.id, enteredAt: start }); expect(r.response.review.p50Hours).toBe(2); }); test("automated moves before the batch aren't counted", () => { const t = task({ unattended: true, status: "awaiting_fix" }); const r = buildFlowReport({ tag: "b", tasks: [t], actorSince: null, startedAt: start, now: NOW, transitions: [tr(t, "evaluating", "pending_confirmation", D0), tr(t, "pending_confirmation", "awaiting_fix", D0 + 1000)], }); expect(r.totals.automated).toBe(0); }); });