@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 95 lines 3.55 KB
Raw
1 import { retryWorkerScriptNotFound } from "@alchemy.run/cloudflare/Email/retry";
2 import { describe, expect, it } from "@alchemy.run/test";
3 import * as Effect from "effect/Effect";
4 import * as Fiber from "effect/Fiber";
5 import * as TestClock from "effect/testing/TestClock";
6
7 // Cloudflare rejects a rule whose `worker` action names a script it cannot see
8 // yet with code 2016 / `WorkerScriptNotFound`. Distilled surfaces that as a
9 // tagged error; only the tag matters to the retry, so a minimal stand-in keeps
10 // this test free of the generated SDK.
11 class WorkerScriptNotFound {
12 readonly _tag = "WorkerScriptNotFound";
13 constructor(readonly code = 2016) {}
14 }
15 class SomethingElse {
16 readonly _tag = "Conflict";
17 }
18
19 // Fails the first `failures` attempts with `error`, then succeeds.
20 const flaky = <E>(failures: number, error: E) => {
21 let attempts = 0;
22 return {
23 attempts: () => attempts,
24 effect: Effect.suspend(() => {
25 attempts++;
26 return attempts <= failures ? Effect.fail(error) : Effect.succeed("ok");
27 }),
28 };
29 };
30
31 // The live suite (EmailRuleWorkerTarget.test.ts) cannot exercise this window —
32 // the Worker provider pre-creates a stub script, so the name already resolves
33 // by the time the rule is validated, and that suite passes with the retry
34 // removed. These cases inject the failure instead, so they fail if the retry
35 // regresses.
36 //
37 // Advance virtual time to exercise the production backoff without wall-clock waits.
38 describe(
39 "retryWorkerScriptNotFound",
40 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:email", "local"] },
41 () => {
42 it.effect("retries while the script is not yet visible, then succeeds", () =>
43 Effect.gen(function* () {
44 const target = flaky(2, new WorkerScriptNotFound());
45
46 const fiber = yield* retryWorkerScriptNotFound(target.effect).pipe(Effect.forkChild);
47 yield* TestClock.adjust("1 second");
48 const result = yield* Fiber.join(fiber);
49
50 expect(result).toBe("ok");
51 // Two not-visible-yet failures plus the success.
52 expect(target.attempts()).toBe(3);
53 }),
54 );
55
56 it.effect("does not retry an unrelated failure", () =>
57 Effect.gen(function* () {
58 const target = flaky(99, new SomethingElse());
59
60 const outcome = yield* Effect.result(retryWorkerScriptNotFound(target.effect));
61
62 expect(outcome._tag).toBe("Failure");
63 // Attempted once and given up — no backoff burned on a permanent error.
64 expect(target.attempts()).toBe(1);
65 }),
66 );
67
68 it.effect(
69 "gives up after a bounded budget and re-raises the original error",
70 () =>
71 Effect.gen(function* () {
72 const error = new WorkerScriptNotFound();
73 // Never recovers: a genuinely missing Worker must still fail and say so
74 // rather than hanging.
75 const target = flaky(Number.MAX_SAFE_INTEGER, error);
76
77 const fiber = yield* Effect.result(retryWorkerScriptNotFound(target.effect)).pipe(
78 Effect.forkChild,
79 );
80 yield* TestClock.adjust("30 seconds");
81 const outcome = yield* Fiber.join(fiber);
82
83 expect(outcome._tag).toBe("Failure");
84 // The error surfaces unchanged, not wrapped in a retry-exhausted error.
85 if (outcome._tag === "Failure") {
86 expect(outcome.failure).toBe(error);
87 }
88 // `times: 8` — the initial attempt plus eight retries, and no more.
89 expect(target.attempts()).toBe(9);
90 }),
91 // Bound regressions even if virtual retries stop making progress.
92 { timeout: 5_000 },
93 );
94 },
95 );