import assert from "node:assert/strict"; import { test } from "node:test"; import { BranchPattern, matchesAny } from "../config/branch-pattern.ts"; import { JobGraph } from "../config/job-graph.ts"; import { gateEnvironment, type EnvironmentRules, type GateDecision } from "../runs/environment-gate.ts"; import { Run, type Gate } from "../runs/run.ts"; import { InvalidJobTransition } from "../runs/states.ts"; import { approvedTrust, decideTrust } from "../runs/trust.ts"; function graph(needs: Record): JobGraph { const checked = JobGraph.check(new Map(Object.entries(needs).map(([name, required]) => [name, { needs: required }]))); if ("problems" in checked) assert.fail(JSON.stringify(checked.problems)); return checked.graph; } const open: Gate = () => null; const pipeline = () => graph({ test: [], lint: [], build: ["test", "lint"], deploy: ["build"] }); test("a new run is queued and starts only the jobs that need nothing", () => { const run = new Run(pipeline()); assert.equal(run.state, "queued"); assert.deepEqual(run.advance(open).start, [ { job: "test", waitSeconds: 0 }, { job: "lint", waitSeconds: 0 }, ]); assert.equal(run.state, "running"); assert.deepEqual(run.advance(open).start, [], "a queued job is not handed out twice"); }); test("a job starts when every job it needs succeeds, and the run succeeds at the end", () => { const run = new Run(pipeline()); run.advance(open); for (const job of ["test", "lint"]) run.started(job); run.finished("test", "succeeded"); assert.deepEqual(run.advance(open).start, []); run.finished("lint", "succeeded"); assert.deepEqual(run.advance(open).start.map((entry) => entry.job), ["build"]); run.started("build"); run.finished("build", "succeeded"); run.advance(open); run.started("deploy"); run.finished("deploy", "succeeded"); assert.equal(run.state, "succeeded"); }); test("a failure skips every job after it, in one advance", () => { const run = new Run(pipeline()); run.advance(open); run.started("test"); run.started("lint"); run.finished("test", "failed", "npm test exited 1"); const advance = run.advance(open); assert.deepEqual(advance.skipped, ["build", "deploy"]); assert.equal(run.state, "running", "lint still runs"); run.finished("lint", "succeeded"); assert.equal(run.state, "failed"); }); test("an environment that needs approval holds the job until a person approves", () => { const run = new Run(graph({ build: [], deploy: ["build"] })); const gate: Gate = (job) => (job === "deploy" ? { kind: "needs_approval", waitSeconds: 0, reason: "production requires approval." } : null); run.advance(gate); run.started("build"); run.finished("build", "succeeded"); assert.deepEqual(run.advance(gate).awaitingApproval, [{ job: "deploy", reason: "production requires approval." }]); assert.equal(run.state, "waiting_approval"); run.approve("deploy", "aman"); assert.equal(run.job("deploy").state, "queued"); assert.equal(run.state, "running"); assert.deepEqual(run.advance(gate).start, [], "an approved job is queued, and advance does not hand it out"); }); test("a rejected deploy cancels the job, and a closed environment fails it with the reason", () => { const rejected = new Run(graph({ deploy: [] })); rejected.advance(() => ({ kind: "needs_approval", waitSeconds: 0, reason: "r" })); rejected.reject("deploy", "aman"); assert.equal(rejected.state, "cancelled"); const closed = new Run(graph({ deploy: [] })); assert.deepEqual(closed.advance(() => ({ kind: "closed", reason: "No such environment." })).refused, [{ job: "deploy", reason: "No such environment." }]); assert.deepEqual(closed.job("deploy"), { name: "deploy", state: "failed", reason: "No such environment." }); }); test("cancel stops live jobs, and a late exit after it is ignored", () => { const run = new Run(pipeline()); run.advance(open); run.started("test"); assert.deepEqual(run.cancel("A newer push replaced this run."), ["test", "lint"]); assert.equal(run.state, "cancelled"); assert.equal(run.finished("test", "failed"), "ignored"); assert.equal(run.job("test").state, "cancelled"); }); test("a job that waits for its image moves on when the image is ready, or fails with the build", () => { const run = new Run(graph({ a: [], b: [] })); run.waitForImage("a"); run.waitForImage("b"); assert.equal(run.state, "waiting_image"); run.imageReady("a"); run.imageFailed("b", "The image build failed at step 2."); assert.deepEqual(run.advance(open).start.map((entry) => entry.job), ["a"]); assert.equal(run.job("b").state, "failed"); }); test("a move the rules do not allow throws", () => { const run = new Run(pipeline()); assert.throws(() => run.approve("test", "aman"), InvalidJobTransition); }); test("saved records load back into the same run", () => { const run = new Run(pipeline()); run.advance(open); run.started("test"); const loaded = new Run(pipeline(), run.records()); assert.deepEqual(loaded.records(), run.records()); assert.equal(loaded.state, "running"); }); test("trust follows who started the run", () => { const protectedBranches = ["main", "release/**"]; assert.deepEqual(decideTrust({ kind: "push", pusher: "human", branch: "main" }, protectedBranches), { trust: "trusted", secrets: "all", reason: "A person pushed to a protected branch." }); assert.equal(decideTrust({ kind: "push", pusher: "human", branch: "release/2026/12" }, protectedBranches).secrets, "all"); assert.equal(decideTrust({ kind: "push", pusher: "human", branch: "feature" }, protectedBranches).secrets, "repo"); assert.equal(decideTrust({ kind: "push", pusher: "human", branch: "feature" }, []).secrets, "repo"); assert.deepEqual(decideTrust({ kind: "push", pusher: "agent", branch: "main" }, protectedBranches), { trust: "untrusted", secrets: "none", reason: "An agent pushed this commit." }); assert.equal(decideTrust({ kind: "push", pusher: "system", branch: "main" }, protectedBranches).trust, "untrusted"); assert.equal(decideTrust({ kind: "pull_request", author: "human", fromFork: true, number: 1, base: "main", head: "feature" }, protectedBranches).secrets, "none"); assert.equal(decideTrust({ kind: "pull_request", author: "agent", fromFork: false, number: 1, base: "main", head: "feature" }, protectedBranches).secrets, "none"); assert.equal(decideTrust({ kind: "pull_request", author: "human", fromFork: false, number: 1, base: "main", head: "feature" }, protectedBranches).secrets, "repo"); assert.equal(decideTrust({ kind: "manual", by: "human", branch: "main" }, protectedBranches).secrets, "all"); assert.equal(decideTrust({ kind: "manual", by: "human", branch: "agent/try" }, protectedBranches).secrets, "repo"); assert.equal(decideTrust({ kind: "manual", by: "agent", branch: "main" }, protectedBranches).trust, "untrusted"); assert.deepEqual(approvedTrust("aman"), { trust: "trusted", secrets: "repo", reason: "aman approved the run." }); }); test("an environment gate checks existence, branch, trust and approval, in that order", () => { const production: EnvironmentRules = { name: "production", allowedBranches: ["main"], requiresApproval: true, waitSeconds: 30 }; const staging: EnvironmentRules = { name: "staging", allowedBranches: [], requiresApproval: false, waitSeconds: 0 }; const kind = (decision: GateDecision) => decision.kind; assert.equal(kind(gateEnvironment("prod", null, { branch: "main", trust: "trusted" })), "closed"); assert.equal(kind(gateEnvironment("production", production, { branch: "feature", trust: "trusted" })), "closed"); assert.equal(kind(gateEnvironment("production", production, { branch: null, trust: "trusted" })), "closed"); assert.deepEqual(gateEnvironment("production", production, { branch: "main", trust: "trusted" }), { kind: "needs_approval", waitSeconds: 30, reason: "The environment \"production\" requires approval." }); assert.equal(kind(gateEnvironment("staging", staging, { branch: "tasks/4", trust: "untrusted" })), "needs_approval"); assert.deepEqual(gateEnvironment("staging", staging, { branch: "tasks/4", trust: "trusted" }), { kind: "open", waitSeconds: 0 }); }); test("branch patterns: * stays in one segment, ** crosses segments, the rest is literal", () => { assert.equal(new BranchPattern("tasks/*").matches("tasks/42"), true); assert.equal(new BranchPattern("tasks/*").matches("tasks/42/fix"), false); assert.equal(new BranchPattern("tasks/**").matches("tasks/42/fix"), true); assert.equal(new BranchPattern("v1.0").matches("v1x0"), false); assert.equal(matchesAny([], "anything"), true); });