@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 104 lines 3.76 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 // The Worker reaches the edge before its assets do: wait for both the
43 // hydrating home page and a second prerendered route.
44 Effect.tap(({ url }) =>
45 Effect.all([request(url!), request(url!.replace(/\/+$/, "") + "/about")]).pipe(
46 Effect.flatMap(([home, about]) =>
47 home.status === 200 &&
48 home.body.includes("Statically generated home") &&
49 home.body.includes("<script") &&
50 about.status === 200
51 ? Effect.void
52 : Effect.fail(new Error("The example has not reached the edge yet")),
53 ),
54 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 25 }),
55 Effect.timeout("60 seconds"),
56 ),
57 ),
58 ),
59 { timeout: 120_000 },
60 );
61 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 120_000 });
62 const base = Effect.map(stack, ({ url }) => url!.replace(/\/+$/, ""));
63
64 test(
65 "serves prerendered routes and hydration data",
66 Effect.gen(function* () {
67 const url = yield* base;
68 for (const [path, heading] of [
69 ["/", "Statically generated home"],
70 ["/about", "Statically generated about page"],
71 ]) {
72 const page = yield* request(url + path + "?count=7");
73 expect(page.status).toBe(200);
74 expect(page.body).toContain(heading!);
75 expect(page.body).toContain("data-foldkit-app");
76 expect(page.body).toContain("data-foldkit-build");
77 const head = yield* request(url + path, { method: "HEAD" });
78 expect(head.status).toBe(200);
79 expect(head.body).toBe("");
80 }
81 expect((yield* requestOnce(url + "/not-prerendered")).status).toBe(404);
82 }),
83 { timeout: 120_000 },
84 );
85
86 test(
87 "serves the generated JavaScript and Tailwind stylesheet",
88 Effect.gen(function* () {
89 const url = yield* base;
90 const page = yield* request(url);
91 const script = page.body.match(/<script[^>]+src="([^" ]+)"/);
92 const style = page.body.match(/<link[^>]+href="([^" ]+\.css)"/);
93 expect(script).not.toBeNull();
94 expect(style).not.toBeNull();
95 const javascript = yield* request(new URL(script![1]!, url).href);
96 expect(javascript.status).toBe(200);
97 expect(javascript.headers.get("content-type")).toContain("javascript");
98 const css = yield* request(new URL(style![1]!, url).href);
99 expect(css.status).toBe(200);
100 expect(css.headers.get("content-type")).toContain("text/css");
101 expect(css.body).toContain(".text-4xl");
102 }),
103 { timeout: 120_000 },
104 );