import { test, expect, describe } from "bun:test"; import { DEFAULT_REVIEW_POLICY, QA_REOPEN_NOTE_HEAD, QA_REPRO_NOTE_HEAD, parseReviewPolicy, qaUnreproHandoffNote, policyOf, qaFeedbackComments, qaReopenNoteText, routeToQa, validateReviewPolicy, type MeegoComment, type ReviewPolicyConfig, } from "@bbbug/shared/review_policy"; import { buildProgress, fixBucketOf, type ProgressTask } from "@bbbug/shared/batch_center"; import { EMPTY_METHOD_COVERAGE, type MethodCoverage } from "@bbbug/shared/method_coverage"; const QA: ReviewPolicyConfig = { id: "qa_first", maxChangedUnits: 5 }; const cov = (methods: number, unparsed = 0, extra: Partial = {}): MethodCoverage => ({ ...EMPTY_METHOD_COVERAGE, verdict: "consistent", methods: Array.from({ length: methods }, (_, i) => ({ file: `a${i}.kt`, owner: "A", name: `m${i}`, startLine: 1, endLine: 2 })), unparsedFiles: Array.from({ length: unparsed }, (_, i) => `res${i}.xml`), ...extra, }); describe("review policy config", () => { test("missing or garbage config is the default policy, exactly today's flow", () => { expect(parseReviewPolicy(null)).toEqual(DEFAULT_REVIEW_POLICY); expect(parseReviewPolicy({ id: "nope" })).toEqual(DEFAULT_REVIEW_POLICY); const d = policyOf(DEFAULT_REVIEW_POLICY); expect(d).toMatchObject({ handsToQa: false, qaAccept: false, qaReopen: "triage", countsQaSendbacks: false }); expect(policyOf(null)).toEqual(d); }); test("qa_first switches every branch point", () => { expect(policyOf(QA)).toMatchObject({ handsToQa: true, qaAccept: true, qaReopen: "rework", countsQaSendbacks: true }); expect(parseReviewPolicy({ id: "qa_first", maxChangedUnits: "7" })).toEqual({ id: "qa_first", maxChangedUnits: 7 }); expect(parseReviewPolicy({ id: "qa_first" })).toEqual({ id: "qa_first", maxChangedUnits: null }); }); test("saving rejects bad input instead of silently defaulting", () => { expect(() => validateReviewPolicy({ id: "qa" })).toThrow(); expect(() => validateReviewPolicy({ id: "qa_first", maxChangedUnits: -1 })).toThrow(); expect(() => validateReviewPolicy({ id: "qa_first", maxChangedUnits: 1.5 })).toThrow(); expect(validateReviewPolicy({ id: "qa_first", maxChangedUnits: "" })).toEqual({ id: "qa_first", maxChangedUnits: null }); expect(validateReviewPolicy({ id: "rd_review", maxChangedUnits: 3 })).toEqual({ id: "rd_review", maxChangedUnits: 3 }); }); }); describe("routing a finished fix", () => { const ok = { meegoId: "7379909155", failureReason: null, criticVerdict: null }; test("default policy never skips RD", () => { expect(routeToQa(DEFAULT_REVIEW_POLICY, { ...ok, methodCoverage: cov(1) })).toMatchObject({ toQa: false, reason: "policy" }); }); test("small, confident fixes with a ticket go straight to QA", () => { expect(routeToQa(QA, { ...ok, methodCoverage: cov(3, 2) })).toEqual({ toQa: true, reason: "ok", units: 5 }); }); test("anything uncertain or unmeasurable goes to RD first", () => { expect(routeToQa(QA, { ...ok, meegoId: null, methodCoverage: cov(1) }).reason).toBe("no_meego"); expect(routeToQa(QA, { ...ok, failureReason: "自动评审 UNCERTAIN", methodCoverage: cov(1) }).reason).toBe("uncertain"); expect(routeToQa(QA, { ...ok, criticVerdict: "UNCERTAIN", methodCoverage: cov(1) }).reason).toBe("uncertain"); expect(routeToQa(QA, { ...ok, methodCoverage: null }).reason).toBe("no_diff_scan"); expect(routeToQa(QA, { ...ok, methodCoverage: cov(1, 0, { error: "git failed" }) }).reason).toBe("no_diff_scan"); expect(routeToQa(QA, { ...ok, methodCoverage: cov(1, 0, { verdict: "baseline-unreliable" }) }).reason).toBe("diff_unreliable"); expect(routeToQa(QA, { ...ok, methodCoverage: cov(5, 1) })).toEqual({ toQa: false, reason: "too_large", units: 6 }); // MR 没提交成功不是「拿不准」:要 RD 以自己的名义提交,单独一个原因,抽屉里才写得对。 expect(routeToQa(QA, { ...ok, failureReason: "MR 提交失败:none of watcher could successfully create", methodCoverage: cov(1) }).reason).toBe("mr_failed"); }); test("no limit hands every confident fix with a ticket to QA, even without a diff scan", () => { const all: ReviewPolicyConfig = { id: "qa_first", maxChangedUnits: null }; expect(routeToQa(all, { ...ok, methodCoverage: null }).toQa).toBe(true); expect(routeToQa(all, { ...ok, methodCoverage: cov(40) })).toEqual({ toQa: true, reason: "ok", units: 40 }); expect(routeToQa(all, { ...ok, meegoId: null }).toQa).toBe(false); }); }); describe("what counts as fixed", () => { const fs = (meegoState: string | null, reviewPolicy?: "qa_first") => ({ status: "fix_succeeded" as const, meegoState, mrUrl: "https://bits/1", reviewPolicy }); test("default policy: approval is fixed, as before", () => { expect(fixBucketOf(fs("RESOLVED"))).toBe("fixed"); expect(fixBucketOf(fs("CLOSED"))).toBe("fixed"); expect(fixBucketOf(fs("REOPENED"))).toBe("reopened"); }); test("qa_first: approval only hands to QA; fixed once QA closes the ticket", () => { expect(fixBucketOf(fs("RESOLVED", "qa_first"))).toBe("review"); expect(fixBucketOf(fs("OPEN", "qa_first"))).toBe("review"); expect(fixBucketOf(fs("CLOSED", "qa_first"))).toBe("fixed"); expect(fixBucketOf(fs("done", "qa_first"))).toBe("fixed"); expect(fixBucketOf(fs("REOPENED", "qa_first"))).toBe("reopened"); // 没有工单的单无处验收,照旧按复核通过算。 expect(fixBucketOf(fs(null, "qa_first"))).toBe("fixed"); }); test("unreproducible: the platform's 'can't reproduce' only hands to QA; QA closing it confirms", () => { const un = (meegoState: string | null, reviewPolicy?: "qa_first", mrUrl: string | null = null) => ({ status: "unreproducible" as const, meegoState, mrUrl, reviewPolicy }); // 默认策略不变:置了已解决就算确认。 expect(fixBucketOf(un("RESOLVED"))).toBe("fixed"); expect(fixBucketOf(un("RESOLVED", "qa_first"))).toBe("review"); expect(fixBucketOf(un("CLOSED", "qa_first"))).toBe("fixed"); expect(fixBucketOf(un("CLOSED", "qa_first", "https://bits/1"))).toBe("closed"); // QA 重开了:回到「复现不了 / 修不了」等人处理(写了原因的平台会自动重修)。 expect(fixBucketOf(un("REOPENED", "qa_first"))).toBe("recall"); expect(fixBucketOf(un("OPEN", "qa_first"))).toBe("recall"); }); test("progress follows the policy annotation", () => { const tasks = [ { id: "a", status: "fix_succeeded", meegoState: "RESOLVED", mrState: null, mrUrl: null }, { id: "b", status: "fix_succeeded", meegoState: "CLOSED", mrState: null, mrUrl: null }, ] as ProgressTask[]; expect(buildProgress(tasks, new Map(), 0).fixed).toBe(2); expect(buildProgress(tasks.map((t) => ({ ...t, reviewPolicy: "qa_first" as const })), new Map(), 0).fixed).toBe(1); }); }); describe("QA reopen reason from the ticket", () => { const c = (p: Partial): MeegoComment => ({ author: "qa1", at: 2_000, text: "还是能复现", imageUuids: [], ...p }); test("only comments written after hand-over by someone other than the platform count", () => { const got = qaFeedbackComments([ c({ at: 500, text: "老评论" }), c({ author: "jiaopengwen", text: "AI fix complete." }), c({ at: 3_000, text: "上下分屏还是被裁剪" }), c({ at: 2_500 }), ], { since: 1_000, platformUserKey: "jiaopengwen" }); expect(got.map((x) => x.at)).toEqual([2_500, 3_000]); }); test("an image-only comment counts; an empty placeholder does not", () => { const got = qaFeedbackComments([c({ text: "[img]", imageUuids: ["u1"] }), c({ text: " [img] ", imageUuids: [] })], { since: 0, platformUserKey: "x" }); expect(got).toHaveLength(1); }); test("the review note names the QA, keeps the text and embeds transferred images", () => { const note = qaReopenNoteText([{ who: "zhangwenyu.666", text: "verification[img]", images: ["images/plan-1.png"] }, { who: null, text: "预期只弹一次", images: [] }]); expect(note.startsWith(QA_REOPEN_NOTE_HEAD)).toBe(true); expect(note).toContain("@zhangwenyu.666:\nverification\n![](images/plan-1.png)"); expect(note).toContain("预期只弹一次"); expect(note).not.toContain("[img]"); }); test("a reopened unreproducible bug gets a repro-first header, and the hand-off note tells QA what to do", () => { expect(qaReopenNoteText([{ who: "qa", text: "登录后先进 inbox 再回 profile", images: [] }], QA_REPRO_NOTE_HEAD).startsWith(QA_REPRO_NOTE_HEAD)).toBe(true); const n = qaUnreproHandoffNote("https://streamline/x"); expect(n).toContain("关单"); expect(n).toContain("写明复现方式"); expect(n).toContain("https://streamline/x"); }); });