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 Console from "effect/Console"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import Stack from "../alchemy.run.ts"; |
| 11 | |
| 12 | // Fresh `workers.dev` URLs transiently 404 while the route propagates. |
| 13 | // `HttpClient.execute`/`get` resolve successfully on that 404, so a plain |
| 14 | // `Effect.retry` never fires — these helpers fail on the cold-start window and |
| 15 | // retry until the real response (which may be 200/204/400) comes back. |
| 16 | const { executeWhenReady, getWhenReady } = Test; |
| 17 | |
| 18 | class AssetNotReady extends Data.TaggedError("AssetNotReady")<{ |
| 19 | body: string; |
| 20 | }> {} |
| 21 | |
| 22 | // While the static-asset manifest is still propagating, requests can serve a |
| 23 | // stale or placeholder body with a 200 — the status alone can't distinguish |
| 24 | // "not yet" from "served", so retry until the body matches. |
| 25 | const getBodyWhenReady = (url: string, expected: string) => |
| 26 | Effect.gen(function* () { |
| 27 | const res = yield* getWhenReady(url); |
| 28 | expect(res.status).toBe(200); |
| 29 | const body = yield* res.text; |
| 30 | if (!body.includes(expected)) { |
| 31 | return yield* Effect.fail(new AssetNotReady({ body })); |
| 32 | } |
| 33 | return body; |
| 34 | }).pipe( |
| 35 | Effect.retry({ |
| 36 | while: (error) => error instanceof AssetNotReady, |
| 37 | schedule: Schedule.max([ |
| 38 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]), |
| 39 | Schedule.recurs(20), |
| 40 | ]), |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | class NotYetVisible extends Data.TaggedError("NotYetVisible")<{}> {} |
| 45 | |
| 46 | // Right after a fresh deploy, a read that follows a successful write can |
| 47 | // briefly answer 404 (the object isn't visible to that read yet). Retry the |
| 48 | // read-back, bounded, until the written object comes back. |
| 49 | const getWhenWritten = (url: string) => |
| 50 | Effect.gen(function* () { |
| 51 | const client = yield* HttpClient.HttpClient; |
| 52 | const res = yield* client.get(url); |
| 53 | if (res.status === 404) return yield* new NotYetVisible(); |
| 54 | return res; |
| 55 | }).pipe( |
| 56 | Effect.retry({ |
| 57 | while: (error) => error._tag === "NotYetVisible", |
| 58 | schedule: Schedule.spaced("1 second"), |
| 59 | times: 10, |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 64 | providers: Cloudflare.providers(), |
| 65 | state: Cloudflare.state(), |
| 66 | }); |
| 67 | |
| 68 | const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), { |
| 69 | timeout: 240_000, |
| 70 | }); |
| 71 | afterAll( |
| 72 | Effect.gen(function* () { |
| 73 | if (!process.env.NO_DESTROY) { |
| 74 | yield* destroy(Stack); |
| 75 | } |
| 76 | }), |
| 77 | { timeout: 180_000 }, |
| 78 | ); |
| 79 | |
| 80 | const route = (url: string, params: Record<string, string>) => |
| 81 | `${url}/api/hello?${new URLSearchParams(params).toString()}`; |
| 82 | |
| 83 | // Stable per-option keys so re-runs (e.g. NO_DESTROY=1) overwrite cleanly |
| 84 | // instead of leaving stale objects behind. |
| 85 | const KEYS = { |
| 86 | binding: "integ:via-binding", |
| 87 | fetch: "integ:via-fetch", |
| 88 | rpc: "integ:via-rpc", |
| 89 | httpClient: "integ:via-http-client", |
| 90 | }; |
| 91 | |
| 92 | test( |
| 93 | "deploys and exposes a url", |
| 94 | Effect.gen(function* () { |
| 95 | const { websiteUrl } = yield* stack; |
| 96 | expect(websiteUrl).toBeString(); |
| 97 | }), |
| 98 | { timeout: 180_000 }, |
| 99 | ); |
| 100 | |
| 101 | test( |
| 102 | "serves the server-rendered home page", |
| 103 | Effect.gen(function* () { |
| 104 | const { websiteUrl } = yield* stack; |
| 105 | const base = websiteUrl.replace(/\/+$/, ""); |
| 106 | // The `GREETING` env value from alchemy.run.ts, read through the |
| 107 | // `cloudflare:workers` env proxy in the server function — proves the |
| 108 | // Worker rendered it at request time. |
| 109 | const html = yield* getBodyWhenReady(base, "Hello from TanStack Start on Cloudflare!"); |
| 110 | // The Card component rendered under the heading. |
| 111 | expect(html).toContain("Styled with Tailwind CSS"); |
| 112 | expect(html).toContain("This card is a React component styled with Tailwind utilities."); |
| 113 | }), |
| 114 | { timeout: 180_000 }, |
| 115 | ); |
| 116 | |
| 117 | test( |
| 118 | "compiles tailwind from vite.config.ts", |
| 119 | Effect.gen(function* () { |
| 120 | const { websiteUrl } = yield* stack; |
| 121 | const base = websiteUrl.replace(/\/+$/, ""); |
| 122 | const res = yield* getWhenReady(base); |
| 123 | expect(res.status).toBe(200); |
| 124 | const html = yield* res.text; |
| 125 | // The SSR'd markup uses Tailwind utilities... |
| 126 | expect(html).toContain("text-3xl"); |
| 127 | // ...and links the stylesheet Vite emitted via the project-owned |
| 128 | // vite.config.ts (the @tailwindcss/vite plugin), proving Alchemy loaded |
| 129 | // the config file natively instead of the programmatic fallback. |
| 130 | const match = html.match(/<link[^>]*href="([^"]+\.css[^"]*)"/); |
| 131 | expect(match).not.toBeNull(); |
| 132 | const href = match![1]!; |
| 133 | const cssUrl = href.startsWith("http") ? href : `${base}${href}`; |
| 134 | const css = yield* getBodyWhenReady(cssUrl, ".text-3xl"); |
| 135 | expect(css).toContain(".text-3xl"); |
| 136 | expect(css).toContain(".font-bold"); |
| 137 | }), |
| 138 | { timeout: 180_000 }, |
| 139 | ); |
| 140 | |
| 141 | test( |
| 142 | "option 1 — direct R2 binding round-trips through PUT and GET", |
| 143 | Effect.gen(function* () { |
| 144 | const { websiteUrl } = yield* stack; |
| 145 | const key = KEYS.binding; |
| 146 | |
| 147 | const put = yield* executeWhenReady( |
| 148 | HttpClientRequest.put(route(websiteUrl, { key, via: "binding" })).pipe( |
| 149 | HttpClientRequest.bodyText("hello-binding", "text/plain"), |
| 150 | ), |
| 151 | ); |
| 152 | expect(put.status).toBe(204); |
| 153 | |
| 154 | const get = yield* getWhenWritten(route(websiteUrl, { key, via: "binding" })); |
| 155 | expect(get.status).toBe(200); |
| 156 | expect(yield* get.text).toBe("hello-binding"); |
| 157 | }), |
| 158 | { timeout: 180_000 }, |
| 159 | ); |
| 160 | |
| 161 | test( |
| 162 | "option 2 — service-binding fetch into the Backend worker", |
| 163 | Effect.gen(function* () { |
| 164 | const { websiteUrl } = yield* stack; |
| 165 | const key = KEYS.fetch; |
| 166 | |
| 167 | // Write through option 2's PUT path (Backend's fetch handler stores it |
| 168 | // in R2), then read it back through option 2's GET (also Backend.fetch). |
| 169 | const put = yield* executeWhenReady( |
| 170 | HttpClientRequest.put(route(websiteUrl, { key, via: "fetch" })).pipe( |
| 171 | HttpClientRequest.bodyText("hello-fetch", "text/plain"), |
| 172 | ), |
| 173 | ); |
| 174 | expect(put.status).toBe(204); |
| 175 | |
| 176 | const get = yield* getWhenWritten(route(websiteUrl, { key, via: "fetch" })); |
| 177 | expect(get.status).toBe(200); |
| 178 | expect(yield* get.text).toBe("hello-fetch"); |
| 179 | }), |
| 180 | { timeout: 180_000 }, |
| 181 | ); |
| 182 | |
| 183 | test( |
| 184 | "option 3 — service-binding RPC method via toPromiseApi", |
| 185 | Effect.gen(function* () { |
| 186 | const { websiteUrl } = yield* stack; |
| 187 | const key = KEYS.rpc; |
| 188 | |
| 189 | // Seed the bucket via option 1 (direct binding) so the RPC `hello` |
| 190 | // method has something to read. |
| 191 | const seed = yield* executeWhenReady( |
| 192 | HttpClientRequest.put(route(websiteUrl, { key, via: "binding" })).pipe( |
| 193 | HttpClientRequest.bodyText("hello-rpc", "text/plain"), |
| 194 | ), |
| 195 | ); |
| 196 | expect(seed.status).toBe(204); |
| 197 | |
| 198 | // RPC GET reads through Backend.hello — exercises toPromiseApi. |
| 199 | const get = yield* getWhenWritten(route(websiteUrl, { key, via: "rpc" })); |
| 200 | expect(get.status).toBe(200); |
| 201 | expect(yield* get.text).toBe("hello-rpc"); |
| 202 | }), |
| 203 | { timeout: 180_000 }, |
| 204 | ); |
| 205 | |
| 206 | test( |
| 207 | "option 4 — service-binding HTTP client", |
| 208 | Effect.gen(function* () { |
| 209 | const { websiteUrl } = yield* stack; |
| 210 | const key = KEYS.httpClient; |
| 211 | |
| 212 | // Seed the bucket via option 1 (direct binding) so the RPC `hello` |
| 213 | // method has something to read. |
| 214 | const seed = yield* executeWhenReady( |
| 215 | HttpClientRequest.put(route(websiteUrl, { key, via: "http-client" })).pipe( |
| 216 | HttpClientRequest.bodyText("hello-http-client", "text/plain"), |
| 217 | ), |
| 218 | ); |
| 219 | expect(seed.status).toBe(204); |
| 220 | |
| 221 | // HTTP client GET reads through Backend.hello — exercises toPromiseApi. |
| 222 | const get = yield* getWhenWritten(route(websiteUrl, { key, via: "http-client" })); |
| 223 | expect(get.status).toBe(200); |
| 224 | expect(yield* get.text).toBe("hello-http-client"); |
| 225 | }), |
| 226 | { timeout: 180_000 }, |
| 227 | ); |
| 228 | |
| 229 | test( |
| 230 | "missing `key` returns 400", |
| 231 | Effect.gen(function* () { |
| 232 | const { websiteUrl } = yield* stack; |
| 233 | |
| 234 | // `400` is the real answer; `getWhenReady` only retries the propagation |
| 235 | // `404`/`5xx` window, so it returns the `400` as soon as the route is live. |
| 236 | const res = yield* getWhenReady(route(websiteUrl, { via: "binding" })); |
| 237 | expect(res.status).toBe(400); |
| 238 | }), |
| 239 | ); |
| 240 | |
| 241 | test( |
| 242 | "RPC for a non-existent key returns 404", |
| 243 | Effect.gen(function* () { |
| 244 | const { websiteUrl } = yield* stack; |
| 245 | const client = yield* HttpClient.HttpClient; |
| 246 | |
| 247 | const res = yield* client.get(route(websiteUrl, { key: "integ:does-not-exist", via: "rpc" })); |
| 248 | expect(res.status).toBe(404); |
| 249 | }), |
| 250 | ); |
| 251 | |
| 252 | test( |
| 253 | "PUT via=rpc returns 400 (RPC `hello` is read-only)", |
| 254 | Effect.gen(function* () { |
| 255 | const { websiteUrl } = yield* stack; |
| 256 | |
| 257 | const res = yield* executeWhenReady( |
| 258 | HttpClientRequest.put(route(websiteUrl, { key: "integ:via-options", via: "rpc" })).pipe( |
| 259 | HttpClientRequest.bodyText("nope"), |
| 260 | ), |
| 261 | ); |
| 262 | expect(res.status).toBe(400); |
| 263 | }), |
| 264 | ); |