214 lines
10 KiB
JavaScript
214 lines
10 KiB
JavaScript
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: "<!-- reims-pr-review old -->\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: "<!-- reims-pr-review latest -->\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("<!--"), false);
|
|
assert.equal(prev.summary.includes("latest summary"), true);
|
|
assert.equal(prev.inlineFindings.length, 1);
|
|
assert.deepEqual(prev.changedFilesSincePreviousReview, ["internal/app/a.go"]);
|
|
});
|
|
|
|
const mergeAssessment = {
|
|
value: "Adds a needed feature", upgrade: "", ui: "", implementation: "", decision: "merge",
|
|
reason: "The change is ready", pending_checks: [],
|
|
};
|
|
const passingCI = { status: "completed", conclusion: "success", steps: [{ name: "CI tests", conclusion: "success" }] };
|
|
const fullReview = { mergeable: true, incomplete: false, hasFindings: false, locations: new Map() };
|
|
|
|
test("the final CI refresh does one lookup and can pick up a passing rerun", async () => {
|
|
const pull = {
|
|
base: { ref: "main", repo: { default_branch: "main" } },
|
|
head: { sha: "head", ref: "feature", repo: { full_name: "fork/Reims-Go" } },
|
|
};
|
|
const run = { id: 100, run_attempt: 2, head_sha: "head", head_branch: "feature", head_repository: pull.head.repo, pull_requests: [] };
|
|
let calls = 0;
|
|
const queued = await waitForCI(async () => {
|
|
calls++;
|
|
return { data: { workflow_runs: [{ ...run, status: "queued" }] } };
|
|
}, "/repos/test/Reims-Go", pull, 42, { wait: false });
|
|
assert.equal(queued.status, "queued");
|
|
assert.equal(calls, 1);
|
|
|
|
const passed = await waitForCI(async (route) => route.endsWith("/100/attempts/2/jobs")
|
|
? { data: { jobs: [{ name: "verify", conclusion: "success", steps: passingCI.steps }] } }
|
|
: { data: { workflow_runs: [{ ...run, status: "completed", conclusion: "success" }] } },
|
|
"/repos/test/Reims-Go", pull, 42, { wait: false });
|
|
assert.equal(passed.conclusion, "success");
|
|
assert.equal(passed.attempt, 2);
|
|
assert.match(reviewSummary(chinese, mergeAssessment, passed, fullReview), /可以合并/);
|
|
});
|
|
|
|
test("reviewSummary distinguishes CI awaiting action, waiting and failure", () => {
|
|
const approval = reviewSummary(chinese, mergeAssessment, { status: "completed", conclusion: "action_required", id: 1 }, fullReview);
|
|
assert.match(approval, /CI 需要人工处理/);
|
|
assert.match(approval, /代码未见阻塞,等待 CI/);
|
|
assert.doesNotMatch(approval, /失败|必做实测|先验证关键场景/);
|
|
|
|
for (const ci of [{ status: "unavailable" }, { status: "queued", id: 1 }, { status: "completed", conclusion: "cancelled", id: 1 }]) {
|
|
const waiting = reviewSummary(chinese, mergeAssessment, ci, fullReview);
|
|
assert.match(waiting, /代码未见阻塞,等待 CI/);
|
|
assert.doesNotMatch(waiting, /修改后再审|先验证关键场景/);
|
|
}
|
|
const failed = reviewSummary(chinese, mergeAssessment, { ...passingCI, conclusion: "failure" }, fullReview);
|
|
assert.match(failed, /修改后再审/);
|
|
assert.match(failed, /处理 CI 失败项/);
|
|
});
|
|
|
|
test("reviewSummary only recommends fixes for verified findings or CI failures and omits empty sections", () => {
|
|
const assessment = { ...mergeAssessment, decision: "changes", reason: "Refactor the redundant guard" };
|
|
const clean = reviewSummary(chinese, assessment, passingCI, fullReview);
|
|
assert.match(clean, /可以合并/);
|
|
assert.doesNotMatch(clean, /修改后再审/);
|
|
assert.doesNotMatch(clean, /Refactor|界面规范与风格|实现精简与重复逻辑/);
|
|
|
|
const issue = reviewSummary(chinese, mergeAssessment, passingCI, { ...fullReview, hasFindings: true });
|
|
assert.match(issue, /修改后再审/);
|
|
assert.match(issue, /实质问题/);
|
|
|
|
const conflict = reviewSummary(chinese, mergeAssessment, passingCI, { ...fullReview, mergeable: false });
|
|
assert.match(conflict, /修改后再审.*合并冲突/);
|
|
const partial = reviewSummary(chinese, mergeAssessment, passingCI, { ...fullReview, incomplete: true });
|
|
assert.match(partial, /先审完剩余改动/);
|
|
assert.doesNotMatch(partial, /先验证关键场景/);
|
|
});
|
|
|
|
test("reviewSummary reserves pre-merge manual checks for supported risks in changed code", () => {
|
|
const context = { ...fullReview, locations: new Map([["disk.go", diffAnchors("@@ -1 +1,2 @@\n existing guard\n+replaceDisk()")]]) };
|
|
const disk = {
|
|
path: "disk.go", evidence: { side: "RIGHT", line: 2, text: "replaceDisk()" }, impact: "data",
|
|
scenario: "Force a host write failure and verify the old disk reopens.", consequence: "Data becomes inaccessible after a failed rollback.",
|
|
};
|
|
const assessment = { ...mergeAssessment, decision: "verify", reason: "Retest everything", pending_checks: [
|
|
"Re-run CI and check small-window styling.",
|
|
{ ...disk, impact: "style", scenario: "Check label width." },
|
|
{ ...disk, path: "unknown.go", scenario: "Unrelated host test." },
|
|
{ ...disk, evidence: { side: "RIGHT", line: 1, text: "existing guard" }, scenario: "Verify an unchanged guard." },
|
|
] };
|
|
const clear = reviewSummary(chinese, assessment, passingCI, context);
|
|
assert.match(clear, /可以合并/);
|
|
assert.doesNotMatch(clear, /合并前必做实测|先验证关键场景|Retest everything/);
|
|
const blocked = reviewSummary(chinese, { ...assessment, pending_checks: [...assessment.pending_checks, disk] }, passingCI, context);
|
|
assert.match(blocked, /合并前必做实测/);
|
|
assert.match(blocked, /Force a host write failure.*Data becomes inaccessible/);
|
|
assert.match(blocked, /先验证关键场景/);
|
|
assert.doesNotMatch(blocked, /Re-run CI|label width|Unrelated host test|unchanged guard/);
|
|
});
|