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

main
ts 170 lines 6.16 KB
Raw
1 import { TerminalWriteInput } from "@t3tools/contracts";
2 import * as Schema from "effect/Schema";
3 import { describe, expect, it, vi } from "vite-plus/test";
4
5 import { createTerminalInputWriter } from "./terminalInputWriter";
6
7 const isWriteInput = Schema.is(TerminalWriteInput);
8 const fitsWrite = (data: string) => isWriteInput({ threadId: "t", terminalId: "default", data });
9
10 /** A `send` whose calls stay pending until the test settles them. */
11 function heldSend() {
12 const sent: Array<{ readonly chunk: string; readonly settle: (ok: boolean) => void }> = [];
13 const waiters: Array<{ readonly count: number; readonly resolve: () => void }> = [];
14 return {
15 sent,
16 chunks: () => sent.map(({ chunk }) => chunk),
17 /** Resolves once `count` chunks have been sent. */
18 sends: (count: number) =>
19 new Promise<void>((resolve) => {
20 if (sent.length >= count) resolve();
21 else waiters.push({ count, resolve });
22 }),
23 send: (chunk: string) =>
24 new Promise<boolean>((settle) => {
25 sent.push({ chunk, settle });
26 for (const waiter of waiters) if (sent.length >= waiter.count) waiter.resolve();
27 }),
28 };
29 }
30
31 describe("createTerminalInputWriter", () => {
32 const line = ' {"file":"src/日本語.ts","message":"Review 🧑🏽‍💻 café changes"},\n';
33 const paste = (length: number) =>
34 line.repeat(Math.ceil(length / line.length)).slice(0, length - 1) + "\n";
35
36 it("delivers a bracketed paste over the limit as writes the server accepts", async () => {
37 const data = `\x1b[200~${paste(100_000)}\x1b[201~`;
38 const sent: string[] = [];
39 await createTerminalInputWriter()(data, async (chunk) => {
40 sent.push(chunk);
41 return true;
42 });
43
44 expect(sent).toHaveLength(2);
45 expect(sent.every(fitsWrite)).toBe(true);
46 expect(sent.join("")).toBe(data);
47 });
48
49 it("sends one chunk per round trip and holds later input behind a split write", async () => {
50 const write = createTerminalInputWriter();
51 const { sent, chunks, sends, send } = heldSend();
52 const large = `\x1b[200~${"p".repeat(70_000)}\x1b[201~`;
53 const [first, second] = [large.slice(0, 65_536), large.slice(65_536)];
54
55 void write("a", send);
56 const pasted = write(large, send);
57 void write("b", send);
58 void write("c", send);
59 expect(chunks()).toEqual(["a"]);
60 sent[0]!.settle(true);
61 await sends(2);
62 expect(chunks()).toEqual(["a", first]);
63
64 sent[1]!.settle(true);
65 await sends(3);
66 expect(chunks()).toEqual(["a", first, second]);
67 sent[2]!.settle(true);
68 await pasted;
69 await sends(4);
70 expect(chunks()).toEqual(["a", first, second, "b"]);
71 sent[3]!.settle(true);
72 await sends(5);
73 expect(chunks()).toEqual(["a", first, second, "b", "c"]);
74 });
75
76 it("dispatches small writes without waiting for acknowledgements and queues a paste behind them", async () => {
77 const write = createTerminalInputWriter();
78 const { sent, chunks, sends, send } = heldSend();
79 const inputs = Array.from({ length: 32 }, (_, index) => `key-${index}`);
80 const smallWrites = inputs.map((data) => write(data, send));
81 expect(chunks()).toEqual(inputs);
82
83 const large = "p".repeat(70_000);
84 const pasted = write(large, send);
85 const after = write("after", send);
86 // Finishing newer sends first must not let queued input overtake the oldest write.
87 for (let index = inputs.length - 1; index > 0; index--) sent[index]!.settle(true);
88 await Promise.all(smallWrites.slice(1));
89 expect(sent).toHaveLength(32);
90 sent[0]!.settle(true);
91 await sends(33);
92 expect(sent[32]!.chunk).toBe(large.slice(0, 65_536));
93 sent[32]!.settle(true);
94 await sends(34);
95 expect(sent[33]!.chunk).toBe(large.slice(65_536));
96 sent[33]!.settle(true);
97 await pasted;
98 await sends(35);
99 expect(sent[34]!.chunk).toBe("after");
100 sent[34]!.settle(true);
101 await after;
102 });
103
104 it("closes an interrupted bracketed paste before later input", async () => {
105 const write = createTerminalInputWriter();
106 const { sent, chunks, sends, send } = heldSend();
107 const large = `\x1b[200~${"p".repeat(140_000)}\x1b[201~`;
108 const pasted = write(large, send);
109 const after = write("after", send);
110 sent[0]!.settle(true);
111 await sends(2);
112 sent[1]!.settle(false);
113 await sends(3);
114 expect(chunks()).toEqual([large.slice(0, 65_536), large.slice(65_536, 131_072), "\x1b[201~"]);
115 sent[2]!.settle(false);
116 await pasted;
117 await sends(4);
118 expect(sent[3]!.chunk).toBe("\x1b[201~");
119 sent[3]!.settle(true);
120 await sends(5);
121 expect(sent[4]!.chunk).toBe("after");
122 sent[4]!.settle(true);
123 await after;
124 });
125
126 it("closes an opening chunk whose acknowledgement was lost without retrying payload", async () => {
127 const sent: string[] = [];
128 const write = createTerminalInputWriter();
129 const send = async (chunk: string) => {
130 sent.push(chunk);
131 return chunk === "\x1b[201~" || chunk === "after";
132 };
133 const large = `\x1b[200~${"p".repeat(70_000)}\x1b[201~`;
134 await write(large, send);
135 await write("after", send);
136 expect(sent).toEqual([large.slice(0, 65_536), "\x1b[201~", "after"]);
137 });
138
139 it("reports each input blocked by failed framing recovery without retrying payload", async () => {
140 const sent: string[] = [];
141 const interrupted = vi.fn();
142 const write = createTerminalInputWriter();
143 const send = async (chunk: string) => {
144 sent.push(chunk);
145 return false;
146 };
147 const recover = async () => {
148 sent.push("\x1b[201~");
149 return false;
150 };
151 const large = `\x1b[200~${"p".repeat(70_000)}\x1b[201~`;
152 await write(large, send, recover, interrupted);
153 await write("after", send, recover, interrupted);
154 expect(sent).toEqual([large.slice(0, 65_536), "\x1b[201~", "\x1b[201~"]);
155 expect(interrupted).toHaveBeenCalledTimes(2);
156 });
157
158 it("drops the rest of a split write after a failed chunk and keeps later input", async () => {
159 const write = createTerminalInputWriter();
160 const { sent, chunks, sends, send } = heldSend();
161
162 const pasted = write("q".repeat(140_000), send);
163 void write("next", send);
164 sent[0]!.settle(false);
165 await pasted;
166 await sends(2);
167
168 expect(chunks()).toEqual(["q".repeat(65_536), "next"]);
169 });
170 });