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

main
ts 106 lines 3.52 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 Effect from "effect/Effect";
6 import Stack from "../alchemy.run.ts";
7
8 // A fresh CloudFront distribution (and the Lambda behind it) can serve
9 // transient 404/5xx responses while it propagates. `Test.getWhenReady`
10 // fails on that cold-start window and retries until the site serves a
11 // real response.
12 const { getWhenReady } = Test;
13
14 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
15 providers: AWS.providers(),
16 state: AWS.state(),
17 });
18
19 // The first deploy runs the full Waku build AND creates a CloudFront
20 // distribution (~5-10 minutes), so give the hook far more headroom than
21 // the default 120s.
22 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
23 timeout: 1_200_000,
24 });
25 // Deleting the CloudFront distribution takes several minutes too.
26 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
27 timeout: 1_200_000,
28 });
29
30 const base = Effect.map(stack, ({ url }) => {
31 if (!url) throw new Error("expected the site to expose a CloudFront url");
32 return String(url).replace(/\/+$/, "");
33 });
34
35 test(
36 "deploys and exposes a url",
37 Effect.gen(function* () {
38 const { url } = yield* stack;
39 expect(url).toBeString();
40 }),
41 { timeout: 180_000 },
42 );
43
44 test(
45 "serves the server-rendered home page",
46 Effect.gen(function* () {
47 const url = yield* base;
48 const res = yield* getWhenReady(url);
49 expect(res.status).toBe(200);
50 const html = yield* res.text;
51 // The `GREETING` env value from alchemy.run.ts, read via `getEnv` in the
52 // dynamic RSC page — proves the Lambda rendered it at request time.
53 expect(html).toContain("Hello from Waku on AWS!");
54 expect(html).toContain("This page is rendered by the server on every request.");
55 }),
56 { timeout: 180_000 },
57 );
58
59 test(
60 "compiles tailwind from waku.config.ts",
61 Effect.gen(function* () {
62 const url = yield* base;
63 const res = yield* getWhenReady(url);
64 expect(res.status).toBe(200);
65 const html = yield* res.text;
66 // The utility class from src/pages/index.tsx made it into the markup.
67 expect(html).toContain("text-3xl");
68
69 // Locate the emitted stylesheet. Waku links the compiled CSS bundle in
70 // the document head; the @tailwindcss/vite plugin registered via
71 // waku.config.ts's `vite` field is what compiles it.
72 const links = [...html.matchAll(/<link\b[^>]*>/g)]
73 .map((m) => m[0])
74 .filter((tag) => /rel="stylesheet"/.test(tag))
75 .map((tag) => /href="([^"]+)"/.exec(tag)?.[1])
76 .filter((href): href is string => !!href);
77 expect(links.length).toBeGreaterThan(0);
78
79 let compiled = "";
80 for (const href of links) {
81 const cssUrl = href.startsWith("http")
82 ? href
83 : `${url}${href.startsWith("/") ? "" : "/"}${href}`;
84 const cssRes = yield* getWhenReady(cssUrl);
85 expect(cssRes.status).toBe(200);
86 compiled += yield* cssRes.text;
87 }
88
89 // The compiled rule proves tailwind ran through the plugin from
90 // waku.config.ts — not just that the class name appears in markup.
91 expect(compiled).toContain(".text-3xl");
92 expect(compiled).toContain("font-bold");
93 }),
94 { timeout: 180_000 },
95 );
96
97 test(
98 "serves a static asset from public/",
99 Effect.gen(function* () {
100 const url = yield* base;
101 const res = yield* getWhenReady(`${url}/hello.txt`);
102 expect(res.status).toBe(200);
103 expect(yield* res.text).toContain("hello from public/");
104 }),
105 { timeout: 180_000 },
106 );