@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 123 lines 4.31 KB
Raw
1 import { expect } from "bun:test";
2 import * as Hetzner from "@alchemy.run/hetzner";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as Alchemy from "alchemy";
5 import * as Console from "effect/Console";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import 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.
14 const { getWhenReady } = Test;
15
16 class 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.
23 const 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([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
37 Schedule.recurs(20),
38 ]),
39 }),
40 );
41
42 const hasCreds = !!process.env.HCLOUD_TOKEN;
43
44 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
45 providers: Hetzner.providers(),
46 state: Alchemy.localState(),
47 profile: process.env.ALCHEMY_PROFILE,
48 });
49
50 // The first deploy runs the full Vite build AND creates a Hetzner
51 // distribution (~5-10 minutes), so give the hook far more headroom than
52 // the default 120s.
53 if (!hasCreds) {
54 test.skip("skipped without HCLOUD_TOKEN", Effect.void);
55 } else {
56 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
57 timeout: 600_000,
58 });
59 // Deleting the Hetzner deployment takes several minutes too.
60 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
61 timeout: 600_000,
62 });
63
64 const base = Effect.map(stack, ({ url }) => {
65 if (!url) throw new Error("expected the site to expose a url");
66 return String(url).replace(/\/+$/, "");
67 });
68
69 // Resolve an asset href from index.html against the site base.
70 const resolve = (baseUrl: string, href: string) =>
71 href.startsWith("http") ? href : `${baseUrl}${href.startsWith("/") ? "" : "/"}${href}`;
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 index HTML",
84 Effect.gen(function* () {
85 const url = yield* base;
86 const html = yield* getBodyWhenReady(url, '<div id="root">');
87 expect(html).toContain("<title>Foldkit on Hetzner</title>");
88 }),
89 { timeout: 180_000 },
90 );
91
92 test(
93 "ships the Foldkit app in the client bundle",
94 Effect.gen(function* () {
95 const url = yield* base;
96 // Client-only SPA: the greeting and card view render in the browser, so
97 // assert on the built JS bundle Vite links from index.html.
98 const html = yield* getBodyWhenReady(url, '<script type="module"');
99 const script = html.match(/<script[^>]*type="module"[^>]*src="([^"]+)"/);
100 expect(script).not.toBeNull();
101 const js = yield* getBodyWhenReady(resolve(url, script![1]!), "Hello from Foldkit!");
102 expect(js).toContain("Hello from Foldkit!");
103 expect(js).toContain("Styled with Tailwind CSS");
104 }),
105 { timeout: 180_000 },
106 );
107
108 test(
109 "compiles tailwind from vite.config.ts",
110 Effect.gen(function* () {
111 const url = yield* base;
112 // Assert on the compiled stylesheet Vite links from index.html — the
113 // compiled rule for a utility used in src/main.ts only exists if the
114 // @tailwindcss/vite plugin from the project's own vite.config.ts ran.
115 const html = yield* getBodyWhenReady(url, "stylesheet");
116 const link = html.match(/<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/);
117 expect(link).not.toBeNull();
118 const css = yield* getBodyWhenReady(resolve(url, link![1]!), ".text-3xl");
119 expect(css).toContain(".text-3xl");
120 }),
121 { timeout: 180_000 },
122 );
123 }