Files
ghbot/test/required-checks.test.ts
wdawdwa 3f793d746e feat: make retry policy error-aware and expand unit coverage
- retry: retry only 5xx/429/transport errors; fail fast on 4xx
- checks/diff/r2/cacheStore: add direct unit tests (D-08, D-10 area)
- cacheStore: validate pullNumber in history object keys
- r2: export normalizeEndpoint for testability
2026-08-22 16:09:16 +08:00

85 lines
2.6 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import type { Octokit } from "@octokit/rest";
import { requiredChecksAreGreen } from "../src/github/checks.js";
type CheckRun = {
name: string;
status: string;
conclusion?: string | null;
};
function octokitWith(params: {
checkRuns: CheckRun[];
combinedStatus?: { state: string; statuses: unknown[] };
}): Octokit {
return {
rest: {
checks: {
listForRef: async () => ({
data: { check_runs: params.checkRuns }
})
},
repos: {
getCombinedStatusForRef: async () => ({
data: params.combinedStatus ?? { state: "success", statuses: [] }
})
}
}
} as unknown as Octokit;
}
test("all green checks and statuses pass", async () => {
const octokit = octokitWith({
checkRuns: [
{ name: "ci", status: "completed", conclusion: "success" },
{ name: "lint", status: "completed", conclusion: "neutral" },
{ name: "docs", status: "completed", conclusion: "skipped" }
]
});
const result = await requiredChecksAreGreen(octokit, { owner: "a", repo: "b", ref: "sha" });
assert.equal(result.ok, true);
});
test("pending or failed checks block the merge", async () => {
const pending = await requiredChecksAreGreen(
octokitWith({ checkRuns: [{ name: "ci", status: "in_progress" }] }),
{ owner: "a", repo: "b", ref: "sha" }
);
assert.equal(pending.ok, false);
assert.match(pending.reason ?? "", /in_progress\/pending/);
const failed = await requiredChecksAreGreen(
octokitWith({
checkRuns: [{ name: "ci", status: "completed", conclusion: "failure" }]
}),
{ owner: "a", repo: "b", ref: "sha" }
);
assert.equal(failed.ok, false);
assert.match(failed.reason ?? "", /failure/);
});
test("ghbot-owned check runs are ignored", async () => {
for (const ignoredName of ["ghbot review", "bot-review", "acme/ bot-review"]) {
const result = await requiredChecksAreGreen(
octokitWith({
checkRuns: [{ name: ignoredName, status: "completed", conclusion: "action_required" }]
}),
{ owner: "a", repo: "b", ref: "sha" }
);
assert.equal(result.ok, true, `${ignoredName} should be ignored`);
}
});
test("non-success commit statuses block when present", async () => {
const result = await requiredChecksAreGreen(
octokitWith({
checkRuns: [],
combinedStatus: { state: "failure", statuses: [{ state: "failure" }] }
}),
{ owner: "a", repo: "b", ref: "sha" }
);
assert.equal(result.ok, false);
assert.match(result.reason ?? "", /Commit status is failure/);
});