@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 111 lines 3.84 KB
Raw
1 import { expect } from "bun:test";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as Console from "effect/Console";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import Stack from "../alchemy.run.ts";
9
10 // Fresh `workers.dev` URLs transiently 404 while the route propagates.
11 // `Test.getWhenReady` fails on that cold-start window and retries until the
12 // worker serves a real response.
13 const { getWhenReady } = Test;
14
15 class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
16 body: string;
17 }> {}
18
19 // While the static-asset manifest is still propagating, Cloudflare can serve
20 // placeholder content with a 200 — the status alone can't distinguish
21 // "not yet" from "served", so retry until the body matches.
22 const getBodyWhenReady = (url: string, expected: string) =>
23 Effect.gen(function* () {
24 const res = yield* getWhenReady(url);
25 expect(res.status).toBe(200);
26 const body = yield* res.text;
27 if (!body.includes(expected)) {
28 return yield* Effect.fail(new AssetNotReady({ body }));
29 }
30 return body;
31 }).pipe(
32 Effect.retry({
33 while: (error) => error instanceof AssetNotReady,
34 schedule: Schedule.max([
35 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
36 Schedule.recurs(20),
37 ]),
38 }),
39 );
40
41 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
42 providers: Cloudflare.providers(),
43 state: Cloudflare.state(),
44 });
45
46 // The first deploy runs the full Vite build, so give the hook more headroom
47 // than the default 120s.
48 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
49 timeout: 600_000,
50 });
51 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
52
53 const base = Effect.map(stack, ({ url }) => {
54 if (!url) throw new Error("expected the site to expose a workers.dev url");
55 return url.replace(/\/+$/, "");
56 });
57
58 // Resolve an asset href from index.html against the site base.
59 const resolve = (baseUrl: string, href: string) =>
60 href.startsWith("http") ? href : `${baseUrl}${href.startsWith("/") ? "" : "/"}${href}`;
61
62 test(
63 "deploys and exposes a url",
64 Effect.gen(function* () {
65 const { url } = yield* stack;
66 expect(url).toBeString();
67 }),
68 { timeout: 180_000 },
69 );
70
71 test(
72 "serves the index HTML",
73 Effect.gen(function* () {
74 const url = yield* base;
75 const html = yield* getBodyWhenReady(url, '<div id="root">');
76 expect(html).toContain("<title>Vite on Cloudflare</title>");
77 }),
78 { timeout: 180_000 },
79 );
80
81 test(
82 "ships the React app in the client bundle",
83 Effect.gen(function* () {
84 const url = yield* base;
85 // Client-only SPA: the greeting and Card render in the browser, so
86 // assert on the built JS bundle Vite links from index.html.
87 const html = yield* getBodyWhenReady(url, '<script type="module"');
88 const script = html.match(/<script[^>]*type="module"[^>]*src="([^"]+)"/);
89 expect(script).not.toBeNull();
90 const js = yield* getBodyWhenReady(resolve(url, script![1]!), "Hello from Vite!");
91 expect(js).toContain("Hello from Vite!");
92 expect(js).toContain("Styled with Tailwind CSS");
93 }),
94 { timeout: 180_000 },
95 );
96
97 test(
98 "compiles tailwind from vite.config.ts",
99 Effect.gen(function* () {
100 const url = yield* base;
101 // Assert on the compiled stylesheet Vite links from index.html — the
102 // compiled rule for a utility used in src/main.tsx only exists if the
103 // @tailwindcss/vite plugin from the project's own vite.config.ts ran.
104 const html = yield* getBodyWhenReady(url, "stylesheet");
105 const link = html.match(/<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/);
106 expect(link).not.toBeNull();
107 const css = yield* getBodyWhenReady(resolve(url, link![1]!), ".text-3xl");
108 expect(css).toContain(".text-3xl");
109 }),
110 { timeout: 180_000 },
111 );