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
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | EnvironmentId, |
| 5 | retainedTerminalHistory, |
| 6 | TerminalSessionSnapshot, |
| 7 | ThreadId, |
| 8 | } from "@t3tools/contracts"; |
| 9 | |
| 10 | import { |
| 11 | applyTerminalAttachStreamEvent, |
| 12 | applyTerminalMetadataStreamEvent, |
| 13 | combineTerminalSessionState, |
| 14 | EMPTY_TERMINAL_BUFFER_STATE, |
| 15 | INITIAL_TERMINAL_OUTPUT_CURSOR, |
| 16 | nextTerminalAttachSeedState, |
| 17 | readTerminalOutputUpdate, |
| 18 | terminalOutputText, |
| 19 | } from "./terminalSession.ts"; |
| 20 | |
| 21 | const TARGET = { |
| 22 | environmentId: EnvironmentId.make("env-local"), |
| 23 | threadId: ThreadId.make("thread-1"), |
| 24 | terminalId: "term-1", |
| 25 | } as const; |
| 26 | |
| 27 | const BASE_SNAPSHOT: TerminalSessionSnapshot = { |
| 28 | threadId: TARGET.threadId, |
| 29 | terminalId: TARGET.terminalId, |
| 30 | cwd: "/repo", |
| 31 | worktreePath: null, |
| 32 | status: "running", |
| 33 | pid: 123, |
| 34 | history: "hello", |
| 35 | exitCode: null, |
| 36 | exitSignal: null, |
| 37 | label: "Terminal 1", |
| 38 | updatedAt: "2026-04-01T00:00:00.000Z", |
| 39 | }; |
| 40 | |
| 41 | describe("terminal session reducers", () => { |
| 42 | it("prefers live attach status over stale metadata after the attach stream starts", () => { |
| 43 | const summary = applyTerminalMetadataStreamEvent([], { |
| 44 | type: "snapshot", |
| 45 | terminals: [ |
| 46 | { |
| 47 | threadId: BASE_SNAPSHOT.threadId, |
| 48 | terminalId: BASE_SNAPSHOT.terminalId, |
| 49 | cwd: BASE_SNAPSHOT.cwd, |
| 50 | worktreePath: BASE_SNAPSHOT.worktreePath, |
| 51 | status: "running", |
| 52 | pid: BASE_SNAPSHOT.pid, |
| 53 | exitCode: BASE_SNAPSHOT.exitCode, |
| 54 | exitSignal: BASE_SNAPSHOT.exitSignal, |
| 55 | updatedAt: BASE_SNAPSHOT.updatedAt, |
| 56 | hasRunningSubprocess: false, |
| 57 | label: BASE_SNAPSHOT.label, |
| 58 | }, |
| 59 | ], |
| 60 | })[0]!; |
| 61 | const attached = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, { |
| 62 | type: "error", |
| 63 | threadId: TARGET.threadId, |
| 64 | terminalId: TARGET.terminalId, |
| 65 | message: "Terminal disconnected.", |
| 66 | }); |
| 67 | |
| 68 | expect(combineTerminalSessionState(summary, attached)).toMatchObject({ |
| 69 | status: "error", |
| 70 | error: "Terminal disconnected.", |
| 71 | version: 1, |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | it("keeps a startup failure carried by an attach snapshot", () => { |
| 76 | const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { |
| 77 | type: "snapshot", |
| 78 | snapshot: { ...BASE_SNAPSHOT, status: "error", pid: null, error: "Failed to spawn PTY." }, |
| 79 | }); |
| 80 | |
| 81 | expect(attached).toMatchObject({ status: "error", error: "Failed to spawn PTY." }); |
| 82 | }); |
| 83 | |
| 84 | it("follows the shared PTY grid from snapshots and activity without disturbing output", () => { |
| 85 | const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { |
| 86 | type: "snapshot", |
| 87 | snapshot: { ...BASE_SNAPSHOT, cols: 130, rows: 15 }, |
| 88 | }); |
| 89 | const activity = { |
| 90 | type: "activity", |
| 91 | threadId: TARGET.threadId, |
| 92 | terminalId: TARGET.terminalId, |
| 93 | hasRunningSubprocess: false, |
| 94 | label: "Terminal 1", |
| 95 | } as const; |
| 96 | const narrowed = applyTerminalAttachStreamEvent(attached, { ...activity, cols: 54, rows: 15 }); |
| 97 | // Activity from a server without geometry, or a repeat, must not reset the renderer. |
| 98 | const unchanged = applyTerminalAttachStreamEvent( |
| 99 | applyTerminalAttachStreamEvent(narrowed, activity), |
| 100 | { ...activity, cols: 54, rows: 15 }, |
| 101 | ); |
| 102 | |
| 103 | expect(attached.grid).toEqual({ cols: 130, rows: 15 }); |
| 104 | expect(narrowed).toEqual({ ...attached, grid: { cols: 54, rows: 15 } }); |
| 105 | expect(unchanged).toBe(narrowed); |
| 106 | }); |
| 107 | |
| 108 | it("preserves a BOM code point at a new output chunk boundary", () => { |
| 109 | const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, { |
| 110 | type: "snapshot", |
| 111 | snapshot: { ...BASE_SNAPSHOT, history: "" }, |
| 112 | }); |
| 113 | const cursor = readTerminalOutputUpdate(initial.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor; |
| 114 | const data = `${"x".repeat(16_384)}\uFEFF${"y".repeat(20)}`; |
| 115 | const state = applyTerminalAttachStreamEvent(initial, { |
| 116 | type: "output", |
| 117 | threadId: TARGET.threadId, |
| 118 | terminalId: TARGET.terminalId, |
| 119 | data, |
| 120 | }); |
| 121 | |
| 122 | expect(terminalOutputText(state.output)).toBe(data); |
| 123 | expect(readTerminalOutputUpdate(state.output, cursor)).toMatchObject({ type: "append", data }); |
| 124 | expect(state.output.retainedBytes).toBe(new TextEncoder().encode(data).byteLength); |
| 125 | }); |
| 126 | |
| 127 | it("preserves a leading BOM in the retained snapshot tail", () => { |
| 128 | const state = applyTerminalAttachStreamEvent( |
| 129 | EMPTY_TERMINAL_BUFFER_STATE, |
| 130 | { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history: "discard\uFEFFtail" } }, |
| 131 | 7, |
| 132 | ); |
| 133 | |
| 134 | expect(terminalOutputText(state.output)).toBe("\uFEFFtail"); |
| 135 | expect(state.output.retainedBytes).toBe(7); |
| 136 | const serverTail = retainedTerminalHistory("discardtail", 7); |
| 137 | expect( |
| 138 | applyTerminalAttachStreamEvent( |
| 139 | EMPTY_TERMINAL_BUFFER_STATE, |
| 140 | { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history: serverTail } }, |
| 141 | 7, |
| 142 | ), |
| 143 | ).toEqual(state); |
| 144 | }); |
| 145 | |
| 146 | it("trims whole Unicode code points from a partially retained chunk", () => { |
| 147 | let state = applyTerminalAttachStreamEvent( |
| 148 | EMPTY_TERMINAL_BUFFER_STATE, |
| 149 | { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history: "é界🙂end" } }, |
| 150 | 12, |
| 151 | ); |
| 152 | let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor; |
| 153 | for (const [data, expected, retainedBytes] of [ |
| 154 | ["x", "界🙂endx", 11], |
| 155 | ["yz", "🙂endxyz", 10], |
| 156 | ["abc", "endxyzabc", 9], |
| 157 | ] as const) { |
| 158 | state = applyTerminalAttachStreamEvent( |
| 159 | state, |
| 160 | { type: "output", threadId: TARGET.threadId, terminalId: TARGET.terminalId, data }, |
| 161 | 12, |
| 162 | ); |
| 163 | const update = readTerminalOutputUpdate(state.output, cursor); |
| 164 | expect(update).toMatchObject({ type: "append", data }); |
| 165 | expect(terminalOutputText(state.output)).toBe(expected); |
| 166 | expect(state.output.retainedBytes).toBe(retainedBytes); |
| 167 | cursor = update.cursor; |
| 168 | } |
| 169 | }); |
| 170 | |
| 171 | it("does not retain the tail of a control sequence whose introducer was trimmed", () => { |
| 172 | for (const [history, maxBytes, replayable] of [ |
| 173 | ["prefix\u001b[31mOK", 6, /^(?:[a-z]*\u001b\[31mOK|O?K?)$/], |
| 174 | ["prefix\u001b]0;HIDDEN\u0007OK", 10, /^(?:[a-z]*\u001b\]0;HIDDEN\u0007OK|O?K?)$/], |
| 175 | ["prefix\u001b]0;HIDDEN\u0007OK", 4, /^(?:[a-z]*\u001b\]0;HIDDEN\u0007OK|O?K?)$/], |
| 176 | ] as const) { |
| 177 | const state = applyTerminalAttachStreamEvent( |
| 178 | EMPTY_TERMINAL_BUFFER_STATE, |
| 179 | { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history } }, |
| 180 | maxBytes, |
| 181 | ); |
| 182 | expect(terminalOutputText(state.output)).toBe("OK"); |
| 183 | |
| 184 | // The same text as live output: a mounted terminal still receives every byte, while one |
| 185 | // that mounts later, even inside the sequence, never prints the rest of it. |
| 186 | let live = applyTerminalAttachStreamEvent( |
| 187 | EMPTY_TERMINAL_BUFFER_STATE, |
| 188 | { type: "snapshot", snapshot: { ...BASE_SNAPSHOT, history: "" } }, |
| 189 | maxBytes, |
| 190 | ); |
| 191 | const readers = [{ cursor: INITIAL_TERMINAL_OUTPUT_CURSOR, written: "" }]; |
| 192 | for (const data of history) { |
| 193 | live = applyTerminalAttachStreamEvent( |
| 194 | live, |
| 195 | { type: "output", threadId: TARGET.threadId, terminalId: TARGET.terminalId, data }, |
| 196 | maxBytes, |
| 197 | ); |
| 198 | readers.push({ cursor: INITIAL_TERMINAL_OUTPUT_CURSOR, written: "" }); |
| 199 | for (const reader of readers) { |
| 200 | const update = readTerminalOutputUpdate(live.output, reader.cursor); |
| 201 | if (update.type === "reset") reader.written = update.data; |
| 202 | if (update.type === "append") reader.written += update.data; |
| 203 | reader.cursor = update.cursor; |
| 204 | } |
| 205 | } |
| 206 | expect(readers[0]!.written).toBe(history); |
| 207 | for (const reader of readers) expect(reader.written).toMatch(replayable); |
| 208 | expect(terminalOutputText(live.output)).toBe("OK"); |
| 209 | } |
| 210 | }); |
| 211 | |
| 212 | it("delivers all output when several events arrive before a renderer reads", () => { |
| 213 | const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, { |
| 214 | type: "snapshot", |
| 215 | snapshot: BASE_SNAPSHOT, |
| 216 | }); |
| 217 | const cursor = readTerminalOutputUpdate(initial.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor; |
| 218 | let state = initial; |
| 219 | for (const data of [" one", " two", " three"]) { |
| 220 | state = applyTerminalAttachStreamEvent(state, { |
| 221 | type: "output", |
| 222 | threadId: TARGET.threadId, |
| 223 | terminalId: TARGET.terminalId, |
| 224 | data, |
| 225 | }); |
| 226 | } |
| 227 | const update = readTerminalOutputUpdate(state.output, cursor); |
| 228 | expect(update).toMatchObject({ type: "append", data: " one two three" }); |
| 229 | expect(readTerminalOutputUpdate(state.output, update.cursor).type).toBe("none"); |
| 230 | }); |
| 231 | |
| 232 | it("bounds historical chunk inspection when reading new tail output", () => { |
| 233 | let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, { |
| 234 | type: "snapshot", |
| 235 | snapshot: { ...BASE_SNAPSHOT, history: "" }, |
| 236 | }); |
| 237 | for (let index = 0; index < 512; index += 1) { |
| 238 | state = applyTerminalAttachStreamEvent(state, { |
| 239 | type: "output", |
| 240 | threadId: TARGET.threadId, |
| 241 | terminalId: TARGET.terminalId, |
| 242 | data: "history\r\n", |
| 243 | }); |
| 244 | } |
| 245 | const cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor; |
| 246 | const next = applyTerminalAttachStreamEvent(state, { |
| 247 | type: "output", |
| 248 | threadId: TARGET.threadId, |
| 249 | terminalId: TARGET.terminalId, |
| 250 | data: "latest\r\n", |
| 251 | }); |
| 252 | let historicalChunkReads = 0; |
| 253 | const chunks = next.output.chunks.map((chunk, index) => |
| 254 | index < state.output.chunks.length |
| 255 | ? { |
| 256 | ...chunk, |
| 257 | get startOffset() { |
| 258 | historicalChunkReads += 1; |
| 259 | return chunk.startOffset; |
| 260 | }, |
| 261 | } |
| 262 | : chunk, |
| 263 | ); |
| 264 | |
| 265 | expect(readTerminalOutputUpdate({ ...next.output, chunks }, cursor)).toMatchObject({ |
| 266 | type: "append", |
| 267 | data: "latest\r\n", |
| 268 | }); |
| 269 | expect(historicalChunkReads).toBeLessThanOrEqual(2); |
| 270 | }); |
| 271 | |
| 272 | it("preserves the byte-limited tail and resets a cursor before a partially trimmed chunk", () => { |
| 273 | const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, { |
| 274 | type: "snapshot", |
| 275 | snapshot: { ...BASE_SNAPSHOT, history: "" }, |
| 276 | }); |
| 277 | const staleCursor = readTerminalOutputUpdate( |
| 278 | initial.output, |
| 279 | INITIAL_TERMINAL_OUTPUT_CURSOR, |
| 280 | ).cursor; |
| 281 | const first = applyTerminalAttachStreamEvent(initial, { |
| 282 | type: "output", |
| 283 | threadId: TARGET.threadId, |
| 284 | terminalId: TARGET.terminalId, |
| 285 | data: "hello", |
| 286 | }); |
| 287 | const caughtUpCursor = readTerminalOutputUpdate(first.output, staleCursor).cursor; |
| 288 | const state = applyTerminalAttachStreamEvent( |
| 289 | first, |
| 290 | { type: "output", threadId: TARGET.threadId, terminalId: TARGET.terminalId, data: " world" }, |
| 291 | 8, |
| 292 | ); |
| 293 | |
| 294 | expect(readTerminalOutputUpdate(state.output, caughtUpCursor)).toMatchObject({ |
| 295 | type: "append", |
| 296 | data: " world", |
| 297 | }); |
| 298 | expect(readTerminalOutputUpdate(state.output, staleCursor)).toMatchObject({ |
| 299 | type: "reset", |
| 300 | data: "lo world", |
| 301 | }); |
| 302 | expect(state.output.retainedBytes).toBe(8); |
| 303 | }); |
| 304 | |
| 305 | it("resets for repeated snapshots and restart even when output text repeats, and erases on clear", () => { |
| 306 | let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, { |
| 307 | type: "snapshot", |
| 308 | snapshot: BASE_SNAPSHOT, |
| 309 | }); |
| 310 | let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor; |
| 311 | const attachedGeneration = state.output.generation; |
| 312 | // A socket reconnect resubscribes the same stream, so the server's new snapshot gets no seed. |
| 313 | state = applyTerminalAttachStreamEvent(state, { type: "snapshot", snapshot: BASE_SNAPSHOT }); |
| 314 | const repeated = readTerminalOutputUpdate(state.output, cursor); |
| 315 | expect(repeated).toMatchObject({ type: "reset", data: "hello" }); |
| 316 | expect(state.version).toBe(2); |
| 317 | // Viewers replay their measured grid once per generation; the new attachment holds the old size. |
| 318 | expect(state.output.generation).not.toBe(attachedGeneration); |
| 319 | cursor = repeated.cursor; |
| 320 | |
| 321 | state = applyTerminalAttachStreamEvent(state, { |
| 322 | type: "cleared", |
| 323 | threadId: TARGET.threadId, |
| 324 | terminalId: TARGET.terminalId, |
| 325 | }); |
| 326 | // Clear erases text in place: a full reset would drop the running program's modes. |
| 327 | const erase = "\u001b[H\u001b[2J\u001b[3J"; |
| 328 | const cleared = readTerminalOutputUpdate(state.output, cursor); |
| 329 | expect(cleared).toMatchObject({ type: "append", data: erase }); |
| 330 | expect(readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR)).toMatchObject({ |
| 331 | type: "reset", |
| 332 | data: `hello${erase}`, |
| 333 | }); |
| 334 | expect(terminalOutputText(state.output)).toBe(""); |
| 335 | const afterClear = applyTerminalAttachStreamEvent(state, { |
| 336 | type: "output", |
| 337 | threadId: TARGET.threadId, |
| 338 | terminalId: TARGET.terminalId, |
| 339 | data: "next", |
| 340 | }); |
| 341 | expect(terminalOutputText(afterClear.output)).toBe("next"); |
| 342 | state = applyTerminalAttachStreamEvent(state, { |
| 343 | type: "restarted", |
| 344 | threadId: TARGET.threadId, |
| 345 | terminalId: TARGET.terminalId, |
| 346 | snapshot: BASE_SNAPSHOT, |
| 347 | }); |
| 348 | expect(readTerminalOutputUpdate(state.output, cleared.cursor)).toMatchObject({ |
| 349 | type: "reset", |
| 350 | data: "hello", |
| 351 | }); |
| 352 | expect(state.lifecycleVersion).toBe(2); |
| 353 | }); |
| 354 | |
| 355 | it("does not reuse a renderer cursor when a fresh attach has matching counters", () => { |
| 356 | const first = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { |
| 357 | type: "snapshot", |
| 358 | snapshot: BASE_SNAPSHOT, |
| 359 | }); |
| 360 | const cursor = readTerminalOutputUpdate(first.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor; |
| 361 | const next = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { |
| 362 | type: "snapshot", |
| 363 | snapshot: { ...BASE_SNAPSHOT, history: "other" }, |
| 364 | }); |
| 365 | expect(next.output.resetVersion).toBe(first.output.resetVersion); |
| 366 | expect(next.output.nextOffset).toBe(first.output.nextOffset); |
| 367 | expect(readTerminalOutputUpdate(next.output, cursor)).toMatchObject({ |
| 368 | type: "reset", |
| 369 | data: "other", |
| 370 | }); |
| 371 | expect(next.lifecycleVersion).toBe(0); |
| 372 | }); |
| 373 | |
| 374 | it("appends every unread character across a compaction boundary", () => { |
| 375 | let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, { |
| 376 | type: "snapshot", |
| 377 | snapshot: { ...BASE_SNAPSHOT, history: "" }, |
| 378 | }); |
| 379 | let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor; |
| 380 | const writes = Array.from({ length: 1200 }, (_, index) => ["x", "é", "界", "🙂"][index % 4]!); |
| 381 | for (const [index, data] of writes.entries()) { |
| 382 | state = applyTerminalAttachStreamEvent(state, { |
| 383 | type: "output", |
| 384 | threadId: TARGET.threadId, |
| 385 | terminalId: TARGET.terminalId, |
| 386 | data, |
| 387 | }); |
| 388 | if (index === 99) cursor = readTerminalOutputUpdate(state.output, cursor).cursor; |
| 389 | } |
| 390 | expect(readTerminalOutputUpdate(state.output, cursor)).toMatchObject({ |
| 391 | type: "append", |
| 392 | data: writes.slice(100).join(""), |
| 393 | }); |
| 394 | }); |
| 395 | }); |