@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 251 lines 11.8 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as Path from "effect/Path";
8 import * as Schedule from "effect/Schedule";
9 import * as pathe from "pathe";
10 import { cloneFixture } from "../Utils/Fixture.ts";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const main = pathe.resolve(import.meta.dirname, "fixtures/worker.ts");
15 const assetsFixture = pathe.resolve(import.meta.dirname, "fixtures/assets-only");
16
17 const actionOf = (plan: any, logicalId: string) =>
18 (Object.values(plan.resources) as any[]).find(
19 (node: any) => node.resource.LogicalId === logicalId,
20 )?.action;
21
22 describe.concurrent(
23 "Cloudflare.Worker assets plan convergence",
24 { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] },
25 () => {
26 // A Worker whose `assets` is a plain `{ directory }` (no precomputed
27 // `hash` from an upstream build) must still converge to a noop plan when
28 // the directory contents are unchanged. The diff hashes the tree the same
29 // way the apply does, so users don't have to hand-roll their own
30 // directory hashing to get convergent plans.
31 test.provider(
32 "unchanged assets directory converges to a noop plan",
33 (stack) =>
34 Effect.gen(function* () {
35 const fs = yield* FileSystem.FileSystem;
36 const path = yield* Path.Path;
37
38 yield* stack.destroy();
39
40 // Two independent mutable copies of the assets fixture: `dirA`
41 // feeds the main+assets worker, `dirB` feeds the assets-only and
42 // script+assets workers, so editing `dirA` must dirty only the
43 // first worker.
44 const dirA = yield* cloneFixture(assetsFixture, { prefix: "alchemy-assets-plan-a-" });
45 const dirB = yield* cloneFixture(assetsFixture, { prefix: "alchemy-assets-plan-b-" });
46
47 // One worker per `hasChanged` branch that previously returned a
48 // conservative "changed" whenever `assets` carried no precomputed
49 // hash: bundled main + assets, assets-only (no entry), and inline
50 // script + assets.
51 const program = (a: string, b: string) =>
52 Effect.gen(function* () {
53 const withMain = yield* Cloudflare.Worker("AssetsPlanWithMain", {
54 main,
55 assets: { directory: a },
56 compatibility: { date: "2024-01-01" },
57 });
58 const assetsOnly = yield* Cloudflare.Worker("AssetsPlanAssetsOnly", {
59 assets: { directory: b, notFoundHandling: "404-page" },
60 compatibility: { date: "2024-01-01" },
61 });
62 const withScript = yield* Cloudflare.Worker("AssetsPlanScript", {
63 script: `export default { fetch: () => new Response("assets-plan-script") };`,
64 assets: { directory: b },
65 compatibility: { date: "2024-01-01" },
66 });
67 return { withMain, assetsOnly, withScript };
68 });
69
70 yield* stack.deploy(program(dirA, dirB));
71
72 // Nothing changed → every worker must plan as a noop. This is the
73 // regression: the diff used to refuse to read the assets directory
74 // and conservatively reported "update" on every plan.
75 const settled = yield* stack.plan(program(dirA, dirB));
76 expect(actionOf(settled, "AssetsPlanWithMain")).toBe("noop");
77 expect(actionOf(settled, "AssetsPlanAssetsOnly")).toBe("noop");
78 expect(actionOf(settled, "AssetsPlanScript")).toBe("noop");
79
80 // Editing an asset in dirA must dirty exactly the worker that
81 // serves it — content changes still surface as updates.
82 yield* fs.writeFileString(
83 path.join(dirA, "index.html"),
84 "<html><body>alchemy-assets-plan-index-v2</body></html>",
85 );
86 const changed = yield* stack.plan(program(dirA, dirB));
87 expect(actionOf(changed, "AssetsPlanWithMain")).toBe("update");
88 expect(actionOf(changed, "AssetsPlanAssetsOnly")).toBe("noop");
89 expect(actionOf(changed, "AssetsPlanScript")).toBe("noop");
90
91 // Deploying the change re-settles the plan.
92 yield* stack.deploy(program(dirA, dirB));
93 const resettled = yield* stack.plan(program(dirA, dirB));
94 expect(actionOf(resettled, "AssetsPlanWithMain")).toBe("noop");
95 expect(actionOf(resettled, "AssetsPlanAssetsOnly")).toBe("noop");
96 expect(actionOf(resettled, "AssetsPlanScript")).toBe("noop");
97
98 // Path invariance: identical bytes at a different absolute path
99 // must hash identically — a plan made from a different checkout
100 // (CI runner → laptop, monorepo root → workspace root) converges
101 // without spurious updates. The directory path is deliberately
102 // excluded from the content hash.
103 const dirB2 = yield* cloneFixture(dirB, { prefix: "alchemy-assets-plan-b2-" });
104 const moved = yield* stack.plan(program(dirA, dirB2));
105 expect(actionOf(moved, "AssetsPlanAssetsOnly")).toBe("noop");
106 expect(actionOf(moved, "AssetsPlanScript")).toBe("noop");
107
108 // `.assetsignore` and the files it excludes participate in neither
109 // the manifest nor the hash — adding them must stay a noop.
110 yield* fs.writeFileString(path.join(dirB, ".assetsignore"), "junk.txt");
111 yield* fs.writeFileString(path.join(dirB, "junk.txt"), "not-an-asset");
112 const ignored = yield* stack.plan(program(dirA, dirB));
113 expect(actionOf(ignored, "AssetsPlanAssetsOnly")).toBe("noop");
114 expect(actionOf(ignored, "AssetsPlanScript")).toBe("noop");
115
116 // `_headers` is excluded from the manifest but shipped via the
117 // asset config, so editing it must dirty every worker serving the
118 // directory.
119 yield* fs.writeFileString(path.join(dirB, "_headers"), "/*\n X-Assets-Plan: v1\n");
120 const headers = yield* stack.plan(program(dirA, dirB));
121 expect(actionOf(headers, "AssetsPlanWithMain")).toBe("noop");
122 expect(actionOf(headers, "AssetsPlanAssetsOnly")).toBe("update");
123 expect(actionOf(headers, "AssetsPlanScript")).toBe("update");
124 yield* fs.remove(path.join(dirB, "_headers"));
125 const headersReverted = yield* stack.plan(program(dirA, dirB));
126 expect(actionOf(headersReverted, "AssetsPlanAssetsOnly")).toBe("noop");
127 expect(actionOf(headersReverted, "AssetsPlanScript")).toBe("noop");
128
129 // A directory that's missing at plan time (e.g. produced by an
130 // upstream build step that only runs during apply) degrades to
131 // "update" — the plan must not crash.
132 yield* fs.remove(dirA, { recursive: true });
133 const missing = yield* stack.plan(program(dirA, dirB));
134 expect(actionOf(missing, "AssetsPlanWithMain")).toBe("update");
135 expect(actionOf(missing, "AssetsPlanAssetsOnly")).toBe("noop");
136 expect(actionOf(missing, "AssetsPlanScript")).toBe("noop");
137
138 yield* stack.destroy();
139 }),
140 { timeout: 360_000 },
141 );
142
143 // The migration path off a hand-rolled hash: a worker deployed with a
144 // user-supplied `assets.hash` whose props then drop the hash must plan
145 // exactly one update (the stored custom hash can't match the content
146 // hash), converge after the deploy, and dirty again if a different
147 // supplied hash is introduced.
148 test.provider(
149 "removing a supplied assets hash converges after one deploy",
150 (stack) =>
151 Effect.gen(function* () {
152 yield* stack.destroy();
153
154 const dir = yield* cloneFixture(assetsFixture, {
155 prefix: "alchemy-assets-hash-migration-",
156 });
157
158 const program = (hash?: string) =>
159 Effect.gen(function* () {
160 return yield* Cloudflare.Worker("AssetsHashMigration", {
161 main,
162 assets: hash ? { directory: dir, hash } : { directory: dir },
163 compatibility: { date: "2024-01-01" },
164 });
165 });
166
167 // Deployed with a hand-rolled hash: the supplied hash is
168 // authoritative, so an identical plan is a noop without reading
169 // the directory.
170 yield* stack.deploy(program("hand-rolled-hash-v1"));
171 const settled = yield* stack.plan(program("hand-rolled-hash-v1"));
172 expect(actionOf(settled, "AssetsHashMigration")).toBe("noop");
173
174 // Dropping the hash falls back to content hashing — the stored
175 // custom hash can't match, so exactly one update is planned...
176 const dropped = yield* stack.plan(program());
177 expect(actionOf(dropped, "AssetsHashMigration")).toBe("update");
178
179 // ...and after deploying, the stored hash is the content hash and
180 // the plan converges.
181 yield* stack.deploy(program());
182 const converged = yield* stack.plan(program());
183 expect(actionOf(converged, "AssetsHashMigration")).toBe("noop");
184
185 // Re-introducing a different supplied hash dirties the plan again.
186 const reintroduced = yield* stack.plan(program("hand-rolled-hash-v2"));
187 expect(actionOf(reintroduced, "AssetsHashMigration")).toBe("update");
188
189 yield* stack.destroy();
190 }),
191 { timeout: 360_000 },
192 );
193
194 // `.assetsignore` excludes a folder the deploying user cannot read (e.g.
195 // a root-owned cache). Its files must not be uploaded, and the walk must
196 // not read it at all — reading it used to fail the deploy with EACCES.
197 test.provider(
198 "excludes .assetsignore'd folders without reading them",
199 (stack) =>
200 Effect.gen(function* () {
201 const fs = yield* FileSystem.FileSystem;
202 const path = yield* Path.Path;
203 const client = yield* HttpClient.HttpClient;
204
205 yield* stack.destroy();
206
207 const dir = yield* cloneFixture(assetsFixture, { prefix: "alchemy-assets-ignore-" });
208 yield* fs.writeFileString(path.join(dir, ".assetsignore"), "private/\n*.map\n");
209 yield* fs.writeFileString(path.join(dir, "app.js.map"), "{}");
210 const locked = path.join(dir, "private", "cache");
211 yield* fs.makeDirectory(locked, { recursive: true });
212 yield* fs.writeFileString(path.join(dir, "private", "secret.txt"), "secret");
213 yield* Effect.acquireRelease(fs.chmod(locked, 0o000), () =>
214 fs.chmod(locked, 0o755).pipe(Effect.ignore),
215 );
216
217 const worker = yield* stack.deploy(
218 Cloudflare.Worker("AssetsIgnoreWorker", {
219 assets: { directory: dir, notFoundHandling: "404-page" },
220 compatibility: { date: "2024-01-01" },
221 }),
222 );
223
224 const status = (pathname: string) =>
225 client.get(new URL(pathname, worker.url).href).pipe(
226 Effect.map((response) => response.status),
227 Effect.repeat({
228 schedule: Schedule.spaced("1 second"),
229 until: (code) => code !== 503 && code !== 522,
230 times: 30,
231 }),
232 );
233 // The served index proves the Worker is live before asserting 404s.
234 expect(
235 yield* status("/index.html").pipe(
236 Effect.repeat({
237 schedule: Schedule.spaced("1 second"),
238 until: (code) => code === 200,
239 times: 30,
240 }),
241 ),
242 ).toBe(200);
243 expect(yield* status("/private/secret.txt")).toBe(404);
244 expect(yield* status("/app.js.map")).toBe(404);
245
246 yield* stack.destroy();
247 }),
248 { timeout: 180_000 },
249 );
250 },
251 );