| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { MIRROR_READ_AREAS, type RepoArea, type RepoMirror, mirrorOffers, mirrorUpdatesWait, mirrorWritable } from "./mirrors.ts"; |
| 5 | |
| 6 | const mirror = (state: RepoMirror["state"], warm = false): RepoMirror => ({ |
| 7 | state, |
| 8 | remote: "github.com/acme/web", |
| 9 | url: "https://github.com/acme/web", |
| 10 | since: "2026-10-08T10:00:00Z", |
| 11 | warm, |
| 12 | }); |
| 13 | const AREAS: RepoArea[] = ["code", "issues", "pulls", "agents", "workflows", "deployments", "security", "insights", "settings", "updates"]; |
| 14 | const WORK: RepoArea[] = ["issues", "pulls", "agents", "deployments", "updates"]; |
| 15 | |
| 16 | test("only a takeover takes writes", () => { |
| 17 | assert.equal(mirrorWritable(null), true); |
| 18 | assert.equal(mirrorWritable(undefined), true); |
| 19 | assert.equal(mirrorWritable(mirror("takeover")), true); |
| 20 | for (const state of ["standby", "ci", "handing_back"] as const) assert.equal(mirrorWritable(mirror(state)), false, state); |
| 21 | }); |
| 22 | |
| 23 | test("a mirror offers a read-only copy until it is taken over", () => { |
| 24 | for (const state of ["standby", "ci", "handing_back"] as const) { |
| 25 | for (const area of MIRROR_READ_AREAS) assert.ok(mirrorOffers(mirror(state), area), `${state} ${area}`); |
| 26 | for (const area of WORK) assert.ok(!mirrorOffers(mirror(state), area), `${state} ${area}`); |
| 27 | } |
| 28 | for (const area of AREAS) { |
| 29 | assert.ok(mirrorOffers(mirror("takeover"), area), `taken over: ${area}`); |
| 30 | assert.ok(mirrorOffers(null, area), `leads: ${area}`); |
| 31 | } |
| 32 | }); |
| 33 | |
| 34 | test("workflows run in CI failover, during a takeover, and on a standby kept warm", () => { |
| 35 | assert.equal(mirrorOffers(mirror("standby"), "workflows"), false); |
| 36 | assert.equal(mirrorOffers(mirror("standby", true), "workflows"), true); |
| 37 | assert.equal(mirrorOffers(mirror("ci"), "workflows"), true); |
| 38 | assert.equal(mirrorOffers(mirror("takeover"), "workflows"), true); |
| 39 | assert.equal(mirrorOffers(mirror("handing_back"), "workflows"), false); |
| 40 | assert.equal(mirrorOffers(mirror("handing_back", true), "workflows"), false, "nothing starts while handing back"); |
| 41 | }); |
| 42 | |
| 43 | test("why updates wait names the repository and its remote", () => { |
| 44 | const why = mirrorUpdatesWait("acme", "web", mirror("standby")); |
| 45 | assert.ok(why.startsWith("Security updates wait until acme/web is taken over or moved to g1t.")); |
| 46 | assert.ok(why.includes("github.com/acme/web")); |
| 47 | }); |