import assert from "node:assert/strict"; import test from "node:test"; import type { Approval, RunAction } from "../src/lib/api.ts"; import { coveredSteps, coversSequence } from "../src/runs/sequence.ts"; function action(id: string, outcome?: { state: string; error?: string }, rejudged?: { level: string; reason?: string }, stop?: string): RunAction { return { decision: { id, level: "propose_and_wait", at: "2026-01-01T00:00:00Z", action: { connector: "host", name: id } }, rejudged: rejudged && { id: `${id}-again`, level: rejudged.level, reason: rejudged.reason, at: "2026-01-01T00:01:00Z", action: { connector: "host", name: id } }, stop, effect: outcome && { ...outcome, at: "2026-01-01T00:02:00Z" }, } as RunAction; } function request(state: string, ...ids: string[]): Approval { return { id: "request-1", run_id: "run-1", step: 0, kind: "action", action: "host snapshot", target: "container:web", level: "propose_and_wait", summary: "", state, steps: ids.map((id) => ({ decision_id: id, action: `host ${id}`, target: "container:web", level: "propose_and_wait" })), requested_at: "2026-01-01T00:00:00Z", }; } test("a covered sequence reports what became of each action in the order it was approved", () => { const cases: { name: string; state: string; /** The actions the request covers, in the order it covers them. */ covers: string[]; ended: boolean; actions: RunAction[]; want: [number, string, string][]; }[] = [ { name: "every action ran", state: "approved", covers: ["a", "b"], ended: true, actions: [action("b", { state: "ran" }), action("a", { state: "ran" })], want: [ [1, "ran", ""], [2, "ran", ""], ], }, { name: "an action that failed stops the rest", state: "approved", covers: ["a", "b", "c"], ended: true, actions: [action("a", { state: "ran" }), action("b", { state: "failed", error: "permission denied" }), action("c")], want: [ [1, "ran", ""], [2, "failed", "permission denied"], [3, "not run", ""], ], }, { name: "an action whose outcome can't be confirmed stops the rest", state: "approved", covers: ["a", "b"], ended: true, actions: [action("a", { state: "unknown", error: "connection reset" }), action("b")], want: [ [1, "unknown", "connection reset"], [2, "not run", ""], ], }, { name: "an action refused when judged again stops the rest", state: "approved", covers: ["a", "b", "c"], ended: true, actions: [action("a", { state: "ran" }), action("b", undefined, { level: "never", reason: "a release is in flight" }), action("c")], want: [ [1, "ran", ""], [2, "refused", "a release is in flight"], [3, "not run", ""], ], }, { name: "an action that no longer matches its approval stops the rest and says why", state: "approved", covers: ["a", "b", "c"], ended: true, actions: [ action("a", { state: "ran" }), action("b", undefined, { level: "escalate" }, "judgement is stricter than when the action was approved: now escalate"), action("c"), ], want: [ [1, "ran", ""], [2, "not run", "judgement is stricter than when the action was approved: now escalate"], [3, "not run", ""], ], }, { name: "a denied request runs nothing", state: "denied", covers: ["a", "b"], ended: true, actions: [action("a"), action("b")], want: [ [1, "not run", ""], [2, "not run", ""], ], }, { name: "a sequence nobody answered yet has no outcome", state: "pending", covers: ["a", "b"], ended: false, actions: [action("a"), action("b")], want: [ [1, "", ""], [2, "", ""], ], }, { name: "a run that ended before the sequence reached its actions ran none of them", state: "approved", covers: ["a", "b"], ended: true, actions: [action("a"), action("b")], want: [ [1, "not run", ""], [2, "not run", ""], ], }, ]; for (const tt of cases) { const got = coveredSteps(request(tt.state, ...tt.covers), tt.actions, tt.ended).map( (step) => [step.position, step.state, step.detail] as [number, string, string], ); assert.deepEqual(got, tt.want, tt.name); } }); test("a request covers a sequence only when it names more than one action", () => { const single = { ...request("pending", "a"), steps: undefined }; const cases: [Approval, boolean][] = [ [request("pending", "a"), false], [request("pending", "a", "b"), true], [single, false], ]; for (const [approval, want] of cases) { assert.equal(coversSequence(approval), want, JSON.stringify(approval.steps)); } });