@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, 04:45 UTC

main
ts 118 lines 4.2 KB
Raw
1 import { describe, expect, it } from "@alchemy.run/test";
2 import * as Effect from "effect/Effect";
3 import * as Exit from "effect/Exit";
4 import { makeServerRpcSession, type ServerWebSocketLike } from "@/Local/RpcServerSession.ts";
5
6 describe("Local.RpcServerSession", { tags: ["unit", "local"] }, () => {
7 it.effect("happy path: paired sessions round-trip a method call", () =>
8 Effect.gen(function* () {
9 const mainA = { ping: async (x: number) => x + 1 };
10 const mainB = { pong: async (x: string) => `${x}!` };
11 const { a } = pairSessions(mainA, mainB);
12 const remote = a.session.getRemoteMain() as unknown as {
13 pong: (x: string) => Promise<string>;
14 };
15 const result = yield* Effect.promise(() => remote.pong("hi"));
16 expect(result).toBe("hi!");
17 }),
18 );
19
20 it.effect("string and Buffer payloads both decode", () =>
21 Effect.gen(function* () {
22 const received: Array<string> = [];
23 const ws: ServerWebSocketLike = {
24 send: () => {},
25 close: () => {},
26 };
27 // Build a session purely to observe `dispatch.message` decoding.
28 // Use a noop main; we won't call anything on it.
29 const { dispatch } = makeServerRpcSession(ws, {});
30 const origMessage = dispatch.message;
31 dispatch.message = (data: any) => {
32 if (typeof data === "string") {
33 received.push(`str:${data}`);
34 } else {
35 received.push(`buf:${data.toString("utf-8")}`);
36 }
37 return origMessage(data);
38 };
39 dispatch.message("hello");
40 dispatch.message(Buffer.from("world", "utf-8") as Buffer<ArrayBuffer>);
41 expect(received).toEqual(["str:hello", "buf:world"]);
42 }),
43 );
44
45 it.effect("dispatch.message after dispatch.close is dropped silently", () =>
46 Effect.gen(function* () {
47 const ws: ServerWebSocketLike = {
48 send: () => {},
49 close: () => {},
50 };
51 const { dispatch } = makeServerRpcSession(ws, {});
52 dispatch.close(1000, "bye");
53 // Should not throw, should be a no-op.
54 expect(() => dispatch.message("anything")).not.toThrow();
55 }),
56 );
57
58 it.effect("dispatch.close rejects a pending getRemoteMain call", () =>
59 Effect.gen(function* () {
60 // mainB never resolves on the wire because we never deliver anything
61 // back; the close event should reject the in-flight call.
62 const closes: Array<{ code?: number; reason?: string }> = [];
63 const ws: ServerWebSocketLike = {
64 send: () => {
65 // intentionally drop outbound traffic
66 },
67 close: (code, reason) => {
68 closes.push({ code, reason });
69 },
70 };
71 const { session, dispatch } = makeServerRpcSession(ws, {});
72 const remote = session.getRemoteMain() as unknown as {
73 ping: (n: number) => Promise<number>;
74 };
75 const inFlight = remote.ping(1);
76 dispatch.close(1006, "abnormal");
77 // The dropped outbound traffic means the only way `inFlight` can
78 // resolve is by rejection after `dispatch.close`. Wrap it as an
79 // Effect that fails on rejection so we can assert with `Exit`.
80 const exit = yield* Effect.exit(Effect.tryPromise(() => inFlight));
81 expect(Exit.isFailure(exit)).toBe(true);
82 }),
83 );
84 });
85
86 /**
87 * Wire two `ServerRpcSession`s together over in-memory fake websockets so we
88 * can drive `RpcSession.getRemoteMain()` end-to-end without touching the
89 * platform WS server.
90 */
91 const pairSessions = <A extends Record<string, any>, B extends Record<string, any>>(
92 mainA: A,
93 mainB: B,
94 ) => {
95 const closes: Array<{ side: "a" | "b"; code?: number; reason?: string }> = [];
96 let a!: ReturnType<typeof makeServerRpcSession<A>>;
97 let b!: ReturnType<typeof makeServerRpcSession<B>>;
98
99 const wsA: ServerWebSocketLike = {
100 send: (msg) => {
101 queueMicrotask(() => b.dispatch.message(msg));
102 },
103 close: (code, reason) => {
104 closes.push({ side: "a", code, reason });
105 },
106 };
107 const wsB: ServerWebSocketLike = {
108 send: (msg) => {
109 queueMicrotask(() => a.dispatch.message(msg));
110 },
111 close: (code, reason) => {
112 closes.push({ side: "b", code, reason });
113 },
114 };
115 a = makeServerRpcSession<A>(wsA, mainA);
116 b = makeServerRpcSession<B>(wsB, mainB);
117 return { a, b, wsA, wsB, closes };
118 };