154 lines · 8.4 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { BranchPattern, matchesAny } from "../config/branch-pattern.ts"; |
| 4 | import { JobGraph } from "../config/job-graph.ts"; |
| 5 | import { gateEnvironment, type EnvironmentRules, type GateDecision } from "../runs/environment-gate.ts"; |
| 6 | import { Run, type Gate } from "../runs/run.ts"; |
| 7 | import { InvalidJobTransition } from "../runs/states.ts"; |
| 8 | import { approvedTrust, decideTrust } from "../runs/trust.ts"; |
| 9 | |
| 10 | function graph(needs: Record<string, string[]>): JobGraph { |
| 11 | const checked = JobGraph.check(new Map(Object.entries(needs).map(([name, required]) => [name, { needs: required }]))); |
| 12 | if ("problems" in checked) assert.fail(JSON.stringify(checked.problems)); |
| 13 | return checked.graph; |
| 14 | } |
| 15 | |
| 16 | const open: Gate = () => null; |
| 17 | const pipeline = () => graph({ test: [], lint: [], build: ["test", "lint"], deploy: ["build"] }); |
| 18 | |
| 19 | test("a new run is queued and starts only the jobs that need nothing", () => { |
| 20 | const run = new Run(pipeline()); |
| 21 | assert.equal(run.state, "queued"); |
| 22 | assert.deepEqual(run.advance(open).start, [ |
| 23 | { job: "test", waitSeconds: 0 }, |
| 24 | { job: "lint", waitSeconds: 0 }, |
| 25 | ]); |
| 26 | assert.equal(run.state, "running"); |
| 27 | assert.deepEqual(run.advance(open).start, [], "a queued job is not handed out twice"); |
| 28 | }); |
| 29 | |
| 30 | test("a job starts when every job it needs succeeds, and the run succeeds at the end", () => { |
| 31 | const run = new Run(pipeline()); |
| 32 | run.advance(open); |
| 33 | for (const job of ["test", "lint"]) run.started(job); |
| 34 | run.finished("test", "succeeded"); |
| 35 | assert.deepEqual(run.advance(open).start, []); |
| 36 | run.finished("lint", "succeeded"); |
| 37 | assert.deepEqual(run.advance(open).start.map((entry) => entry.job), ["build"]); |
| 38 | run.started("build"); |
| 39 | run.finished("build", "succeeded"); |
| 40 | run.advance(open); |
| 41 | run.started("deploy"); |
| 42 | run.finished("deploy", "succeeded"); |
| 43 | assert.equal(run.state, "succeeded"); |
| 44 | }); |
| 45 | |
| 46 | test("a failure skips every job after it, in one advance", () => { |
| 47 | const run = new Run(pipeline()); |
| 48 | run.advance(open); |
| 49 | run.started("test"); |
| 50 | run.started("lint"); |
| 51 | run.finished("test", "failed", "npm test exited 1"); |
| 52 | const advance = run.advance(open); |
| 53 | assert.deepEqual(advance.skipped, ["build", "deploy"]); |
| 54 | assert.equal(run.state, "running", "lint still runs"); |
| 55 | run.finished("lint", "succeeded"); |
| 56 | assert.equal(run.state, "failed"); |
| 57 | }); |
| 58 | |
| 59 | test("an environment that needs approval holds the job until a person approves", () => { |
| 60 | const run = new Run(graph({ build: [], deploy: ["build"] })); |
| 61 | const gate: Gate = (job) => (job === "deploy" ? { kind: "needs_approval", waitSeconds: 0, reason: "production requires approval." } : null); |
| 62 | run.advance(gate); |
| 63 | run.started("build"); |
| 64 | run.finished("build", "succeeded"); |
| 65 | assert.deepEqual(run.advance(gate).awaitingApproval, [{ job: "deploy", reason: "production requires approval." }]); |
| 66 | assert.equal(run.state, "waiting_approval"); |
| 67 | run.approve("deploy", "aman"); |
| 68 | assert.equal(run.job("deploy").state, "queued"); |
| 69 | assert.equal(run.state, "running"); |
| 70 | assert.deepEqual(run.advance(gate).start, [], "an approved job is queued, and advance does not hand it out"); |
| 71 | }); |
| 72 | |
| 73 | test("a rejected deploy cancels the job, and a closed environment fails it with the reason", () => { |
| 74 | const rejected = new Run(graph({ deploy: [] })); |
| 75 | rejected.advance(() => ({ kind: "needs_approval", waitSeconds: 0, reason: "r" })); |
| 76 | rejected.reject("deploy", "aman"); |
| 77 | assert.equal(rejected.state, "cancelled"); |
| 78 | |
| 79 | const closed = new Run(graph({ deploy: [] })); |
| 80 | assert.deepEqual(closed.advance(() => ({ kind: "closed", reason: "No such environment." })).refused, [{ job: "deploy", reason: "No such environment." }]); |
| 81 | assert.deepEqual(closed.job("deploy"), { name: "deploy", state: "failed", reason: "No such environment." }); |
| 82 | }); |
| 83 | |
| 84 | test("cancel stops live jobs, and a late exit after it is ignored", () => { |
| 85 | const run = new Run(pipeline()); |
| 86 | run.advance(open); |
| 87 | run.started("test"); |
| 88 | assert.deepEqual(run.cancel("A newer push replaced this run."), ["test", "lint"]); |
| 89 | assert.equal(run.state, "cancelled"); |
| 90 | assert.equal(run.finished("test", "failed"), "ignored"); |
| 91 | assert.equal(run.job("test").state, "cancelled"); |
| 92 | }); |
| 93 | |
| 94 | test("a job that waits for its image moves on when the image is ready, or fails with the build", () => { |
| 95 | const run = new Run(graph({ a: [], b: [] })); |
| 96 | run.waitForImage("a"); |
| 97 | run.waitForImage("b"); |
| 98 | assert.equal(run.state, "waiting_image"); |
| 99 | run.imageReady("a"); |
| 100 | run.imageFailed("b", "The image build failed at step 2."); |
| 101 | assert.deepEqual(run.advance(open).start.map((entry) => entry.job), ["a"]); |
| 102 | assert.equal(run.job("b").state, "failed"); |
| 103 | }); |
| 104 | |
| 105 | test("a move the rules do not allow throws", () => { |
| 106 | const run = new Run(pipeline()); |
| 107 | assert.throws(() => run.approve("test", "aman"), InvalidJobTransition); |
| 108 | }); |
| 109 | |
| 110 | test("saved records load back into the same run", () => { |
| 111 | const run = new Run(pipeline()); |
| 112 | run.advance(open); |
| 113 | run.started("test"); |
| 114 | const loaded = new Run(pipeline(), run.records()); |
| 115 | assert.deepEqual(loaded.records(), run.records()); |
| 116 | assert.equal(loaded.state, "running"); |
| 117 | }); |
| 118 | |
| 119 | test("trust follows who started the run", () => { |
| 120 | const protectedBranches = ["main", "release/**"]; |
| 121 | assert.deepEqual(decideTrust({ kind: "push", pusher: "human", branch: "main" }, protectedBranches), { trust: "trusted", secrets: "all", reason: "A person pushed to a protected branch." }); |
| 122 | assert.equal(decideTrust({ kind: "push", pusher: "human", branch: "release/2026/12" }, protectedBranches).secrets, "all"); |
| 123 | assert.equal(decideTrust({ kind: "push", pusher: "human", branch: "feature" }, protectedBranches).secrets, "repo"); |
| 124 | assert.equal(decideTrust({ kind: "push", pusher: "human", branch: "feature" }, []).secrets, "repo"); |
| 125 | assert.deepEqual(decideTrust({ kind: "push", pusher: "agent", branch: "main" }, protectedBranches), { trust: "untrusted", secrets: "none", reason: "An agent pushed this commit." }); |
| 126 | assert.equal(decideTrust({ kind: "push", pusher: "system", branch: "main" }, protectedBranches).trust, "untrusted"); |
| 127 | assert.equal(decideTrust({ kind: "pull_request", author: "human", fromFork: true, number: 1, base: "main", head: "feature" }, protectedBranches).secrets, "none"); |
| 128 | assert.equal(decideTrust({ kind: "pull_request", author: "agent", fromFork: false, number: 1, base: "main", head: "feature" }, protectedBranches).secrets, "none"); |
| 129 | assert.equal(decideTrust({ kind: "pull_request", author: "human", fromFork: false, number: 1, base: "main", head: "feature" }, protectedBranches).secrets, "repo"); |
| 130 | assert.equal(decideTrust({ kind: "manual", by: "human", branch: "main" }, protectedBranches).secrets, "all"); |
| 131 | assert.equal(decideTrust({ kind: "manual", by: "human", branch: "agent/try" }, protectedBranches).secrets, "repo"); |
| 132 | assert.equal(decideTrust({ kind: "manual", by: "agent", branch: "main" }, protectedBranches).trust, "untrusted"); |
| 133 | assert.deepEqual(approvedTrust("aman"), { trust: "trusted", secrets: "repo", reason: "aman approved the run." }); |
| 134 | }); |
| 135 | |
| 136 | test("an environment gate checks existence, branch, trust and approval, in that order", () => { |
| 137 | const production: EnvironmentRules = { name: "production", allowedBranches: ["main"], requiresApproval: true, waitSeconds: 30 }; |
| 138 | const staging: EnvironmentRules = { name: "staging", allowedBranches: [], requiresApproval: false, waitSeconds: 0 }; |
| 139 | const kind = (decision: GateDecision) => decision.kind; |
| 140 | assert.equal(kind(gateEnvironment("prod", null, { branch: "main", trust: "trusted" })), "closed"); |
| 141 | assert.equal(kind(gateEnvironment("production", production, { branch: "feature", trust: "trusted" })), "closed"); |
| 142 | assert.equal(kind(gateEnvironment("production", production, { branch: null, trust: "trusted" })), "closed"); |
| 143 | assert.deepEqual(gateEnvironment("production", production, { branch: "main", trust: "trusted" }), { kind: "needs_approval", waitSeconds: 30, reason: "The environment \"production\" requires approval." }); |
| 144 | assert.equal(kind(gateEnvironment("staging", staging, { branch: "tasks/4", trust: "untrusted" })), "needs_approval"); |
| 145 | assert.deepEqual(gateEnvironment("staging", staging, { branch: "tasks/4", trust: "trusted" }), { kind: "open", waitSeconds: 0 }); |
| 146 | }); |
| 147 | |
| 148 | test("branch patterns: * stays in one segment, ** crosses segments, the rest is literal", () => { |
| 149 | assert.equal(new BranchPattern("tasks/*").matches("tasks/42"), true); |
| 150 | assert.equal(new BranchPattern("tasks/*").matches("tasks/42/fix"), false); |
| 151 | assert.equal(new BranchPattern("tasks/**").matches("tasks/42/fix"), true); |
| 152 | assert.equal(new BranchPattern("v1.0").matches("v1x0"), false); |
| 153 | assert.equal(matchesAny([], "anything"), true); |
| 154 | }); |