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 can serve transient 404/5xx responses
12// while it propagates. `Test.getWhenReady` fails on that cold-start
13// window and retries until the site serves a real response.
14const { getWhenReady } = Test;
15
16class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
17 body: string;
18}> {}
19
20// While the asset manifest and Hetzner 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 hasCreds = !!process.env.HCLOUD_TOKEN;
46
47const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
48 providers: Hetzner.providers(),
49 state: Alchemy.localState(),
50 profile: process.env.ALCHEMY_PROFILE,
51});
52
53// The first deploy runs the full Vite build AND creates a Hetzner
54// distribution (~5-10 minutes), so give the hook far more headroom than
55// the default 120s.
56if (!hasCreds) {
57 test.skip("skipped without HCLOUD_TOKEN", Effect.void);
58} else {
59 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
60 timeout: 600_000,
61 });
62 // Deleting the Hetzner deployment takes several minutes too.
63 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
64 timeout: 600_000,
65 });
66
67 const base = Effect.map(stack, ({ url }) => {
68 if (!url) throw new Error("expected the site to expose a url");
69 return String(url).replace(/\/+$/, "");
70 });
71
72 // Resolve an asset href from index.html against the site base.
73 const resolve = (baseUrl: string, href: string) =>
74 href.startsWith("http")
75 ? href
76 : `${baseUrl}${href.startsWith("/") ? "" : "/"}${href}`;
77
78 test(
79 "deploys and exposes a url",
80 Effect.gen(function* () {
81 const { url } = yield* stack;
82 expect(url).toBeString();
83 }),
84 { timeout: 180_000 },
85 );
86
87 test(
88 "serves the index HTML",
89 Effect.gen(function* () {
90 const url = yield* base;
91 const html = yield* getBodyWhenReady(url, '<div id="root">');
92 expect(html).toContain("<title>Foldkit on Hetzner</title>");
93 }),
94 { timeout: 180_000 },
95 );
96
97 test(
98 "ships the Foldkit app in the client bundle",
99 Effect.gen(function* () {
100 const url = yield* base;
101 // Client-only SPA: the greeting and card view render in the browser, so
102 // assert on the built JS bundle Vite links from index.html.
103 const html = yield* getBodyWhenReady(url, '<script type="module"');
104 const script = html.match(/<script[^>]*type="module"[^>]*src="([^"]+)"/);
105 expect(script).not.toBeNull();
106 const js = yield* getBodyWhenReady(
107 resolve(url, script![1]!),
108 "Hello from Foldkit!",
109 );
110 expect(js).toContain("Hello from Foldkit!");
111 expect(js).toContain("Styled with Tailwind CSS");
112 }),
113 { timeout: 180_000 },
114 );
115
116 test(
117 "compiles tailwind from vite.config.ts",
118 Effect.gen(function* () {
119 const url = yield* base;
120 // Assert on the compiled stylesheet Vite links from index.html — the
121 // compiled rule for a utility used in src/main.ts only exists if the
122 // @tailwindcss/vite plugin from the project's own vite.config.ts ran.
123 const html = yield* getBodyWhenReady(url, "stylesheet");
124 const link = html.match(
125 /<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/,
126 );
127 expect(link).not.toBeNull();
128 const css = yield* getBodyWhenReady(resolve(url, link![1]!), ".text-3xl");
129 expect(css).toContain(".text-3xl");
130 }),
131 { timeout: 180_000 },
132 );
133}