@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 60 lines 2.47 KB
Raw
1 import { fromCloudflareFetcher } from "@alchemy.run/cloudflare/Fetcher";
2 import { describe, expect, it } from "@alchemy.run/test";
3 import * as Effect from "effect/Effect";
4 import * as HttpClientRequest from "effect/http/HttpClientRequest";
5
6 // A Cloudflare fetcher whose first `failures` `.fetch()` calls reject with the
7 // given message, then resolve with a 200 — models a Durable Object / service
8 // binding that is briefly mid-propagation after deploy (workerd routes to a
9 // stale script version that has no fetch handler yet).
10 //
11 // The retry lives in `fromCloudflareFetcher` — the single adapter every CF
12 // binding flows through — NOT in any one higher-level wrapper (`toHttpClient`,
13 // the RPC DO transport, etc.), because the "no fetch handler" window is a
14 // property of invoking the binding itself, so every consumer should be
15 // resilient to it without re-implementing the retry.
16 const flakyFetcher = (failures: number, message: string) => {
17 let attempts = 0;
18 return {
19 attempts: () => attempts,
20 fetch: (_request: any, _opts?: any) => {
21 attempts++;
22 return attempts <= failures
23 ? Promise.reject(new Error(message))
24 : Promise.resolve(new Response(null, { status: 200 }));
25 },
26 connect: () => {
27 throw new Error("connect not supported in test");
28 },
29 };
30 };
31
32 describe("fromCloudflareFetcher", { tags: ["unit", "provider:cloudflare", "local"] }, () => {
33 // `it.live` uses the real clock so the retry's backoff delays actually
34 // elapse (the default `it.effect` TestClock would never advance them).
35 it.live("retries the just-deployed 'no fetch handler' propagation window", () =>
36 Effect.gen(function* () {
37 const fetcher = flakyFetcher(2, "Handler does not export a fetch() function.");
38 const client = fromCloudflareFetcher(fetcher as any);
39
40 const res = yield* client.fetch(HttpClientRequest.get("http://do/"));
41
42 expect(res.status).toBe(200);
43 // Two not-ready failures + one success.
44 expect(fetcher.attempts()).toBe(3);
45 }),
46 );
47
48 it.live("does not retry unrelated failures", () =>
49 Effect.gen(function* () {
50 const fetcher = flakyFetcher(99, "some other boom");
51 const client = fromCloudflareFetcher(fetcher as any);
52
53 const outcome = yield* client.fetch(HttpClientRequest.get("http://do/")).pipe(Effect.exit);
54
55 expect(outcome._tag).toBe("Failure");
56 // A non-propagation error is surfaced on the first attempt — no retry.
57 expect(fetcher.attempts()).toBe(1);
58 }),
59 );
60 });