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} from "./ThreadTerminalDrawer";
15
16describe("terminal selection actions", () => {
17 it("does not let an old selection popup cancel its replacement right-click menu", () => {
18 expect(
19 shouldClearTerminalSelectionAction({
20 actionPending: false,
21 openMenuRequestId: 3,
22 currentRequestId: 4,
23 }),
24 ).toBe(false);
25 expect(
26 shouldClearTerminalSelectionAction({
27 actionPending: false,
28 openMenuRequestId: null,
29 currentRequestId: 4,
30 }),
31 ).toBe(false);
32 });
33
34 it("handles an exit that lands while the terminal surface is still loading", () => {
35 expect(shouldHandleTerminalExit("exited", "running", false, 1)).toBe(true);
36 expect(shouldHandleTerminalExit("exited", "exited", false, 1)).toBe(false);
37 expect(shouldHandleTerminalExit("closed", "running", true, 1)).toBe(false);
38 });
39});
40
41it("retains visible output and selection until a replacement subscription receives its snapshot", () => {
42 const terminal = { resetAndWrite: vi.fn(), write: vi.fn(), clearSelection: vi.fn() };
43 const snapshot = {
44 threadId: "thread",
45 terminalId: "term-1",
46 cwd: "/repo",
47 worktreePath: null,
48 status: "running" as const,
49 pid: 123,
50 history: "host output",
51 exitCode: null,
52 exitSignal: null,
53 label: "Terminal 1",
54 updatedAt: "2026-09-05T00:00:00.000Z",
55 };
56 const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
57 type: "snapshot",
58 snapshot,
59 });
60 const cursor = synchronizeTerminalOutput(terminal, attached, INITIAL_TERMINAL_OUTPUT_CURSOR);
61 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output");
62 terminal.resetAndWrite.mockClear();
63 terminal.clearSelection.mockClear();
64
65 const pending = combineTerminalSessionState(
66 { ...snapshot, hasRunningSubprocess: false },
67 EMPTY_TERMINAL_BUFFER_STATE,
68 );
69 expect(pending.status).toBe("running");
70 expect(synchronizeTerminalOutput(terminal, pending, cursor)).toBe(cursor);
71 expect(terminal.resetAndWrite).not.toHaveBeenCalled();
72 expect(terminal.write).not.toHaveBeenCalled();
73 expect(terminal.clearSelection).not.toHaveBeenCalled();
74
75 const observed = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
76 type: "snapshot",
77 snapshot: { ...snapshot, history: "host output\nobserved output" },
78 });
79 const observedCursor = synchronizeTerminalOutput(terminal, observed, cursor);
80 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output\nobserved output");
81
82 const cleared = applyTerminalAttachStreamEvent(observed, {
83 type: "cleared",
84 threadId: "thread",
85 terminalId: "term-1",
86 });
87 synchronizeTerminalOutput(terminal, cleared, observedCursor);
88 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("");
89});