@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 349 lines 13 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 Effect from "effect/Effect";
5 import * as HttpClient from "effect/http/HttpClient";
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 Stack from "./fixtures/rpc-do-namespace-do-rpc/stack.ts";
11 import { WorkerRpcs as RpcWorkerWorkerRpcs } from "./fixtures/rpc-worker-rpc-http/group.ts";
12 import RpcWorkerStack from "./fixtures/rpc-worker-rpc-http/stack.ts";
13
14 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
15 providers: Cloudflare.providers(),
16 });
17
18 // `Test.rpcClientLayer` guards the transport against edge-generated HTML
19 // bodies (workers.dev placeholder, error pages) that the RPC protocol would
20 // otherwise surface as an opaque `RpcClientDefect`; see Test/Http.ts.
21 const rpcClientLayer = Test.rpcClientLayer;
22
23 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
24
25 // Cap exponential backoff at 3s so retries stay bounded when CF edge is
26 // slow (otherwise the geometric blow-up dominates wall time).
27 const readinessSchedule = Schedule.min([
28 Schedule.exponential("500 millis"),
29 Schedule.spaced("3 seconds"),
30 ]);
31
32 // Suffix DO instance ids with a per-process random tag so reruns under
33 // `NO_DESTROY=1` don't collide with persisted state from earlier runs
34 // (the DO's `count` lives in `state.storage`).
35 const runId = Math.random().toString(36).slice(2, 10);
36 const k = (name: string) => `${name}-${runId}`;
37
38 // Retry transient 5xx/network errors from a freshly-deployed worker. The
39 // worker→DO binding can take a while to propagate to every edge POP, during
40 // which requests fail with 500 before the DO method ever runs — so retrying
41 // non-idempotent calls (increment) through this window is safe (the
42 // 100-concurrent test below relies on the same property).
43 const retryHttp = <A, E, R>(eff: Effect.Effect<A, E, R>) =>
44 eff.pipe(Effect.retry({ schedule: readinessSchedule, times: 10 }));
45
46 const resetCounter = (url: string, id: string) =>
47 Effect.gen(function* () {
48 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
49 yield* client.post(`${url}/counter/${id}/reset`).pipe(retryHttp);
50 });
51
52 // Full-suite concurrency + workers.dev propagation routinely needs more than
53 // a dozen attempts before the DO binding / subdomain is routable.
54 const readinessRetries = 30;
55
56 // The `*DO` RPC handlers forward to the Durable Object via `getByName(...)`.
57 // On a freshly-deployed worker the DO-namespace binding hasn't propagated to
58 // every Cloudflare edge yet, so the first calls fail with `Worker not found.`.
59 // The worker fixture wraps the DO call in `Effect.orDie` / `Stream.orDie`, so
60 // that error arrives at the client as a DEFECT — and `Effect.retry` does not
61 // retry defects. Promote defects to failures so the readiness retry can absorb
62 // the transient binding-propagation error (a genuine bug would simply keep
63 // failing until the retry budget is exhausted).
64 const retryReadyN =
65 (times: number) =>
66 <A, E, R>(eff: Effect.Effect<A, E, R>) =>
67 eff.pipe(
68 Effect.catchDefect((defect) => Effect.fail(defect)),
69 Effect.retry({ schedule: readinessSchedule, times }),
70 );
71
72 const retryReady = retryReadyN(readinessRetries);
73
74 // Gate HTTP deploy on workers.dev + DO binding readiness. Without this the
75 // first test burns its 30s budget on 404s while the subdomain propagates.
76 const stack = beforeAll(
77 deploy(Stack).pipe(
78 Effect.tap((outputs) =>
79 Effect.gen(function* () {
80 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
81 yield* client
82 .post(`${outputs.url}/counter/${k("warmup")}/reset`)
83 .pipe(retryReadyN(readinessRetries));
84 }),
85 ),
86 ),
87 );
88 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
89
90 // Gate the deploy on the worker→DO binding having propagated to the edge:
91 // hit both the unary and streaming `*DO` paths once, retrying through the
92 // transient `Worker not found.` window, so individual tests can call the
93 // `*DO` RPCs directly without each having to re-implement the readiness retry.
94 const rpcWorkerStack = beforeAll(
95 deploy(RpcWorkerStack).pipe(
96 Effect.tap((outputs) =>
97 Effect.gen(function* () {
98 const c = yield* RpcClient.make(RpcWorkerWorkerRpcs);
99 yield* c.PingDO({ message: "warmup" }).pipe(retryReady);
100 yield* c.CountDO({ upto: 1 }).pipe(Stream.runCollect, retryReady);
101 }).pipe(Effect.scoped, Effect.provide(rpcClientLayer(outputs.url))),
102 ),
103 // just give it some extra time to propagate
104 Effect.tap(Effect.sleep("5 seconds")),
105 ),
106 );
107 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(RpcWorkerStack));
108
109 test(
110 "RpcDurableObject: Increment / Get round-trip via Worker",
111 Effect.gen(function* () {
112 const { url } = yield* stack;
113 const alpha = k("alpha");
114 yield* resetCounter(url, alpha);
115 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
116
117 const incRes = yield* client.post(`${url}/counter/${alpha}/increment`).pipe(retryHttp);
118 expect(incRes.status).toBe(200);
119 const inc = (yield* incRes.json) as { count: number };
120 expect(inc.count).toBe(1);
121
122 yield* client.post(`${url}/counter/${alpha}/increment`).pipe(retryHttp);
123 yield* client.post(`${url}/counter/${alpha}/increment`).pipe(retryHttp);
124
125 const getRes = yield* client.get(`${url}/counter/${alpha}`).pipe(retryHttp);
126 expect(getRes.status).toBe(200);
127 const got = (yield* getRes.json) as { count: number };
128 expect(got.count).toBe(3);
129 }).pipe(logLevel),
130 {
131 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
132 timeout: 60_000,
133 },
134 );
135
136 test(
137 "RpcDurableObject: separate getByName(id) instances are isolated",
138 Effect.gen(function* () {
139 const { url } = yield* stack;
140 const betaId = k("beta");
141 const gammaId = k("gamma");
142 yield* resetCounter(url, betaId);
143 yield* resetCounter(url, gammaId);
144 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
145
146 yield* client.post(`${url}/counter/${betaId}/increment`).pipe(retryHttp);
147
148 const beta = (yield* (yield* client.get(`${url}/counter/${betaId}`).pipe(retryHttp)).json) as {
149 count: number;
150 };
151 const gamma = (yield* (yield* client.get(`${url}/counter/${gammaId}`).pipe(retryHttp))
152 .json) as {
153 count: number;
154 };
155 expect(beta.count).toBe(1);
156 expect(gamma.count).toBe(0);
157 }).pipe(logLevel),
158 {
159 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
160 timeout: 30_000,
161 },
162 );
163
164 test(
165 "RpcDurableObject: streaming RPC via getByName(id).CountUpTo",
166 Effect.gen(function* () {
167 const { url } = yield* stack;
168 const delta = k("delta");
169 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
170
171 // The stream route commits a 200 before the DO stream produces data, so
172 // a transient `Worker not found.` during binding propagation dies AFTER
173 // the headers are sent and surfaces as a truncated/empty 200 body —
174 // invisible to status-based retries. `CountUpTo` is a pure stream, so
175 // retry on content until all four lines arrive.
176 const lines = yield* client.get(`${url}/counter/${delta}/stream?upto=4`).pipe(
177 Effect.flatMap((res) => res.text),
178 Effect.flatMap((body) => {
179 const lines = body.split("\n").filter((l) => l.length > 0);
180 return lines.length === 4
181 ? Effect.succeed(lines)
182 : Effect.fail(new Error(`truncated stream body: ${JSON.stringify(lines)}`));
183 }),
184 retryHttp,
185 );
186 expect(lines).toEqual(["1", "2", "3", "4"]);
187 }).pipe(logLevel),
188 {
189 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
190 timeout: 30_000,
191 },
192 );
193
194 test(
195 "RpcWorker + RpcDurableObject: Worker proxies *DO RPCs through the typed namespace",
196 Effect.gen(function* () {
197 const { url } = yield* rpcWorkerStack;
198
199 yield* Effect.gen(function* () {
200 const c = yield* RpcClient.make(RpcWorkerWorkerRpcs);
201 const ping = yield* c.Ping({ message: "hi" }).pipe(retryReady);
202 expect(ping.echo).toBe("hi");
203
204 const pingDO = yield* c.PingDO({ message: "via DO" }).pipe(retryReady);
205 expect(pingDO.echo).toBe("via DO");
206 }).pipe(Effect.scoped, Effect.provide(rpcClientLayer(url)));
207 }).pipe(logLevel),
208 {
209 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
210 timeout: 30_000,
211 },
212 );
213
214 test(
215 "RpcDurableObject: 100 concurrent Increment calls do not hang",
216 Effect.gen(function* () {
217 const { url } = yield* stack;
218 const concurrent = k("concurrent");
219 yield* resetCounter(url, concurrent);
220 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
221
222 yield* client.post(`${url}/counter/${concurrent}/increment`).pipe(retryHttp);
223
224 const N = 100;
225 const results = yield* Effect.forEach(
226 Array.from({ length: N }, (_, i) => i),
227 () =>
228 client.post(`${url}/counter/${concurrent}/increment`).pipe(
229 Effect.flatMap((res) => res.json),
230 Effect.timeout("10 seconds"),
231 // Under full-suite load the account churns through worker
232 // deploys/deletes and workers.dev routing intermittently 404s an
233 // existing worker for several seconds; `times: 3` (~3.5s) is not
234 // enough to ride out a blip when 100 requests each get 4 chances.
235 // Retry only HTTP failures (a 404/5xx never reached the DO, so the
236 // increment can't double-count); a timeout is ambiguous and stays
237 // un-retried so the final-count assertion holds.
238 Effect.retry({
239 while: (e) => e._tag === "HttpClientError",
240 schedule: readinessSchedule,
241 times: 10,
242 }),
243 ),
244 { concurrency: 32 },
245 );
246
247 expect(results).toHaveLength(N);
248 const finalRes = yield* client.get(`${url}/counter/${concurrent}`).pipe(retryHttp);
249 const final = (yield* finalRes.json) as { count: number };
250 expect(final.count).toBe(N + 1);
251 }).pipe(logLevel),
252 {
253 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
254 timeout: 30_000,
255 },
256 );
257
258 test(
259 "RpcWorker + RpcDurableObject: 100 concurrent unary RPCs do not hang",
260 Effect.gen(function* () {
261 const { url } = yield* rpcWorkerStack;
262
263 yield* Effect.gen(function* () {
264 const c = yield* RpcClient.make(RpcWorkerWorkerRpcs);
265
266 const N = 100;
267 const results = yield* Effect.forEach(
268 Array.from({ length: N }, (_, i) => i),
269 (i) =>
270 c.Ping({ message: `m-${i}` }).pipe(
271 Effect.timeout("10 seconds"),
272 // A cold PoP mid-propagation returns Cloudflare's HTML error
273 // page, which is not valid ndjson and surfaces as a retryable
274 // `RpcClientError` (`RpcClientDefect: Error decoding HTTP
275 // response`); `times: 3` (~6s) is not enough to ride out a
276 // multi-second blip across 100 requests. Ping is idempotent.
277 retryReadyN(8),
278 ),
279 { concurrency: 32 },
280 );
281
282 expect(results).toHaveLength(N);
283 for (let i = 0; i < N; i++) {
284 expect(results[i].echo).toBe(`m-${i}`);
285 }
286 }).pipe(Effect.scoped, Effect.provide(rpcClientLayer(url)));
287 }).pipe(logLevel),
288 {
289 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
290 timeout: 60_000,
291 },
292 );
293
294 test(
295 "RpcWorker + RpcDurableObject: 100 concurrent *DO unary RPCs do not hang",
296 Effect.gen(function* () {
297 const { url } = yield* rpcWorkerStack;
298
299 yield* Effect.gen(function* () {
300 const c = yield* RpcClient.make(RpcWorkerWorkerRpcs);
301
302 const N = 100;
303 const results = yield* Effect.forEach(
304 Array.from({ length: N }, (_, i) => i),
305 (i) => c.PingDO({ message: `m-${i}` }).pipe(Effect.timeout("10 seconds"), retryReadyN(5)),
306 { concurrency: 16 },
307 );
308
309 expect(results).toHaveLength(N);
310 for (let i = 0; i < N; i++) {
311 expect(results[i].echo).toBe(`m-${i}`);
312 }
313 }).pipe(Effect.scoped, Effect.provide(rpcClientLayer(url)));
314 }).pipe(logLevel),
315 {
316 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
317 timeout: 30_000,
318 },
319 );
320
321 test(
322 "RpcWorker + RpcDurableObject: 100 concurrent streaming *DO RPCs do not hang",
323 Effect.gen(function* () {
324 const { url } = yield* rpcWorkerStack;
325
326 yield* Effect.gen(function* () {
327 const c = yield* RpcClient.make(RpcWorkerWorkerRpcs);
328
329 const N = 100;
330 const results = yield* Effect.forEach(
331 Array.from({ length: N }, (_, i) => i),
332 (i) =>
333 c
334 .CountDO({ upto: 3 + (i % 3) })
335 .pipe(Stream.runCollect, Effect.timeout("10 seconds"), retryReadyN(5)),
336 { concurrency: 16 },
337 );
338
339 expect(results).toHaveLength(N);
340 for (let i = 0; i < N; i++) {
341 expect(results[i]).toEqual(Array.from({ length: 3 + (i % 3) }, (_, n) => n + 1));
342 }
343 }).pipe(Effect.scoped, Effect.provide(rpcClientLayer(url)));
344 }).pipe(logLevel),
345 {
346 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
347 timeout: 30_000,
348 },
349 );