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