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 Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import DynamicLoaderGetWorker from "./fixtures/dynamic-worker-loader/get-worker.ts"; |
| 10 | |
| 11 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 12 | // matching the process topology of the real `alchemy dev` command. |
| 13 | const { test } = Test.make({ |
| 14 | providers: Cloudflare.providers(), |
| 15 | dev: true, |
| 16 | }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 21 | status: number; |
| 22 | body: string; |
| 23 | }> {} |
| 24 | |
| 25 | const getJsonReady = (url: string) => |
| 26 | Effect.gen(function* () { |
| 27 | const client = yield* HttpClient.HttpClient; |
| 28 | const res = yield* client.get(url).pipe( |
| 29 | Effect.flatMap((res) => |
| 30 | res.status === 200 |
| 31 | ? Effect.succeed(res) |
| 32 | : res.text.pipe( |
| 33 | Effect.flatMap((body) => |
| 34 | Effect.fail(new WorkerNotReady({ status: res.status, body })), |
| 35 | ), |
| 36 | ), |
| 37 | ), |
| 38 | Effect.retry({ |
| 39 | while: (e): e is WorkerNotReady => e instanceof WorkerNotReady, |
| 40 | schedule: Schedule.max([ |
| 41 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 42 | Schedule.recurs(10), |
| 43 | ]), |
| 44 | }), |
| 45 | ); |
| 46 | return yield* res.json; |
| 47 | }).pipe(Effect.orDie); |
| 48 | |
| 49 | /** |
| 50 | * Deploys an Effect-native Worker that calls `loader.get()` then |
| 51 | * `worker.fetch()` against local workerd. Native get() returns a WorkerStub |
| 52 | * (fetcher is getEntrypoint()); wrapping it as a Fetcher made fetch throw |
| 53 | * (#1382). A second request with the same id must reuse the isolate. |
| 54 | */ |
| 55 | test.provider( |
| 56 | "local worker get() fetch works and reuses the isolate", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const deployed = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const worker = yield* DynamicLoaderGetWorker; |
| 64 | return { worker }; |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 69 | |
| 70 | const first = (yield* getJsonReady(`${deployed.worker.url}/?id=local-reuse`)) as { |
| 71 | id: string; |
| 72 | hits: number; |
| 73 | }; |
| 74 | expect(first).toEqual({ id: "local-reuse", hits: 1 }); |
| 75 | |
| 76 | const second = (yield* getJsonReady(`${deployed.worker.url}/?id=local-reuse`)) as { |
| 77 | id: string; |
| 78 | hits: number; |
| 79 | }; |
| 80 | expect(second).toEqual({ id: "local-reuse", hits: 2 }); |
| 81 | |
| 82 | const named = (yield* getJsonReady( |
| 83 | `${deployed.worker.url}/?id=local-named&entrypoint=1`, |
| 84 | )) as { id: string; hits: number }; |
| 85 | expect(named).toEqual({ id: "local-named", hits: 1 }); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | }).pipe(logLevel), |
| 89 | { |
| 90 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"], |
| 91 | timeout: 180_000, |
| 92 | }, |
| 93 | ); |