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

main
ts 110 lines 4.47 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { describe } 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 Path from "effect/Path";
7 import * as pathe from "pathe";
8 import { cloneFixture } from "../Utils/Fixture.ts";
9 import {
10 expectUrlAbsent,
11 expectUrlContains,
12 expectUrlHeader,
13 expectUrlRedirect,
14 } from "../Utils/Http.ts";
15
16 const { test } = Test.make({ providers: Cloudflare.providers() });
17
18 const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures/assets-config");
19
20 const workerScript = (marker: string) => `
21 export default {
22 fetch: async () => new Response(${JSON.stringify(marker)}),
23 };
24 `;
25
26 // Regression test for https://github.com/alchemy-run/alchemy/issues/927:
27 // `_redirects` / `_headers` were read from the assets directory and folded
28 // into the change-detection hash, but their contents were never sent to
29 // Cloudflare, so the rules silently never applied.
30 describe.concurrent(
31 "Cloudflare.Worker assets config files",
32 { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] },
33 () => {
34 test.provider(
35 "redirects and headers apply, update, and survive keep-assets deploys",
36 (stack) =>
37 Effect.gen(function* () {
38 const fs = yield* FileSystem.FileSystem;
39 const path = yield* Path.Path;
40
41 yield* stack.destroy();
42
43 const deploy = (assets: string | { directory: string; hash: string }, marker: string) =>
44 stack.deploy(
45 Effect.gen(function* () {
46 return yield* Cloudflare.Worker("AssetsConfigFiles", {
47 script: workerScript(marker),
48 assets,
49 workersDev: true,
50 compatibility: { date: "2024-01-01" },
51 });
52 }),
53 );
54
55 // 1. Fresh deploy: rules from the fixture's `_redirects` /
56 // `_headers` must reach Cloudflare and apply.
57 const worker = yield* deploy(fixtureDir, "worker-v1");
58 const url = worker.url!;
59 yield* expectUrlContains(`${url}/`, "alchemy-assets-config-index");
60 yield* expectUrlRedirect(`${url}/old-path`, "/index.html", {
61 status: 301,
62 label: "initial redirect",
63 });
64 yield* expectUrlHeader(`${url}/`, "x-alchemy-test", "assets-config-header", {
65 label: "initial header",
66 });
67 // The special files themselves stay excluded from serving.
68 yield* expectUrlAbsent(`${url}/_redirects`, "/old-path", {
69 timeout: "15 seconds",
70 label: "_redirects not served",
71 });
72
73 // 2. Update: editing only `_redirects` must deploy the new rules.
74 const dir = yield* cloneFixture(fixtureDir, { prefix: "alchemy-assets-config-" });
75 yield* fs.writeFileString(path.join(dir, "_redirects"), "/moved /index.html 302\n");
76 yield* deploy(dir, "worker-v2");
77 yield* expectUrlRedirect(`${url}/moved`, "/index.html", {
78 status: 302,
79 label: "updated redirect",
80 });
81 // The old rule is gone: `/old-path` now falls through to the
82 // user worker instead of redirecting.
83 yield* expectUrlContains(`${url}/old-path`, "worker-v2", {
84 label: "removed redirect falls through",
85 });
86
87 // 3. Precomputed-hash deploys: the first one uploads, the second
88 // hits the keep-assets skip path (hash matches stored state)
89 // which never walks the directory — it must still re-send the
90 // `_redirects` / `_headers` contents or the PUT wipes them.
91 const prebuilt = { directory: dir, hash: "assets-config-files-test" };
92 yield* deploy(prebuilt, "worker-v3");
93 yield* expectUrlContains(`${url}/some-worker-route`, "worker-v3");
94 yield* deploy(prebuilt, "worker-v4");
95 // Once the new script serves, the skip-path deploy has landed.
96 yield* expectUrlContains(`${url}/some-worker-route`, "worker-v4");
97 yield* expectUrlRedirect(`${url}/moved`, "/index.html", {
98 status: 302,
99 label: "redirect after keep-assets deploy",
100 });
101 yield* expectUrlHeader(`${url}/`, "x-alchemy-test", "assets-config-header", {
102 label: "header after keep-assets deploy",
103 });
104
105 yield* stack.destroy();
106 }),
107 { timeout: 360_000 },
108 );
109 },
110 );