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

ThreadTerminalDrawer.test.ts

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