The best way to control Claude Code, Codex and more from any computer or phone

main
ts 107 lines 4.06 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 "combines prerendered pages with request-time rendering",
66 Effect.gen(function* () {
67 const url = yield* base;
68 const home = yield* request(url + "/?count=7");
69 expect(home.status).toBe(200);
70 expect(home.body).toContain("Statically generated home");
71 expect(home.body).toContain("Count: 0");
72 const about = yield* request(url + "/about");
73 expect(about.status).toBe(200);
74 expect(about.body).toContain("Statically generated about page");
75 for (const count of [3, 7]) {
76 const counter = yield* request(url + `/counter?count=${count}`);
77 expect(counter.status).toBe(200);
78 expect(counter.body).toContain("Request-rendered counter");
79 expect(counter.body).toMatch(new RegExp(`id="count"[^>]*>${count}<`));
80 expect(counter.body).toContain("data-foldkit-flags");
81 expect(counter.body).toContain("data-foldkit-build");
82 expect(counter.headers.get("cache-control")).toBe("private, no-store");
83 }
84 expect((yield* requestOnce(url + "/unknown")).status).toBe(404);
85 }),
86 { timeout: 120_000 },
87 );
88
89 test(
90 "serves the generated JavaScript and Tailwind stylesheet",
91 Effect.gen(function* () {
92 const url = yield* base;
93 const page = yield* request(url);
94 const script = page.body.match(/<script[^>]+src="([^" ]+)"/);
95 const style = page.body.match(/<link[^>]+href="([^" ]+\.css)"/);
96 expect(script).not.toBeNull();
97 expect(style).not.toBeNull();
98 const javascript = yield* request(new URL(script![1]!, url).href);
99 expect(javascript.status).toBe(200);
100 expect(javascript.headers.get("content-type")).toContain("javascript");
101 const css = yield* request(new URL(style![1]!, url).href);
102 expect(css.status).toBe(200);
103 expect(css.headers.get("content-type")).toContain("text/css");
104 expect(css.body).toContain(".text-4xl");
105 }),
106 { timeout: 120_000 },
107 );