import { test, expect, describe } from "bun:test"; import { buildRevisionChain, mergeReviewNotes, hasRevisionChain } from "../src/lib/reviewChain"; import type { ReportRoundEntry, ReportRoundList } from "../src/lib/api"; import type { ReviewNote } from "../src/types"; const note = (round: number, text: string, over: Partial = {}): ReviewNote => ({ at: 1_700_000_000_000 + round * 1000, source: "human", round, text, ...over, }); const entry = (round: number, over: Partial = {}): ReportRoundEntry => ({ round, hasMarkdown: true, markdownAt: 1_700_000_000_000 + round * 100, markdownChars: 500, hasReadable: true, readableAt: 1_700_000_000_000 + round * 200, readablePromptName: "baseline", reviewNotes: [], isCurrent: false, ...over, }); /** 面板真实返回的形状:最新那批在最前面。 */ const list = (currentRound: number, rounds: ReportRoundEntry[]): ReportRoundList => ({ currentRound, rounds: [...rounds].sort((a, b) => b.round - a.round), }); describe("mergeReviewNotes", () => { test("folds an mr_review block into the reviewer's own note that follows it", () => { const merged = mergeReviewNotes([ note(2, "generated block", { source: "mr_review", mrComments: [{ id: "c1", threadId: "t1", path: null, line: null, author: "a", excerpt: "e" }] }), note(2, "解一下冲突"), ]); expect(merged).toHaveLength(1); expect(merged[0].text).toBe("解一下冲突"); expect(merged[0].attached).toHaveLength(1); }); test("an mr_review with nothing after it still shows up, chips only", () => { const merged = mergeReviewNotes([ note(2, "block", { source: "mr_review", mrComments: [{ id: "c1", threadId: "t1", path: null, line: null, author: "a", excerpt: "e" }] }), ]); expect(merged).toHaveLength(1); expect(merged[0].text).toBe(""); expect(merged[0].attached).toHaveLength(1); }); }); describe("buildRevisionChain", () => { // 样本取自真实任务 c55ecb4c:5 批报告,4 次人工打回。 const five = list(5, [ entry(1, { reviewNotes: [note(1, "加 AB 开关")] }), entry(2, { reviewNotes: [note(2, "mr 有冲突")] }), entry(3, { reviewNotes: [note(3, "又冲突了")] }), entry(4, { reviewNotes: [note(4, "AB 加错位置了")] }), entry(5, { isCurrent: true }), ]); test("每条意见都指向它催生的那一批,而不是它评的那一批", () => { const c = buildRevisionChain(five); expect(c.edges.map((e) => [e.from, e.to])).toEqual([[1, 2], [2, 3], [3, 4], [4, 5]]); // 「第 5 批因何而来」= 第 4 批之后那条意见。这是横幅唯一的取数口径。 expect(c.byTo.get(5)?.note.text).toBe("AB 加错位置了"); expect(c.byTo.has(1)).toBe(false); }); test("批次按升序排,即便接口给的是倒序", () => { expect(buildRevisionChain(five).nodes.map((n) => n.round)).toEqual([1, 2, 3, 4, 5]); }); test("已经开工、还没落归档的那一批也在链上,标 pending", () => { const running = list(6, [ entry(4, { reviewNotes: [note(4, "AB 加错位置了")] }), entry(5, { reviewNotes: [note(5, "深色模式下偏亮")], isCurrent: true }), ]); const c = buildRevisionChain(running); const last = c.nodes[c.nodes.length - 1]; expect(last).toMatchObject({ round: 6, pending: true }); // 那条边是真的存在的,只是下游报告还没生成 —— 不能因此说它没催生任何一批。 expect(c.byFrom.get(5)).toMatchObject({ to: 6, pending: true }); }); test("刚打回、还没派发时不编「催生了第几批」", () => { const c = buildRevisionChain(list(3, [ entry(2), entry(3, { reviewNotes: [note(3, "再改改")], isCurrent: true }), ])); expect(c.byFrom.get(3)).toMatchObject({ to: null, pending: false }); }); test("同一间隔里多条意见:最后一条当主意见,其余进 extra", () => { const c = buildRevisionChain(list(2, [ entry(1, { reviewNotes: [note(1, "critic 打回", { source: "critic" }), note(1, "人也补了一条")] }), entry(2, { isCurrent: true }), ])); const e = c.byFrom.get(1)!; expect(e.note.text).toBe("人也补了一条"); expect(e.extra.map((n) => n.source)).toEqual(["critic"]); }); test("跳批(中间那批没归档)时边接到下一个真的存在的批次", () => { const c = buildRevisionChain(list(4, [ entry(1, { reviewNotes: [note(1, "改")] }), entry(4, { isCurrent: true }), ])); expect(c.byFrom.get(1)).toMatchObject({ to: 4, pending: false }); }); test("只跑过一批、也没人打回时不画链", () => { expect(hasRevisionChain(list(1, [entry(1, { isCurrent: true })]))).toBe(false); // 跑过一批但被打回了(第 2 批正在跑):这时链有话要说。 expect(hasRevisionChain(list(2, [entry(1, { reviewNotes: [note(1, "改")], isCurrent: true })]))).toBe(true); }); });