|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import test from "node:test"; |
| 4 | +import { parse } from "yaml"; |
| 5 | + |
| 6 | +const liveWorkflow = readFileSync(".github/workflows/clawsweeper-dispatch.yml", "utf8").replace( |
| 7 | + /\r\n/g, |
| 8 | + "\n", |
| 9 | +); |
| 10 | +const documentation = readFileSync("docs/target-dispatcher.md", "utf8").replace(/\r\n/g, "\n"); |
| 11 | +const templateMatch = documentation.match(/```yaml\n(name: ClawSweeper Dispatch[\s\S]*?)\n```/); |
| 12 | + |
| 13 | +assert.ok(templateMatch, "target dispatcher workflow template is missing"); |
| 14 | +const documentedWorkflow = `${templateMatch[1]}\n`; |
| 15 | + |
| 16 | +type WorkflowStep = { |
| 17 | + name?: string; |
| 18 | + if?: string; |
| 19 | + run?: string; |
| 20 | + env?: Record<string, string>; |
| 21 | + with?: Record<string, string>; |
| 22 | + "continue-on-error"?: boolean; |
| 23 | +}; |
| 24 | + |
| 25 | +function dispatchSteps(source: string): WorkflowStep[] { |
| 26 | + const workflow = parse(source) as { |
| 27 | + jobs?: { dispatch?: { steps?: WorkflowStep[] } }; |
| 28 | + }; |
| 29 | + return workflow.jobs?.dispatch?.steps ?? []; |
| 30 | +} |
| 31 | + |
| 32 | +function namedStep(steps: WorkflowStep[], name: string): WorkflowStep { |
| 33 | + const step = steps.find((candidate) => candidate.name === name); |
| 34 | + assert.ok(step, `missing workflow step: ${name}`); |
| 35 | + return step; |
| 36 | +} |
| 37 | + |
| 38 | +function normalizeWhitespace(value: string | undefined): string { |
| 39 | + return (value ?? "").replace(/\s+/g, " ").trim(); |
| 40 | +} |
| 41 | + |
| 42 | +test("documented target dispatcher template matches the live workflow", () => { |
| 43 | + assert.equal(documentedWorkflow, liveWorkflow); |
| 44 | +}); |
| 45 | + |
| 46 | +test("target dispatcher acknowledges non-draft PR receipts before review dispatch", () => { |
| 47 | + for (const source of [liveWorkflow, documentedWorkflow]) { |
| 48 | + const steps = dispatchSteps(source); |
| 49 | + const tokenIndex = steps.findIndex( |
| 50 | + (step) => step.name === "Create target PR acknowledgement token", |
| 51 | + ); |
| 52 | + const acknowledgementIndex = steps.findIndex( |
| 53 | + (step) => step.name === "Acknowledge received pull request", |
| 54 | + ); |
| 55 | + const dispatchIndex = steps.findIndex( |
| 56 | + (step) => step.name === "Dispatch exact ClawSweeper review", |
| 57 | + ); |
| 58 | + assert.ok(tokenIndex >= 0 && tokenIndex < acknowledgementIndex); |
| 59 | + assert.ok(acknowledgementIndex < dispatchIndex); |
| 60 | + |
| 61 | + const token = namedStep(steps, "Create target PR acknowledgement token"); |
| 62 | + const acknowledgement = namedStep(steps, "Acknowledge received pull request"); |
| 63 | + const expectedGate = [ |
| 64 | + "${{", |
| 65 | + "github.event_name == 'pull_request_target' &&", |
| 66 | + "(", |
| 67 | + "github.event.action == 'ready_for_review' ||", |
| 68 | + "(github.event.action == 'opened' && github.event.pull_request.draft == false)", |
| 69 | + ") &&", |
| 70 | + "env.HAS_CLAWSWEEPER_APP_PRIVATE_KEY == 'true'", |
| 71 | + "}}", |
| 72 | + ].join(" "); |
| 73 | + |
| 74 | + assert.equal(normalizeWhitespace(token.if), expectedGate); |
| 75 | + assert.equal(normalizeWhitespace(acknowledgement.if), expectedGate); |
| 76 | + assert.equal(token["continue-on-error"], true); |
| 77 | + assert.equal(acknowledgement["continue-on-error"], true); |
| 78 | + assert.deepEqual( |
| 79 | + Object.keys(token.with ?? {}).filter((key) => key.startsWith("permission-")), |
| 80 | + ["permission-issues"], |
| 81 | + ); |
| 82 | + assert.equal(token.with?.["permission-issues"], "write"); |
| 83 | + assert.equal(acknowledgement.env?.ACK_TOKEN, "${{ steps.pr_ack_token.outputs.token }}"); |
| 84 | + |
| 85 | + const run = acknowledgement.run ?? ""; |
| 86 | + assert.match(run, /issues\/\$ITEM_NUMBER\/comments\?per_page=100/); |
| 87 | + assert.match(run, /--arg marker_prefix "clawsweeper-pr-ack:"/); |
| 88 | + assert.match(run, /--arg marker_suffix " item=\$ITEM_NUMBER -->"/); |
| 89 | + assert.match(run, /"<!-- clawsweeper-pr-ack:\$SOURCE_ACTION item=\$ITEM_NUMBER -->"/); |
| 90 | + assert.match( |
| 91 | + run, |
| 92 | + /"Pull request received\. I will update this pull request when review starts\."/, |
| 93 | + ); |
| 94 | + assert.match(run, /issues\/\$ITEM_NUMBER\/comments"\s*\\\s*--method POST/); |
| 95 | + } |
| 96 | +}); |
0 commit comments