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

main
ts 367 lines 13.7 KB
Raw
1 import { describe, expect, it } from "@alchemy.run/test";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as FetchHttpClient from "effect/http/FetchHttpClient";
5 import * as HttpClient from "effect/http/HttpClient";
6 import * as HttpEffect from "effect/http/HttpEffect";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Stream from "effect/Stream";
9 import { safeHttpEffect } from "@/Http";
10 import * as Rpc from "@/Rpc";
11
12 // ---------------------------------------------------------------------------
13 // In-memory loopback: the server `serveRpc` handler is turned into a Web
14 // `Request` handler and injected as the `fetch` implementation behind the
15 // real `FetchHttpClient`. This exercises the full wire protocol (headers,
16 // JSON bodies, streamed NDJSON / raw bytes) with zero sockets and zero
17 // container — just the protocol round-tripping in process.
18 // ---------------------------------------------------------------------------
19
20 class BoomError extends Data.TaggedError("BoomError")<{
21 readonly code: number;
22 }> {}
23
24 // The fallback handler stands in for the user's non-RPC routes.
25 const fallback = Effect.succeed(HttpServerResponse.text("fallback", { status: 200 }));
26
27 const withRpc = <A, E>(
28 shape: Record<string, unknown>,
29 use: (stub: any) => Effect.Effect<A, E, any>,
30 ): Effect.Effect<A, E> => {
31 const webHandler = HttpEffect.toWebHandler(safeHttpEffect(Rpc.serveRpc(shape, fallback)));
32 const fetchImpl = ((url: any, init?: any) =>
33 webHandler(new Request(url, init))) as typeof globalThis.fetch;
34
35 return Effect.gen(function* () {
36 const client = yield* HttpClient.HttpClient;
37 const stub = Rpc.makeFetchRpcStub<any>({
38 fetch: (request) => client.execute(request),
39 baseUrl: "http://rpc",
40 });
41 return yield* use(stub);
42 }).pipe(
43 Effect.provide(FetchHttpClient.layer),
44 Effect.provideService(FetchHttpClient.Fetch, fetchImpl),
45 ) as Effect.Effect<A, E>;
46 };
47
48 const shape = {
49 ping: () => Effect.succeed("pong"),
50 echo: (a: string, b: number) => Effect.succeed({ a, b }),
51 maybe: (key: string) => Effect.succeed(key === "present" ? "value" : null),
52 identity: (value: unknown) => Effect.succeed(value),
53 boomTagged: () => Effect.fail(new BoomError({ code: 42 })),
54 boomPlain: () => Effect.fail(new Error("plain boom")),
55 countJsonl: (n: number) => Stream.range(1, n).pipe(Stream.map((i) => ({ i }))),
56 emptyStream: () => Stream.empty,
57 // An INFINITE source: only a truly streaming (lazily-pulled) transport can
58 // return a finite prefix of this. A buffer-then-stringify implementation
59 // would try to realize the whole stream and never produce a result.
60 infinite: () => Stream.iterate(0, (n) => n + 1),
61 bytes: () => Stream.fromIterable([new Uint8Array([1, 2, 3]), new Uint8Array([4, 5])]),
62 failingStream: () =>
63 Stream.range(1, 2).pipe(
64 Stream.map((i) => ({ i })),
65 Stream.concat(Stream.fail(new BoomError({ code: 7 }))),
66 ),
67 };
68
69 describe("Rpc fetch protocol", { tags: ["unit", "local"] }, () => {
70 describe("value methods (Effect)", () => {
71 it.effect("returns a primitive value", () =>
72 withRpc(shape, (stub) =>
73 Effect.gen(function* () {
74 expect(yield* stub.ping()).toBe("pong");
75 }),
76 ),
77 );
78
79 it.effect("round-trips arguments and an object result", () =>
80 withRpc(shape, (stub) =>
81 Effect.gen(function* () {
82 expect(yield* stub.echo("hi", 7)).toEqual({ a: "hi", b: 7 });
83 }),
84 ),
85 );
86
87 it.effect("supports a null result", () =>
88 withRpc(shape, (stub) =>
89 Effect.gen(function* () {
90 expect(yield* stub.maybe("absent")).toBe(null);
91 expect(yield* stub.maybe("present")).toBe("value");
92 }),
93 ),
94 );
95
96 it.effect("round-trips nested/array/null argument shapes", () =>
97 withRpc(shape, (stub) =>
98 Effect.gen(function* () {
99 const value = { a: [1, 2, { b: "x" }], c: null, d: true };
100 expect(yield* stub.identity(value)).toEqual(value);
101 }),
102 ),
103 );
104
105 it.effect("is usable as an Effect via .pipe (orDie)", () =>
106 withRpc(shape, (stub) =>
107 Effect.gen(function* () {
108 expect(yield* stub.ping().pipe(Effect.orDie)).toBe("pong");
109 }),
110 ),
111 );
112 });
113
114 describe("error channel", () => {
115 it.effect("lifts a remote tagged failure into the error channel", () =>
116 withRpc(shape, (stub) =>
117 Effect.gen(function* () {
118 const error = (yield* stub.boomTagged().pipe(Effect.flip)) as {
119 _tag: string;
120 code: number;
121 };
122 expect(error._tag).toBe("BoomError");
123 expect(error.code).toBe(42);
124 }),
125 ),
126 );
127
128 it.effect("lifts a remote plain Error into the error channel", () =>
129 withRpc(shape, (stub) =>
130 Effect.gen(function* () {
131 const error = (yield* stub.boomPlain().pipe(Effect.flip)) as {
132 name: string;
133 message: string;
134 };
135 expect(error.message).toBe("plain boom");
136 }),
137 ),
138 );
139
140 it.effect("fails an unknown method (404 → RpcCallError)", () =>
141 withRpc(shape, (stub) =>
142 Effect.gen(function* () {
143 const error = (yield* stub.doesNotExist().pipe(Effect.flip)) as {
144 _tag: string;
145 };
146 expect(error._tag).toBe("RpcCallError");
147 }),
148 ),
149 );
150 });
151
152 describe("streaming methods (Stream)", () => {
153 const collect = <T>(streamLike: any) =>
154 Effect.gen(function* () {
155 const out: T[] = [];
156 yield* Stream.runForEach(streamLike, (v: T) =>
157 Effect.sync(() => {
158 out.push(v);
159 }),
160 );
161 return out;
162 });
163
164 it.effect("round-trips an NDJSON object stream", () =>
165 withRpc(shape, (stub) =>
166 Effect.gen(function* () {
167 const items = yield* collect(stub.countJsonl(3));
168 expect(items).toEqual([{ i: 1 }, { i: 2 }, { i: 3 }]);
169 }),
170 ),
171 );
172
173 it.effect("handles an empty stream", () =>
174 withRpc(shape, (stub) =>
175 Effect.gen(function* () {
176 expect(yield* collect(stub.emptyStream())).toEqual([]);
177 }),
178 ),
179 );
180
181 it.effect("round-trips a raw byte stream (content preserved)", () =>
182 withRpc(shape, (stub) =>
183 Effect.gen(function* () {
184 const chunks = yield* collect<Uint8Array>(stub.bytes());
185 const flat = chunks.flatMap((c) => Array.from(c));
186 expect(flat).toEqual([1, 2, 3, 4, 5]);
187 }),
188 ),
189 );
190
191 it.effect("consumes an infinite stream incrementally (not buffered)", () =>
192 withRpc(shape, (stub) =>
193 Effect.gen(function* () {
194 // If the server buffered + stringified the whole stream before
195 // responding, an infinite source would never produce a result and
196 // this would hit the timeout below. Taking a finite prefix only
197 // succeeds because the body is pulled lazily, element by element.
198 const out = yield* collect<number>(stub.infinite().pipe(Stream.take(5)));
199 expect(out).toEqual([0, 1, 2, 3, 4]);
200 }),
201 ).pipe(Effect.timeout("15 seconds")),
202 );
203
204 it.effect("server stops producing once the client stops consuming (backpressure)", () =>
205 Effect.gen(function* () {
206 // The shape runs in-process, so this closure-captured array sees
207 // exactly how far the server stream was pulled.
208 const produced: number[] = [];
209 const localShape = {
210 counter: () =>
211 Stream.iterate(0, (n) => n + 1).pipe(
212 Stream.tap((n) => Effect.sync(() => produced.push(n))),
213 ),
214 };
215
216 const out = yield* withRpc(localShape, (stub) =>
217 collect<number>(stub.counter().pipe(Stream.take(3))),
218 ).pipe(Effect.timeout("15 seconds"));
219
220 expect(out).toEqual([0, 1, 2]);
221 // The client only consumed 3 elements. A streaming transport pulls
222 // lazily under backpressure, so the server produces the consumed
223 // prefix plus at most a bounded buffer — never the infinite tail a
224 // buffer-then-stringify implementation would.
225 expect(produced.length).toBeGreaterThanOrEqual(3);
226 expect(produced.length).toBeLessThan(10_000);
227 }),
228 );
229
230 it.effect("lifts a mid-stream failure into RpcRemoteStreamError", () =>
231 withRpc(shape, (stub) =>
232 Effect.gen(function* () {
233 const result = yield* stub.failingStream().pipe(Stream.runCollect, Effect.flip);
234 expect((result as { _tag: string })._tag).toBe("RpcRemoteStreamError");
235 const error = (result as { error: { _tag: string; code: number } }).error;
236 expect(error._tag).toBe("BoomError");
237 expect(error.code).toBe(7);
238 }),
239 ),
240 );
241
242 it.effect("delivers values before a mid-stream failure", () =>
243 withRpc(shape, (stub) =>
244 Effect.gen(function* () {
245 const seen: Array<{ i: number }> = [];
246 yield* stub.failingStream().pipe(
247 Stream.runForEach((v: { i: number }) =>
248 Effect.sync(() => {
249 seen.push(v);
250 }),
251 ),
252 Effect.flip,
253 );
254 expect(seen).toEqual([{ i: 1 }, { i: 2 }]);
255 }),
256 ),
257 );
258 });
259
260 describe("fallback routing", () => {
261 it.effect("non-RPC requests fall through to the fallback handler", () =>
262 Effect.gen(function* () {
263 const webHandler = HttpEffect.toWebHandler(safeHttpEffect(Rpc.serveRpc(shape, fallback)));
264 const res = yield* Effect.promise(() => webHandler(new Request("http://rpc/not-rpc")));
265 expect(res.status).toBe(200);
266 expect(yield* Effect.promise(() => res.text())).toBe("fallback");
267 }),
268 );
269 });
270
271 // -------------------------------------------------------------------------
272 // Nested RPC: a shape method that *forwards* another RPC call returns an
273 // `asEffectOrStream` value — an `Effect` that is ALSO branded as a `Stream`
274 // so a single return is usable either way. This is exactly the shape of a
275 // Worker→DO→Container chain (e.g. `readObjectRpc: (k) => container.readObject(k)`).
276 // The dispatcher MUST run it as an effect rather than mis-detecting the
277 // brand and serving it as a stream envelope (which decodes to `{channel:{}}`
278 // on the caller). See `serveRpc` and the Worker/DO bridges.
279 // -------------------------------------------------------------------------
280 describe("nested RPC (asEffectOrStream forwarding)", () => {
281 const nested = {
282 // value method forwarding a nested call that resolves to a value
283 forwardValue: (k: string) => Rpc.asEffectOrStream(Effect.succeed(`deep:${k}`)),
284 // value method forwarding a nested call that resolves to a Stream
285 forwardStream: (n: number) =>
286 Rpc.asEffectOrStream(Effect.succeed(Stream.range(1, n).pipe(Stream.map((i) => ({ i }))))),
287 };
288
289 it.effect("a forwarded value round-trips as a value (not a Stream)", () =>
290 withRpc(nested, (stub) =>
291 Effect.gen(function* () {
292 const value = yield* stub.forwardValue("x");
293 expect(value).toBe("deep:x");
294 }),
295 ),
296 );
297
298 it.effect("a forwarded Stream round-trips as a Stream", () =>
299 withRpc(nested, (stub) =>
300 Effect.gen(function* () {
301 const out: Array<{ i: number }> = [];
302 yield* Stream.runForEach(stub.forwardStream(3), (v: { i: number }) =>
303 Effect.sync(() => {
304 out.push(v);
305 }),
306 );
307 expect(out).toEqual([{ i: 1 }, { i: 2 }, { i: 3 }]);
308 }),
309 ),
310 );
311 });
312
313 // -------------------------------------------------------------------------
314 // Benchmark: the loopback exercises the FULL wire protocol (serialize args →
315 // serveRpc → JSON response → decode) with zero sockets and zero container,
316 // so it isolates the RPC layer's per-call overhead from network latency.
317 // This is the quantitative backstop for "RPC is not the bottleneck": a
318 // catastrophic regression (accidental buffering, O(n)-per-call work, a real
319 // socket sneaking in) blows the per-call ceiling, while normal network jitter
320 // (the 400-700ms seen against a live container's R2 read) lives entirely
321 // outside this measurement.
322 // -------------------------------------------------------------------------
323 describe("performance", () => {
324 const N = 2_000;
325 // In-process protocol overhead is sub-millisecond; a generous ceiling so
326 // the assertion only trips on a real regression, never on a slow CI box.
327 const MAX_MS_PER_CALL = 5;
328
329 const benchmark = (
330 label: string,
331 stub: any,
332 call: (stub: any) => Effect.Effect<unknown, unknown, any>,
333 expected: unknown,
334 ) =>
335 Effect.gen(function* () {
336 // Warm the JIT + connection-less path, and assert correctness once so
337 // the timing loop below isn't measuring a broken (e.g. erroring) path.
338 expect(yield* call(stub)).toEqual(expected);
339 const start = yield* Effect.sync(() => performance.now());
340 for (let i = 0; i < N; i++) {
341 yield* call(stub);
342 }
343 const elapsed = yield* Effect.sync(() => performance.now() - start);
344 const perCall = elapsed / N;
345 yield* Effect.logInfo(
346 `RPC ${label}: ${perCall.toFixed(4)} ms/call ` +
347 `(${N} calls in ${elapsed.toFixed(0)} ms)`,
348 );
349 expect(perCall).toBeLessThan(MAX_MS_PER_CALL);
350 });
351
352 it.effect(
353 "no-arg value-method RPC overhead stays sub-millisecond",
354 () => withRpc(shape, (stub) => benchmark("ping", stub, (s) => s.ping(), "pong")),
355 { timeout: 60_000 },
356 );
357
358 it.effect(
359 "arg + object-result RPC overhead stays sub-millisecond",
360 () =>
361 withRpc(shape, (stub) =>
362 benchmark("echo", stub, (s) => s.echo("hi", 7), { a: "hi", b: 7 }),
363 ),
364 { timeout: 60_000 },
365 );
366 });
367 });