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 = { resetAndWrite: vi.fn(), write: vi.fn(), clearSelection: vi.fn() };
44 const snapshot = {
45 threadId: "thread",
46 terminalId: "term-1",
47 cwd: "/repo",
48 worktreePath: null,
49 status: "running" as const,
50 pid: 123,
51 history: "host output",
52 exitCode: null,
53 exitSignal: null,
54 label: "Terminal 1",
55 updatedAt: "2026-09-05T00:00:00.000Z",
56 };
57 const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
58 type: "snapshot",
59 snapshot,
60 });
61 const cursor = synchronizeTerminalOutput(terminal, attached, INITIAL_TERMINAL_OUTPUT_CURSOR);
62 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output");
63 terminal.resetAndWrite.mockClear();
64 terminal.clearSelection.mockClear();
65
66 const pending = combineTerminalSessionState(
67 { ...snapshot, hasRunningSubprocess: false },
68 EMPTY_TERMINAL_BUFFER_STATE,
69 );
70 expect(pending.status).toBe("running");
71 expect(synchronizeTerminalOutput(terminal, pending, cursor)).toBe(cursor);
72 expect(terminal.resetAndWrite).not.toHaveBeenCalled();
73 expect(terminal.write).not.toHaveBeenCalled();
74 expect(terminal.clearSelection).not.toHaveBeenCalled();
75
76 const observed = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
77 type: "snapshot",
78 snapshot: { ...snapshot, history: "host output\nobserved output" },
79 });
80 const observedCursor = synchronizeTerminalOutput(terminal, observed, cursor);
81 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output\nobserved output");
82
83 const cleared = applyTerminalAttachStreamEvent(observed, {
84 type: "cleared",
85 threadId: "thread",
86 terminalId: "term-1",
87 });
88 synchronizeTerminalOutput(terminal, cleared, observedCursor);
89 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("");
90});
91
92it("reports an attach failure that leaves the output unchanged", () => {
93 const terminal = { resetAndWrite: vi.fn(), write: vi.fn(), clearSelection: vi.fn() };
94 const previous = { ...EMPTY_TERMINAL_BUFFER_STATE, error: null };
95 const failed = { ...previous, error: "Terminal attach failed" };
96
97 expect(
98 synchronizeTerminalSession(terminal, previous, failed, INITIAL_TERMINAL_OUTPUT_CURSOR),
99 ).toBe(INITIAL_TERMINAL_OUTPUT_CURSOR);
100 expect(terminal.write).toHaveBeenCalledWith("\r\n[terminal] Terminal attach failed\r\n");
101 expect(terminal.resetAndWrite).not.toHaveBeenCalled();
102 expect(terminal.clearSelection).not.toHaveBeenCalled();
103});