1import * as AWS from "alchemy/AWS";
2import * as Test from "alchemy/Test/Bun";
3import { expect } from "bun:test";
4import * as Console from "effect/Console";
5import * as Data from "effect/Data";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8import 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.
14const { getWhenReady } = Test;
15
16class 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.
23const 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([
37 Schedule.exponential("500 millis"),
38 Schedule.spaced("3 seconds"),
39 ]),
40 Schedule.recurs(20),
41 ]),
42 }),
43 );
44
45const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
46 providers: AWS.providers(),
47 state: AWS.state(),
48});
49
50// The first deploy runs the full Nuxt build AND creates a CloudFront
51// distribution (~5-10 minutes), so give the hook far more headroom than
52// the default 120s.
53const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
54 timeout: 1_200_000,
55});
56// Deleting the CloudFront distribution takes several minutes too.
57afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
58 timeout: 1_200_000,
59});
60
61const base = Effect.map(stack, ({ url }) => {
62 if (!url) throw new Error("expected the site to expose a CloudFront url");
63 return String(url).replace(/\/+$/, "");
64});
65
66test(
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
75test(
76 "serves the server-rendered home page",
77 Effect.gen(function* () {
78 const url = yield* base;
79 const res = yield* getWhenReady(url);
80 expect(res.status).toBe(200);
81 const html = yield* res.text;
82 expect(html).toContain("Nuxt on AWS");
83 // The `GREETING` env value from alchemy.run.ts, read via
84 // `process.env` during SSR — proves the Lambda rendered it at
85 // request time.
86 expect(html).toContain("Hello from Nuxt on AWS!");
87 }),
88 { timeout: 180_000 },
89);
90
91test(
92 "serves the api route with the environment value",
93 Effect.gen(function* () {
94 const url = yield* base;
95 const body = yield* getBodyWhenReady(
96 `${url}/api/hello`,
97 "Hello from Nuxt on AWS!",
98 );
99 expect(JSON.parse(body)).toEqual({
100 greeting: "Hello from Nuxt on AWS!",
101 });
102 }),
103 { timeout: 180_000 },
104);
105
106test(
107 "serves the prerendered about page",
108 Effect.gen(function* () {
109 const url = yield* base;
110 const res = yield* getWhenReady(`${url}/about`);
111 expect(res.status).toBe(200);
112 }),
113 { timeout: 180_000 },
114);
115
116test(
117 "compiles tailwind from nuxt.config.ts",
118 Effect.gen(function* () {
119 const url = yield* base;
120 // The utility classes on the home page prove the markup made it through
121 // the build; the compiled rule proves the @tailwindcss/vite plugin from
122 // the project's own nuxt.config.ts ran during it.
123 const html = yield* getBodyWhenReady(url, "text-3xl");
124 expect(html).toContain("text-3xl");
125
126 // Nuxt either links the compiled stylesheet (/_nuxt/*.css) or inlines it
127 // into a <style> tag depending on its inlineStyles feature — accept both.
128 const links = [...html.matchAll(/href="([^"]+\.css)"/g)].map((m) => m[1]!);
129 let css = [...html.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/g)]
130 .map((m) => m[1]!)
131 .join("\n");
132 for (const link of links) {
133 const href = link.startsWith("http") ? link : `${url}${link}`;
134 css += yield* getBodyWhenReady(href, "{");
135 }
136 expect(css).toContain(".text-3xl");
137 expect(css).toContain("font-bold");
138 }),
139 { timeout: 180_000 },
140);
141
142test(
143 "serves a static asset from public/",
144 Effect.gen(function* () {
145 const url = yield* base;
146 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
147 expect(body).toContain("User-agent: *");
148 }),
149 { timeout: 180_000 },
150);