import assert from "node:assert/strict"; import test from "node:test"; import { chinese } from "./pr-review-language.mjs"; import { diffAnchors, extractPreviousReview, hasSkipCIToken, inlineComments, reviewSummary, selectDiffs, shouldSkipAutoReview, waitForCI, } from "./pr-review.mjs"; test("hasSkipCIToken recognizes standard skip tokens case-insensitively", () => { assert.equal(hasSkipCIToken("ui: tweak layout\n\n[skip ci]"), true); assert.equal(hasSkipCIToken("[CI SKIP] docs update"), true); assert.equal(hasSkipCIToken("chore: update [no ci]"), true); assert.equal(hasSkipCIToken("chore: [skip actions]"), true); assert.equal(hasSkipCIToken("chore: [actions skip]"), true); assert.equal(hasSkipCIToken("feat: normal commit"), false); assert.equal(hasSkipCIToken(""), false); }); test("shouldSkipAutoReview skips drafts and skip-ci commits only for automatic triggers", () => { assert.equal(shouldSkipAutoReview({ isManual: false, draft: true, commitMessage: "feat: wip" }), "PR is a draft"); assert.equal( shouldSkipAutoReview({ isManual: false, draft: false, commitMessage: "fix: typo\n\n[skip ci]" }), "head commit requested [skip ci]", ); assert.equal(shouldSkipAutoReview({ isManual: false, draft: false, commitMessage: "fix: typo" }), null); assert.equal(shouldSkipAutoReview({ isManual: true, draft: true, commitMessage: "[skip ci]" }), null); }); test("diffAnchors includes added, deleted, and context lines while selectDiffs tracks partial hunks", () => { const patch = [ "@@ -10,4 +10,4 @@", " keep1", "-oldLine", "+newLine", " keep2", ].join("\n"); const anchors = diffAnchors(patch); assert.equal(anchors.has("RIGHT:10"), true); assert.equal(anchors.has("LEFT:11"), true); assert.equal(anchors.has("RIGHT:11"), true); assert.equal(anchors.has("RIGHT:12"), true); assert.equal(anchors.has("LEFT:10"), false); const selected = selectDiffs([{ filename: "a.go", patch, changes: 2 }]); assert.equal(selected.diffs.length, 1); assert.equal(selected.partial, 0); const comments = inlineComments( [ { path: "a.go", side: "RIGHT", line: 10, priority: "P2", impact: "correctness", trigger: "Existing configuration", evidence: { side: "RIGHT", line: 11, text: "newLine" }, body: "Context anchor works" }, { path: "a.go", side: "LEFT", line: 11, priority: "P2", impact: "compatibility", trigger: "Upgrading from an old release", evidence: { side: "LEFT", line: 11, text: "oldLine" }, body: "Deleted anchor works" }, { path: "a.go", side: "RIGHT", line: 99, priority: "P1", impact: "data", trigger: "Restoring a disk", evidence: { side: "RIGHT", line: 11, text: "newLine" }, body: "Out of range is dropped" }, ], selected.locations, ); assert.equal(comments.length, 2); assert.equal(comments[0].line, 10); assert.equal(comments[1].side, "LEFT"); }); test("inlineComments drops nits and unsupported evidence and limits findings to three", () => { const patch = "@@ -1 +1,5 @@\n existing guard\n+crash first\n+crash second\n+crash third\n+crash fourth"; const locations = new Map([["a.go", diffAnchors(patch)]]); const finding = { path: "a.go", side: "RIGHT", line: 2, priority: "P2", impact: "correctness", trigger: "Starting an existing VM", evidence: { side: "RIGHT", line: 2, text: "crash first" }, body: "Starting an existing VM fails", }; const comments = inlineComments([ { ...finding, priority: "P3" }, { ...finding, impact: "style" }, { ...finding, trigger: "" }, { ...finding, evidence: { side: "RIGHT", line: 1, text: "existing guard" } }, { ...finding, evidence: { side: "RIGHT", line: 2, text: "invented cause" } }, finding, finding, ...["second", "third", "fourth"].map((name, index) => ({ ...finding, line: index + 3, evidence: { side: "RIGHT", line: index + 3, text: "crash " + name }, })), ], locations); assert.deepEqual(comments.map(({ line }) => line), [2, 3, 4]); }); test("extractPreviousReview returns latest bot review with stripped marker and changed files", () => { const reviews = [ { id: 1, commit_id: "aaaa111", user: { login: "github-actions[bot]" }, state: "COMMENTED", body: "\n## 自动代码审核\n\nold summary", }, { id: 2, commit_id: "bbbb222", user: { login: "human-reviewer" }, state: "CHANGES_REQUESTED", body: "human comment", }, { id: 3, commit_id: "cccc333", user: { login: "github-actions[bot]" }, state: "COMMENTED", body: "\n## 自动代码审核\n\nlatest summary", }, ]; const prev = extractPreviousReview( reviews, [{ path: "internal/app/a.go", side: "RIGHT", line: 42, body: "**[P2]** keep ForegroundPID check" }], ["internal/app/a.go"], ); assert.ok(prev); assert.equal(prev.commit, "cccc333"); assert.equal(prev.summary.includes("