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

main
ts 127 lines 4.41 KB
Raw
1 import { expect } from "bun:test";
2 import * as AWS from "@alchemy.run/aws";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as Console from "effect/Console";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import Stack from "../alchemy.run.ts";
9
10 // A fresh CloudFront distribution can serve transient 404/5xx responses
11 // while it propagates. `Test.getWhenReady` fails on that cold-start
12 // window and retries until the site serves a real response.
13 const { getWhenReady } = Test;
14
15 class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
16 body: string;
17 }> {}
18
19 // While the asset manifest and CloudFront edge caches are still
20 // propagating, a 200 body can be stale — the status alone can't
21 // distinguish "not yet" from "served", so retry until the body matches.
22 const getBodyWhenReady = (url: string, expected: string) =>
23 Effect.gen(function* () {
24 const res = yield* getWhenReady(url);
25 expect(res.status).toBe(200);
26 const body = yield* res.text;
27 if (!body.includes(expected)) {
28 return yield* Effect.fail(new AssetNotReady({ body }));
29 }
30 return body;
31 }).pipe(
32 Effect.retry({
33 while: (error) => error instanceof AssetNotReady,
34 schedule: Schedule.max([
35 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
36 Schedule.recurs(20),
37 ]),
38 }),
39 );
40
41 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
42 providers: AWS.providers(),
43 state: AWS.state(),
44 });
45
46 // The first deploy runs the full Vite build AND creates a CloudFront
47 // distribution (~5-10 minutes), so give the hook far more headroom than
48 // the default 120s.
49 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
50 timeout: 1_200_000,
51 });
52 // Deleting the CloudFront distribution takes several minutes too.
53 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
54 timeout: 1_200_000,
55 });
56
57 const base = Effect.map(stack, ({ url }) => {
58 if (!url) throw new Error("expected the site to expose a CloudFront url");
59 return String(url).replace(/\/+$/, "");
60 });
61
62 // Resolve an asset href from index.html against the site base.
63 const resolve = (baseUrl: string, href: string) =>
64 href.startsWith("http") ? href : `${baseUrl}${href.startsWith("/") ? "" : "/"}${href}`;
65
66 test(
67 "deploys and exposes a url",
68 Effect.gen(function* () {
69 const { url } = yield* stack;
70 expect(url).toBeString();
71 }),
72 { timeout: 180_000 },
73 );
74
75 test(
76 "serves the index HTML",
77 Effect.gen(function* () {
78 const url = yield* base;
79 const html = yield* getBodyWhenReady(url, '<div id="root">');
80 expect(html).toContain("<title>Vite on AWS</title>");
81 }),
82 { timeout: 180_000 },
83 );
84
85 test(
86 "ships the React app in the client bundle",
87 Effect.gen(function* () {
88 const url = yield* base;
89 // Client-only SPA: the greeting and Card render in the browser, so
90 // assert on the built JS bundle Vite links from index.html.
91 const html = yield* getBodyWhenReady(url, '<script type="module"');
92 const script = html.match(/<script[^>]*type="module"[^>]*src="([^"]+)"/);
93 expect(script).not.toBeNull();
94 const js = yield* getBodyWhenReady(resolve(url, script![1]!), "Hello from Vite!");
95 expect(js).toContain("Hello from Vite!");
96 expect(js).toContain("Styled with Tailwind CSS");
97 }),
98 { timeout: 180_000 },
99 );
100
101 test(
102 "compiles tailwind from vite.config.ts",
103 Effect.gen(function* () {
104 const url = yield* base;
105 // Assert on the compiled stylesheet Vite links from index.html — the
106 // compiled rule for a utility used in src/main.tsx only exists if the
107 // @tailwindcss/vite plugin from the project's own vite.config.ts ran.
108 const html = yield* getBodyWhenReady(url, "stylesheet");
109 const link = html.match(/<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/);
110 expect(link).not.toBeNull();
111 const css = yield* getBodyWhenReady(resolve(url, link![1]!), ".text-3xl");
112 expect(css).toContain(".text-3xl");
113 }),
114 { timeout: 180_000 },
115 );
116
117 test(
118 "answers unmatched paths with the index page (spa default)",
119 Effect.gen(function* () {
120 const url = yield* base;
121 // `spa` defaults on for AWS.Website.Vite: a miss serves the index
122 // page with a 200 so client-side routes deep-link correctly.
123 const html = yield* getBodyWhenReady(`${url}/some/client/route`, '<div id="root">');
124 expect(html).toContain("<title>Vite on AWS</title>");
125 }),
126 { timeout: 180_000 },
127 );