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 SolidStart (vite/nitro) build AND
55// creates a Hetzner Server (~5-10 minutes), so give the hook far more
56// headroom than 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 // The `GREETING` env value from alchemy.run.ts, read via
90 // `process.env` in the index route — proves the Hetzner Server
91 // rendered it at request time.
92 expect(html).toContain("Hello from SolidStart on Hetzner!");
93 expect(html).toContain("Styled with Tailwind CSS");
94 }),
95 { timeout: 180_000 },
96 );
97
98 test(
99 "compiles tailwind from vite.config.ts",
100 Effect.gen(function* () {
101 const url = yield* base;
102 // The utility class in the markup proves the page shipped with Tailwind
103 // classes; wait until the deployed HTML includes it.
104 const html = yield* getBodyWhenReady(url, "text-3xl");
105
106 // The compiled rule for the utility must be served from one of the
107 // linked stylesheets — it only exists if the @tailwindcss/vite plugin
108 // from the project's own vite.config.ts ran during the build.
109 // SolidStart emits `href` before `rel`, so match attribute-order-
110 // agnostically and collect every stylesheet link.
111 const hrefs = [...html.matchAll(/<link\b[^>]*>/g)]
112 .map((m) => m[0])
113 .filter((tag) => /rel="stylesheet"/.test(tag))
114 .map((tag) => tag.match(/href="([^"]+)"/)?.[1])
115 .filter((href): href is string => !!href);
116 expect(hrefs.length).toBeGreaterThan(0);
117
118 const stylesheets = yield* Effect.all(
119 hrefs.map((href) =>
120 Effect.gen(function* () {
121 const cssUrl = href.startsWith("http")
122 ? href
123 : `${url}${href.startsWith("/") ? "" : "/"}${href}`;
124 const res = yield* getWhenReady(cssUrl);
125 expect(res.status).toBe(200);
126 return yield* res.text;
127 }),
128 ),
129 );
130 expect(stylesheets.some((css) => css.includes(".text-3xl"))).toBe(true);
131 }),
132 { timeout: 180_000 },
133 );
134
135 test(
136 "serves a static asset from public/",
137 Effect.gen(function* () {
138 const url = yield* base;
139 const body = yield* getBodyWhenReady(
140 `${url}/robots.txt`,
141 "User-agent: *",
142 );
143 expect(body).toContain("User-agent: *");
144 }),
145 { timeout: 180_000 },
146 );
147}