Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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