The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Alchemy from "alchemy"; |
| 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 { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as pathe from "pathe"; |
| 12 | |
| 13 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 14 | // matching the process topology of the real `alchemy dev` command. |
| 15 | const { test } = Test.make({ |
| 16 | providers: Cloudflare.providers(), |
| 17 | dev: true, |
| 18 | }); |
| 19 | |
| 20 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 21 | |
| 22 | /** 8x4 solid red PNG (checked-in fixture, generated once with Sharp). */ |
| 23 | const PNG_RED_8X4 = Uint8Array.from( |
| 24 | Buffer.from( |
| 25 | "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAECAIAAAA8r+mnAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAAEklEQVR4nGP4z8CAFWEXJUsCAFpeH+EeQoQoAAAAAElFTkSuQmCC", |
| 26 | "base64", |
| 27 | ), |
| 28 | ); |
| 29 | |
| 30 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 31 | status: number; |
| 32 | body: string; |
| 33 | }> {} |
| 34 | |
| 35 | /** |
| 36 | * POST bytes to a fixture route, retrying until the worker serves a 200 |
| 37 | * (fresh dev workers can briefly 503 while workerd boots). Non-200s carry the |
| 38 | * body so a persistent fixture error surfaces. |
| 39 | */ |
| 40 | const postBytesReady = (url: string, bytes: Uint8Array) => |
| 41 | Effect.gen(function* () { |
| 42 | const client = yield* HttpClient.HttpClient; |
| 43 | return yield* client |
| 44 | .execute(HttpClientRequest.post(url).pipe(HttpClientRequest.bodyUint8Array(bytes))) |
| 45 | .pipe( |
| 46 | Effect.flatMap((res) => |
| 47 | res.status === 200 |
| 48 | ? Effect.succeed(res) |
| 49 | : res.text.pipe( |
| 50 | Effect.flatMap((body) => |
| 51 | Effect.fail(new WorkerNotReady({ status: res.status, body })), |
| 52 | ), |
| 53 | ), |
| 54 | ), |
| 55 | Effect.retry({ |
| 56 | while: (e): e is WorkerNotReady => e instanceof WorkerNotReady, |
| 57 | schedule: Schedule.max([ |
| 58 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 59 | Schedule.recurs(10), |
| 60 | ]), |
| 61 | }), |
| 62 | ); |
| 63 | }).pipe(Effect.orDie); |
| 64 | |
| 65 | const fixtureMain = pathe.resolve(import.meta.dirname, "fixtures/local-worker.ts"); |
| 66 | |
| 67 | interface InfoResponse { |
| 68 | format: string; |
| 69 | fileSize: number; |
| 70 | width: number; |
| 71 | height: number; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Under `alchemy dev` the `images` binding is emulated by cloudflare-runtime: |
| 76 | * the wrapped `cloudflare-internal:images-api` module targets a local service |
| 77 | * that runs pixel work (resize/rotate/transcode) via Sharp on this machine — |
| 78 | * no Cloudflare credentials or preview session are involved for the binding. |
| 79 | */ |
| 80 | test.provider( |
| 81 | "images binding round-trips against the local Sharp simulator", |
| 82 | (stack) => |
| 83 | Effect.gen(function* () { |
| 84 | yield* stack.destroy(); |
| 85 | |
| 86 | const deployed = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | const worker = yield* Cloudflare.Worker("images-local-worker", { |
| 89 | main: fixtureMain, |
| 90 | env: { IMAGES: Cloudflare.Images.Images("IMAGES") }, |
| 91 | }); |
| 92 | return { url: worker.url.as<string>() }; |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | // Served from the local dev proxy — proof the worker runs locally. |
| 97 | expect(deployed.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 98 | |
| 99 | // info() reads format and dimensions through the local simulator. |
| 100 | const infoRes = yield* postBytesReady(`${deployed.url}/info`, PNG_RED_8X4); |
| 101 | const info = (yield* infoRes.json) as unknown as InfoResponse; |
| 102 | expect(info.format).toBe("image/png"); |
| 103 | expect(info.width).toBe(8); |
| 104 | expect(info.height).toBe(4); |
| 105 | |
| 106 | // input().transform({ width }).output() resizes via Sharp; the output |
| 107 | // is a real PNG (magic bytes)... |
| 108 | const transformRes = yield* postBytesReady( |
| 109 | `${deployed.url}/transform?width=4&format=image/png`, |
| 110 | PNG_RED_8X4, |
| 111 | ); |
| 112 | expect(transformRes.headers["content-type"]).toBe("image/png"); |
| 113 | const outputBytes = new Uint8Array(yield* transformRes.arrayBuffer); |
| 114 | expect(Array.from(outputBytes.slice(0, 4))).toEqual([0x89, 0x50, 0x4e, 0x47]); |
| 115 | |
| 116 | // ...and info() on the output confirms the resize (contain-fit keeps |
| 117 | // the 2:1 aspect ratio: 8x4 -> 4x2). |
| 118 | const outputInfoRes = yield* postBytesReady(`${deployed.url}/info`, outputBytes); |
| 119 | const outputInfo = (yield* outputInfoRes.json) as unknown as InfoResponse; |
| 120 | expect(outputInfo.format).toBe("image/png"); |
| 121 | expect(outputInfo.width).toBe(4); |
| 122 | expect(outputInfo.height).toBe(2); |
| 123 | |
| 124 | // GIF output surfaces the documented local-mode 415 (code 9520). |
| 125 | const gifRes = yield* postBytesReady( |
| 126 | `${deployed.url}/transform?format=image/gif`, |
| 127 | PNG_RED_8X4, |
| 128 | ); |
| 129 | const gifBody = (yield* gifRes.json) as { |
| 130 | error: boolean; |
| 131 | code: number; |
| 132 | message: string; |
| 133 | }; |
| 134 | expect(gifBody.error).toBe(true); |
| 135 | expect(gifBody.code).toBe(9520); |
| 136 | expect(gifBody.message).toContain("GIF output is not supported in local mode"); |
| 137 | |
| 138 | yield* stack.destroy(); |
| 139 | }).pipe(logLevel), |
| 140 | { |
| 141 | tags: [ |
| 142 | "provider:cloudflare", |
| 143 | "provider:cloudflare:images", |
| 144 | "provider:cloudflare:worker", |
| 145 | "local", |
| 146 | ], |
| 147 | timeout: 180_000, |
| 148 | }, |
| 149 | ); |
| 150 | |
| 151 | /** |
| 152 | * `Alchemy.remote()` opts the binding OUT of local emulation: the |
| 153 | * locally-served worker drives the real Images service through the |
| 154 | * remote-bindings preview session. The live suite |
| 155 | * (test/Cloudflare/Images/Images.test.ts) covers deployed-binding behavior; |
| 156 | * this leg pins the dev-mode remote lowering path. |
| 157 | */ |
| 158 | test.provider( |
| 159 | "Alchemy.remote() proxies the binding to the real Images service", |
| 160 | (stack) => |
| 161 | Effect.gen(function* () { |
| 162 | yield* stack.destroy(); |
| 163 | |
| 164 | const deployed = yield* stack.deploy( |
| 165 | Effect.gen(function* () { |
| 166 | const worker = yield* Cloudflare.Worker("images-remote-worker", { |
| 167 | main: fixtureMain, |
| 168 | env: { |
| 169 | IMAGES: Cloudflare.Images.Images("IMAGES").pipe(Alchemy.remote()), |
| 170 | }, |
| 171 | }); |
| 172 | return { url: worker.url.as<string>() }; |
| 173 | }), |
| 174 | ); |
| 175 | |
| 176 | // Still served locally — only the binding is remote. |
| 177 | expect(deployed.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 178 | |
| 179 | const infoRes = yield* postBytesReady(`${deployed.url}/info`, PNG_RED_8X4); |
| 180 | const info = (yield* infoRes.json) as unknown as InfoResponse; |
| 181 | expect(info.format).toBe("image/png"); |
| 182 | expect(info.width).toBe(8); |
| 183 | expect(info.height).toBe(4); |
| 184 | |
| 185 | yield* stack.destroy(); |
| 186 | }).pipe(logLevel), |
| 187 | { |
| 188 | tags: [ |
| 189 | "provider:cloudflare", |
| 190 | "provider:cloudflare:images", |
| 191 | "provider:cloudflare:worker", |
| 192 | "live", |
| 193 | ], |
| 194 | timeout: 180_000, |
| 195 | }, |
| 196 | ); |