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

terminalSession.test.ts

274 lines10.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { EnvironmentId, TerminalSessionSnapshot, ThreadId } from "@t3tools/contracts";
4​
5import {
6 applyTerminalAttachStreamEvent,
7 applyTerminalMetadataStreamEvent,
8 combineTerminalSessionState,
9 EMPTY_TERMINAL_BUFFER_STATE,
10 INITIAL_TERMINAL_OUTPUT_CURSOR,
11 nextTerminalAttachSeedState,
12 readTerminalOutputUpdate,
13 terminalOutputText,
14} from "./terminalSession.ts";
15​
16const TARGET = {
17 environmentId: EnvironmentId.make("env-local"),
18 threadId: ThreadId.make("thread-1"),
19 terminalId: "term-1",
20} as const;
21​
22const BASE_SNAPSHOT: TerminalSessionSnapshot = {
23 threadId: TARGET.threadId,
24 terminalId: TARGET.terminalId,
25 cwd: "/repo",
26 worktreePath: null,
27 status: "running",
28 pid: 123,
29 history: "hello",
30 exitCode: null,
31 exitSignal: null,
32 label: "Terminal 1",
33 updatedAt: "2026-04-01T00:00:00.000Z",
34};
35​
36describe("terminal session reducers", () => {
37 it("prefers live attach status over stale metadata after the attach stream starts", () => {
38 const summary = applyTerminalMetadataStreamEvent([], {
39 type: "snapshot",
40 terminals: [
41 {
42 threadId: BASE_SNAPSHOT.threadId,
43 terminalId: BASE_SNAPSHOT.terminalId,
44 cwd: BASE_SNAPSHOT.cwd,
45 worktreePath: BASE_SNAPSHOT.worktreePath,
46 status: "running",
47 pid: BASE_SNAPSHOT.pid,
48 exitCode: BASE_SNAPSHOT.exitCode,
49 exitSignal: BASE_SNAPSHOT.exitSignal,
50 updatedAt: BASE_SNAPSHOT.updatedAt,
51 hasRunningSubprocess: false,
52 label: BASE_SNAPSHOT.label,
53 },
54 ],
55 })[0]!;
56 const attached = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
57 type: "error",
58 threadId: TARGET.threadId,
59 terminalId: TARGET.terminalId,
60 message: "Terminal disconnected.",
61 });
62​
63 expect(combineTerminalSessionState(summary, attached)).toMatchObject({
64 status: "error",
65 error: "Terminal disconnected.",
66 version: 1,
67 });
68 });
69​
70 it("follows the shared PTY grid from snapshots and activity without disturbing output", () => {
71 const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
72 type: "snapshot",
73 snapshot: { ...BASE_SNAPSHOT, cols: 130, rows: 15 },
74 });
75 const activity = {
76 type: "activity",
77 threadId: TARGET.threadId,
78 terminalId: TARGET.terminalId,
79 hasRunningSubprocess: false,
80 label: "Terminal 1",
81 } as const;
82 const narrowed = applyTerminalAttachStreamEvent(attached, { ...activity, cols: 54, rows: 15 });
83 // Activity from a server without geometry, or a repeat, must not reset the renderer.
84 const unchanged = applyTerminalAttachStreamEvent(
85 applyTerminalAttachStreamEvent(narrowed, activity),
86 { ...activity, cols: 54, rows: 15 },
87 );
88​
89 expect(attached.grid).toEqual({ cols: 130, rows: 15 });
90 expect(narrowed).toEqual({ ...attached, grid: { cols: 54, rows: 15 } });
91 expect(unchanged).toBe(narrowed);
92 });
93​
94 it("preserves a BOM code point at a new output chunk boundary", () => {
95 const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
96 type: "snapshot",
97 snapshot: { ...BASE_SNAPSHOT, history: "" },
98 });
99 const cursor = readTerminalOutputUpdate(initial.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
100 const data = `${"x".repeat(16_384)}\uFEFF${"y".repeat(20)}`;
101 const state = applyTerminalAttachStreamEvent(initial, {
102 type: "output",
103 threadId: TARGET.threadId,
104 terminalId: TARGET.terminalId,
105 data,
106 });
107​
108 expect(terminalOutputText(state.output)).toBe(data);
109 expect(readTerminalOutputUpdate(state.output, cursor)).toMatchObject({ type: "append", data });
110 expect(state.output.retainedBytes).toBe(new TextEncoder().encode(data).byteLength);
111 });
112​
113 it("preserves a leading BOM in the retained snapshot tail", () => {
114 const state = applyTerminalAttachStreamEvent(
115 EMPTY_TERMINAL_BUFFER_STATE,
116 { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history: "discard\uFEFFtail" } },
117 7,
118 );
119​
120 expect(terminalOutputText(state.output)).toBe("\uFEFFtail");
121 expect(state.output.retainedBytes).toBe(7);
122 });
123​
124 it("trims whole Unicode code points from a partially retained chunk", () => {
125 let state = applyTerminalAttachStreamEvent(
126 EMPTY_TERMINAL_BUFFER_STATE,
127 { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history: "é界🙂end" } },
128 12,
129 );
130 let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
131 for (const [data, expected, retainedBytes] of [
132 ["x", "界🙂endx", 11],
133 ["yz", "🙂endxyz", 10],
134 ["abc", "endxyzabc", 9],
135 ] as const) {
136 state = applyTerminalAttachStreamEvent(
137 state,
138 { type: "output", threadId: TARGET.threadId, terminalId: TARGET.terminalId, data },
139 12,
140 );
141 const update = readTerminalOutputUpdate(state.output, cursor);
142 expect(update).toMatchObject({ type: "append", data });
143 expect(terminalOutputText(state.output)).toBe(expected);
144 expect(state.output.retainedBytes).toBe(retainedBytes);
145 cursor = update.cursor;
146 }
147 });
148​
149 it("delivers all output when several events arrive before a renderer reads", () => {
150 const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
151 type: "snapshot",
152 snapshot: BASE_SNAPSHOT,
153 });
154 const cursor = readTerminalOutputUpdate(initial.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
155 let state = initial;
156 for (const data of [" one", " two", " three"]) {
157 state = applyTerminalAttachStreamEvent(state, {
158 type: "output",
159 threadId: TARGET.threadId,
160 terminalId: TARGET.terminalId,
161 data,
162 });
163 }
164 const update = readTerminalOutputUpdate(state.output, cursor);
165 expect(update).toMatchObject({ type: "append", data: " one two three" });
166 expect(readTerminalOutputUpdate(state.output, update.cursor).type).toBe("none");
167 });
168​
169 it("preserves the byte-limited tail and resets a cursor before a partially trimmed chunk", () => {
170 const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
171 type: "snapshot",
172 snapshot: { ...BASE_SNAPSHOT, history: "" },
173 });
174 const staleCursor = readTerminalOutputUpdate(
175 initial.output,
176 INITIAL_TERMINAL_OUTPUT_CURSOR,
177 ).cursor;
178 const first = applyTerminalAttachStreamEvent(initial, {
179 type: "output",
180 threadId: TARGET.threadId,
181 terminalId: TARGET.terminalId,
182 data: "hello",
183 });
184 const caughtUpCursor = readTerminalOutputUpdate(first.output, staleCursor).cursor;
185 const state = applyTerminalAttachStreamEvent(
186 first,
187 { type: "output", threadId: TARGET.threadId, terminalId: TARGET.terminalId, data: " world" },
188 8,
189 );
190​
191 expect(readTerminalOutputUpdate(state.output, caughtUpCursor)).toMatchObject({
192 type: "append",
193 data: " world",
194 });
195 expect(readTerminalOutputUpdate(state.output, staleCursor)).toMatchObject({
196 type: "reset",
197 data: "lo world",
198 });
199 expect(state.output.retainedBytes).toBe(8);
200 });
201​
202 it("resets for repeated snapshots, clear, and restart even when output text repeats", () => {
203 let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
204 type: "snapshot",
205 snapshot: BASE_SNAPSHOT,
206 });
207 let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
208 state = applyTerminalAttachStreamEvent(state, { type: "snapshot", snapshot: BASE_SNAPSHOT });
209 const repeated = readTerminalOutputUpdate(state.output, cursor);
210 expect(repeated).toMatchObject({ type: "reset", data: "hello" });
211 expect(state.version).toBe(2);
212 cursor = repeated.cursor;
213​
214 state = applyTerminalAttachStreamEvent(state, {
215 type: "cleared",
216 threadId: TARGET.threadId,
217 terminalId: TARGET.terminalId,
218 });
219 const cleared = readTerminalOutputUpdate(state.output, cursor);
220 expect(cleared).toMatchObject({ type: "reset", data: "" });
221 state = applyTerminalAttachStreamEvent(state, {
222 type: "restarted",
223 threadId: TARGET.threadId,
224 terminalId: TARGET.terminalId,
225 snapshot: BASE_SNAPSHOT,
226 });
227 expect(readTerminalOutputUpdate(state.output, cleared.cursor)).toMatchObject({
228 type: "reset",
229 data: "hello",
230 });
231 expect(state.lifecycleVersion).toBe(2);
232 });
233​
234 it("does not reuse a renderer cursor when a fresh attach has matching counters", () => {
235 const first = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
236 type: "snapshot",
237 snapshot: BASE_SNAPSHOT,
238 });
239 const cursor = readTerminalOutputUpdate(first.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
240 const next = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
241 type: "snapshot",
242 snapshot: { ...BASE_SNAPSHOT, history: "other" },
243 });
244 expect(next.output.resetVersion).toBe(first.output.resetVersion);
245 expect(next.output.nextOffset).toBe(first.output.nextOffset);
246 expect(readTerminalOutputUpdate(next.output, cursor)).toMatchObject({
247 type: "reset",
248 data: "other",
249 });
250 expect(next.lifecycleVersion).toBe(0);
251 });
252​
253 it("appends every unread character across a compaction boundary", () => {
254 let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
255 type: "snapshot",
256 snapshot: { ...BASE_SNAPSHOT, history: "" },
257 });
258 let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
259 const writes = Array.from({ length: 1200 }, (_, index) => ["x", "é", "界", "🙂"][index % 4]!);
260 for (const [index, data] of writes.entries()) {
261 state = applyTerminalAttachStreamEvent(state, {
262 type: "output",
263 threadId: TARGET.threadId,
264 terminalId: TARGET.terminalId,
265 data,
266 });
267 if (index === 99) cursor = readTerminalOutputUpdate(state.output, cursor).cursor;
268 }
269 expect(readTerminalOutputUpdate(state.output, cursor)).toMatchObject({
270 type: "append",
271 data: writes.slice(100).join(""),
272 });
273 });
274});