@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 139 lines 4.72 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 Next.js + OpenNext build AND creates a
48 // CloudFront distribution (~5-10 minutes), so give the hook far more
49 // headroom than 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 // In-repo ONLY: bun's isolated-install store breaks OpenNext's traced
73 // node_modules when the Lambda zip flattens the `next` symlink (see the
74 // README) — Lambda-served routes 500 with a missing @swc/helpers module.
75 // A standalone copy of this example passes all five tests. Set
76 // AWS_WEBSITE_NEXTJS_LAMBDA=1 to run them here (e.g. once the
77 // symlink-preserving bundle lands).
78 const lambdaRoutesBroken = !process.env.AWS_WEBSITE_NEXTJS_LAMBDA;
79
80 test.skipIf(lambdaRoutesBroken)(
81 "serves the server-rendered home page",
82 Effect.gen(function* () {
83 const url = yield* base;
84 const res = yield* getWhenReady(url);
85 expect(res.status).toBe(200);
86 const html = yield* res.text;
87 expect(html).toContain("Next.js on AWS");
88 // The `GREETING` env value from alchemy.run.ts, read via
89 // `process.env` in the force-dynamic page — proves the Lambda
90 // rendered it at request time.
91 expect(html).toContain("Hello from Next.js on AWS!");
92 }),
93 { timeout: 180_000 },
94 );
95
96 test.skipIf(lambdaRoutesBroken)(
97 "serves the dynamic API route",
98 Effect.gen(function* () {
99 const url = yield* base;
100 const res = yield* getWhenReady(`${url}/api/hello`);
101 expect(res.status).toBe(200);
102 const body = (yield* res.json) as { hello: string };
103 expect(body.hello).toBe("world");
104 }),
105 { timeout: 180_000 },
106 );
107
108 test.skipIf(lambdaRoutesBroken)(
109 "compiles tailwind via postcss",
110 Effect.gen(function* () {
111 const url = yield* base;
112 // The page markup uses Tailwind utilities — proving the project's own
113 // postcss.config.mjs (@tailwindcss/postcss) ran inside `next build`.
114 const html = yield* getBodyWhenReady(url, "text-3xl");
115 expect(html).toContain("text-3xl");
116
117 // Next.js links the compiled stylesheet from under /_next/static/
118 // (chunks/*.css as of Next 16).
119 const match = html.match(/\/_next\/static\/[^"']+\.css/);
120 expect(match).not.toBeNull();
121
122 // The stylesheet must contain the compiled Tailwind rule, not just the
123 // class name in markup.
124 const css = yield* getBodyWhenReady(`${url}${match![0]}`, ".text-3xl");
125 expect(css).toContain(".text-3xl");
126 expect(css).toContain(".font-bold");
127 }),
128 { timeout: 180_000 },
129 );
130
131 test(
132 "serves a static asset from public/",
133 Effect.gen(function* () {
134 const url = yield* base;
135 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
136 expect(body).toContain("User-agent: *");
137 }),
138 { timeout: 180_000 },
139 );