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