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, vi } from "vite-plus/test"; |
| 2 | import { |
| 3 | applyTerminalAttachStreamEvent, |
| 4 | combineTerminalSessionState, |
| 5 | EMPTY_TERMINAL_BUFFER_STATE, |
| 6 | INITIAL_TERMINAL_OUTPUT_CURSOR, |
| 7 | nextTerminalAttachSeedState, |
| 8 | } from "@t3tools/client-runtime/state/terminal"; |
| 9 | |
| 10 | import { |
| 11 | shouldHandleTerminalExit, |
| 12 | synchronizeTerminalOutput, |
| 13 | synchronizeTerminalSession, |
| 14 | } from "./ThreadTerminalDrawer"; |
| 15 | |
| 16 | describe("terminal selection actions", () => { |
| 17 | it("handles an exit that lands while the terminal surface is still loading", () => { |
| 18 | expect(shouldHandleTerminalExit("exited", "running", false, 1)).toBe(true); |
| 19 | expect(shouldHandleTerminalExit("exited", "exited", false, 1)).toBe(false); |
| 20 | expect(shouldHandleTerminalExit("closed", "running", true, 1)).toBe(false); |
| 21 | }); |
| 22 | }); |
| 23 | |
| 24 | it("retains visible output and selection until a replacement subscription receives its snapshot", () => { |
| 25 | const terminal = { |
| 26 | resetAndWrite: vi.fn(), |
| 27 | write: vi.fn(), |
| 28 | getSelection: vi.fn(() => "selected"), |
| 29 | getSelectionCellCount: vi.fn(() => 8), |
| 30 | clearSelection: vi.fn(), |
| 31 | }; |
| 32 | const snapshot = { |
| 33 | threadId: "thread", |
| 34 | terminalId: "term-1", |
| 35 | cwd: "/repo", |
| 36 | worktreePath: null, |
| 37 | status: "running" as const, |
| 38 | pid: 123, |
| 39 | history: "host output", |
| 40 | exitCode: null, |
| 41 | exitSignal: null, |
| 42 | label: "Terminal 1", |
| 43 | updatedAt: "2026-09-05T00:00:00.000Z", |
| 44 | }; |
| 45 | const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { |
| 46 | type: "snapshot", |
| 47 | snapshot, |
| 48 | }); |
| 49 | const cursor = synchronizeTerminalOutput(terminal, attached, INITIAL_TERMINAL_OUTPUT_CURSOR); |
| 50 | expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output"); |
| 51 | terminal.resetAndWrite.mockClear(); |
| 52 | terminal.clearSelection.mockClear(); |
| 53 | |
| 54 | const pending = combineTerminalSessionState( |
| 55 | { ...snapshot, hasRunningSubprocess: false }, |
| 56 | EMPTY_TERMINAL_BUFFER_STATE, |
| 57 | ); |
| 58 | expect(pending.status).toBe("running"); |
| 59 | expect(synchronizeTerminalOutput(terminal, pending, cursor)).toBe(cursor); |
| 60 | expect(terminal.resetAndWrite).not.toHaveBeenCalled(); |
| 61 | expect(terminal.write).not.toHaveBeenCalled(); |
| 62 | expect(terminal.clearSelection).not.toHaveBeenCalled(); |
| 63 | |
| 64 | const observed = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { |
| 65 | type: "snapshot", |
| 66 | snapshot: { ...snapshot, history: "host output\nobserved output" }, |
| 67 | }); |
| 68 | const observedCursor = synchronizeTerminalOutput(terminal, observed, cursor); |
| 69 | expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output\nobserved output"); |
| 70 | |
| 71 | const cleared = applyTerminalAttachStreamEvent(observed, { |
| 72 | type: "cleared", |
| 73 | threadId: "thread", |
| 74 | terminalId: "term-1", |
| 75 | }); |
| 76 | terminal.resetAndWrite.mockClear(); |
| 77 | synchronizeTerminalOutput(terminal, cleared, observedCursor); |
| 78 | expect(terminal.resetAndWrite).not.toHaveBeenCalled(); |
| 79 | expect(terminal.write).toHaveBeenLastCalledWith("\u001b[H\u001b[2J\u001b[3J"); |
| 80 | }); |
| 81 | |
| 82 | it("reports an attach failure that leaves the output unchanged", () => { |
| 83 | const terminal = { |
| 84 | resetAndWrite: vi.fn(), |
| 85 | write: vi.fn(), |
| 86 | getSelection: vi.fn(() => "selected"), |
| 87 | getSelectionCellCount: vi.fn(() => 8), |
| 88 | clearSelection: vi.fn(), |
| 89 | }; |
| 90 | const previous = { ...EMPTY_TERMINAL_BUFFER_STATE, error: null }; |
| 91 | const failed = { ...previous, error: "Terminal attach failed" }; |
| 92 | |
| 93 | expect( |
| 94 | synchronizeTerminalSession(terminal, previous, failed, INITIAL_TERMINAL_OUTPUT_CURSOR), |
| 95 | ).toBe(INITIAL_TERMINAL_OUTPUT_CURSOR); |
| 96 | expect(terminal.write).toHaveBeenCalledWith("\r\n[terminal] Terminal attach failed\r\n"); |
| 97 | expect(terminal.resetAndWrite).not.toHaveBeenCalled(); |
| 98 | expect(terminal.getSelection).not.toHaveBeenCalled(); |
| 99 | expect(terminal.getSelectionCellCount).not.toHaveBeenCalled(); |
| 100 | expect(terminal.clearSelection).not.toHaveBeenCalled(); |
| 101 | |
| 102 | const snapshot = { |
| 103 | threadId: "thread", |
| 104 | terminalId: "term-1", |
| 105 | cwd: "/repo", |
| 106 | worktreePath: null, |
| 107 | status: "error" as const, |
| 108 | pid: null, |
| 109 | history: "", |
| 110 | exitCode: null, |
| 111 | exitSignal: null, |
| 112 | label: "Terminal", |
| 113 | updatedAt: "2026-11-15T00:00:00.000Z", |
| 114 | error: "Terminal attach failed", |
| 115 | }; |
| 116 | const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { |
| 117 | type: "snapshot", |
| 118 | snapshot, |
| 119 | }); |
| 120 | const cursor = synchronizeTerminalSession( |
| 121 | terminal, |
| 122 | failed, |
| 123 | attached, |
| 124 | INITIAL_TERMINAL_OUTPUT_CURSOR, |
| 125 | ); |
| 126 | terminal.write.mockClear(); |
| 127 | const reconnected = applyTerminalAttachStreamEvent(attached, { type: "snapshot", snapshot }); |
| 128 | synchronizeTerminalSession(terminal, attached, reconnected, cursor); |
| 129 | expect(terminal.resetAndWrite).toHaveBeenLastCalledWith(""); |
| 130 | expect(terminal.write).toHaveBeenCalledWith("\r\n[terminal] Terminal attach failed\r\n"); |
| 131 | }); |