| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { type Attempt, shipStateOf, shipmentOf, sitePath, walkPage } from "./shipped.ts"; |
| 5 | |
| 6 | const commit = (hash: string, parent: string | null = null) => ({ hash, parents: parent ? [parent] : [] }); |
| 7 | |
| 8 | test("a deployment ships what no deployment shipped before, and stops at the last one's history", () => { |
| 9 | const page = [commit("c3", "c2"), commit("c2", "c1"), commit("c1", "c0")]; |
| 10 | // Nothing shipped yet, a page not full: all of it, and no more pages. |
| 11 | assert.deepEqual(walkPage(page, new Set(), false), { ship: ["c3", "c2", "c1"], more: false, next: "c0" }); |
| 12 | // A full page with nothing known: read on from the last commit's first parent. |
| 13 | assert.deepEqual(walkPage(page, new Set(), true), { ship: ["c3", "c2", "c1"], more: true, next: "c0" }); |
| 14 | // The last deployment shipped c1: only what came after it, and the walk stops. |
| 15 | assert.deepEqual(walkPage(page, new Set(["c1"]), true), { ship: ["c3", "c2"], more: false, next: "c0" }); |
| 16 | // A rollback to a commit shipped before ships nothing. |
| 17 | assert.deepEqual(walkPage([commit("c1", "c0")], new Set(["c1", "c0"]), false).ship, []); |
| 18 | // A merged branch's older commits after the last deployment's commit in the log still ship. |
| 19 | assert.deepEqual(walkPage([commit("m", "c1"), commit("c1", "c0"), commit("f1", "c0")], new Set(["c1"]), false).ship, ["m", "f1"]); |
| 20 | }); |
| 21 | |
| 22 | const ask = { sha: "abc", landed_at: "2026-10-14T10:00:00.000Z" }; |
| 23 | const at = (minutes: number) => new Date(Date.parse(ask.landed_at) + minutes * 60_000).toISOString(); |
| 24 | const attempt = (minutes: number, outcome: Attempt["outcome"], url = `/acme/web/deployments/d${minutes}`): Attempt => ({ at: at(minutes), outcome, url, environment: "production" }); |
| 25 | |
| 26 | test("a change shipped is live from the deployment that first shipped it", () => { |
| 27 | const shipped = { deployed_at: at(9), url: "/acme/web/actions/runs/run_1", environment: "production" }; |
| 28 | assert.deepEqual(shipStateOf(ask, shipped, [attempt(3, "failed"), attempt(9, "live")], at(-60)), { |
| 29 | sha: "abc", |
| 30 | state: "live", |
| 31 | deployed_at: at(9), |
| 32 | url: "/acme/web/actions/runs/run_1", |
| 33 | environment: "production", |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | test("a change not shipped is deploying, failed or waiting, by the production attempts after it", () => { |
| 38 | const start = at(-60); |
| 39 | assert.equal(shipStateOf(ask, null, [attempt(2, "running")], start).state, "deploying"); |
| 40 | const failed = shipStateOf(ask, null, [attempt(2, "failed")], start); |
| 41 | assert.equal(failed.state, "failed"); |
| 42 | assert.equal(failed.url, "/acme/web/deployments/d2"); |
| 43 | assert.equal(shipStateOf(ask, null, [], start).state, "waiting"); |
| 44 | // A failure fixed by a later deployment that did not ship it (another branch, say): waiting again. |
| 45 | assert.equal(shipStateOf(ask, null, [attempt(2, "failed"), attempt(5, "live")], start).state, "waiting"); |
| 46 | // Attempts before it landed say nothing about it. |
| 47 | assert.equal(shipStateOf(ask, null, [attempt(-5, "failed")], start).state, "waiting"); |
| 48 | }); |
| 49 | |
| 50 | test("a change that landed before recording began goes live with the first success after it", () => { |
| 51 | const start = at(120); |
| 52 | const live = shipStateOf(ask, null, [attempt(-5, "live"), attempt(4, "failed"), attempt(30, "live")], start); |
| 53 | assert.equal(live.state, "live"); |
| 54 | assert.equal(live.deployed_at, at(30)); |
| 55 | // Even when the first walk wrote it down later, as of that walk. |
| 56 | assert.equal(shipStateOf(ask, { deployed_at: start, url: null, environment: "production" }, [attempt(30, "live")], start).deployed_at, at(30)); |
| 57 | // Nothing recorded at all yet: the same. |
| 58 | assert.equal(shipStateOf(ask, null, [attempt(30, "live")], null).deployed_at, at(30)); |
| 59 | }); |
| 60 | |
| 61 | test("only a production success is a shipment, linked to its page", () => { |
| 62 | const repo = { namespace: "acme", name: "web" }; |
| 63 | const deployment = { |
| 64 | id: "dep_1", |
| 65 | sha: "abc", |
| 66 | environment: "production", |
| 67 | production_environment: true, |
| 68 | source: "actions" as const, |
| 69 | project: null, |
| 70 | run_url: "https://g1t.sh/acme/web/actions/runs/run_1", |
| 71 | updated_at: "2026-10-14T10:09:00.000Z", |
| 72 | }; |
| 73 | const status = { state: "success" as const, created_at: "2026-10-14T10:09:00.000Z" }; |
| 74 | assert.deepEqual(shipmentOf({ repoId: "rep_1", deployment, deploymentStatus: status }, repo, "https://g1t.sh"), { |
| 75 | deploymentId: "dep_1", |
| 76 | repoId: "rep_1", |
| 77 | repo, |
| 78 | sha: "abc", |
| 79 | environment: "production", |
| 80 | deployedAt: "2026-10-14T10:09:00.000Z", |
| 81 | url: "/acme/web/actions/runs/run_1", |
| 82 | }); |
| 83 | assert.equal(shipmentOf({ repoId: "rep_1", deployment, deploymentStatus: { ...status, state: "in_progress" } }, repo, "https://g1t.sh"), null); |
| 84 | assert.equal(shipmentOf({ repoId: "rep_1", deployment: { ...deployment, production_environment: false }, deploymentStatus: status }, repo, "https://g1t.sh"), null); |
| 85 | const build = { ...deployment, id: "dpl_1", source: "g1t_page" as const, project: "site", run_url: null }; |
| 86 | assert.equal(shipmentOf({ repoId: "rep_1", deployment: build, deploymentStatus: status }, repo, "https://g1t.sh")?.url, "/acme/site/deployments/dpl_1"); |
| 87 | // Reported through the API with no run: the repository's deployments. |
| 88 | assert.equal(shipmentOf({ repoId: "rep_1", deployment: { ...deployment, source: "api" as const, run_url: null }, deploymentStatus: status }, repo, "https://g1t.sh")?.url, "/acme/web/deployments"); |
| 89 | }); |
| 90 | |
| 91 | test("addresses stay on the site", () => { |
| 92 | assert.equal(sitePath("https://g1t.sh/acme/web/actions/runs/1", "https://g1t.sh"), "/acme/web/actions/runs/1"); |
| 93 | assert.equal(sitePath("/acme/web", "https://g1t.sh"), "/acme/web"); |
| 94 | assert.equal(sitePath("https://elsewhere.example/x", "https://g1t.sh"), null); |
| 95 | assert.equal(sitePath(null, "https://g1t.sh"), null); |
| 96 | }); |