Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadTerminalDrawer.test.ts

208 lines6.7 KB

1import { afterEach, 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 terminalContextMenuItems,
14 synchronizeTerminalOutput,
15 terminalSelectionLineRange,
16 terminalSelectionMenuItems,
17 terminalThemeFromApp,
18} from "./ThreadTerminalDrawer";
19​
20describe("terminal selection menus", () => {
21 it("omits Add to chat when the terminal has no chat target", () => {
22 expect(terminalSelectionMenuItems().map(({ id }) => id)).toEqual(["add-to-chat", "copy"]);
23 expect(terminalContextMenuItems({ hasSelection: true }).map(({ id }) => id)).toEqual([
24 "add-to-chat",
25 "copy",
26 "paste",
27 ]);
28​
29 expect(terminalSelectionMenuItems({ canAddToChat: false }).map(({ id }) => id)).toEqual([
30 "copy",
31 ]);
32 expect(
33 terminalContextMenuItems({ hasSelection: true, canAddToChat: false }).map(({ id }) => id),
34 ).toEqual(["copy", "paste"]);
35 });
36});
37​
38describe("terminalThemeFromApp", () => {
39 afterEach(() => {
40 vi.unstubAllGlobals();
41 });
42​
43 it("uses terminal colors inherited by the mount instead of a light document theme", () => {
44 const root = { classList: { contains: () => false } };
45 const body = {};
46 const drawer = {};
47 let canvasColor = "#000";
48 const colors: Record<string, [number, number, number, number]> = {
49 "#000": [0, 0, 0, 255],
50 "#fff": [255, 255, 255, 255],
51 "#ddd": [221, 221, 221, 255],
52 "#111": [17, 17, 17, 255],
53 };
54​
55 vi.stubGlobal("document", {
56 documentElement: root,
57 body,
58 querySelector: () => drawer,
59 createElement: () => ({
60 width: 0,
61 height: 0,
62 getContext: () => ({
63 clearRect: () => undefined,
64 fillRect: () => undefined,
65 get fillStyle() {
66 return canvasColor;
67 },
68 set fillStyle(value: string) {
69 canvasColor = value;
70 },
71 getImageData: () => ({ data: colors[canvasColor] ?? [0, 0, 0, 0] }),
72 }),
73 }),
74 });
75 vi.stubGlobal("getComputedStyle", (element: object) => {
76 const local = element === drawer;
77 const values = local
78 ? {
79 "--terminal-background": "#000",
80 "--terminal-foreground": "#fff",
81 "--terminal-cursor": "#ddd",
82 "--terminal-selection-background": "rgba(255, 255, 255, 0.2)",
83 }
84 : {
85 "--terminal-background": "#fff",
86 "--terminal-foreground": "#111",
87 };
88 return {
89 backgroundColor: local ? "#000" : "#fff",
90 color: local ? "#fff" : "#111",
91 colorScheme: local ? "dark" : "light",
92 getPropertyValue: (name: string) => values[name as keyof typeof values] ?? "",
93 };
94 });
95​
96 const theme = terminalThemeFromApp();
97​
98 expect(theme.background).toEqual({ r: 0, g: 0, b: 0 });
99 expect(theme.foreground).toEqual({ r: 255, g: 255, b: 255 });
100 expect(theme.cursor).toEqual({ r: 221, g: 221, b: 221 });
101 });
102});
103​
104describe("terminal selection actions", () => {
105 it("clears a pending or currently owned menu when the selection disappears", () => {
106 expect(
107 shouldClearTerminalSelectionAction({
108 actionPending: true,
109 openMenuRequestId: null,
110 currentRequestId: 4,
111 }),
112 ).toBe(true);
113 expect(
114 shouldClearTerminalSelectionAction({
115 actionPending: false,
116 openMenuRequestId: 4,
117 currentRequestId: 4,
118 }),
119 ).toBe(true);
120 });
121​
122 it("does not let an old selection popup cancel its replacement right-click menu", () => {
123 expect(
124 shouldClearTerminalSelectionAction({
125 actionPending: false,
126 openMenuRequestId: 3,
127 currentRequestId: 4,
128 }),
129 ).toBe(false);
130 expect(
131 shouldClearTerminalSelectionAction({
132 actionPending: false,
133 openMenuRequestId: null,
134 currentRequestId: 4,
135 }),
136 ).toBe(false);
137 });
138​
139 it("uses Ghostty's physical screen range for visually wrapped selections", () => {
140 expect(
141 terminalSelectionLineRange({
142 start: { y: 4 },
143 end: { y: 6 },
144 }),
145 ).toEqual({ lineStart: 5, lineEnd: 7 });
146 });
147​
148 it("handles an exit that lands while the terminal surface is still loading", () => {
149 expect(shouldHandleTerminalExit("exited", "running", false, 1)).toBe(true);
150 expect(shouldHandleTerminalExit("exited", "exited", false, 1)).toBe(false);
151 expect(shouldHandleTerminalExit("closed", "running", true, 1)).toBe(false);
152 });
153​
154 it.each(["closed", "exited"] as const)("ignores an unsynchronized %s seed", (status) => {
155 expect(shouldHandleTerminalExit(status, "running", false, 0)).toBe(false);
156 expect(shouldHandleTerminalExit(status, "running", false, 1)).toBe(true);
157 });
158});
159​
160it("retains visible output and selection until a replacement subscription receives its snapshot", () => {
161 const terminal = { resetAndWrite: vi.fn(), write: vi.fn(), clearSelection: vi.fn() };
162 const snapshot = {
163 threadId: "thread",
164 terminalId: "term-1",
165 cwd: "/repo",
166 worktreePath: null,
167 status: "running" as const,
168 pid: 123,
169 history: "host output",
170 exitCode: null,
171 exitSignal: null,
172 label: "Terminal 1",
173 updatedAt: "2026-09-05T00:00:00.000Z",
174 };
175 const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
176 type: "snapshot",
177 snapshot,
178 });
179 const cursor = synchronizeTerminalOutput(terminal, attached, INITIAL_TERMINAL_OUTPUT_CURSOR);
180 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output");
181 terminal.resetAndWrite.mockClear();
182 terminal.clearSelection.mockClear();
183​
184 const pending = combineTerminalSessionState(
185 { ...snapshot, hasRunningSubprocess: false },
186 EMPTY_TERMINAL_BUFFER_STATE,
187 );
188 expect(pending.status).toBe("running");
189 expect(synchronizeTerminalOutput(terminal, pending, cursor)).toBe(cursor);
190 expect(terminal.resetAndWrite).not.toHaveBeenCalled();
191 expect(terminal.write).not.toHaveBeenCalled();
192 expect(terminal.clearSelection).not.toHaveBeenCalled();
193​
194 const observed = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
195 type: "snapshot",
196 snapshot: { ...snapshot, history: "host output\nobserved output" },
197 });
198 const observedCursor = synchronizeTerminalOutput(terminal, observed, cursor);
199 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("host output\nobserved output");
200​
201 const cleared = applyTerminalAttachStreamEvent(observed, {
202 type: "cleared",
203 threadId: "thread",
204 terminalId: "term-1",
205 });
206 synchronizeTerminalOutput(terminal, cleared, observedCursor);
207 expect(terminal.resetAndWrite).toHaveBeenLastCalledWith("");
208});