Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalInputWriter.test.ts

182 lines6.5 KB

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