The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Fiber from "effect/Fiber"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 8 | import * as UrlParams from "effect/http/UrlParams"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import * as Result from "effect/Result"; |
| 11 | import * as Schema from "effect/Schema"; |
| 12 | import * as TestClock from "effect/testing/TestClock"; |
| 13 | import { |
| 14 | selectPackages, |
| 15 | dependencyLevels, |
| 16 | expandBraces, |
| 17 | Group, |
| 18 | rewriteDependencies, |
| 19 | tarballFile, |
| 20 | } from "../src/cli/pack.ts"; |
| 21 | import { publish } from "../src/cli/publish.ts"; |
| 22 | import { GroupName, PackageName, type Manifest } from "../src/Manifest.ts"; |
| 23 | import { manifestArtifactName, tarballUrl } from "../src/Protocol.ts"; |
| 24 | import { Policy } from "../src/Registry.ts"; |
| 25 | import { installTag, parseInstallPath, tagsFor } from "../src/Registry/Handler.ts"; |
| 26 | |
| 27 | describe("registry", () => { |
| 28 | const run = { |
| 29 | repo: "alchemy-run/alchemy", |
| 30 | runId: 1, |
| 31 | attempt: 1, |
| 32 | headSha: "abcdef0123456789abcdef0123456789abcdef01", |
| 33 | headBranch: "feat/x", |
| 34 | headRepo: "alchemy-run/alchemy", |
| 35 | pr: 7, |
| 36 | }; |
| 37 | |
| 38 | test("runs on the repository's own commits get every tag", () => { |
| 39 | expect(tagsFor(run)).toEqual([run.headSha, "abcdef0", "pr:7", "pr:7:abcdef0", "branch:feat/x"]); |
| 40 | expect(tagsFor({ ...run, pr: null, headBranch: "main" })).toEqual([ |
| 41 | run.headSha, |
| 42 | "abcdef0", |
| 43 | "branch:main", |
| 44 | ]); |
| 45 | expect(installTag(run)).toBe("pr:7:abcdef0"); |
| 46 | expect(installTag({ ...run, pr: null, headBranch: "main" })).toBe("abcdef0"); |
| 47 | }); |
| 48 | |
| 49 | test("runs from forks get only their pull request revision tag", () => { |
| 50 | const fork = { ...run, headRepo: "someone/alchemy" }; |
| 51 | expect(tagsFor(fork)).toEqual(["pr:7:abcdef0"]); |
| 52 | expect(installTag(fork)).toBe("pr:7:abcdef0"); |
| 53 | expect(tagsFor({ ...fork, pr: null })).toEqual([]); |
| 54 | }); |
| 55 | |
| 56 | test("install paths", () => { |
| 57 | expect(parseInstallPath("/alchemy/pr:7:abcdef0", undefined)).toEqual({ |
| 58 | kind: "tag", |
| 59 | name: "alchemy", |
| 60 | tag: "pr:7:abcdef0", |
| 61 | }); |
| 62 | expect(parseInstallPath("/@alchemy.run/pkg/branch:feat/x", undefined)).toEqual({ |
| 63 | kind: "tag", |
| 64 | name: "@alchemy.run/pkg", |
| 65 | tag: "branch:feat/x", |
| 66 | }); |
| 67 | expect(parseInstallPath("/core/abc1234", "@distilled.cloud")).toEqual({ |
| 68 | kind: "tag", |
| 69 | name: "@distilled.cloud/core", |
| 70 | tag: "abc1234", |
| 71 | }); |
| 72 | expect(parseInstallPath(`/alchemy/-/${"a".repeat(64)}.tgz`, undefined)).toEqual({ |
| 73 | kind: "tarball", |
| 74 | name: "alchemy", |
| 75 | sha256: "a".repeat(64), |
| 76 | }); |
| 77 | for (const path of ["/", "/alchemy", "/alchemy/", "/alchemy/%E0%A4%A"]) { |
| 78 | expect(parseInstallPath(path, undefined)).toBeUndefined(); |
| 79 | } |
| 80 | }); |
| 81 | |
| 82 | test("tarball file names never collide", () => { |
| 83 | expect(tarballFile("alchemy")).toBe("alchemy.tgz"); |
| 84 | expect(tarballFile("@a/b-c")).toBe("a+b-c.tgz"); |
| 85 | expect(tarballFile("@a-b/c")).toBe("a-b+c.tgz"); |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | describe("Policy", () => { |
| 90 | const policy = { |
| 91 | repos: ["alchemy-run/alchemy", "alchemy-run/distilled"], |
| 92 | ttl: Duration.weeks(1), |
| 93 | }; |
| 94 | |
| 95 | test("round-trips through its JSON encoding", () => { |
| 96 | const encoded = Schema.encodeSync(Policy)(policy); |
| 97 | expect(encoded.ttl).toBe(7 * 24 * 60 * 60 * 1000); |
| 98 | expect(Schema.decodeUnknownSync(Policy)(encoded)).toEqual(policy); |
| 99 | }); |
| 100 | }); |
| 101 | |
| 102 | describe("names", () => { |
| 103 | test("package names follow npm's rules for new packages", () => { |
| 104 | const decode = Schema.decodeUnknownSync(PackageName); |
| 105 | for (const name of ["alchemy", "@alchemy.run/pkg", "a-b_c.d~e"]) { |
| 106 | expect(decode(name)).toBe(name); |
| 107 | } |
| 108 | for (const name of [ |
| 109 | "Alchemy", |
| 110 | "@Scope/pkg", |
| 111 | ".hidden", |
| 112 | "_private", |
| 113 | "a b", |
| 114 | "a`b", |
| 115 | "a\nb", |
| 116 | "@scope", |
| 117 | "a/b", |
| 118 | "x".repeat(215), |
| 119 | ]) { |
| 120 | expect(() => decode(name)).toThrow(); |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | test("group names are plain labels", () => { |
| 125 | const decode = Schema.decodeUnknownSync(GroupName); |
| 126 | expect(decode("Alchemy")).toBe("Alchemy"); |
| 127 | expect(decode("@alchemy.run")).toBe("@alchemy.run"); |
| 128 | expect(decode("Distilled SDKs v2")).toBe("Distilled SDKs v2"); |
| 129 | for (const name of ["", " lead", "a`b", "a\nb", "###", "x".repeat(65)]) { |
| 130 | expect(() => decode(name)).toThrow(); |
| 131 | } |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | describe("workspace", () => { |
| 136 | test("expandBraces", () => { |
| 137 | expect(expandBraces("./packages/*")).toEqual(["./packages/*"]); |
| 138 | expect(expandBraces("./packages/{alchemy, pkg}")).toEqual([ |
| 139 | "./packages/alchemy", |
| 140 | "./packages/pkg", |
| 141 | ]); |
| 142 | expect(expandBraces("./{a,b}/packages/{x,y}")).toEqual([ |
| 143 | "./a/packages/x", |
| 144 | "./a/packages/y", |
| 145 | "./b/packages/x", |
| 146 | "./b/packages/y", |
| 147 | ]); |
| 148 | }); |
| 149 | |
| 150 | test("Group", () => { |
| 151 | const decode = Schema.decodeUnknownSync(Group); |
| 152 | const encode = Schema.encodeSync(Group); |
| 153 | expect(decode("Alchemy=./packages/*")).toEqual({ |
| 154 | name: "Alchemy", |
| 155 | pattern: "./packages/*", |
| 156 | collapsed: false, |
| 157 | }); |
| 158 | expect(decode("Distilled[Collapsed]=./submodules/*")).toEqual({ |
| 159 | name: "Distilled", |
| 160 | pattern: "./submodules/*", |
| 161 | collapsed: true, |
| 162 | }); |
| 163 | expect(decode("@alchemy.run[Collapsed]=./packages/{a,b}")).toEqual({ |
| 164 | name: "@alchemy.run", |
| 165 | pattern: "./packages/{a,b}", |
| 166 | collapsed: true, |
| 167 | }); |
| 168 | expect(encode({ name: "Distilled", pattern: "./submodules/*", collapsed: true })).toBe( |
| 169 | "Distilled[Collapsed]=./submodules/*", |
| 170 | ); |
| 171 | for (const spec of ["Distilled[Hidden]=./x", "=x", "Alchemy=", "Alchemy"]) { |
| 172 | expect(() => decode(spec)).toThrow("NAME=GLOB or NAME[Collapsed]=GLOB"); |
| 173 | } |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | describe("tarball", () => { |
| 178 | test("tarballUrl strips trailing slashes and keeps scopes", () => { |
| 179 | expect(tarballUrl("https://pkg.ing/", "@alchemy.run/pkg", "abc")).toBe( |
| 180 | "https://pkg.ing/@alchemy.run/pkg/-/abc.tgz", |
| 181 | ); |
| 182 | }); |
| 183 | |
| 184 | test("dependencyLevels orders dependencies first and rejects cycles", async () => { |
| 185 | const levels = await Effect.runPromise( |
| 186 | dependencyLevels( |
| 187 | new Map([ |
| 188 | ["alchemy", new Set(["core", "runtime", "outside"])], |
| 189 | ["runtime", new Set(["utils"])], |
| 190 | ["core", new Set()], |
| 191 | ["utils", new Set()], |
| 192 | ["better-auth", new Set(["alchemy"])], |
| 193 | ]), |
| 194 | ), |
| 195 | ); |
| 196 | expect(levels).toEqual([["core", "utils"], ["runtime"], ["alchemy"], ["better-auth"]]); |
| 197 | const cycle = await Effect.runPromise( |
| 198 | Effect.result( |
| 199 | dependencyLevels( |
| 200 | new Map([ |
| 201 | ["a", new Set(["b"])], |
| 202 | ["b", new Set(["a"])], |
| 203 | ]), |
| 204 | ), |
| 205 | ), |
| 206 | ); |
| 207 | expect(cycle._tag).toBe("Failure"); |
| 208 | }); |
| 209 | |
| 210 | test("rewriteDependencies only touches published packages", async () => { |
| 211 | const manifest = JSON.stringify({ |
| 212 | name: "alchemy", |
| 213 | dependencies: { |
| 214 | "@distilled.cloud/core": "1.0.0-rc.8", |
| 215 | effect: "^4.0.0", |
| 216 | }, |
| 217 | peerDependencies: { "@alchemy.run/frontend-frameworks": "2.0.0" }, |
| 218 | exports: { ".": "./src/index.ts" }, |
| 219 | }); |
| 220 | const links = new Map([ |
| 221 | ["@distilled.cloud/core", "https://pkg.ing/@distilled.cloud/core/-/aa.tgz"], |
| 222 | [ |
| 223 | "@alchemy.run/frontend-frameworks", |
| 224 | "https://pkg.ing/@alchemy.run/frontend-frameworks/-/bb.tgz", |
| 225 | ], |
| 226 | ]); |
| 227 | const result = await Effect.runPromise(rewriteDependencies(manifest, links)); |
| 228 | const rewritten = JSON.parse(result.text); |
| 229 | expect(Object.keys(rewritten)).toEqual(Object.keys(JSON.parse(manifest))); |
| 230 | expect(rewritten.dependencies).toEqual({ |
| 231 | "@distilled.cloud/core": "https://pkg.ing/@distilled.cloud/core/-/aa.tgz", |
| 232 | effect: "^4.0.0", |
| 233 | }); |
| 234 | expect(rewritten.peerDependencies).toEqual({ |
| 235 | "@alchemy.run/frontend-frameworks": |
| 236 | "https://pkg.ing/@alchemy.run/frontend-frameworks/-/bb.tgz", |
| 237 | }); |
| 238 | expect(rewritten.exports).toEqual({ ".": "./src/index.ts" }); |
| 239 | expect(result.rewrites.map((r) => r.name).sort()).toEqual([ |
| 240 | "@alchemy.run/frontend-frameworks", |
| 241 | "@distilled.cloud/core", |
| 242 | ]); |
| 243 | }); |
| 244 | }); |
| 245 | |
| 246 | describe("Api", () => { |
| 247 | test("manifest artifact name carries the manifest hash", () => { |
| 248 | expect(manifestArtifactName("ab".repeat(32))).toBe(`pkg-manifest-${"ab".repeat(32)}`); |
| 249 | }); |
| 250 | }); |
| 251 | |
| 252 | const env = { |
| 253 | GITHUB_REPOSITORY: "alchemy-run/alchemy", |
| 254 | GITHUB_RUN_ID: "123", |
| 255 | GITHUB_RUN_ATTEMPT: "2", |
| 256 | }; |
| 257 | const previous = Object.fromEntries(Object.keys(env).map((key) => [key, process.env[key]])); |
| 258 | beforeAll(() => Object.assign(process.env, env)); |
| 259 | afterAll(() => { |
| 260 | for (const [key, value] of Object.entries(previous)) { |
| 261 | if (value === undefined) delete process.env[key]; |
| 262 | else process.env[key] = value; |
| 263 | } |
| 264 | }); |
| 265 | |
| 266 | const manifest: Manifest = { |
| 267 | version: 1, |
| 268 | groups: [{ name: "Alchemy", collapsed: false }], |
| 269 | registry: "https://pkg.ing", |
| 270 | head: "abcdef0123456789", |
| 271 | packages: ["alchemy", "@alchemy.run/pkg"].map((name, index) => ({ |
| 272 | name, |
| 273 | version: "1.0.0", |
| 274 | group: "Alchemy", |
| 275 | dir: `packages/${index}`, |
| 276 | file: `${index}.tgz`, |
| 277 | sha256: String(index).repeat(64), |
| 278 | size: 3, |
| 279 | })), |
| 280 | }; |
| 281 | const missing = { |
| 282 | _tag: "MissingTarballs", |
| 283 | missing: [{ name: manifest.packages[1]!.name, sha256: manifest.packages[1]!.sha256 }], |
| 284 | }; |
| 285 | const published = { |
| 286 | packages: manifest.packages.map(({ name, group }) => ({ |
| 287 | name, |
| 288 | group, |
| 289 | url: `https://pkg.ing/${name}/abcdef0`, |
| 290 | tags: ["abcdef0"], |
| 291 | })), |
| 292 | }; |
| 293 | |
| 294 | const scenarios = [ |
| 295 | "already present", |
| 296 | "upload", |
| 297 | "still missing", |
| 298 | "run catching up", |
| 299 | "rejected", |
| 300 | ] as const; |
| 301 | |
| 302 | for (const scenario of scenarios) { |
| 303 | test(`publish: ${scenario}`, async () => { |
| 304 | const requests: string[] = []; |
| 305 | const reads: string[] = []; |
| 306 | let attempts = 0; |
| 307 | const http = HttpClient.make((request) => |
| 308 | Effect.sync(() => { |
| 309 | const query = UrlParams.toString(request.urlParams); |
| 310 | requests.push(`${request.method} ${request.url}${query ? `?${query}` : ""}`); |
| 311 | if (request.method === "PUT") { |
| 312 | expect(request.headers["content-length"]).toBe("3"); |
| 313 | return HttpClientResponse.fromWeb( |
| 314 | request, |
| 315 | Response.json({ |
| 316 | ...manifest.packages[1], |
| 317 | uploaded: true, |
| 318 | }), |
| 319 | ); |
| 320 | } |
| 321 | attempts++; |
| 322 | const response = |
| 323 | scenario === "rejected" || (scenario === "run catching up" && attempts <= 2) |
| 324 | ? Response.json( |
| 325 | { _tag: "RunNotInProgress", message: "run is not in progress" }, |
| 326 | { status: 409 }, |
| 327 | ) |
| 328 | : scenario === "still missing" || (scenario === "upload" && attempts === 1) |
| 329 | ? Response.json(missing, { status: 409 }) |
| 330 | : Response.json(published); |
| 331 | return HttpClientResponse.fromWeb(request, response); |
| 332 | }), |
| 333 | ); |
| 334 | const result = await Effect.gen(function* () { |
| 335 | // Retries wait on the clock, so run against a test clock moved far |
| 336 | // enough for every retry to fire. |
| 337 | const fiber = yield* publish({ |
| 338 | cwd: "/workspace", |
| 339 | dir: ".pkg", |
| 340 | registry: "https://pkg.ing/", |
| 341 | }).pipe( |
| 342 | Effect.provideService(HttpClient.HttpClient, http), |
| 343 | Effect.provide( |
| 344 | FileSystem.layerNoop({ |
| 345 | readFileString: () => Effect.succeed(JSON.stringify(manifest)), |
| 346 | readFile: (path) => |
| 347 | Effect.sync(() => { |
| 348 | reads.push(path); |
| 349 | return new Uint8Array([1, 2, 3]); |
| 350 | }), |
| 351 | }), |
| 352 | ), |
| 353 | Effect.provide(Path.layer), |
| 354 | Effect.result, |
| 355 | Effect.forkChild, |
| 356 | ); |
| 357 | yield* TestClock.adjust("10 minutes"); |
| 358 | return yield* Fiber.join(fiber); |
| 359 | }).pipe(Effect.provide(TestClock.layer()), Effect.runPromise); |
| 360 | const uploads = scenario === "upload" || scenario === "still missing"; |
| 361 | // A run GitHub does not report as in progress yet is retried every five |
| 362 | // seconds for three minutes. |
| 363 | const publishes = |
| 364 | scenario === "rejected" ? 37 : scenario === "run catching up" ? 3 : uploads ? 2 : 1; |
| 365 | expect(attempts).toBe(publishes); |
| 366 | expect(reads).toEqual(uploads ? ["/workspace/.pkg/1.tgz"] : []); |
| 367 | expect(requests).toEqual( |
| 368 | uploads |
| 369 | ? [ |
| 370 | "POST https://pkg.ing/api/publish", |
| 371 | `PUT https://pkg.ing/api/tarballs/%40alchemy.run%2Fpkg/${"1".repeat(64)}?repo=alchemy-run%2Falchemy&runId=123&attempt=2`, |
| 372 | "POST https://pkg.ing/api/publish", |
| 373 | ] |
| 374 | : Array.from({ length: publishes }, () => "POST https://pkg.ing/api/publish"), |
| 375 | ); |
| 376 | if (scenario === "still missing" || scenario === "rejected") { |
| 377 | expect(Result.isFailure(result)).toBe(true); |
| 378 | if (Result.isFailure(result)) { |
| 379 | expect(result.failure._tag).toBe("PublishError"); |
| 380 | expect(String(result.failure)).toContain( |
| 381 | scenario === "rejected" |
| 382 | ? "run is not in progress" |
| 383 | : "registry still reports missing tarballs after upload", |
| 384 | ); |
| 385 | } |
| 386 | } else { |
| 387 | expect(Result.getOrThrow(result)).toEqual(published); |
| 388 | } |
| 389 | }); |
| 390 | } |
| 391 | |
| 392 | describe("partial publication", () => { |
| 393 | const packages = ["core", "aws", "cloudflare", "app", "unrelated"].map((name) => ({ |
| 394 | name, |
| 395 | version: "1.0.0", |
| 396 | dir: `packages/${name}`, |
| 397 | absDir: `/workspace/packages/${name}`, |
| 398 | group: "SDKs", |
| 399 | })); |
| 400 | const deps = new Map([ |
| 401 | ["core", new Set<string>()], |
| 402 | ["aws", new Set(["core"])], |
| 403 | ["cloudflare", new Set(["core"])], |
| 404 | ["app", new Set(["cloudflare"])], |
| 405 | ["unrelated", new Set<string>()], |
| 406 | ]); |
| 407 | const names = (files: string[], extra: string[] = []) => |
| 408 | selectPackages(packages, deps, files, extra).map((pkg) => pkg.name); |
| 409 | |
| 410 | test("includes transitive dependents and dependencies without unrelated siblings", () => { |
| 411 | expect(names(["packages/cloudflare/src/r2.ts"])).toEqual(["core", "cloudflare", "app"]); |
| 412 | expect(names(["packages/aws/src/s3.ts", "packages/cloudflare/src/r2.ts"])).toEqual([ |
| 413 | "core", |
| 414 | "aws", |
| 415 | "cloudflare", |
| 416 | "app", |
| 417 | ]); |
| 418 | expect(names(["packages/core/src/index.ts"])).toEqual(["core", "aws", "cloudflare", "app"]); |
| 419 | }); |
| 420 | |
| 421 | test("ignores unrelated paths and respects directory boundaries", () => { |
| 422 | expect(names(["README.md", "packages/aws-other/index.ts"])).toEqual([]); |
| 423 | expect(names([])).toEqual([]); |
| 424 | }); |
| 425 | |
| 426 | test("shared build inputs and configured prefixes select everything", () => { |
| 427 | for (const file of [ |
| 428 | "pnpm-lock.yaml", |
| 429 | "package.json", |
| 430 | ".github/workflows/pkg.yml", |
| 431 | "scripts/build.ts", |
| 432 | ]) { |
| 433 | expect(names([file], ["scripts/**"])).toEqual(packages.map((pkg) => pkg.name)); |
| 434 | } |
| 435 | expect(names(["scripts-other/build.ts"], ["scripts/**"])).toEqual([]); |
| 436 | }); |
| 437 | |
| 438 | test("submodule gitlink changes select contained packages", () => { |
| 439 | expect( |
| 440 | selectPackages( |
| 441 | packages.map((pkg) => ({ ...pkg, dir: `submodules/sdk/${pkg.dir}` })), |
| 442 | deps, |
| 443 | ["submodules/sdk"], |
| 444 | ).length, |
| 445 | ).toBe(packages.length); |
| 446 | }); |
| 447 | |
| 448 | test("dependency cycles terminate selection and are rejected by packing order", async () => { |
| 449 | const cycle = new Map([ |
| 450 | ["core", new Set(["aws"])], |
| 451 | ["aws", new Set(["core"])], |
| 452 | ]); |
| 453 | expect( |
| 454 | selectPackages(packages, cycle, ["packages/aws/index.ts"]).map((pkg) => pkg.name), |
| 455 | ).toEqual(["core", "aws"]); |
| 456 | expect((await Effect.runPromise(Effect.result(dependencyLevels(cycle))))._tag).toBe("Failure"); |
| 457 | }); |
| 458 | |
| 459 | test("empty manifests do not contact the registry", async () => { |
| 460 | const result = await Effect.runPromise( |
| 461 | publish({ |
| 462 | cwd: "/workspace", |
| 463 | dir: ".pkg", |
| 464 | registry: manifest.registry, |
| 465 | }).pipe( |
| 466 | Effect.provideService( |
| 467 | HttpClient.HttpClient, |
| 468 | HttpClient.make(() => Effect.die("Unexpected registry request")), |
| 469 | ), |
| 470 | Effect.provide( |
| 471 | FileSystem.layerNoop({ |
| 472 | readFileString: () => Effect.succeed(JSON.stringify({ ...manifest, packages: [] })), |
| 473 | }), |
| 474 | ), |
| 475 | Effect.provide(Path.layer), |
| 476 | ), |
| 477 | ); |
| 478 | expect(result).toEqual({ packages: [] }); |
| 479 | }); |
| 480 | }); |