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 * as pathe from "pathe"; |
| 10 | |
| 11 | // Mirrors the marker logged by `fixtures/tail/tail-producer.ts` on every |
| 12 | // request. Duplicated (not imported) because the fixture's default export |
| 13 | // must be its only export — extra named exports become workerd top-level |
| 14 | // exports and fail startup validation. |
| 15 | const TAIL_MARKER = "alchemy-local-tail-marker"; |
| 16 | |
| 17 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 18 | // matching the process topology of the real `alchemy dev` command. |
| 19 | const { test } = Test.make({ |
| 20 | providers: Cloudflare.providers(), |
| 21 | dev: true, |
| 22 | }); |
| 23 | |
| 24 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 25 | |
| 26 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 27 | status: number; |
| 28 | }> {} |
| 29 | |
| 30 | /** GET a route, retrying until the freshly started workerd serves a 200. */ |
| 31 | const getTextReady = (url: string) => |
| 32 | Effect.gen(function* () { |
| 33 | const client = yield* HttpClient.HttpClient; |
| 34 | const res = yield* client.get(url).pipe( |
| 35 | Effect.flatMap((res) => |
| 36 | res.status === 200 |
| 37 | ? Effect.succeed(res) |
| 38 | : Effect.fail(new WorkerNotReady({ status: res.status })), |
| 39 | ), |
| 40 | Effect.retry({ |
| 41 | while: (e): e is WorkerNotReady => e instanceof WorkerNotReady, |
| 42 | schedule: Schedule.max([ |
| 43 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 44 | Schedule.recurs(10), |
| 45 | ]), |
| 46 | }), |
| 47 | ); |
| 48 | return yield* res.text; |
| 49 | }).pipe(Effect.orDie); |
| 50 | |
| 51 | /** |
| 52 | * Under `alchemy dev` a Worker's `tailConsumers` lower into workerd's native |
| 53 | * `tails` service designators: each consumer script name resolves through |
| 54 | * the dev registry proxy — the same path cross-worker service bindings take — |
| 55 | * and the producer's trace events are delivered to the consumer's `tail()` |
| 56 | * handler after each response, exactly like the deployed `tail_consumers` |
| 57 | * setting. The consumer records each batch into a locally-emulated KV |
| 58 | * namespace and serves it back on `/events`, so this exercises the full |
| 59 | * local roundtrip with zero cloud calls. |
| 60 | */ |
| 61 | test.provider( |
| 62 | "local producer delivers trace events to its local tail consumer", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const deployed = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | const events = yield* Cloudflare.KV.Namespace("TailEvents"); |
| 70 | const consumer = yield* Cloudflare.Worker("TailConsumer", { |
| 71 | main: pathe.resolve(import.meta.dirname, "fixtures/tail/tail-consumer.ts"), |
| 72 | env: { EVENTS: events }, |
| 73 | }); |
| 74 | const producer = yield* Cloudflare.Worker("TailProducer", { |
| 75 | main: pathe.resolve(import.meta.dirname, "fixtures/tail/tail-producer.ts"), |
| 76 | tailConsumers: [consumer], |
| 77 | }); |
| 78 | return { events, consumer, producer }; |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | // Dev markers: the KV namespace is locally emulated (no cloud call |
| 83 | // ran), and both workers serve from the local dev proxy. |
| 84 | expect(deployed.events.namespaceId).toMatch(/^dev:/); |
| 85 | expect(deployed.consumer.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 86 | expect(deployed.producer.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 87 | |
| 88 | // Attribute parity with the live provider: the consumer is recorded |
| 89 | // by script name. |
| 90 | expect(deployed.producer.tailConsumers).toEqual([{ service: deployed.consumer.workerName }]); |
| 91 | |
| 92 | // Readiness probes for both freshly started workerds. |
| 93 | expect(yield* getTextReady(deployed.consumer.url!)).toBe("tail-consumer-ok"); |
| 94 | expect(yield* getTextReady(deployed.producer.url!)).toBe("producer-ok"); |
| 95 | |
| 96 | // Drive the producer and poll the consumer's read route until a trace |
| 97 | // batch carrying the producer's log marker arrives. Delivery is |
| 98 | // near-immediate once both are registered, but the producer's registry |
| 99 | // proxy may pick the consumer up asynchronously — each poll re-invokes |
| 100 | // the producer so early dropped events don't strand the test. |
| 101 | const client = yield* HttpClient.HttpClient; |
| 102 | const batches = yield* Effect.gen(function* () { |
| 103 | yield* client.get(deployed.producer.url!).pipe( |
| 104 | Effect.flatMap((res) => res.text), |
| 105 | Effect.orDie, |
| 106 | ); |
| 107 | const res = yield* client.get(`${deployed.consumer.url}/events`).pipe(Effect.orDie); |
| 108 | const body = (yield* res.json.pipe(Effect.orDie)) as { |
| 109 | batches?: unknown; |
| 110 | }; |
| 111 | return Array.isArray(body.batches) ? (body.batches as string[]) : []; |
| 112 | }).pipe( |
| 113 | Effect.repeat({ |
| 114 | schedule: Schedule.spaced("1 second"), |
| 115 | until: (batches): boolean => batches.some((batch) => batch.includes(TAIL_MARKER)), |
| 116 | times: 20, |
| 117 | }), |
| 118 | ); |
| 119 | |
| 120 | const batch = batches.find((batch) => batch.includes(TAIL_MARKER)); |
| 121 | expect(batch).toBeDefined(); |
| 122 | // The recorded batch is the JSON-serialized TraceItem array from the |
| 123 | // producer invocation — outcome + the console.log marker. |
| 124 | const items = JSON.parse(batch!) as { |
| 125 | outcome?: string; |
| 126 | logs?: { message?: unknown[] }[]; |
| 127 | }[]; |
| 128 | expect( |
| 129 | items.some((item) => item.logs?.some((log) => log.message?.includes(TAIL_MARKER))), |
| 130 | ).toBe(true); |
| 131 | |
| 132 | yield* stack.destroy(); |
| 133 | }).pipe(logLevel), |
| 134 | { |
| 135 | tags: ["provider:cloudflare", "provider:cloudflare:kv", "provider:cloudflare:worker", "local"], |
| 136 | timeout: 180_000, |
| 137 | }, |
| 138 | ); |