Open History to see the last commit
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("preserves a BOM code point at a new output chunk boundary", () => {
71 const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
72 type: "snapshot",
73 snapshot: { ...BASE_SNAPSHOT, history: "" },
74 });
75 const cursor = readTerminalOutputUpdate(initial.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
76 const data = `${"x".repeat(16_384)}\uFEFF${"y".repeat(20)}`;
77 const state = applyTerminalAttachStreamEvent(initial, {
78 type: "output",
79 threadId: TARGET.threadId,
80 terminalId: TARGET.terminalId,
81 data,
82 });
83
84 expect(terminalOutputText(state.output)).toBe(data);
85 expect(readTerminalOutputUpdate(state.output, cursor)).toMatchObject({ type: "append", data });
86 expect(state.output.retainedBytes).toBe(new TextEncoder().encode(data).byteLength);
87 });
88
89 it("preserves a leading BOM in the retained snapshot tail", () => {
90 const state = applyTerminalAttachStreamEvent(
91 EMPTY_TERMINAL_BUFFER_STATE,
92 { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history: "discard\uFEFFtail" } },
93 7,
94 );
95
96 expect(terminalOutputText(state.output)).toBe("\uFEFFtail");
97 expect(state.output.retainedBytes).toBe(7);
98 });
99
100 it("trims whole Unicode code points from a partially retained chunk", () => {
101 let state = applyTerminalAttachStreamEvent(
102 EMPTY_TERMINAL_BUFFER_STATE,
103 { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history: "é界🙂end" } },
104 12,
105 );
106 let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
107 for (const [data, expected, retainedBytes] of [
108 ["x", "界🙂endx", 11],
109 ["yz", "🙂endxyz", 10],
110 ["abc", "endxyzabc", 9],
111 ] as const) {
112 state = applyTerminalAttachStreamEvent(
113 state,
114 { type: "output", threadId: TARGET.threadId, terminalId: TARGET.terminalId, data },
115 12,
116 );
117 const update = readTerminalOutputUpdate(state.output, cursor);
118 expect(update).toMatchObject({ type: "append", data });
119 expect(terminalOutputText(state.output)).toBe(expected);
120 expect(state.output.retainedBytes).toBe(retainedBytes);
121 cursor = update.cursor;
122 }
123 });
124
125 it("delivers all output when several events arrive before a renderer reads", () => {
126 const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
127 type: "snapshot",
128 snapshot: BASE_SNAPSHOT,
129 });
130 const cursor = readTerminalOutputUpdate(initial.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
131 let state = initial;
132 for (const data of [" one", " two", " three"]) {
133 state = applyTerminalAttachStreamEvent(state, {
134 type: "output",
135 threadId: TARGET.threadId,
136 terminalId: TARGET.terminalId,
137 data,
138 });
139 }
140 const update = readTerminalOutputUpdate(state.output, cursor);
141 expect(update).toMatchObject({ type: "append", data: " one two three" });
142 expect(readTerminalOutputUpdate(state.output, update.cursor).type).toBe("none");
143 });
144
145 it("preserves the byte-limited tail and resets a cursor before a partially trimmed chunk", () => {
146 const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
147 type: "snapshot",
148 snapshot: { ...BASE_SNAPSHOT, history: "" },
149 });
150 const staleCursor = readTerminalOutputUpdate(
151 initial.output,
152 INITIAL_TERMINAL_OUTPUT_CURSOR,
153 ).cursor;
154 const first = applyTerminalAttachStreamEvent(initial, {
155 type: "output",
156 threadId: TARGET.threadId,
157 terminalId: TARGET.terminalId,
158 data: "hello",
159 });
160 const caughtUpCursor = readTerminalOutputUpdate(first.output, staleCursor).cursor;
161 const state = applyTerminalAttachStreamEvent(
162 first,
163 { type: "output", threadId: TARGET.threadId, terminalId: TARGET.terminalId, data: " world" },
164 8,
165 );
166
167 expect(readTerminalOutputUpdate(state.output, caughtUpCursor)).toMatchObject({
168 type: "append",
169 data: " world",
170 });
171 expect(readTerminalOutputUpdate(state.output, staleCursor)).toMatchObject({
172 type: "reset",
173 data: "lo world",
174 });
175 expect(state.output.retainedBytes).toBe(8);
176 });
177
178 it("resets for repeated snapshots, clear, and restart even when output text repeats", () => {
179 let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
180 type: "snapshot",
181 snapshot: BASE_SNAPSHOT,
182 });
183 let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
184 state = applyTerminalAttachStreamEvent(state, { type: "snapshot", snapshot: BASE_SNAPSHOT });
185 const repeated = readTerminalOutputUpdate(state.output, cursor);
186 expect(repeated).toMatchObject({ type: "reset", data: "hello" });
187 expect(state.version).toBe(2);
188 cursor = repeated.cursor;
189
190 state = applyTerminalAttachStreamEvent(state, {
191 type: "cleared",
192 threadId: TARGET.threadId,
193 terminalId: TARGET.terminalId,
194 });
195 const cleared = readTerminalOutputUpdate(state.output, cursor);
196 expect(cleared).toMatchObject({ type: "reset", data: "" });
197 state = applyTerminalAttachStreamEvent(state, {
198 type: "restarted",
199 threadId: TARGET.threadId,
200 terminalId: TARGET.terminalId,
201 snapshot: BASE_SNAPSHOT,
202 });
203 expect(readTerminalOutputUpdate(state.output, cleared.cursor)).toMatchObject({
204 type: "reset",
205 data: "hello",
206 });
207 expect(state.lifecycleVersion).toBe(2);
208 });
209
210 it("does not reuse a renderer cursor when a fresh attach has matching counters", () => {
211 const first = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
212 type: "snapshot",
213 snapshot: BASE_SNAPSHOT,
214 });
215 const cursor = readTerminalOutputUpdate(first.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
216 const next = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
217 type: "snapshot",
218 snapshot: { ...BASE_SNAPSHOT, history: "other" },
219 });
220 expect(next.output.resetVersion).toBe(first.output.resetVersion);
221 expect(next.output.nextOffset).toBe(first.output.nextOffset);
222 expect(readTerminalOutputUpdate(next.output, cursor)).toMatchObject({
223 type: "reset",
224 data: "other",
225 });
226 expect(next.lifecycleVersion).toBe(0);
227 });
228
229 it("appends every unread character across a compaction boundary", () => {
230 let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
231 type: "snapshot",
232 snapshot: { ...BASE_SNAPSHOT, history: "" },
233 });
234 let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
235 const writes = Array.from({ length: 1200 }, (_, index) => ["x", "é", "界", "🙂"][index % 4]!);
236 for (const [index, data] of writes.entries()) {
237 state = applyTerminalAttachStreamEvent(state, {
238 type: "output",
239 threadId: TARGET.threadId,
240 terminalId: TARGET.terminalId,
241 data,
242 });
243 if (index === 99) cursor = readTerminalOutputUpdate(state.output, cursor).cursor;
244 }
245 expect(readTerminalOutputUpdate(state.output, cursor)).toMatchObject({
246 type: "append",
247 data: writes.slice(100).join(""),
248 });
249 });
250});