The best way to control Claude Code, Codex and more from any computer or phone
| 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("Server-rendered counter") |
| 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 | "renders request cookies and serializes hydration flags", |
| 61 | Effect.gen(function* () { |
| 62 | const url = yield* base; |
| 63 | for (const count of [0, 7]) { |
| 64 | const page = yield* request(url + "/counter", { |
| 65 | headers: { cookie: `count=${count}` }, |
| 66 | }); |
| 67 | expect(page.status).toBe(200); |
| 68 | expect(page.body).toContain(`<title>Count ${count}</title>`); |
| 69 | expect(page.body).toMatch(new RegExp(`id="count"[^>]*>${count}<`)); |
| 70 | expect(page.body).toContain("Rendered on the Server"); |
| 71 | expect(page.body).toContain("data-foldkit-flags"); |
| 72 | expect(page.body).toContain("data-foldkit-build"); |
| 73 | expect(page.headers.get("cache-control")).toBe("private, no-store"); |
| 74 | expect(page.headers.get("vary")).toContain("cookie"); |
| 75 | } |
| 76 | const head = yield* request(url, { method: "HEAD" }); |
| 77 | expect(head.status).toBe(200); |
| 78 | expect(head.body).toBe(""); |
| 79 | expect((yield* requestOnce(url + "/assets/missing.js")).status).toBe(404); |
| 80 | }), |
| 81 | { timeout: 120_000 }, |
| 82 | ); |
| 83 | |
| 84 | test( |
| 85 | "serves the generated JavaScript and Tailwind stylesheet", |
| 86 | Effect.gen(function* () { |
| 87 | const url = yield* base; |
| 88 | const page = yield* request(url); |
| 89 | const script = page.body.match(/<script[^>]+src="([^" ]+)"/); |
| 90 | const style = page.body.match(/<link[^>]+href="([^" ]+\.css)"/); |
| 91 | expect(script).not.toBeNull(); |
| 92 | expect(style).not.toBeNull(); |
| 93 | const javascript = yield* request(new URL(script![1]!, url).href); |
| 94 | expect(javascript.status).toBe(200); |
| 95 | expect(javascript.headers.get("content-type")).toContain("javascript"); |
| 96 | const css = yield* request(new URL(style![1]!, url).href); |
| 97 | expect(css.status).toBe(200); |
| 98 | expect(css.headers.get("content-type")).toContain("text/css"); |
| 99 | expect(css.body).toContain(".text-6xl"); |
| 100 | }), |
| 101 | { timeout: 120_000 }, |
| 102 | ); |