g1t/scripts/deploy/deploy.test.mjs
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 1 | // node --test "scripts/deploy/*.test.mjs" (npm run test:deploy) |
| 2 | import assert from "node:assert/strict"; | |
| 3 | import { execFileSync } from "node:child_process"; | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 4 | import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 5 | import { tmpdir } from "node:os"; | |
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 6 | import { join } from "node:path"; |
| 7 | import { test } from "node:test"; | |
| 8 | ||
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 9 | import { isStaticElf } from "../build-runner.mjs"; |
| 10 | import { | |
| 11 | DEPLOY_CONFIG, | |
| 12 | baseInputs, | |
| 13 | baseState, | |
| 14 | baseTag, | |
| 15 | contentHash, | |
| 16 | lockFor, | |
| The runner's base image, pushed as Wrangler builds images, with pushes that fail loudly | 17 | pushImage, |
| 18 | pushedDigest, | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 19 | readBaseLock, |
| 20 | registryHas, | |
| 21 | removeDeployConfig, | |
| 22 | runnerRef, | |
| 23 | runnerTag, | |
| 24 | writeDeployConfig, | |
| 25 | } from "./image.mjs"; | |
| 26 | ||
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 27 | import { annotation, commitFrom, liveCommit, pendingFrom, versionFrom } from "./cloudflare.mjs"; |
| 28 | import { changedNames, parseCargoLock, parseNpmLock, reaches } from "./lockfiles.mjs"; | |
| 29 | import { decide, planJson, pool } from "./plan.mjs"; | |
| 30 | import { | |
| 31 | ROOT, | |
| 32 | buildGroups, | |
| 33 | cargoWorkspace, | |
| 34 | findWranglerConfigs, | |
| 35 | loadStack, | |
| 36 | npmCiArgs, | |
| 37 | npmWorkspace, | |
| 38 | outsideImports, | |
| 39 | pick, | |
| 40 | problems, | |
| 41 | resolveStack, | |
| 42 | resolvedStack, | |
| 43 | touches, | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 44 | touchesBase, |
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 45 | touchesImage, |
| 46 | } from "./stack.mjs"; | |
| 47 | ||
| 48 | const stack = resolvedStack(); | |
| 49 | const unit = (id) => stack.units.find((u) => u.id === id); | |
| 50 | ||
| 51 | // ── The manifest ────────────────────────────────────────────────────────── | |
| 52 | ||
| 53 | test("the manifest matches every wrangler config in the repository", () => { | |
| 54 | assert.deepEqual(problems(stack, findWranglerConfigs()), []); | |
| 55 | }); | |
| 56 | ||
| 57 | test("every wrangler.jsonc is found, and only checked-in ones", () => { | |
| 58 | const configs = findWranglerConfigs(); | |
| 59 | assert.ok(configs.includes("services/events/wrangler.jsonc")); | |
| 60 | assert.ok(configs.includes("apps/web/wrangler.jsonc")); | |
| 61 | assert.ok(!configs.some((c) => c.includes("/build/")), "build output is not a unit"); | |
| 62 | assert.equal(configs.length, stack.units.length); | |
| 63 | }); | |
| 64 | ||
| 65 | test("problems are found: an unlisted config, a later-stage binding, a wrong database, an unnamed KV", () => { | |
| 66 | const broken = loadStack(); | |
| 67 | const events = broken.units.find((u) => u.id === "events"); | |
| 68 | events.stage = "front"; | |
| 69 | const identity = broken.units.find((u) => u.id === "identity"); | |
| 70 | identity.d1 = { database: "g1t-identity", migrations: "migrations" }; | |
| 71 | broken.resources = { kv: {} }; | |
| 72 | const found = problems(broken, [...findWranglerConfigs(), "services/new/wrangler.jsonc"]); | |
| 73 | assert.ok(found.some((p) => p.includes("services/new/wrangler.jsonc is not in"))); | |
| 74 | assert.ok(found.some((p) => p.includes("binds to events (front), which ships after it"))); | |
| 75 | assert.ok(found.some((p) => p.startsWith("Unit identity: d1 is"))); | |
| 76 | assert.ok(found.some((p) => p.includes("has no name under resources.kv"))); | |
| 77 | }); | |
| 78 | ||
| 79 | test("stages follow bindings: nothing binds to a unit that ships after it", () => { | |
| 80 | const order = stack.stages; | |
| 81 | for (const u of stack.units) { | |
| 82 | for (const target of u.bindsTo) { | |
| 83 | const other = stack.units.find((o) => o.worker === target); | |
| 84 | assert.ok(order.indexOf(other.stage) <= order.indexOf(u.stage), `${u.id} -> ${other.id}`); | |
| 85 | } | |
| 86 | } | |
| 87 | }); | |
| 88 | ||
| 89 | test("Rust workers build with the shared script and the same wasm-opt level", () => { | |
| 90 | for (const u of stack.units.filter((u) => u.kind === "rust-worker")) { | |
| 91 | assert.equal(u.config.build.command, "node ../../scripts/build-rust-worker.mjs", u.id); | |
| 92 | const toml = readFileSync(join(ROOT, u.path, "Cargo.toml"), "utf8"); | |
| 93 | assert.match(toml, /\[package\.metadata\.wasm-pack\.profile\.release\]\s*\nwasm-opt = \["-O1"\]/, u.id); | |
| 94 | } | |
| 95 | }); | |
| 96 | ||
| 97 | // ── What each unit is built from ────────────────────────────────────────── | |
| 98 | ||
| 99 | test("shared crates and packages are read from workspace metadata", () => { | |
| 100 | assert.deepEqual(unit("events").dependsOn, ["crates/contracts", "crates/kit"]); | |
| 101 | assert.deepEqual(unit("repos").dependsOn, ["crates/contracts", "crates/kit", "crates/scan", "crates/secrets"]); | |
| 102 | assert.ok(unit("actions").dependsOn.includes("crates/actions")); | |
| 103 | assert.ok(!unit("events").dependsOn.includes("crates/scan")); | |
| 104 | assert.deepEqual(unit("web").dependsOn, ["packages/contracts", "packages/theme"]); | |
| 105 | assert.deepEqual(unit("projects").dependsOn, ["packages/contracts"]); | |
| 106 | assert.deepEqual(unit("pages").dependsOn, []); | |
| 107 | assert.equal(unit("events").crate, "g1t-events"); | |
| 108 | assert.equal(unit("web").pkg, "@g1t/web"); | |
| 109 | }); | |
| 110 | ||
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 111 | test("the runner's image is built from the runner crate and what it uses, on the base its lock names", () => { |
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 112 | const runner = unit("runner"); |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 113 | // The runner bumps lockfiles with the scanner's lockfile parser (security updates). |
| 114 | assert.deepEqual(runner.image.dirs, ["crates/actions", "crates/runner", "crates/scan"]); | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 115 | assert.deepEqual(runner.image.baseDirs, ["services/runner/base"]); |
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 116 | assert.ok(runner.dependsOn.includes("crates/runner")); |
| 117 | assert.ok(touchesImage(runner, ["crates/actions/src/expr.rs"])); | |
| 118 | assert.ok(touchesImage(runner, ["services/runner/Dockerfile"])); | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 119 | assert.ok(touchesImage(runner, ["services/runner/base.json"])); |
| 120 | assert.ok(touchesImage(runner, ["scripts/build-runner.mjs"])); | |
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 121 | assert.ok(!touchesImage(runner, ["services/runner/src/index.ts"])); |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 122 | // The base's Dockerfile reaches the image only through a new lock. |
| 123 | assert.ok(!touchesImage(runner, ["services/runner/base/Dockerfile"])); | |
| 124 | assert.ok(touchesBase(runner, ["services/runner/base/Dockerfile"])); | |
| 125 | assert.ok(!touchesBase(unit("events"), ["services/runner/base/Dockerfile"])); | |
| 126 | }); | |
| 127 | ||
| 128 | // ── The runner's images ─────────────────────────────────────────────────── | |
| 129 | ||
| 130 | test("services/runner/base.json records the base its folder builds", () => { | |
| 131 | const runner = unit("runner"); | |
| 132 | const lock = readBaseLock(runner); | |
| 133 | assert.ok(lock, "services/runner/base.json is missing: node scripts/deploy.mjs build-base"); | |
| 134 | assert.equal( | |
| 135 | lock.inputs, | |
| 136 | baseInputs(runner), | |
| 137 | "services/runner/base changed since base.json was written: node scripts/deploy.mjs build-base, and commit base.json", | |
| 138 | ); | |
| 139 | assert.match(lock.image, /^registry\.cloudflare\.com\/[0-9a-f]{32}\/g1t-runner:base-\d{8}-[0-9a-f]{12}$/); | |
| 140 | assert.ok(lock.image.endsWith(lock.inputs.slice(0, 12))); | |
| 141 | assert.equal(baseState(runner).current, true); | |
| 142 | }); | |
| 143 | ||
| 144 | test("an image's hash ignores line endings, and its tags say what they are", () => { | |
| 145 | const files = { "a.txt": "one\r\ntwo\r\n", "b.txt": "x" }; | |
| 146 | const lf = { "a.txt": "one\ntwo\n", "b.txt": "x" }; | |
| 147 | const hash = (set) => contentHash(Object.keys(set), "/", (file) => Buffer.from(set[file])); | |
| 148 | assert.equal(hash(files), hash(lf)); | |
| 149 | assert.notEqual(hash(lf), hash({ ...lf, "b.txt": "y" })); | |
| 150 | assert.equal(baseTag("0123456789abcdef".repeat(4), new Date("2026-10-06T12:00:00Z")), "base-20261006-0123456789ab"); | |
| 151 | const runner = unit("runner"); | |
| 152 | assert.match(runnerTag(runner), /^[0-9a-f]{16}$/); | |
| 153 | assert.equal(runnerTag(runner), runnerTag(runner)); | |
| 154 | assert.equal(runnerRef(runner, ROOT, "a".repeat(32)), `registry.cloudflare.com/${"a".repeat(32)}/g1t-runner:${runnerTag(runner)}`); | |
| 155 | }); | |
| 156 | ||
| 157 | test("only the lockfile entries the runner is built from name its image", () => { | |
| 158 | const lock = (wasm) => `[[package]] | |
| 159 | name = "g1t-runner" | |
| 160 | version = "0.1.0" | |
| 161 | dependencies = [ | |
| 162 | "ureq", | |
| 163 | ] | |
| 164 | ||
| 165 | [[package]] | |
| 166 | name = "ureq" | |
| 167 | version = "2.12.1" | |
| 168 | ||
| 169 | [[package]] | |
| 170 | name = "wasm-bindgen" | |
| 171 | version = "${wasm}" | |
| 172 | `; | |
| 173 | assert.equal(lockFor(lock("0.2.100"), "g1t-runner"), lockFor(lock("0.2.101"), "g1t-runner")); | |
| 174 | assert.ok(lockFor(lock("0.2.100"), "g1t-runner").includes("ureq")); | |
| 175 | assert.notEqual(lockFor(lock("0.2.100"), "g1t-runner"), lockFor(lock("0.2.100").replace("2.12.1", "2.12.2"), "g1t-runner")); | |
| 176 | }); | |
| 177 | ||
| 178 | test("the runner's deploy config names its image by reference, beside its wrangler.jsonc", () => { | |
| 179 | const runner = unit("runner"); | |
| 180 | const ref = "registry.cloudflare.com/acct/g1t-runner:0123456789abcdef"; | |
| 181 | const name = writeDeployConfig(runner, ref); | |
| 182 | try { | |
| 183 | assert.equal(name, DEPLOY_CONFIG); | |
| 184 | const config = JSON.parse(readFileSync(join(ROOT, runner.path, DEPLOY_CONFIG), "utf8")); | |
| 185 | assert.equal(config.name, "g1t-runner"); | |
| 186 | assert.equal(config.main, "./src/index.ts"); | |
| 187 | assert.ok(config.containers.length >= 1); | |
| 188 | for (const container of config.containers) { | |
| 189 | assert.equal(container.image, ref); | |
| 190 | assert.equal(container.image_build_context, undefined); | |
| 191 | } | |
| 192 | // Every sandbox class shares the image; each has a Durable Object binding. | |
| 193 | const classes = config.containers.map((c) => c.class_name).sort(); | |
| 194 | assert.deepEqual(classes, config.durable_objects.bindings.map((b) => b.class_name).sort()); | |
| 195 | // Generated, never committed, and not taken for a unit of its own. | |
| 196 | assert.ok(readFileSync(join(ROOT, ".gitignore"), "utf8").includes("wrangler.deploy.json")); | |
| 197 | assert.ok(!findWranglerConfigs().some((c) => c.endsWith(DEPLOY_CONFIG))); | |
| 198 | } finally { | |
| 199 | removeDeployConfig(runner); | |
| 200 | } | |
| 201 | assert.ok(!existsSync(join(ROOT, runner.path, DEPLOY_CONFIG))); | |
| 202 | }); | |
| 203 | ||
| 204 | test("the registry is asked for an image's manifest, without Docker", async () => { | |
| 205 | const asked = []; | |
| 206 | const fetchImpl = async (url, init) => { | |
| 207 | asked.push([url, init.method, init.headers.authorization]); | |
| 208 | return { status: url.endsWith(":nope") || url.endsWith("/nope") ? 404 : 200, ok: !url.endsWith("/nope") }; | |
| 209 | }; | |
| 210 | const credentials = async () => ({ username: "v1", password: "secret" }); | |
| 211 | assert.equal(await registryHas("registry.cloudflare.com/acct/g1t-runner:abc", { fetchImpl, credentials }), true); | |
| 212 | assert.equal(await registryHas("registry.cloudflare.com/acct/g1t-runner:nope", { fetchImpl, credentials }), false); | |
| 213 | assert.deepEqual(asked[0], [ | |
| 214 | "https://registry.cloudflare.com/v2/acct/g1t-runner/manifests/abc", | |
| 215 | "HEAD", | |
| 216 | `Basic ${Buffer.from("v1:secret").toString("base64")}`, | |
| 217 | ]); | |
| 218 | await assert.rejects(registryHas("not-a-reference", { fetchImpl, credentials })); | |
| 219 | }); | |
| 220 | ||
| The runner's base image, pushed as Wrangler builds images, with pushes that fail loudly | 221 | test("a push counts only when it ends with a digest, and is tried again", async () => { |
| 222 | const digest = "sha256:" + "a".repeat(64); | |
| 223 | assert.equal(pushedDigest({ code: 0, out: `base: digest: ${digest} size: 856` }), digest); | |
| 224 | assert.equal(pushedDigest({ code: 0, out: "error from registry: blob unknown to registry" }), null); | |
| 225 | assert.equal(pushedDigest({ code: 1, out: `digest: ${digest}` }), null); | |
| 226 | assert.equal(pushedDigest({ code: 0, out: "Pushed" }), null); | |
| 227 | const answers = [{ code: 1, out: "error from registry: blob unknown to registry" }, { code: 0, out: `x: digest: ${digest} size: 1` }]; | |
| 228 | assert.equal(await pushImage("r/x:y", { run: async () => answers.shift() }), digest); | |
| 229 | await assert.rejects(pushImage("r/x:y", { attempts: 2, run: async () => ({ code: 0, out: "blob unknown" }) }), /docker push r\/x:y failed/); | |
| 230 | }); | |
| 231 | ||
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 232 | test("a static Linux binary is told from a dynamic one", () => { |
| 233 | const dir = mkdtempSync(join(tmpdir(), "g1t-elf-")); | |
| 234 | try { | |
| 235 | const elf = (interp) => { | |
| 236 | // 64-bit little-endian x86-64 ELF with one program header. | |
| 237 | const bytes = Buffer.alloc(64 + 56); | |
| 238 | bytes.writeUInt32BE(0x7f454c46, 0); | |
| 239 | bytes[4] = 2; | |
| 240 | bytes[5] = 1; | |
| 241 | bytes.writeUInt16LE(0x3e, 18); | |
| 242 | bytes.writeBigUInt64LE(64n, 32); | |
| 243 | bytes.writeUInt16LE(56, 54); | |
| 244 | bytes.writeUInt16LE(1, 56); | |
| 245 | bytes.writeUInt32LE(interp ? 3 : 1, 64); | |
| 246 | return bytes; | |
| 247 | }; | |
| 248 | writeFileSync(join(dir, "static"), elf(false)); | |
| 249 | writeFileSync(join(dir, "dynamic"), elf(true)); | |
| 250 | writeFileSync(join(dir, "text"), "#!/bin/sh\n"); | |
| 251 | assert.equal(isStaticElf(join(dir, "static")), true); | |
| 252 | assert.equal(isStaticElf(join(dir, "dynamic")), false); | |
| 253 | assert.equal(isStaticElf(join(dir, "text")), false); | |
| 254 | } finally { | |
| 255 | rmSync(dir, { recursive: true, force: true }); | |
| 256 | } | |
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 257 | }); |
| 258 | ||
| 259 | test("path dependencies agree with Cargo's own resolved graph", (t) => { | |
| 260 | let full; | |
| 261 | try { | |
| 262 | full = JSON.parse(execFileSync("cargo", ["metadata", "--format-version", "1", "--offline"], { cwd: ROOT, encoding: "utf8", maxBuffer: 256 << 20 })); | |
| 263 | } catch { | |
| 264 | t.skip("cargo metadata could not resolve offline"); | |
| 265 | return; | |
| 266 | } | |
| 267 | const cargo = cargoWorkspace(ROOT); | |
| 268 | const dirOf = new Map(full.packages.filter((p) => p.source === null).map((p) => [p.id, p.manifest_path])); | |
| 269 | const nodes = new Map(full.resolve.nodes.map((n) => [n.id, n])); | |
| 270 | for (const u of stack.units.filter((u) => u.crate)) { | |
| 271 | const root = full.packages.find((p) => p.name === u.crate).id; | |
| 272 | const seen = new Set(); | |
| 273 | const stackIds = [root]; | |
| 274 | while (stackIds.length) { | |
| 275 | const id = stackIds.pop(); | |
| 276 | for (const dep of nodes.get(id).deps) { | |
| 277 | if (!dep.dep_kinds.some((k) => k.kind !== "dev")) continue; | |
| 278 | if (dirOf.has(dep.pkg) && !seen.has(dep.pkg)) { | |
| 279 | seen.add(dep.pkg); | |
| 280 | stackIds.push(dep.pkg); | |
| 281 | } | |
| 282 | } | |
| 283 | } | |
| 284 | const names = [...seen].map((id) => full.packages.find((p) => p.id === id).name); | |
| 285 | const dirs = names.map((name) => cargo.get(name).dir).sort(); | |
| 286 | assert.deepEqual(dirs, u.dependsOn.filter((d) => d.startsWith("crates/")).sort(), u.id); | |
| 287 | } | |
| 288 | }); | |
| 289 | ||
| 290 | test("every import that leaves a unit's folder is covered by its dependencies or inputs", () => { | |
| 291 | for (const u of stack.units) { | |
| 292 | for (const { file, target } of outsideImports(ROOT, u)) { | |
| 293 | const covered = u.dependsOn.some((dir) => target.startsWith(`${dir}/`)) || u.inputs.some((input) => target === input || target.startsWith(input.replace(/\.ts$/, ""))); | |
| 294 | assert.ok(covered, `${file} imports ${target}, which ${u.id}'s manifest entry does not name`); | |
| 295 | } | |
| 296 | } | |
| 297 | }); | |
| 298 | ||
| 299 | // ── What a change touches ───────────────────────────────────────────────── | |
| 300 | ||
| 301 | const ids = (units, files) => units.filter((u) => touches(u, files)).map((u) => u.id); | |
| 302 | ||
| 303 | test("a shared crate's change reaches every unit built from it, and no other", () => { | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 304 | assert.deepEqual(ids(stack.units, ["crates/scan/src/lib.rs"]), ["repos", "security", "runner"]); |
| Deploys as code: a manifest of every Worker, a deploy tool that ships only what changed in parallel stages, and a g1t Actions workflow | 305 | assert.deepEqual(ids(stack.units, ["crates/secrets/src/lib.rs"]), ["identity", "repos", "integrations", "webhooks", "actions"]); |
| 306 | const kit = ids(stack.units, ["crates/kit/src/lib.rs"]); | |
| 307 | assert.deepEqual(kit, stack.units.filter((u) => u.kind === "rust-worker").map((u) => u.id)); | |
| 308 | assert.deepEqual(ids(stack.units, ["crates/runner/src/main.rs"]), ["runner"]); | |
| 309 | }); | |
| 310 | ||
| 311 | test("a shared package's change reaches what imports it", () => { | |
| 312 | assert.deepEqual(ids(stack.units, ["packages/theme/tokens.css"]), ["status", "web", "sudo", "docs"]); | |
| 313 | assert.ok(ids(stack.units, ["packages/contracts/src/index.ts"]).includes("og")); | |
| 314 | assert.ok(!ids(stack.units, ["packages/contracts/src/index.ts"]).includes("events")); | |
| 315 | }); | |
| 316 | ||
| 317 | test("own folders, declared inputs and root files", () => { | |
| 318 | assert.deepEqual(ids(stack.units, ["services/pages/src/index.ts"]), ["pages"]); | |
| 319 | assert.deepEqual(ids(stack.units, ["apps/web/app/lib/roadmap.ts"]), ["og", "web"]); | |
| 320 | assert.deepEqual(ids(stack.units, ["docs/PLAN.md", "README.md", "deploy/stack.jsonc"]), []); | |
| 321 | assert.deepEqual(ids(stack.units, ["scripts/build-rust-worker.mjs"]), stack.units.filter((u) => u.kind === "rust-worker").map((u) => u.id)); | |
| 322 | // services/events is not a prefix of services/eventsx. | |
| 323 | assert.equal(touches(unit("events"), ["services/eventsx/a.rs"]), null); | |
| 324 | }); | |
| 325 | ||
| 326 | // ── Lockfiles ───────────────────────────────────────────────────────────── | |
| 327 | ||
| 328 | const lock = (sha2) => `version = 4 | |
| 329 | ||
| 330 | [[package]] | |
| 331 | name = "g1t-events" | |
| 332 | version = "0.1.0" | |
| 333 | dependencies = [ | |
| 334 | "g1t-kit", | |
| 335 | ] | |
| 336 | ||
| 337 | [[package]] | |
| 338 | name = "g1t-kit" | |
| 339 | version = "0.1.0" | |
| 340 | dependencies = [ | |
| 341 | "serde", | |
| 342 | ] | |
| 343 | ||
| 344 | [[package]] | |
| 345 | name = "g1t-webhooks" | |
| 346 | version = "0.1.0" | |
| 347 | dependencies = [ | |
| 348 | "g1t-kit", | |
| 349 | "g1t-secrets", | |
| 350 | ] | |
| 351 | ||
| 352 | [[package]] | |
| 353 | name = "g1t-secrets" | |
| 354 | version = "0.1.0" | |
| 355 | dependencies = [ | |
| 356 | "sha2 ${sha2}", | |
| 357 | ] | |
| 358 | ||
| 359 | [[package]] | |
| 360 | name = "serde" | |
| 361 | version = "1.0.0" | |
| 362 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 363 | ||
| 364 | [[package]] | |
| 365 | name = "sha2" | |
| 366 | version = "${sha2}" | |
| 367 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 368 | `; | |
| 369 | ||
| 370 | test("a Cargo.lock change reaches only crates that use what changed", () => { | |
| 371 | const before = parseCargoLock(lock("0.10.8")); | |
| 372 | const after = parseCargoLock(lock("0.10.9")); | |
| 373 | const names = changedNames(before, after); | |
| 374 | assert.deepEqual([...names], ["sha2"]); | |
| 375 | assert.ok(reaches(after, ["g1t-webhooks"], names)); | |
| 376 | assert.ok(!reaches(after, ["g1t-events"], names)); | |
| 377 | }); | |
| 378 | ||
| 379 | test("a package-lock change reaches through workspace links", () => { | |
| 380 | const make = (version) => | |
| 381 | JSON.stringify({ | |
| 382 | lockfileVersion: 3, | |
| 383 | packages: { | |
| 384 | "": { devDependencies: { wrangler: "^4" } }, | |
| 385 | "apps/web": { dependencies: { "@g1t/theme": "*", react: "^19" } }, | |
| 386 | "services/pages": { dependencies: {} }, | |
| 387 | "packages/theme": { dependencies: { fontkit: "^1" } }, | |
| 388 | "node_modules/@g1t/theme": { resolved: "packages/theme", link: true }, | |
| 389 | "node_modules/react": { version: "19.0.0" }, | |
| 390 | "node_modules/fontkit": { version }, | |
| 391 | "node_modules/wrangler": { version: "4.1.0" }, | |
| 392 | }, | |
| 393 | }); | |
| 394 | const before = parseNpmLock(make("1.0.0")); | |
| 395 | const after = parseNpmLock(make("1.0.1")); | |
| 396 | const names = changedNames(before, after); | |
| 397 | assert.deepEqual([...names], ["fontkit"]); | |
| 398 | assert.ok(reaches(after, ["apps/web", ""], names)); | |
| 399 | assert.ok(!reaches(after, ["services/pages", ""], names)); | |
| 400 | }); | |
| 401 | ||
| 402 | // ── Deciding ────────────────────────────────────────────────────────────── | |
| 403 | ||
| 404 | const HEAD = "a".repeat(40); | |
| 405 | const OLD = "b".repeat(40); | |
| 406 | const fakeGit = (files, { has = true, locks = {} } = {}) => ({ | |
| 407 | has: () => has, | |
| 408 | changed: () => files, | |
| 409 | show: (sha, path) => locks[`${sha}:${path}`] ?? "", | |
| 410 | }); | |
| 411 | ||
| 412 | test("decide: changed units deploy, others wait, unknown ones deploy", () => { | |
| 413 | const units = ["events", "repos", "pages", "web"].map(unit); | |
| 414 | const live = { events: { sha: OLD }, repos: { sha: OLD }, pages: { sha: HEAD }, web: { sha: null, why: "never deployed" } }; | |
| 415 | const decisions = decide(units, { live, head: HEAD, gitApi: fakeGit(["crates/scan/src/lib.rs"]) }); | |
| 416 | const by = Object.fromEntries(decisions.map((d) => [d.unit.id, d])); | |
| 417 | assert.equal(by.events.deploy, false); | |
| 418 | assert.equal(by.events.reason, "nothing it is built from changed"); | |
| 419 | assert.equal(by.repos.deploy, true); | |
| 420 | assert.match(by.repos.reason, /crates\/scan\/src\/lib.rs via crates\/scan/); | |
| 421 | assert.equal(by.pages.deploy, false); | |
| 422 | assert.equal(by.pages.reason, "up to date"); | |
| 423 | assert.equal(by.web.deploy, true); | |
| 424 | assert.match(by.web.reason, /never deployed/); | |
| 425 | }); | |
| 426 | ||
| 427 | test("decide: --force deploys unchanged units; a commit missing from history deploys", () => { | |
| 428 | const forced = decide([unit("events")], { live: { events: { sha: HEAD } }, head: HEAD, force: true, gitApi: fakeGit([]) }); | |
| 429 | assert.equal(forced[0].deploy, true); | |
| 430 | const shallow = decide([unit("events")], { live: { events: { sha: OLD } }, head: HEAD, gitApi: fakeGit([], { has: false }) }); | |
| 431 | assert.equal(shallow[0].deploy, true); | |
| 432 | assert.match(shallow[0].reason, /does not have/); | |
| 433 | }); | |
| 434 | ||
| 435 | test("decide: Cargo.lock counts only where it reaches", () => { | |
| 436 | const locks = { [`${OLD}:Cargo.lock`]: lock("0.10.8"), [`${HEAD}:Cargo.lock`]: lock("0.10.9") }; | |
| 437 | const units = [unit("events"), unit("webhooks")]; | |
| 438 | const live = { events: { sha: OLD }, webhooks: { sha: OLD } }; | |
| 439 | const decisions = decide(units, { live, head: HEAD, gitApi: fakeGit(["Cargo.lock"], { locks }) }); | |
| 440 | assert.deepEqual(decisions.map((d) => [d.unit.id, d.deploy]), [["events", false], ["webhooks", true]]); | |
| 441 | }); | |
| 442 | ||
| 443 | test("decide: the runner's image rebuilds only when what it is built from changed", () => { | |
| 444 | const live = { runner: { sha: OLD } }; | |
| 445 | const code = decide([unit("runner")], { live, head: HEAD, gitApi: fakeGit(["services/runner/src/index.ts"]) }); | |
| 446 | assert.deepEqual([code[0].deploy, code[0].image], [true, false]); | |
| 447 | const image = decide([unit("runner")], { live, head: HEAD, gitApi: fakeGit(["crates/runner/src/main.rs"]) }); | |
| 448 | assert.deepEqual([image[0].deploy, image[0].image], [true, true]); | |
| 449 | }); | |
| 450 | ||
| 451 | test("the plan as data: migrations, and each stage's units in jobs that share a build", () => { | |
| 452 | const units = ["events", "repos", "projects", "api", "web", "docs"].map(unit); | |
| 453 | const decisions = units.map((u) => ({ unit: u, deploy: true, reason: "x", since: null, image: false })); | |
| 454 | const data = planJson(stack, decisions, { events: { pending: ["0003_x.sql"] }, repos: { pending: [] } }, HEAD); | |
| 455 | assert.deepEqual(data.migrations, [{ unit: "events", database: "g1t-events", pending: ["0003_x.sql"] }]); | |
| 456 | assert.deepEqual(data.stages.core.jobs, [ | |
| 457 | { group: "rust", units: "events,repos", rust: true }, | |
| 458 | { group: "ts", units: "projects", rust: false }, | |
| 459 | ]); | |
| 460 | assert.deepEqual(data.stages.edge.jobs, [{ group: "rust", units: "api", rust: true }]); | |
| 461 | assert.deepEqual(data.stages.front.jobs, [ | |
| 462 | { group: "web", units: "web", rust: false }, | |
| 463 | { group: "docs", units: "docs", rust: false }, | |
| 464 | ]); | |
| 465 | assert.deepEqual(data.stage_order, ["core", "edge", "front"]); | |
| 466 | assert.deepEqual(buildGroups([]), []); | |
| 467 | // Ten Rust workers go to three jobs; a unit whose image rebuilds gets its own. | |
| 468 | const core = stack.units.filter((u) => u.stage === "core"); | |
| 469 | const jobs = buildGroups(core, ["runner"]); | |
| 470 | assert.deepEqual(jobs.filter((j) => j.rust).map((j) => j.units.split(",").length), [4, 3, 3]); | |
| 471 | assert.ok(jobs.some((j) => j.group === "runner-image" && j.units === "runner")); | |
| 472 | assert.ok(!jobs.find((j) => j.group === "ts").units.includes("runner")); | |
| 473 | }); | |
| 474 | ||
| 475 | test("units are picked by short name, folder or Worker name", () => { | |
| 476 | assert.deepEqual(pick(stack, ["billing,services/web".replace("services/web", "apps/web"), "g1t-api"]).map((u) => u.id), ["billing", "web", "api"]); | |
| 477 | assert.throws(() => pick(stack, ["nope"]), /No unit called nope/); | |
| 478 | }); | |
| 479 | ||
| 480 | test("a CI job installs only what its units need", () => { | |
| 481 | const npm = npmWorkspace(); | |
| 482 | assert.deepEqual(npmCiArgs([unit("events"), unit("repos")], npm), ["ci", "--no-audit", "--no-fund", "--workspaces=false"]); | |
| 483 | assert.deepEqual(npmCiArgs([unit("events"), unit("status")], npm), [ | |
| 484 | "ci", "--no-audit", "--no-fund", "--include-workspace-root", | |
| 485 | "-w", "@g1t/contracts", "-w", "@g1t/status", "-w", "@g1t/theme", | |
| 486 | ]); | |
| 487 | }); | |
| 488 | ||
| 489 | // ── Cloudflare's answers ────────────────────────────────────────────────── | |
| 490 | ||
| 491 | const version = (id, number, message, triggered = "deployment") => ({ | |
| 492 | id, | |
| 493 | number, | |
| 494 | metadata: { created_on: "2026-10-05T00:00:00Z", author_email: "a@b" }, | |
| 495 | annotations: { "workers/triggered_by": triggered, ...(message ? { "workers/message": message } : {}) }, | |
| 496 | }); | |
| 497 | ||
| 498 | test("a deploy is annotated with its commit, and read back", () => { | |
| 499 | const { message, tag } = annotation(HEAD, "A subject ".repeat(20)); | |
| 500 | assert.ok(message.length <= 100); | |
| 501 | assert.equal(commitFrom(message), HEAD); | |
| 502 | assert.equal(tag, `g1t-${HEAD.slice(0, 12)}`); | |
| 503 | assert.equal(commitFrom(message.replace("g1t-deploy", "g1t-deploy-dirty")), null); | |
| 504 | assert.equal(commitFrom("deployed by hand"), null); | |
| 505 | }); | |
| 506 | ||
| 507 | test("the live commit: the live version's, looking through secret changes", () => { | |
| 508 | const versions = [version("v1", 1, annotation(OLD).message), version("v2", 2, null, "secret"), version("v3", 3, null, "version_upload")]; | |
| 509 | const status = (id) => ({ versions: [{ version_id: id, percentage: 100 }] }); | |
| 510 | assert.equal(liveCommit(status("v1"), versions).sha, OLD); | |
| 511 | assert.equal(liveCommit(status("v2"), versions).sha, OLD); | |
| 512 | assert.equal(liveCommit(status("v3"), versions).sha, null); | |
| 513 | assert.match(liveCommit(status("v9"), versions).why, /not among/); | |
| 514 | const split = liveCommit({ versions: [{ version_id: "v1", percentage: 10 }, { version_id: "v2", percentage: 90 }] }, versions); | |
| 515 | assert.equal(split.sha, OLD); | |
| 516 | assert.equal(split.split, true); | |
| 517 | }); | |
| 518 | ||
| 519 | test("Wrangler's output: pending migrations and the version a deploy made", () => { | |
| 520 | const pending = ` | |
| 521 | ⛅️ wrangler 4.146.0 | |
| 522 | Resource location: remote | |
| 523 | Migrations to be applied: | |
| 524 | ┌──────────────────────┐ | |
| 525 | │ Name │ | |
| 526 | ├──────────────────────┤ | |
| 527 | │ 0021_confidence.sql │ | |
| 528 | ├──────────────────────┤ | |
| 529 | │ 0022_more.sql │ | |
| 530 | └──────────────────────┘`; | |
| 531 | assert.deepEqual(pendingFrom(pending), ["0021_confidence.sql", "0022_more.sql"]); | |
| 532 | assert.deepEqual(pendingFrom("Resource location: remote\n\n✅ No migrations to apply!"), []); | |
| 533 | assert.equal(versionFrom("Deployed g1t-events triggers\nCurrent Version ID: 2c7fc93a-82d9-4850-8a77-ba887d157a4f\n"), "2c7fc93a-82d9-4850-8a77-ba887d157a4f"); | |
| 534 | }); | |
| 535 | ||
| 536 | test("the pool runs everything, no more than its limit at once", async () => { | |
| 537 | let running = 0; | |
| 538 | let most = 0; | |
| 539 | const out = await pool([1, 2, 3, 4, 5, 6, 7], 3, async (n) => { | |
| 540 | running++; | |
| 541 | most = Math.max(most, running); | |
| 542 | await new Promise((resolve) => setTimeout(resolve, 5)); | |
| 543 | running--; | |
| 544 | return n * 2; | |
| 545 | }); | |
| 546 | assert.deepEqual(out, [2, 4, 6, 8, 10, 12, 14]); | |
| 547 | assert.equal(most, 3); | |
| 548 | }); | |
| 549 | ||
| 550 | // ── Everything else that reads the list ─────────────────────────────────── | |
| 551 | ||
| 552 | test("self-hosting builds every Rust service the manifest says it runs", () => { | |
| 553 | const dockerfile = readFileSync(join(ROOT, "deploy/self-host/Dockerfile"), "utf8"); | |
| 554 | const loops = [...dockerfile.matchAll(/for service in ([a-z ]+); do/g)].map((m) => m[1].trim().split(/\s+/).sort()); | |
| 555 | const wanted = stack.units.filter((u) => u.self_host === "run" && u.kind === "rust-worker").map((u) => u.path.split("/").pop()).sort(); | |
| 556 | assert.ok(loops.length >= 2); | |
| 557 | for (const loop of loops) assert.deepEqual(loop, wanted); | |
| 558 | }); | |
| 559 | ||
| 560 | test("docs/SELF_HOSTING.md's table names every unit", () => { | |
| 561 | const doc = readFileSync(join(ROOT, "docs/SELF_HOSTING.md"), "utf8"); | |
| 562 | for (const u of stack.units) assert.ok(doc.includes(`| \`${u.path}\` |`), `${u.path} is missing from docs/SELF_HOSTING.md`); | |
| 563 | }); | |
| 564 | ||
| 565 | test("resolveStack works on a manifest with no workspace crates or packages", () => { | |
| 566 | const bare = resolveStack(loadStack(), { cargo: new Map(), npm: new Map() }); | |
| 567 | assert.deepEqual(bare.units.find((u) => u.id === "events").dependsOn, []); | |
| 568 | }); |