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