import { describe, expect, it, vi } from "vite-plus/test";
import {
applyTerminalAttachStreamEvent,
combineTerminalSessionState,
EMPTY_TERMINAL_BUFFER_STATE,
INITIAL_TERMINAL_OUTPUT_CURSOR,
nextTerminalAttachSeedState,
} from "@t3tools/client-runtime/state/terminal";
import {
shouldClearTerminalSelectionAction,
shouldHandleTerminalExit,
synchronizeTerminalOutput,
synchronizeTerminalSession,
} from "./ThreadTerminalDrawer";
describe("terminal selection actions", () => {
it("does not let an old selection popup cancel its replacement right-click menu", () => {
expect(
shouldClearTerminalSelectionAction({
actionPending: false,
openMenuRequestId: 3,
currentRequestId: 4,
}),
).toBe(false);
expect(
shouldClearTerminalSelectionAction({
actionPending: false,
openMenuRequestId: null,
currentRequestId: 4,
}),
).toBe(false);
});
it("handles an exit that lands while the terminal surface is still loading", () => {
expect(shouldHandleTerminalExit("exited", "running", false, 1)).toBe(true);
expect(shouldHandleTerminalExit("exited", "exited", false, 1)).toBe(false);
expect(shouldHandleTerminalExit("closed", "running", true, 1)).toBe(false);
});
});
it("retains visible output and selection until a replacement subscription receives its snapshot", () => {
const terminal = {
resetAndWrite: vi.fn(),
write: vi.fn(),
getSelection: vi.fn(() => "selected"),
getSelectionCellCount: vi.fn(() => 8),
clearSelection: vi.fn(),
};
const snapshot = {
threadId: "thread",
terminalId: "term-1",
cwd: "/repo",
worktreePath: null,
status: "running" as const,
pid: 123,
history: "host output",
exitCode: null,
exitSignal: null,
label: "Terminal 1",
updatedAt: "2026-09-05T00:00:00.000Z",
};
const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
type: "snapshot",
snapshot,
});
const cursor = synchronizeTerminalOutput(terminal, attached, INITIAL_TERMINAL_OUTPUT_CURSOR);
expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output");
terminal.resetAndWrite.mockClear();
terminal.clearSelection.mockClear();
const pending = combineTerminalSessionState(
{ ...snapshot, hasRunningSubprocess: false },
EMPTY_TERMINAL_BUFFER_STATE,
);
expect(pending.status).toBe("running");
expect(synchronizeTerminalOutput(terminal, pending, cursor)).toBe(cursor);
expect(terminal.resetAndWrite).not.toHaveBeenCalled();
expect(terminal.write).not.toHaveBeenCalled();
expect(terminal.clearSelection).not.toHaveBeenCalled();
const observed = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
type: "snapshot",
snapshot: { ...snapshot, history: "host output\nobserved output" },
});
const observedCursor = synchronizeTerminalOutput(terminal, observed, cursor);
expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output\nobserved output");
const cleared = applyTerminalAttachStreamEvent(observed, {
type: "cleared",
threadId: "thread",
terminalId: "term-1",
});
synchronizeTerminalOutput(terminal, cleared, observedCursor);
expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("");
});
it("reports an attach failure that leaves the output unchanged", () => {
const terminal = {
resetAndWrite: vi.fn(),
write: vi.fn(),
getSelection: vi.fn(() => "selected"),
getSelectionCellCount: vi.fn(() => 8),
clearSelection: vi.fn(),
};
const previous = { ...EMPTY_TERMINAL_BUFFER_STATE, error: null };
const failed = { ...previous, error: "Terminal attach failed" };
expect(
synchronizeTerminalSession(terminal, previous, failed, INITIAL_TERMINAL_OUTPUT_CURSOR),
).toBe(INITIAL_TERMINAL_OUTPUT_CURSOR);
expect(terminal.write).toHaveBeenCalledWith("\r\n[terminal] Terminal attach failed\r\n");
expect(terminal.resetAndWrite).not.toHaveBeenCalled();
expect(terminal.getSelection).not.toHaveBeenCalled();
expect(terminal.getSelectionCellCount).not.toHaveBeenCalled();
expect(terminal.clearSelection).not.toHaveBeenCalled();
});