@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 395 lines 14.9 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 * as Stream from "effect/Stream";
10 import { WorkerRpcs } from "./fixtures/rpc-http/group.ts";
11 import Stack from "./fixtures/rpc-http/stack.ts";
12
13 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
14 providers: Cloudflare.providers(),
15 });
16
17 // `Test.rpcClientLayer` guards the transport against edge-generated HTML
18 // bodies (the workers.dev placeholder — which serves with HTTP 200 —
19 // 1101/1102 error pages, 429/1015 rate limits): the effect RPC HTTP protocol
20 // never inspects status or content-type, so those would otherwise surface as
21 // an opaque `RpcClientDefect: Error decoding HTTP response`. Non-ndjson
22 // responses fail typed (status + body snippet) and are retried at the
23 // transport level, so every RPC call in this file rides out edge-propagation
24 // windows with one shared budget.
25 const clientLayer = Test.rpcClientLayer;
26
27 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
28
29 // Cap exponential backoff at 3s so readiness retries poll densely instead of
30 // sleeping tens of seconds past the propagation window (an uncapped
31 // exponential blows through the 30s test timeouts after ~6 attempts).
32 const readinessSchedule = Schedule.min([
33 Schedule.exponential("500 millis"),
34 Schedule.spaced("3 seconds"),
35 ]);
36
37 // The worker fixture wraps the DO calls in `Effect.orDie` / `Stream.orDie`,
38 // so a transient `Worker not found.` (the worker→DO namespace binding hasn't
39 // propagated to every Cloudflare edge yet) can arrive at the client as a
40 // DEFECT — and `Effect.retry` does not retry defects. Promote defects to
41 // failures so the readiness retry can absorb the propagation window (a
42 // genuine bug simply keeps failing until the retry budget is exhausted).
43 const retryReadyN =
44 (times: number) =>
45 <A, E, R>(eff: Effect.Effect<A, E, R>) =>
46 eff.pipe(
47 Effect.catchDefect((defect) => Effect.fail(defect)),
48 Effect.retry({ schedule: readinessSchedule, times }),
49 );
50
51 const stack = beforeAll(
52 deploy(Stack).pipe(
53 // Ping the Worker to ensure it's ready.
54 // Subsequent calls should succeed without retries.
55 Effect.tap(({ url }) =>
56 Effect.gen(function* () {
57 const client = yield* RpcClient.make(WorkerRpcs);
58 const result = yield* client
59 .Ping({ message: "warmup" })
60 .pipe(Effect.tapError(Console.log), retryReadyN(20));
61 expect(result.echo).toBe("warmup");
62 expect(result.n).toBeGreaterThan(0);
63 }).pipe(Effect.scoped, Effect.provide(clientLayer(url))),
64 ),
65 // Gate on the worker→DO pathway too: under full-suite parallel load the
66 // DO namespace binding propagates noticeably slower than the worker
67 // itself, and the `*DO` tests below would otherwise race that window.
68 // While the binding is still propagating, the worker's internal DO client
69 // fails with an `HttpError` that the fixture's `Effect.orDie` turns into a
70 // server-sent Defect — so this gate has to both promote defects AND carry
71 // enough budget (~2 min of capped backoff) to outlast the slow windows a
72 // full-suite run produces; a 15-attempt/~40s budget was observed to
73 // exhaust and fail the whole file.
74 Effect.tap(({ url }) =>
75 Effect.gen(function* () {
76 const client = yield* RpcClient.make(WorkerRpcs);
77 yield* client.PingDO({ message: "warmup" }).pipe(retryReadyN(40));
78 yield* client.CountDO({ upto: 1 }).pipe(Stream.runCollect, retryReadyN(40));
79 }).pipe(Effect.scoped, Effect.provide(clientLayer(url))),
80 ),
81 // Let edge propagation settle before the (mostly un-retried) bodies run.
82 Effect.tap(() => Effect.sleep("5 seconds")),
83 ),
84 { timeout: 300_000 },
85 );
86 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
87
88 // The Cloudflare Worker fetch adapter (`workersHttpHandler`) currently
89 // short-circuits Effect's standard HTTP lifecycle (it manually
90 // provides `HttpServerRequest` and converts the response to a web
91 // `Response` outside of `HttpEffect.toHandled`). PR #328 reported that
92 // this can deadlock `RpcServer.toHttpEffect` under workerd. This test
93 // hammers a real deployed Worker exposing an Effect RPC group to
94 // surface lifecycle / per-request scope regressions.
95 //
96 // The `*DO` variants exercise the DO fetch pathway
97 // (`DurableObjectBridge.fetch` -> `makeRequestEffect`) via an
98 // `RpcClient` constructed inside the Worker handler whose transport
99 // is `Cloudflare.toHttpClient(rpcDO.getByName(...))`. This mirrors the
100 // HttpApi fixture's `getTaskDO` pattern.
101 test(
102 "RpcServer.toHttpEffect: unary RPC response",
103 Effect.gen(function* () {
104 const { url } = yield* stack;
105 yield* Effect.log("url:", url);
106
107 yield* Effect.gen(function* () {
108 const client = yield* RpcClient.make(WorkerRpcs);
109 // Even after the warmup gate, a fresh request can land on a PoP that
110 // hasn't resolved the script yet and get Cloudflare's HTML error page,
111 // which is not valid ndjson (`RpcClientDefect`). `Ping` is idempotent,
112 // so retry through a bounded schedule.
113 const result = yield* client.Ping({ message: "hello" }).pipe(
114 Effect.tapError(Console.log),
115 Effect.retry({
116 schedule: Schedule.min([
117 Schedule.exponential("500 millis"),
118 Schedule.spaced("2 seconds"),
119 ]),
120 times: 10,
121 }),
122 );
123 expect(result.echo).toBe("hello");
124 expect(result.n).toBeGreaterThan(0);
125 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
126 }).pipe(logLevel),
127 {
128 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
129 timeout: 30_000,
130 },
131 );
132
133 test(
134 "RpcServer.toHttpEffect: streaming RPC response",
135 Effect.gen(function* () {
136 const { url } = yield* stack;
137
138 yield* Effect.gen(function* () {
139 const client = yield* RpcClient.make(WorkerRpcs);
140 // First streaming call can race edge propagation and hit a Cloudflare
141 // HTML error page; retry the whole collect through a bounded schedule.
142 const values = yield* client.Count({ upto: 5 }).pipe(
143 Stream.runCollect,
144 Effect.retry({
145 schedule: Schedule.exponential("500 millis"),
146 times: 10,
147 }),
148 );
149 expect(values).toEqual([1, 2, 3, 4, 5]);
150 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
151 }).pipe(logLevel),
152 {
153 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
154 timeout: 30_000,
155 },
156 );
157
158 test(
159 "RpcServer.toHttpEffect: array payload streams response items in order",
160 Effect.gen(function* () {
161 const { url } = yield* stack;
162
163 yield* Effect.gen(function* () {
164 const client = yield* RpcClient.make(WorkerRpcs);
165 const messages = ["a", "b", "c", "d"];
166 // First streaming call can race edge propagation and hit a Cloudflare
167 // HTML error page; retry the whole collect through a bounded schedule.
168 const values = yield* client.Echo({ messages }).pipe(
169 Stream.runCollect,
170 Effect.retry({
171 schedule: Schedule.exponential("500 millis"),
172 times: 10,
173 }),
174 );
175 expect(values).toEqual(messages.map((message, index) => ({ index, message })));
176 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
177 }).pipe(logLevel),
178 {
179 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
180 timeout: 30_000,
181 },
182 );
183
184 test(
185 "RpcServer.toHttpEffect: 200 concurrent unary calls do not hang",
186 Effect.gen(function* () {
187 const { url } = yield* stack;
188
189 yield* Effect.gen(function* () {
190 const client = yield* RpcClient.make(WorkerRpcs);
191
192 const N = 200;
193 const results = yield* Effect.forEach(
194 Array.from({ length: N }, (_, i) => i),
195 (i) =>
196 client.Ping({ message: `m-${i}` }).pipe(
197 Effect.timeout("5 seconds"),
198 // 64-way fan-out opens many fresh connections, each of which can
199 // land on an edge host still serving an HTML page after the
200 // single-connection warmup succeeded. The transport guard retries
201 // those; this outer budget (matching the siblings' capped
202 // generosity) backstops timeouts and longer bursts — the previous
203 // uncapped `times: 3` (~3.5s window) was the flake.
204 Effect.retry({
205 schedule: Schedule.min([
206 Schedule.exponential("500 millis"),
207 Schedule.spaced("2 seconds"),
208 ]),
209 times: 10,
210 }),
211 ),
212 { concurrency: 64 },
213 );
214
215 expect(results).toHaveLength(N);
216 for (let i = 0; i < N; i++) {
217 expect(results[i].echo).toBe(`m-${i}`);
218 }
219 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
220 }).pipe(logLevel),
221 {
222 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
223 timeout: 60_000,
224 },
225 );
226
227 test(
228 "RpcServer.toHttpEffect: concurrent streaming calls do not hang",
229 Effect.gen(function* () {
230 const { url } = yield* stack;
231
232 yield* Effect.gen(function* () {
233 const client = yield* RpcClient.make(WorkerRpcs);
234
235 const N = 64;
236 const results = yield* Effect.forEach(
237 Array.from({ length: N }, (_, i) => i),
238 (i) =>
239 client.Count({ upto: 3 + (i % 3) }).pipe(
240 Stream.runCollect,
241 Effect.timeout("5 seconds"),
242 // 64 concurrent first-streams all race cold-start: a PoP that
243 // hasn't resolved the script yet returns Cloudflare's HTML error
244 // page, which is not valid ndjson and surfaces as an
245 // `RpcClientDefect` ("Error decoding HTTP response"). Match the
246 // single-stream tests' generosity (capped backoff, ~10 attempts)
247 // so the whole fan-out rides out propagation.
248 Effect.retry({
249 schedule: Schedule.min([
250 Schedule.exponential("500 millis"),
251 Schedule.spaced("2 seconds"),
252 ]),
253 times: 10,
254 }),
255 ),
256 { concurrency: N },
257 );
258
259 expect(results).toHaveLength(N);
260 for (let i = 0; i < N; i++) {
261 expect(results[i]).toEqual(Array.from({ length: 3 + (i % 3) }, (_, n) => n + 1));
262 }
263 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
264 }).pipe(logLevel),
265 {
266 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
267 timeout: 60_000,
268 },
269 );
270
271 // === Durable Object pathway ===
272 // These exercise the Worker's `*DO` handlers, which proxy through an
273 // `RpcClient` whose transport is `Cloudflare.toHttpClient(rpcDO.getByName(...))`.
274
275 test(
276 "RpcServer.toHttpEffect Durable Object unary RPC response",
277 Effect.gen(function* () {
278 const { url } = yield* stack;
279
280 yield* Effect.gen(function* () {
281 const client = yield* RpcClient.make(WorkerRpcs);
282 // First DO call can race edge propagation and hit a Cloudflare HTML
283 // error page (or a `Worker not found.` defect); retry through a
284 // bounded, defect-promoting schedule.
285 const result = yield* client
286 .PingDO({ message: "hello-do" })
287 .pipe(Effect.tapError(Console.log), retryReadyN(10));
288 expect(result.echo).toBe("hello-do");
289 expect(result.n).toBeGreaterThan(0);
290 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
291 }).pipe(logLevel),
292 {
293 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
294 timeout: 30_000,
295 },
296 );
297
298 test(
299 "RpcServer.toHttpEffect Durable Object streaming RPC response",
300 Effect.gen(function* () {
301 const { url } = yield* stack;
302
303 yield* Effect.gen(function* () {
304 const client = yield* RpcClient.make(WorkerRpcs);
305 // First DO streaming call can race edge propagation and hit a Cloudflare
306 // HTML error page (or a `Worker not found.` defect); retry the whole
307 // collect through a bounded, defect-promoting schedule.
308 const values = yield* client.CountDO({ upto: 5 }).pipe(Stream.runCollect, retryReadyN(10));
309 expect(values).toEqual([1, 2, 3, 4, 5]);
310 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
311 }).pipe(logLevel),
312 {
313 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
314 timeout: 30_000,
315 },
316 );
317
318 test(
319 "RpcServer.toHttpEffect Durable Object array payload streams response items in order",
320 Effect.gen(function* () {
321 const { url } = yield* stack;
322
323 yield* Effect.gen(function* () {
324 const client = yield* RpcClient.make(WorkerRpcs);
325 const messages = ["a", "b", "c", "d"];
326 // First streaming call can race edge propagation and hit a Cloudflare
327 // HTML error page (or a `Worker not found.` defect); retry the whole
328 // collect through a bounded, defect-promoting schedule.
329 const values = yield* client.EchoDO({ messages }).pipe(Stream.runCollect, retryReadyN(10));
330 expect(values).toEqual(messages.map((message, index) => ({ index, message })));
331 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
332 }).pipe(logLevel),
333 {
334 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
335 timeout: 30_000,
336 },
337 );
338
339 test(
340 "RpcServer.toHttpEffect Durable Object concurrent unary calls do not hang",
341 Effect.gen(function* () {
342 const { url } = yield* stack;
343
344 yield* Effect.gen(function* () {
345 const client = yield* RpcClient.make(WorkerRpcs);
346
347 const N = 64;
348 const results = yield* Effect.forEach(
349 Array.from({ length: N }, (_, i) => i),
350 (i) =>
351 client.PingDO({ message: `m-${i}` }).pipe(Effect.timeout("10 seconds"), retryReadyN(5)),
352 { concurrency: 16 },
353 );
354
355 expect(results).toHaveLength(N);
356 for (let i = 0; i < N; i++) {
357 expect(results[i].echo).toBe(`m-${i}`);
358 }
359 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
360 }).pipe(logLevel),
361 {
362 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
363 timeout: 60_000,
364 },
365 );
366
367 test(
368 "RpcServer.toHttpEffect Durable Object concurrent streaming calls do not hang",
369 Effect.gen(function* () {
370 const { url } = yield* stack;
371
372 yield* Effect.gen(function* () {
373 const client = yield* RpcClient.make(WorkerRpcs);
374
375 const N = 32;
376 const results = yield* Effect.forEach(
377 Array.from({ length: N }, (_, i) => i),
378 (i) =>
379 client
380 .CountDO({ upto: 3 + (i % 3) })
381 .pipe(Stream.runCollect, Effect.timeout("10 seconds"), retryReadyN(5)),
382 { concurrency: N },
383 );
384
385 expect(results).toHaveLength(N);
386 for (let i = 0; i < N; i++) {
387 expect(results[i]).toEqual(Array.from({ length: 3 + (i % 3) }, (_, n) => n + 1));
388 }
389 }).pipe(Effect.scoped, Effect.provide(clientLayer(url)));
390 }).pipe(logLevel),
391 {
392 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
393 timeout: 30_000,
394 },
395 );