| 1 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 2 | import * as Test from "alchemy/Test/Bun"; |
| 3 | import { expect } from "bun:test"; |
| 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 serves a |
| 20 | // managed "content signals" robots.txt with a 200 — the status alone can't |
| 21 | // distinguish "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([ |
| 36 | Schedule.exponential("500 millis"), |
| 37 | Schedule.spaced("3 seconds"), |
| 38 | ]), |
| 39 | Schedule.recurs(20), |
| 40 | ]), |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 45 | providers: Cloudflare.providers(), |
| 46 | state: Cloudflare.state(), |
| 47 | }); |
| 48 | |
| 49 | // The first deploy runs the full RSC build, so give the hook more headroom |
| 50 | // than the default 120s. |
| 51 | const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), { |
| 52 | timeout: 600_000, |
| 53 | }); |
| 54 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 55 | |
| 56 | const base = Effect.map(stack, ({ url }) => { |
| 57 | if (!url) throw new Error("expected the site to expose a workers.dev url"); |
| 58 | return String(url).replace(/\/+$/, ""); |
| 59 | }); |
| 60 | |
| 61 | test( |
| 62 | "deploys and exposes a url", |
| 63 | Effect.gen(function* () { |
| 64 | const { url } = yield* stack; |
| 65 | expect(url).toBeString(); |
| 66 | }), |
| 67 | { timeout: 180_000 }, |
| 68 | ); |
| 69 | |
| 70 | test( |
| 71 | "serves the server-rendered home page", |
| 72 | Effect.gen(function* () { |
| 73 | const url = yield* base; |
| 74 | const res = yield* getWhenReady(url); |
| 75 | expect(res.status).toBe(200); |
| 76 | const html = yield* res.text; |
| 77 | // The `GREETING` env value from alchemy.run.ts, read via |
| 78 | // `process.env` in the server component — proves the Worker rendered |
| 79 | // it at request time. |
| 80 | expect(html).toContain("Hello from React Router on Cloudflare!"); |
| 81 | expect(html).toContain("Styled with Tailwind CSS"); |
| 82 | }), |
| 83 | { timeout: 180_000 }, |
| 84 | ); |
| 85 | |
| 86 | test( |
| 87 | "compiles tailwind from vite.config.ts", |
| 88 | Effect.gen(function* () { |
| 89 | const url = yield* base; |
| 90 | // The utility class in the markup proves the page shipped with Tailwind |
| 91 | // classes; wait until the deployed HTML includes it. |
| 92 | const html = yield* getBodyWhenReady(url, "text-3xl"); |
| 93 | |
| 94 | // The RSC plugin links the compiled stylesheet from the server-rendered |
| 95 | // document. The compiled rule for the utility only exists if the |
| 96 | // @tailwindcss/vite plugin from the project's own vite.config.ts ran. |
| 97 | const link = html.match( |
| 98 | /<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/, |
| 99 | ); |
| 100 | if (link) { |
| 101 | const href = link[1]!; |
| 102 | const cssUrl = href.startsWith("http") |
| 103 | ? href |
| 104 | : `${url}${href.startsWith("/") ? "" : "/"}${href}`; |
| 105 | const css = yield* getBodyWhenReady(cssUrl, ".text-3xl"); |
| 106 | expect(css).toContain(".text-3xl"); |
| 107 | } else { |
| 108 | const style = html.match(/<style[^>]*>([\s\S]*?)<\/style>/); |
| 109 | expect(style).not.toBeNull(); |
| 110 | expect(style![1]).toContain(".text-3xl"); |
| 111 | } |
| 112 | }), |
| 113 | { timeout: 180_000 }, |
| 114 | ); |
| 115 | |
| 116 | test( |
| 117 | "renders through the ssr environment's chunk", |
| 118 | Effect.gen(function* () { |
| 119 | const url = yield* base; |
| 120 | // /worker-render loads `worker-ssr` from the `ssr` environment via |
| 121 | // `import.meta.viteRsc.loadModule` — proves the `children: ["ssr"]` |
| 122 | // chunks resolve inside the deployed Worker. |
| 123 | const body = yield* getBodyWhenReady(`${url}/worker-render`, '"ok":true'); |
| 124 | expect(body).toContain("Worker render via the ssr environment."); |
| 125 | }), |
| 126 | { timeout: 180_000 }, |
| 127 | ); |
| 128 | |
| 129 | test( |
| 130 | "serves a static asset from public/", |
| 131 | Effect.gen(function* () { |
| 132 | const url = yield* base; |
| 133 | const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *"); |
| 134 | expect(body).toContain("User-agent: *"); |
| 135 | }), |
| 136 | { timeout: 180_000 }, |
| 137 | ); |