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

ThreadTerminalDrawer.test.ts

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