The best way to control Claude Code, Codex and more from any computer or phone

main
ts 122 lines 4.53 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 Console from "effect/Console";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as RpcClient from "effect/rpc/RpcClient";
8 import * as Schedule from "effect/Schedule";
9 import { CallerRpcs, TargetRpcs } from "./fixtures/rpc-worker-binding/group.ts";
10 import Stack from "./fixtures/rpc-worker-binding/stack.ts";
11
12 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
13 providers: Cloudflare.providers(),
14 });
15
16 // `Test.rpcClientLayer` guards the transport against edge-generated HTML
17 // bodies (workers.dev placeholder, error pages) that the RPC protocol would
18 // otherwise surface as an opaque `RpcClientDefect`; see Test/Http.ts.
19 const clientLayer = Test.rpcClientLayer;
20
21 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
22
23 // Cap exponential backoff at 3s so retries stay bounded when the CF edge is
24 // slow (otherwise the geometric blow-up dominates wall time).
25 const readinessSchedule = Schedule.min([
26 Schedule.exponential("500 millis"),
27 Schedule.spaced("3 seconds"),
28 ]);
29
30 // The caller worker forwards to the target via the service binding and wraps
31 // the call in `Effect.orDie` (see fixtures/caller-worker.ts). On a freshly
32 // deployed worker the service binding hasn't propagated to every Cloudflare
33 // edge yet, so the first calls fail with `Worker not found.` — which arrives
34 // at the client as a DEFECT, and `Effect.retry` does not retry defects.
35 // Promote defects to failures so the readiness retry can absorb the transient
36 // binding-propagation error (a genuine bug would simply keep failing until the
37 // retry budget is exhausted).
38 const retryReadyN =
39 (times: number) =>
40 <A, E, R>(eff: Effect.Effect<A, E, R>) =>
41 eff.pipe(
42 Effect.catchDefect((defect) => Effect.fail(defect)),
43 Effect.retry({ schedule: readinessSchedule, times }),
44 );
45
46 const stack = beforeAll(deploy(Stack));
47 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
48
49 test(
50 "RpcWorker: target worker exposes Greet",
51 Effect.gen(function* () {
52 const { targetUrl } = yield* stack;
53 yield* Effect.gen(function* () {
54 const client = yield* RpcClient.make(TargetRpcs);
55 const result = yield* client.Greet({ name: "world" }).pipe(
56 Effect.tapError(Console.log),
57 Effect.retry({
58 schedule: Schedule.exponential("500 millis"),
59 times: 5,
60 }),
61 );
62 expect(result.greeting).toBe("hello world");
63 }).pipe(Effect.scoped, Effect.provide(clientLayer(targetUrl)));
64 }).pipe(logLevel),
65 {
66 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
67 timeout: 60_000,
68 },
69 );
70
71 test(
72 "RpcWorker.bind: caller proxies through service binding to target",
73 Effect.gen(function* () {
74 const { callerUrl } = yield* stack;
75 yield* Effect.gen(function* () {
76 const client = yield* RpcClient.make(CallerRpcs);
77 const result = yield* client
78 .ProxyGreet({ name: "alchemy" })
79 .pipe(Effect.tapError(Console.log), retryReadyN(10));
80 expect(result.greeting).toBe("hello alchemy");
81 }).pipe(Effect.scoped, Effect.provide(clientLayer(callerUrl)));
82 }).pipe(logLevel),
83 {
84 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
85 timeout: 60_000,
86 },
87 );
88
89 test(
90 "RpcWorker.bind: 100 concurrent ProxyGreet calls do not hang",
91 Effect.gen(function* () {
92 const { callerUrl } = yield* stack;
93 yield* Effect.gen(function* () {
94 const client = yield* RpcClient.make(CallerRpcs);
95
96 // Warm the service binding by serially exercising it before the
97 // burst — workerd surfaces "Worker not found" defects until the
98 // target script is fully propagated to the same edge that the
99 // caller worker hits.
100 yield* client.ProxyGreet({ name: "warmup" }).pipe(retryReadyN(10));
101
102 const N = 100;
103 const results = yield* Effect.forEach(
104 Array.from({ length: N }, (_, i) => i),
105 (i) =>
106 client
107 .ProxyGreet({ name: `peer-${i}` })
108 .pipe(Effect.timeout("10 seconds"), retryReadyN(10)),
109 { concurrency: 32 },
110 );
111
112 expect(results).toHaveLength(N);
113 for (let i = 0; i < N; i++) {
114 expect(results[i].greeting).toBe(`hello peer-${i}`);
115 }
116 }).pipe(Effect.scoped, Effect.provide(clientLayer(callerUrl)));
117 }).pipe(logLevel),
118 {
119 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
120 timeout: 60_000,
121 },
122 );