The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 * as pathe from "pathe"; |
| 10 | import EntrypointPropsWorker from "./fixtures/entrypoint-props/effect-worker.ts"; |
| 11 | import { AsyncEntrypointPropsWorker } from "./fixtures/entrypoint-props/stack.ts"; |
| 12 | import type { Api } from "./fixtures/worker-entrypoint-binding/entrypoint-target-worker.ts"; |
| 13 | |
| 14 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 15 | // matching the process topology of the real `alchemy dev` command. |
| 16 | const { test } = Test.make({ |
| 17 | providers: Cloudflare.providers(), |
| 18 | dev: true, |
| 19 | }); |
| 20 | |
| 21 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 22 | |
| 23 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 24 | status: number; |
| 25 | }> {} |
| 26 | |
| 27 | const getReady = (url: string) => |
| 28 | Effect.gen(function* () { |
| 29 | const client = yield* HttpClient.HttpClient; |
| 30 | return yield* client.get(url).pipe( |
| 31 | Effect.flatMap((res) => |
| 32 | res.status === 200 |
| 33 | ? Effect.succeed(res) |
| 34 | : Effect.fail(new WorkerNotReady({ status: res.status })), |
| 35 | ), |
| 36 | Effect.retry({ |
| 37 | while: (error) => error instanceof WorkerNotReady, |
| 38 | schedule: Schedule.exponential("250 millis"), |
| 39 | times: 20, |
| 40 | }), |
| 41 | ); |
| 42 | }).pipe(Effect.orDie); |
| 43 | |
| 44 | /** |
| 45 | * Under `alchemy dev` both Workers run on local workerd, and the |
| 46 | * `Cloudflare.WorkerEntrypoint` env entry lowers to a local service |
| 47 | * designator with `entrypoint` AND `props` — unlike live deploys, workerd |
| 48 | * delivers `ctx.props` today, so this test covers the full contract. |
| 49 | */ |
| 50 | test.provider( |
| 51 | "WorkerEntrypoint binding targets the named entrypoint and delivers ctx.props locally", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const deployed = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | const target = yield* Cloudflare.Worker("entrypoint-local-target", { |
| 59 | main: pathe.resolve( |
| 60 | import.meta.dirname, |
| 61 | "fixtures/worker-entrypoint-binding/entrypoint-target-worker.ts", |
| 62 | ), |
| 63 | env: { |
| 64 | SELF: Cloudflare.WorkerEntrypoint<Api>({ |
| 65 | entrypoint: "Api", |
| 66 | props: { tenant: "self" }, |
| 67 | }), |
| 68 | }, |
| 69 | }); |
| 70 | const caller = yield* Cloudflare.Worker("entrypoint-local-caller", { |
| 71 | main: pathe.resolve( |
| 72 | import.meta.dirname, |
| 73 | "fixtures/worker-entrypoint-binding/entrypoint-caller-worker.ts", |
| 74 | ), |
| 75 | env: { |
| 76 | API: Cloudflare.WorkerEntrypoint<Api>(target, { |
| 77 | entrypoint: "Api", |
| 78 | props: { tenant: "acme" }, |
| 79 | }), |
| 80 | }, |
| 81 | }); |
| 82 | return { target, caller }; |
| 83 | }), |
| 84 | ); |
| 85 | |
| 86 | // Local dev URLs — proof no cloud call ran. |
| 87 | expect(deployed.caller.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 88 | |
| 89 | const greeting = yield* getReady(`${deployed.caller.url}/greet?name=alice`).pipe( |
| 90 | Effect.flatMap((res) => res.text), |
| 91 | ); |
| 92 | expect(greeting).toBe("hello alice from Api"); |
| 93 | |
| 94 | const props = (yield* getReady(`${deployed.caller.url}/props`).pipe( |
| 95 | Effect.flatMap((res) => res.json), |
| 96 | )) as Record<string, unknown>; |
| 97 | expect(props).toEqual({ tenant: "acme" }); |
| 98 | |
| 99 | // The target's own `Api`, bound with `WorkerEntrypoint({ entrypoint, props })`. |
| 100 | const selfGreeting = yield* getReady(`${deployed.target.url}/self-greet?name=bob`).pipe( |
| 101 | Effect.flatMap((res) => res.text), |
| 102 | ); |
| 103 | expect(selfGreeting).toBe("hello bob from Api"); |
| 104 | |
| 105 | const selfProps = (yield* getReady(`${deployed.target.url}/self-props`).pipe( |
| 106 | Effect.flatMap((res) => res.json), |
| 107 | )) as Record<string, unknown>; |
| 108 | expect(selfProps).toEqual({ tenant: "self" }); |
| 109 | |
| 110 | yield* stack.destroy(); |
| 111 | }).pipe(logLevel), |
| 112 | { |
| 113 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"], |
| 114 | timeout: 120_000, |
| 115 | }, |
| 116 | ); |
| 117 | |
| 118 | /** GET a route once the freshly started workerd serves a 200, as text. */ |
| 119 | const getTextReady = (url: string) => |
| 120 | getReady(url).pipe( |
| 121 | Effect.flatMap((res) => res.text), |
| 122 | Effect.orDie, |
| 123 | ); |
| 124 | |
| 125 | /** |
| 126 | * Under `alchemy dev`, local workerd serves `ctx.props` and the |
| 127 | * `ctx.exports` loopback the same way the deployed Worker does. |
| 128 | */ |
| 129 | test.provider( |
| 130 | "local effect and async workers read ctx.props and mint self entrypoint stubs with props", |
| 131 | (stack) => |
| 132 | Effect.gen(function* () { |
| 133 | yield* stack.destroy(); |
| 134 | |
| 135 | const deployed = yield* stack.deploy(EntrypointPropsWorker); |
| 136 | |
| 137 | // Dev marker: served from the local dev proxy, no cloud deploy ran. |
| 138 | expect(deployed.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 139 | |
| 140 | const tenant = `props=${JSON.stringify({ tenant: "acme" })}`; |
| 141 | expect(yield* getTextReady(`${deployed.url}/props`)).toBe("props={}"); |
| 142 | expect(yield* getTextReady(`${deployed.url}/mint`)).toBe(`via-worker:${tenant}`); |
| 143 | expect(yield* getTextReady(`${deployed.url}/mint-do`)).toBe(`via-do:${tenant}`); |
| 144 | expect(yield* getTextReady(`${deployed.url}/missing`)).toBe( |
| 145 | "EntrypointUnavailableError:Missing", |
| 146 | ); |
| 147 | |
| 148 | yield* stack.destroy(); |
| 149 | |
| 150 | const asyncDeployed = yield* stack.deploy(AsyncEntrypointPropsWorker()); |
| 151 | expect(asyncDeployed.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 152 | expect(yield* getTextReady(`${asyncDeployed.url}/props`)).toBe("props={}"); |
| 153 | expect(yield* getTextReady(`${asyncDeployed.url}/mint`)).toBe(`via-worker:${tenant}`); |
| 154 | expect(yield* getTextReady(`${asyncDeployed.url}/mint-named`)).toBe(`via-named:${tenant}`); |
| 155 | expect(yield* getTextReady(`${asyncDeployed.url}/mint-do`)).toBe(`via-do:${tenant}`); |
| 156 | expect(yield* getTextReady(`${asyncDeployed.url}/missing`)).toBe("undefined:Missing"); |
| 157 | |
| 158 | yield* stack.destroy(); |
| 159 | }).pipe(logLevel), |
| 160 | { |
| 161 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"], |
| 162 | timeout: 180_000, |
| 163 | }, |
| 164 | ); |