@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 100 lines 3.6 KB
Raw
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 FetchCallerWorker from "./fixtures/fetch-binding/fetch-caller.ts";
10 import FetchTargetWorker from "./fixtures/fetch-binding/fetch-target.ts";
11
12 // `dev: true` runs local providers behind the RPC sidecar proxy by default,
13 // matching the process topology of the real `alchemy dev` command.
14 const { test } = Test.make({
15 providers: Cloudflare.providers(),
16 dev: true,
17 });
18
19 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
20
21 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
22 status: number;
23 body: string;
24 }> {}
25
26 /**
27 * GET a route, retrying until the freshly started workerd serves a 200 —
28 * including 500s from the caller while the target's dev-registry entry is
29 * still propagating to the caller's service-binding proxy.
30 */
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 : res.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(new WorkerNotReady({ status: res.status, body })),
41 ),
42 ),
43 ),
44 Effect.retry({
45 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
46 schedule: Schedule.max([
47 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
48 Schedule.recurs(10),
49 ]),
50 }),
51 );
52 return yield* res.text;
53 }).pipe(Effect.orDie);
54
55 /**
56 * Pins the `Cloudflare.Workers.Fetch` capability: the caller registers a
57 * service binding for the *target* worker (named by the target's LogicalId,
58 * pointing at the target's workerName) and fetches through it with the
59 * HttpClient-shaped runtime client. Under `alchemy dev` the binding resolves
60 * through the dev registry proxy between two locally-running workerds, so a
61 * green roundtrip proves both the deploy-time registration and the
62 * `env[worker.LogicalId]` runtime lookup line up — the exact pair the
63 * self-binding bug broke.
64 */
65 test.provider(
66 "local caller fetches local target through the Fetch service binding",
67 (stack) =>
68 Effect.gen(function* () {
69 yield* stack.destroy();
70
71 const deployed = yield* stack.deploy(
72 Effect.gen(function* () {
73 const target = yield* FetchTargetWorker;
74 const caller = yield* FetchCallerWorker;
75 return { target, caller };
76 }),
77 );
78
79 // Dev markers: both workers serve from the local dev proxy — no cloud
80 // deploy ran.
81 expect(deployed.target.url).toMatch(/^http:\/\/localhost:\d+$/);
82 expect(deployed.caller.url).toMatch(/^http:\/\/localhost:\d+$/);
83
84 // The target answers directly.
85 expect(yield* getTextReady(`${deployed.target.url}/?name=direct`)).toBe(
86 "fetch-binding-target: hello direct",
87 );
88
89 // The roundtrip through the caller's Fetch service binding.
90 expect(yield* getTextReady(`${deployed.caller.url}/?name=alice`)).toBe(
91 "caller saw: fetch-binding-target: hello alice",
92 );
93
94 yield* stack.destroy();
95 }).pipe(logLevel),
96 {
97 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
98 timeout: 180_000,
99 },
100 );