@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 92 lines 3.2 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 Effect from "effect/Effect";
5 import * as Schedule from "effect/Schedule";
6 import Stack from "../alchemy.run.ts";
7
8 const requestOnce = (url: string, init?: RequestInit) =>
9 Effect.tryPromise(async () => {
10 const response = await fetch(url, {
11 ...init,
12 signal: AbortSignal.timeout(5000),
13 });
14 return {
15 status: response.status,
16 headers: response.headers,
17 body: await response.text(),
18 };
19 });
20
21 // A fresh workers.dev URL can flap between serving and a 404 (or 5xx) while
22 // it propagates across the edge, even after a first successful response, so
23 // ride out those statuses. Assertions on a deliberate 404 use `requestOnce`.
24 const request = (url: string, init?: RequestInit) =>
25 requestOnce(url, init).pipe(
26 Effect.flatMap((response) =>
27 response.status === 404 || response.status >= 500
28 ? Effect.fail(new Error(`${url} answered ${response.status}`))
29 : Effect.succeed(response),
30 ),
31 Effect.retry({ schedule: Schedule.spaced("1 second"), times: 10 }),
32 );
33
34 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
35 providers: Cloudflare.providers(),
36 state: Cloudflare.state(),
37 });
38
39 const stack = beforeAll(
40 destroy(Stack).pipe(
41 Effect.andThen(deploy(Stack)),
42 Effect.tap(({ url }) =>
43 request(url!).pipe(
44 Effect.flatMap((response) =>
45 response.status === 200 && response.body.includes("<title>Foldkit App</title>")
46 ? Effect.void
47 : Effect.fail(new Error("The example has not reached the edge yet")),
48 ),
49 Effect.retry({ schedule: Schedule.spaced("1 second"), times: 8 }),
50 Effect.timeout("60 seconds"),
51 ),
52 ),
53 ),
54 { timeout: 120_000 },
55 );
56 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 120_000 });
57 const base = Effect.map(stack, ({ url }) => url!.replace(/\/+$/, ""));
58
59 test(
60 "serves the SPA shell on root and deep links",
61 Effect.gen(function* () {
62 const url = yield* base;
63 for (const path of ["/", "/counter/42"]) {
64 const page = yield* request(url + path);
65 expect(page.status).toBe(200);
66 expect(page.body).toContain('<div id="root"></div>');
67 expect(page.body).toContain("<title>Foldkit App</title>");
68 expect(page.body).not.toContain("data-foldkit-app");
69 }
70 }),
71 { timeout: 120_000 },
72 );
73
74 test(
75 "serves the generated JavaScript and Tailwind stylesheet",
76 Effect.gen(function* () {
77 const url = yield* base;
78 const page = yield* request(url);
79 const script = page.body.match(/<script[^>]+src="([^" ]+)"/);
80 const style = page.body.match(/<link[^>]+href="([^" ]+\.css)"/);
81 expect(script).not.toBeNull();
82 expect(style).not.toBeNull();
83 const javascript = yield* request(new URL(script![1]!, url).href);
84 expect(javascript.status).toBe(200);
85 expect(javascript.headers.get("content-type")).toContain("javascript");
86 const css = yield* request(new URL(style![1]!, url).href);
87 expect(css.status).toBe(200);
88 expect(css.headers.get("content-type")).toContain("text/css");
89 expect(css.body).toContain(".text-6xl");
90 }),
91 { timeout: 120_000 },
92 );