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

main
ts 115 lines 4.57 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
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 Path from "effect/Path";
7 import * as pathe from "pathe";
8 import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts";
9 import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts";
10
11 // `dev: true` runs local providers behind the RPC sidecar proxy by default,
12 // matching the process topology of the real `alchemy dev` command.
13 const { test } = Test.make({ providers: AWS.providers(), dev: true });
14
15 const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "staticsite-dev");
16 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
17
18 const htmlPage = (marker: string) => `<!doctype html>
19 <html>
20 <head><title>${marker}</title></head>
21 <body><h1>${marker}</h1></body>
22 </html>
23 `;
24
25 describe(
26 "AWS.Website.StaticSite local",
27 { tags: ["provider:aws", "provider:aws:website", "local"] },
28 () => {
29 /**
30 * The `dev.command` path: `alchemy dev` skips the build/upload entirely
31 * and spawns the command as a long-lived child in the sidecar
32 * (`Command.Dev`). The URL the child prints to stdout becomes the site's
33 * `url`, and no cloud resources are declared at all.
34 */
35 test.provider(
36 "dev.command serves the site through the external dev server",
37 (stack) =>
38 Effect.gen(function* () {
39 const fs = yield* FileSystem.FileSystem;
40 const path = yield* Path.Path;
41
42 yield* stack.destroy();
43
44 const cwd = yield* cloneFixture(fixtureDir, {
45 prefix: "alchemy-aws-staticsite-dev-cmd-",
46 tempRoot,
47 entries: ["serve.mjs", "site"],
48 });
49
50 const marker = "aws-staticsite-dev-command-marker";
51 yield* fs.writeFileString(path.join(cwd, "site", "index.html"), htmlPage(marker));
52
53 const deployed = yield* stack.deploy(
54 Effect.gen(function* () {
55 const site = yield* AWS.Website.StaticSite("DevCmdSite", {
56 path: cwd,
57 dev: {
58 command: "bun serve.mjs",
59 env: { DEV_MARKER: "aws-staticsite-dev-env-marker" },
60 },
61 });
62 return { site };
63 }),
64 );
65
66 // The site's url is the dev server's own localhost address,
67 // extracted from the child's stdout — and no cloud rows exist
68 // (proof no AWS call ran).
69 const url = deployed.site.url! as string;
70 expect(url).toMatch(/^http:\/\/localhost:\d+/);
71 expect(deployed.site.bucket).toBeUndefined();
72 expect(deployed.site.distribution).toBeUndefined();
73 expect(deployed.site.files).toBeUndefined();
74
75 // Dev-mode urls contract: `urls` is exactly the dev server's URL
76 // and `url` is always `urls[0]`.
77 expect(deployed.site.urls).toEqual([url]);
78 expect(deployed.site.url).toBe(deployed.site.urls[0]);
79
80 // Content serves through the dev server straight from `site/`.
81 yield* expectUrlContains(`${url}/`, marker, {
82 timeout: "60 seconds",
83 label: "dev.command index",
84 });
85 yield* expectUrlContains(`${url}/index.html`, marker, {
86 timeout: "30 seconds",
87 label: "dev.command explicit path",
88 });
89
90 // `dev.env` reached the spawned child process.
91 yield* expectUrlContains(`${url}/__dev-env`, "aws-staticsite-dev-env-marker", {
92 timeout: "30 seconds",
93 label: "dev.command env passthrough",
94 });
95
96 // ── Live edit: rewrite the content in place. The stack is NOT
97 // re-applied — the external dev server reads from disk per request,
98 // so the very next fetch must serve the new content ──────────────
99 const editedMarker = "aws-staticsite-dev-command-marker-v2";
100 yield* fs.writeFileString(path.join(cwd, "site", "index.html"), htmlPage(editedMarker));
101 yield* expectUrlContains(`${url}/`, editedMarker, {
102 timeout: "30 seconds",
103 label: "dev.command index after live edit",
104 });
105 // `dev.env` still reaches the (unchanged, long-lived) child.
106 yield* expectUrlContains(`${url}/__dev-env`, "aws-staticsite-dev-env-marker", {
107 label: "dev.command env passthrough after live edit",
108 });
109
110 yield* stack.destroy();
111 }),
112 { timeout: 300_000 },
113 );
114 },
115 );