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

main
ts 142 lines 4.56 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 (and the Lambda behind it) can serve
11 // transient 404/5xx responses while it propagates. `Test.getWhenReady`
12 // fails on that cold-start window and retries until the site serves a
13 // real response.
14 const { getWhenReady } = Test;
15
16 class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
17 body: string;
18 }> {}
19
20 // While the asset manifest and CloudFront edge caches are still
21 // propagating, a 200 body can be stale — the status alone can't
22 // distinguish "not yet" from "served", so retry until the body matches.
23 const getBodyWhenReady = (url: string, expected: string) =>
24 Effect.gen(function* () {
25 const res = yield* getWhenReady(url);
26 expect(res.status).toBe(200);
27 const body = yield* res.text;
28 if (!body.includes(expected)) {
29 return yield* Effect.fail(new AssetNotReady({ body }));
30 }
31 return body;
32 }).pipe(
33 Effect.retry({
34 while: (error) => error instanceof AssetNotReady,
35 schedule: Schedule.max([
36 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
37 Schedule.recurs(20),
38 ]),
39 }),
40 );
41
42 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
43 providers: AWS.providers(),
44 state: AWS.state(),
45 });
46
47 // The first deploy runs the full Nuxt build AND creates a CloudFront
48 // distribution (~5-10 minutes), so give the hook far more headroom than
49 // the default 120s.
50 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
51 timeout: 1_200_000,
52 });
53 // Deleting the CloudFront distribution takes several minutes too.
54 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
55 timeout: 1_200_000,
56 });
57
58 const base = Effect.map(stack, ({ url }) => {
59 if (!url) throw new Error("expected the site to expose a CloudFront url");
60 return String(url).replace(/\/+$/, "");
61 });
62
63 test(
64 "deploys and exposes a url",
65 Effect.gen(function* () {
66 const { url } = yield* stack;
67 expect(url).toBeString();
68 }),
69 { timeout: 180_000 },
70 );
71
72 test(
73 "serves the server-rendered home page",
74 Effect.gen(function* () {
75 const url = yield* base;
76 const res = yield* getWhenReady(url);
77 expect(res.status).toBe(200);
78 const html = yield* res.text;
79 expect(html).toContain("Nuxt on AWS");
80 // The `GREETING` env value from alchemy.run.ts, read via
81 // `process.env` during SSR — proves the Lambda rendered it at
82 // request time.
83 expect(html).toContain("Hello from Nuxt on AWS!");
84 }),
85 { timeout: 180_000 },
86 );
87
88 test(
89 "serves the api route with the environment value",
90 Effect.gen(function* () {
91 const url = yield* base;
92 const body = yield* getBodyWhenReady(`${url}/api/hello`, "Hello from Nuxt on AWS!");
93 expect(JSON.parse(body)).toEqual({
94 greeting: "Hello from Nuxt on AWS!",
95 });
96 }),
97 { timeout: 180_000 },
98 );
99
100 test(
101 "serves the prerendered about page",
102 Effect.gen(function* () {
103 const url = yield* base;
104 const res = yield* getWhenReady(`${url}/about`);
105 expect(res.status).toBe(200);
106 }),
107 { timeout: 180_000 },
108 );
109
110 test(
111 "compiles tailwind from nuxt.config.ts",
112 Effect.gen(function* () {
113 const url = yield* base;
114 // The utility classes on the home page prove the markup made it through
115 // the build; the compiled rule proves the @tailwindcss/vite plugin from
116 // the project's own nuxt.config.ts ran during it.
117 const html = yield* getBodyWhenReady(url, "text-3xl");
118 expect(html).toContain("text-3xl");
119
120 // Nuxt either links the compiled stylesheet (/_nuxt/*.css) or inlines it
121 // into a <style> tag depending on its inlineStyles feature — accept both.
122 const links = [...html.matchAll(/href="([^"]+\.css)"/g)].map((m) => m[1]!);
123 let css = [...html.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/g)].map((m) => m[1]!).join("\n");
124 for (const link of links) {
125 const href = link.startsWith("http") ? link : `${url}${link}`;
126 css += yield* getBodyWhenReady(href, "{");
127 }
128 expect(css).toContain(".text-3xl");
129 expect(css).toContain("font-bold");
130 }),
131 { timeout: 180_000 },
132 );
133
134 test(
135 "serves a static asset from public/",
136 Effect.gen(function* () {
137 const url = yield* base;
138 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
139 expect(body).toContain("User-agent: *");
140 }),
141 { timeout: 180_000 },
142 );