@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, 06:00 UTC

main
ts 145 lines 4.95 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 (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.
15 const { getWhenReady } = Test;
16
17 class 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.
24 const 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([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
38 Schedule.recurs(20),
39 ]),
40 }),
41 );
42
43 const hasCreds = !!process.env.HCLOUD_TOKEN;
44
45 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
46 providers: Hetzner.providers(),
47 state: Alchemy.localState(),
48 profile: process.env.ALCHEMY_PROFILE,
49 });
50
51 // The first deploy runs the full Next.js + Node build AND creates a
52 // Hetzner deployment (~5-10 minutes), so give the hook far more
53 // headroom than the default 120s.
54 if (!hasCreds) {
55 test.skip("skipped without HCLOUD_TOKEN", Effect.void);
56 } else {
57 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
58 timeout: 600_000,
59 });
60 // Deleting the Hetzner deployment takes several minutes too.
61 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
62 timeout: 600_000,
63 });
64
65 const base = Effect.map(stack, ({ url }) => {
66 if (!url) throw new Error("expected the site to expose a url");
67 return String(url).replace(/\/+$/, "");
68 });
69
70 test(
71 "deploys and exposes a url",
72 Effect.gen(function* () {
73 const { url } = yield* stack;
74 expect(url).toBeString();
75 }),
76 { timeout: 180_000 },
77 );
78
79 // In-repo ONLY: bun's isolated-install store breaks Node's traced
80 // node_modules when the Hetzner Server zip flattens the `next` symlink (see the
81 // README) — Hetzner Server-served routes 500 with a missing @swc/helpers module.
82 // A standalone copy of this example passes all five tests. Set
83 // AWS_WEBSITE_NEXTJS_LAMBDA=1 to run them here (e.g. once the
84 // symlink-preserving bundle lands).
85 test(
86 "serves the server-rendered home page",
87 Effect.gen(function* () {
88 const url = yield* base;
89 const res = yield* getWhenReady(url);
90 expect(res.status).toBe(200);
91 const html = yield* res.text;
92 expect(html).toContain("Next.js on Hetzner");
93 // The `GREETING` env value from alchemy.run.ts, read via
94 // `process.env` in the force-dynamic page — proves the Hetzner Server
95 // rendered it at request time.
96 expect(html).toContain("Hello from Next.js on Hetzner!");
97 }),
98 { timeout: 180_000 },
99 );
100
101 test(
102 "serves the dynamic API route",
103 Effect.gen(function* () {
104 const url = yield* base;
105 const res = yield* getWhenReady(`${url}/api/hello`);
106 expect(res.status).toBe(200);
107 const body = (yield* res.json) as { hello: string };
108 expect(body.hello).toBe("world");
109 }),
110 { timeout: 180_000 },
111 );
112
113 test(
114 "compiles tailwind via postcss",
115 Effect.gen(function* () {
116 const url = yield* base;
117 // The page markup uses Tailwind utilities — proving the project's own
118 // postcss.config.mjs (@tailwindcss/postcss) ran inside `next build`.
119 const html = yield* getBodyWhenReady(url, "text-3xl");
120 expect(html).toContain("text-3xl");
121
122 // Next.js links the compiled stylesheet from under /_next/static/
123 // (chunks/*.css as of Next 16).
124 const match = html.match(/\/_next\/static\/[^"']+\.css/);
125 expect(match).not.toBeNull();
126
127 // The stylesheet must contain the compiled Tailwind rule, not just the
128 // class name in markup.
129 const css = yield* getBodyWhenReady(`${url}${match![0]}`, ".text-3xl");
130 expect(css).toContain(".text-3xl");
131 expect(css).toContain(".font-bold");
132 }),
133 { timeout: 180_000 },
134 );
135
136 test(
137 "serves a static asset from public/",
138 Effect.gen(function* () {
139 const url = yield* base;
140 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
141 expect(body).toContain("User-agent: *");
142 }),
143 { timeout: 180_000 },
144 );
145 }