The best way to control Claude Code, Codex and more from any computer or phone

main
ts 103 lines 3.61 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 /**
3 * `assets.base` on the precomputed-hash path.
4 *
5 * `Command.Build` / `StaticSite` deploys pass a caller-supplied asset hash
6 * so unchanged builds skip the directory walk and keep the manifest
7 * already on Cloudflare. Changing `base` re-keys the manifest without
8 * changing the build output, so the caller-supplied hash stays identical —
9 * the skip decision must incorporate the prefix or a stale root-keyed
10 * manifest survives the deploy.
11 */
12 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
13 import { expect } from "@alchemy.run/test";
14 import * as Test from "@alchemy.run/test/Alchemy";
15 import * as Effect from "effect/Effect";
16 import * as FileSystem from "effect/FileSystem";
17 import * as Path from "effect/Path";
18 import { MinimumLogLevel } from "effect/References";
19 import { expectUrlAbsent, expectUrlContains } from "../Utils/Http.ts";
20 import { waitForWorkerToBeDeleted } from "../Utils/Worker.ts";
21
22 const { test } = Test.make({ providers: Cloudflare.providers() });
23
24 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
25
26 const marker = "assets-base-rekey-marker";
27
28 test.provider(
29 "assets: changing base with an unchanged precomputed hash re-keys the manifest",
30 (stack) =>
31 Effect.gen(function* () {
32 const { accountId } = yield* yield* CloudflareEnvironment;
33 const fs = yield* FileSystem.FileSystem;
34 const path = yield* Path.Path;
35
36 yield* stack.destroy();
37
38 const directory = yield* fs.makeTempDirectory({
39 prefix: "alchemy-assets-base-",
40 });
41 yield* fs.writeFileString(
42 path.join(directory, "index.html"),
43 `<!doctype html><html><body>${marker}</body></html>`,
44 );
45
46 // The hash stands in for a build-input hash: identical across both
47 // deploys because the build output is identical — only `base` flips.
48 const hash = "assets-base-rekey-v1";
49
50 let workerName: string | undefined;
51
52 yield* Effect.gen(function* () {
53 const first = yield* stack.deploy(
54 Effect.gen(function* () {
55 return yield* Cloudflare.Worker("AssetsBaseWorker", {
56 workersDev: true,
57 assets: { directory, hash },
58 });
59 }),
60 );
61 workerName = first.workerName;
62 expect(first.url).toBeDefined();
63
64 yield* expectUrlContains(`${first.url!}/`, marker, {
65 timeout: "120 seconds",
66 label: "root-keyed manifest",
67 });
68
69 const second = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* Cloudflare.Worker("AssetsBaseWorker", {
72 workersDev: true,
73 assets: { directory, hash, base: "/docs" },
74 });
75 }),
76 );
77
78 // The prefix must reach the served manifest even though the
79 // caller-supplied hash did not change.
80 yield* expectUrlContains(`${second.url!}/docs/`, marker, {
81 timeout: "60 seconds",
82 label: "base-keyed manifest",
83 });
84 yield* expectUrlAbsent(`${second.url!}/`, marker, {
85 timeout: "60 seconds",
86 label: "root no longer serves the shell",
87 });
88 }).pipe(
89 Effect.ensuring(
90 Effect.gen(function* () {
91 yield* stack.destroy().pipe(Effect.ignore);
92 if (workerName) {
93 yield* waitForWorkerToBeDeleted(workerName, accountId).pipe(Effect.ignore);
94 }
95 }),
96 ),
97 );
98 }).pipe(logLevel),
99 {
100 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
101 timeout: 300_000,
102 },
103 );