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