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

main
ts 274 lines 10.2 KB
Raw
1 import * as NodeNet from "node:net";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as Alchemy from "alchemy";
7 import * as Effect from "effect/Effect";
8 import * as FileSystem from "effect/FileSystem";
9 import * as Path from "effect/Path";
10 import { MinimumLogLevel } from "effect/References";
11 import * as pathe from "pathe";
12 import { cloneFixture } from "../Utils/Fixture.ts";
13 import { expectUrlContains } from "../Utils/Http.ts";
14 import { expectWorkerExists, findWorker, waitForWorkerToBeDeleted } from "../Utils/Worker.ts";
15
16 // `dev: true` runs local providers behind the RPC sidecar proxy by default,
17 // matching the process topology of the real `alchemy dev` command.
18 const { test } = Test.make({ providers: Cloudflare.providers(), dev: true });
19
20 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
21
22 const fixtureDir = pathe.resolve(import.meta.dirname, "staticsite-fixture");
23 const workerEntry = pathe.resolve(import.meta.dirname, "fixtures/worker.ts");
24 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
25
26 const staticSiteProps = (cwd: string): Cloudflare.Website.StaticSiteProps => ({
27 command: "bash build.sh",
28 shell: true,
29 cwd,
30 outdir: "dist",
31 main: workerEntry,
32 workersDev: true,
33 compatibility: { date: "2024-01-01" },
34 });
35
36 /** A port that was free a moment ago. */
37 const freePort = Effect.callback<number>((resume) => {
38 const server = NodeNet.createServer();
39 server.listen(0, "127.0.0.1", () => {
40 const { port } = server.address() as NodeNet.AddressInfo;
41 server.close(() => resume(Effect.succeed(port)));
42 });
43 });
44
45 const htmlPage = (marker: string) => `<!doctype html>
46 <html>
47 <head><title>${marker}</title></head>
48 <body><h1>${marker}</h1></body>
49 </html>
50 `;
51
52 // Tests are independent (per-test scratch stacks, private fixture clones),
53 // so run them concurrently; suites are sequential by default.
54 describe.concurrent(
55 "StaticSite dev",
56 { tags: ["provider:cloudflare", "provider:cloudflare:website", "provider:cloudflare:worker"] },
57 () => {
58 /**
59 * The `dev.command` path: `alchemy dev` skips the build entirely and spawns
60 * the command as a long-lived child in the sidecar (`Command.Dev`). The URL
61 * the child prints to stdout becomes the site's `url`, and the Worker opts
62 * out of local emulation (`dev: { mode: "external" }`) — the dev server IS
63 * the site.
64 */
65 test.provider(
66 "StaticSite dev: dev.command serves the site through the external dev server",
67 (stack) =>
68 Effect.gen(function* () {
69 const { accountId } = yield* yield* CloudflareEnvironment;
70 const fs = yield* FileSystem.FileSystem;
71 const path = yield* Path.Path;
72
73 yield* stack.destroy();
74
75 const cwd = yield* cloneFixture(fixtureDir, {
76 prefix: "alchemy-staticsite-dev-cmd-",
77 tempRoot,
78 entries: ["src", "build.sh", "serve.mjs", ".gitignore"],
79 });
80
81 const marker = "staticsite-dev-command-marker";
82 yield* fs.writeFileString(path.join(cwd, "src", "index.html"), htmlPage(marker));
83
84 const site = yield* stack.deploy(
85 Effect.gen(function* () {
86 return yield* Cloudflare.Website.StaticSite("DevCmdSite", {
87 ...staticSiteProps(cwd),
88 dev: { command: "bun serve.mjs", env: { DEV_MARKER: "staticsite-dev-env-marker" } },
89 });
90 }),
91 );
92
93 // The site's url is the dev server's own localhost address,
94 // extracted from the child's stdout.
95 expect(site.url).toBeDefined();
96 expect(site.url).toMatch(/^http:\/\/localhost:\d+/);
97
98 // Content serves through the dev server straight from `src/`.
99 yield* expectUrlContains(`${site.url!}/`, marker, {
100 timeout: "60 seconds",
101 label: "dev.command index",
102 });
103 yield* expectUrlContains(`${site.url!}/index.html`, marker, {
104 timeout: "30 seconds",
105 label: "dev.command explicit path",
106 });
107
108 // `dev.env` reached the spawned child process.
109 yield* expectUrlContains(`${site.url!}/__dev-env`, "staticsite-dev-env-marker", {
110 timeout: "30 seconds",
111 label: "dev.command env passthrough",
112 });
113
114 // The build command was skipped — `dist/` was never produced.
115 expect(yield* fs.exists(path.join(cwd, "dist"))).toBe(false);
116
117 // No cloud Worker was created (external dev mode still records the
118 // real worker name in its stub attributes).
119 expect(site.workerName).toBeDefined();
120 expect(yield* findWorker(site.workerName, accountId)).toBeUndefined();
121
122 yield* stack.destroy();
123 }).pipe(logLevel),
124 { tags: ["live"], timeout: 300_000 },
125 );
126
127 /**
128 * Without `dev.command`, a dev run falls back to build mode: the build
129 * command runs (producing `dist/`) and the Worker serves the built assets
130 * from a local simulator — the `url` is a localhost dev-proxy address and
131 * no cloud Worker exists.
132 */
133 test.provider(
134 "StaticSite dev: without dev.command the build runs and a local Worker serves the assets",
135 (stack) =>
136 Effect.gen(function* () {
137 const { accountId } = yield* yield* CloudflareEnvironment;
138 const fs = yield* FileSystem.FileSystem;
139 const path = yield* Path.Path;
140
141 yield* stack.destroy();
142
143 const cwd = yield* cloneFixture(fixtureDir, {
144 prefix: "alchemy-staticsite-dev-build-",
145 tempRoot,
146 entries: ["src", "build.sh", ".gitignore"],
147 });
148
149 const marker = "staticsite-dev-build-marker";
150 yield* fs.writeFileString(path.join(cwd, "src", "index.html"), htmlPage(marker));
151
152 const site = yield* stack.deploy(
153 Effect.gen(function* () {
154 return yield* Cloudflare.Website.StaticSite("DevBuildSite", staticSiteProps(cwd));
155 }),
156 );
157
158 // Local identity: the url points at the local dev proxy.
159 expect(site.url).toBeDefined();
160 expect(site.url).toMatch(/^http:\/\/localhost:\d+/);
161
162 // The build ran — `dist/` exists with the built page.
163 expect(yield* fs.exists(path.join(cwd, "dist", "index.html"))).toBe(true);
164
165 // The built assets serve through the local Worker simulator.
166 yield* expectUrlContains(`${site.url!}/index.html`, marker, {
167 timeout: "60 seconds",
168 label: "dev build-mode assets",
169 });
170
171 // No cloud Worker was created.
172 expect(yield* findWorker(site.workerName, accountId)).toBeUndefined();
173
174 yield* stack.destroy();
175 }).pipe(logLevel),
176 { tags: ["live"], timeout: 300_000 },
177 );
178
179 /**
180 * Without `dev.command`, `dev.port`/`strictPort` configure the local
181 * Worker that serves the built assets.
182 */
183 test.provider(
184 "StaticSite dev: without dev.command, dev.port picks the local Worker's port",
185 (stack) =>
186 Effect.gen(function* () {
187 const fs = yield* FileSystem.FileSystem;
188 const path = yield* Path.Path;
189
190 yield* stack.destroy();
191
192 const cwd = yield* cloneFixture(fixtureDir, {
193 prefix: "alchemy-staticsite-dev-port-",
194 tempRoot,
195 entries: ["src", "build.sh", ".gitignore"],
196 });
197
198 const marker = "staticsite-dev-port-marker";
199 yield* fs.writeFileString(path.join(cwd, "src", "index.html"), htmlPage(marker));
200 const port = yield* freePort;
201
202 const site = yield* stack.deploy(
203 Effect.gen(function* () {
204 return yield* Cloudflare.Website.StaticSite("DevPortSite", {
205 ...staticSiteProps(cwd),
206 dev: { port, strictPort: true },
207 });
208 }),
209 );
210
211 expect(site.url).toBe(`http://localhost:${port}`);
212 yield* expectUrlContains(`${site.url!}/index.html`, marker, {
213 timeout: "60 seconds",
214 label: "dev.port assets",
215 });
216
217 yield* stack.destroy();
218 }).pipe(logLevel),
219 { tags: ["live"], timeout: 300_000 },
220 );
221
222 /**
223 * `Alchemy.remote()` opts the whole site OUT of local emulation: even in a
224 * dev run, the build executes and the Worker deploys to real Cloudflare.
225 * Destroy deletes the cloud Worker (the state row is stamped live).
226 */
227 test.provider(
228 "StaticSite dev: Alchemy.remote() deploys the real Worker and destroy removes it",
229 (stack) =>
230 Effect.gen(function* () {
231 const { accountId } = yield* yield* CloudflareEnvironment;
232 const fs = yield* FileSystem.FileSystem;
233 const path = yield* Path.Path;
234
235 yield* stack.destroy();
236
237 const cwd = yield* cloneFixture(fixtureDir, {
238 prefix: "alchemy-staticsite-dev-remote-",
239 tempRoot,
240 entries: ["src", "build.sh", ".gitignore"],
241 });
242
243 const marker = "staticsite-dev-remote-marker";
244 yield* fs.writeFileString(path.join(cwd, "src", "index.html"), htmlPage(marker));
245
246 const site = yield* stack.deploy(
247 Effect.gen(function* () {
248 return yield* Cloudflare.Website.StaticSite("RemoteSite", staticSiteProps(cwd)).pipe(
249 Alchemy.remote(),
250 );
251 }),
252 );
253
254 // Real identity: a non-local URL and a Worker that exists on
255 // Cloudflare.
256 expect(site.url).toBeDefined();
257 expect(site.url).not.toMatch(/^http:\/\/localhost/);
258 yield* expectWorkerExists(site.workerName, accountId);
259
260 // The real workers.dev URL serves the built assets.
261 yield* expectUrlContains(`${site.url!}/index.html`, marker, {
262 timeout: "120 seconds",
263 label: "remote() site marker",
264 });
265
266 yield* stack.destroy();
267
268 // The cloud Worker is gone after destroy (stamped-mode delete).
269 yield* waitForWorkerToBeDeleted(site.workerName, accountId);
270 }).pipe(logLevel),
271 { tags: ["live"], timeout: 300_000 },
272 );
273 },
274 );