|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { execFileSync, spawnSync } from "node:child_process"; |
| 3 | +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { delimiter, join } from "node:path"; |
| 6 | +import test from "node:test"; |
| 7 | + |
| 8 | +const SCRIPT = join(process.cwd(), "scripts", "dispatch-receipt-owner.sh"); |
| 9 | +const EXPECTED_TITLE = "Assist openclaw/openclaw#42 [router-proof]"; |
| 10 | + |
| 11 | +function fixtureEnv(options: { |
| 12 | + jobs101?: unknown[]; |
| 13 | + jobs102?: unknown[]; |
| 14 | + runs: unknown[]; |
| 15 | + failJobs?: boolean; |
| 16 | +}) { |
| 17 | + const binDir = mkdtempSync(join(tmpdir(), "dispatch-receipt-owner-")); |
| 18 | + const gh = join(binDir, "gh"); |
| 19 | + writeFileSync( |
| 20 | + gh, |
| 21 | + `#!/bin/sh |
| 22 | +case "$*" in |
| 23 | + *actions/workflows/*) printf '%s\\n' "$FAKE_RUNS_JSON" ;; |
| 24 | + *actions/runs/101/jobs*) |
| 25 | + [ "\${FAKE_FAIL_JOBS:-0}" = 1 ] && exit 7 |
| 26 | + printf '%s\\n' "$FAKE_JOBS_101_JSON" |
| 27 | + ;; |
| 28 | + *actions/runs/102/jobs*) |
| 29 | + [ "\${FAKE_FAIL_JOBS:-0}" = 1 ] && exit 7 |
| 30 | + printf '%s\\n' "$FAKE_JOBS_102_JSON" |
| 31 | + ;; |
| 32 | + *) echo "unexpected gh args: $*" >&2; exit 9 ;; |
| 33 | +esac |
| 34 | +`, |
| 35 | + ); |
| 36 | + chmodSync(gh, 0o755); |
| 37 | + return { |
| 38 | + binDir, |
| 39 | + env: { |
| 40 | + ...process.env, |
| 41 | + PATH: `${binDir}${delimiter}${process.env.PATH ?? ""}`, |
| 42 | + GITHUB_REPOSITORY: "openclaw/clawsweeper", |
| 43 | + FAKE_RUNS_JSON: JSON.stringify({ workflow_runs: options.runs }), |
| 44 | + FAKE_JOBS_101_JSON: JSON.stringify({ jobs: options.jobs101 ?? [] }), |
| 45 | + FAKE_JOBS_102_JSON: JSON.stringify({ jobs: options.jobs102 ?? [] }), |
| 46 | + FAKE_FAIL_JOBS: options.failJobs ? "1" : "0", |
| 47 | + }, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +function runGate(options: Parameters<typeof fixtureEnv>[0], requiredJobName = "assist") { |
| 52 | + const fixture = fixtureEnv(options); |
| 53 | + try { |
| 54 | + return execFileSync("bash", [SCRIPT, "assist.yml", EXPECTED_TITLE, "200", requiredJobName], { |
| 55 | + encoding: "utf8", |
| 56 | + env: fixture.env, |
| 57 | + }).trim(); |
| 58 | + } finally { |
| 59 | + rmSync(fixture.binDir, { recursive: true, force: true }); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +test("dispatch receipt gate keeps an older active run as owner", () => { |
| 64 | + assert.equal( |
| 65 | + runGate({ |
| 66 | + runs: [{ id: 101, display_title: EXPECTED_TITLE, status: "in_progress" }], |
| 67 | + }), |
| 68 | + "owner", |
| 69 | + ); |
| 70 | +}); |
| 71 | + |
| 72 | +test("dispatch receipt gate allows retry after failed owner and receipt-only success", () => { |
| 73 | + for (const requiredJobName of ["assist", "Plan and review cluster"]) { |
| 74 | + assert.equal( |
| 75 | + runGate( |
| 76 | + { |
| 77 | + runs: [ |
| 78 | + { id: 101, display_title: EXPECTED_TITLE, status: "completed", conclusion: "failure" }, |
| 79 | + { id: 102, display_title: EXPECTED_TITLE, status: "completed", conclusion: "success" }, |
| 80 | + ], |
| 81 | + jobs102: [ |
| 82 | + { name: "Deduplicate command dispatch receipt", conclusion: "success" }, |
| 83 | + { name: requiredJobName, conclusion: "skipped" }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + requiredJobName, |
| 87 | + ), |
| 88 | + "none", |
| 89 | + ); |
| 90 | + } |
| 91 | +}); |
| 92 | + |
| 93 | +test("dispatch receipt gate keeps a successfully executed worker as owner", () => { |
| 94 | + assert.equal( |
| 95 | + runGate({ |
| 96 | + runs: [ |
| 97 | + { id: 102, display_title: EXPECTED_TITLE, status: "completed", conclusion: "success" }, |
| 98 | + ], |
| 99 | + jobs102: [{ name: "assist", conclusion: "success" }], |
| 100 | + }), |
| 101 | + "owner", |
| 102 | + ); |
| 103 | +}); |
| 104 | + |
| 105 | +test("dispatch receipt gate fails closed when worker job verification fails", () => { |
| 106 | + const fixture = fixtureEnv({ |
| 107 | + runs: [{ id: 102, display_title: EXPECTED_TITLE, status: "completed", conclusion: "success" }], |
| 108 | + failJobs: true, |
| 109 | + }); |
| 110 | + try { |
| 111 | + const result = spawnSync("bash", [SCRIPT, "assist.yml", EXPECTED_TITLE, "200", "assist"], { |
| 112 | + encoding: "utf8", |
| 113 | + env: fixture.env, |
| 114 | + }); |
| 115 | + assert.equal(result.status, 7); |
| 116 | + assert.equal(result.stdout, ""); |
| 117 | + } finally { |
| 118 | + rmSync(fixture.binDir, { recursive: true, force: true }); |
| 119 | + } |
| 120 | +}); |
0 commit comments