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

ThreadTerminalDrawer.browser.tsx

318 lines7.9 KB

1import "../index.css";
2​
3import { scopeThreadRef } from "@t3tools/client-runtime";
4import { ThreadId } from "@t3tools/contracts";
5import { afterEach, describe, expect, it, vi } from "vitest";
6import { render } from "vitest-browser-react";
7​
8const {
9 terminalConstructorSpy,
10 terminalDisposeSpy,
11 fitAddonFitSpy,
12 fitAddonLoadSpy,
13 environmentApiById,
14 readEnvironmentApiMock,
15 readLocalApiMock,
16} = vi.hoisted(() => ({
17 terminalConstructorSpy: vi.fn(),
18 terminalDisposeSpy: vi.fn(),
19 fitAddonFitSpy: vi.fn(),
20 fitAddonLoadSpy: vi.fn(),
21 environmentApiById: new Map<string, { terminal: { open: ReturnType<typeof vi.fn> } }>(),
22 readEnvironmentApiMock: vi.fn((environmentId: string) => environmentApiById.get(environmentId)),
23 readLocalApiMock: vi.fn<
24 () =>
25 | {
26 contextMenu: { show: ReturnType<typeof vi.fn> };
27 shell: { openExternal: ReturnType<typeof vi.fn> };
28 }
29 | undefined
30 >(() => ({
31 contextMenu: { show: vi.fn(async () => null) },
32 shell: { openExternal: vi.fn(async () => undefined) },
33 })),
34}));
35​
36vi.mock("@xterm/addon-fit", () => ({
37 FitAddon: class MockFitAddon {
38 fit = fitAddonFitSpy;
39 },
40}));
41​
42vi.mock("@xterm/xterm", () => ({
43 Terminal: class MockTerminal {
44 cols = 80;
45 rows = 24;
46 options: { theme?: unknown } = {};
47 buffer = {
48 active: {
49 viewportY: 0,
50 baseY: 0,
51 getLine: vi.fn(() => null),
52 },
53 };
54​
55 constructor(options: unknown) {
56 terminalConstructorSpy(options);
57 }
58​
59 loadAddon(addon: unknown) {
60 fitAddonLoadSpy(addon);
61 }
62​
63 open() {}
64​
65 write() {}
66​
67 clear() {}
68​
69 clearSelection() {}
70​
71 focus() {}
72​
73 refresh() {}
74​
75 scrollToBottom() {}
76​
77 hasSelection() {
78 return false;
79 }
80​
81 getSelection() {
82 return "";
83 }
84​
85 getSelectionPosition() {
86 return null;
87 }
88​
89 attachCustomKeyEventHandler() {
90 return true;
91 }
92​
93 registerLinkProvider() {
94 return { dispose: vi.fn() };
95 }
96​
97 onData() {
98 return { dispose: vi.fn() };
99 }
100​
101 onSelectionChange() {
102 return { dispose: vi.fn() };
103 }
104​
105 dispose() {
106 terminalDisposeSpy();
107 }
108 },
109}));
110​
111vi.mock("~/environmentApi", () => ({
112 readEnvironmentApi: readEnvironmentApiMock,
113}));
114​
115vi.mock("~/localApi", () => ({
116 ensureLocalApi: vi.fn(() => {
117 throw new Error("ensureLocalApi not implemented in browser test");
118 }),
119 readLocalApi: readLocalApiMock,
120}));
121​
122import { TerminalViewport } from "./ThreadTerminalDrawer";
123​
124const THREAD_ID = ThreadId.make("thread-terminal-browser");
125​
126function createEnvironmentApi() {
127 return {
128 terminal: {
129 open: vi.fn(async () => ({
130 threadId: THREAD_ID,
131 terminalId: "default",
132 cwd: "/repo/project",
133 worktreePath: null,
134 status: "running" as const,
135 pid: 123,
136 history: "",
137 exitCode: null,
138 exitSignal: null,
139 updatedAt: "2026-04-07T00:00:00.000Z",
140 })),
141 write: vi.fn(async () => undefined),
142 resize: vi.fn(async () => undefined),
143 },
144 };
146​
147async function mountTerminalViewport(props: {
148 threadRef: ReturnType<typeof scopeThreadRef>;
149 drawerBackgroundColor?: string;
150 drawerTextColor?: string;
151}) {
152 const drawer = document.createElement("div");
153 drawer.className = "thread-terminal-drawer";
154 if (props.drawerBackgroundColor) {
155 drawer.style.backgroundColor = props.drawerBackgroundColor;
156 }
157 if (props.drawerTextColor) {
158 drawer.style.color = props.drawerTextColor;
159 }
160​
161 const host = document.createElement("div");
162 host.style.width = "800px";
163 host.style.height = "400px";
164 drawer.append(host);
165 document.body.append(drawer);
166​
167 const screen = await render(
168 <TerminalViewport
169 threadRef={props.threadRef}
170 threadId={THREAD_ID}
171 terminalId="default"
172 terminalLabel="Terminal"
173 cwd="/repo/project"
174 onSessionExited={() => undefined}
175 onAddTerminalContext={() => undefined}
176 focusRequestId={0}
177 autoFocus={false}
178 resizeEpoch={0}
179 drawerHeight={320}
180 />,
181 { container: host },
182 );
183​
184 return {
185 rerender: async (nextProps: { threadRef: ReturnType<typeof scopeThreadRef> }) => {
186 await screen.rerender(
187 <TerminalViewport
188 threadRef={nextProps.threadRef}
189 threadId={THREAD_ID}
190 terminalId="default"
191 terminalLabel="Terminal"
192 cwd="/repo/project"
193 onSessionExited={() => undefined}
194 onAddTerminalContext={() => undefined}
195 focusRequestId={0}
196 autoFocus={false}
197 resizeEpoch={0}
198 drawerHeight={320}
199 />,
200 );
201 },
202 cleanup: async () => {
203 await screen.unmount();
204 drawer.remove();
205 },
206 };
208​
209describe("TerminalViewport", () => {
210 afterEach(() => {
211 environmentApiById.clear();
212 readEnvironmentApiMock.mockClear();
213 readLocalApiMock.mockClear();
214 terminalConstructorSpy.mockClear();
215 terminalDisposeSpy.mockClear();
216 fitAddonFitSpy.mockClear();
217 fitAddonLoadSpy.mockClear();
218 });
219​
220 it("does not create a terminal when APIs are unavailable", async () => {
221 readEnvironmentApiMock.mockReturnValueOnce(undefined);
222 readLocalApiMock.mockReturnValueOnce(undefined);
223​
224 const mounted = await mountTerminalViewport({
225 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
226 });
227​
228 try {
229 await vi.waitFor(() => {
230 expect(terminalConstructorSpy).not.toHaveBeenCalled();
231 });
232 } finally {
233 await mounted.cleanup();
234 }
235 });
236​
237 it("reopens the terminal when the scoped thread reference changes", async () => {
238 const environmentA = createEnvironmentApi();
239 const environmentB = createEnvironmentApi();
240 environmentApiById.set("environment-a", environmentA);
241 environmentApiById.set("environment-b", environmentB);
242​
243 const mounted = await mountTerminalViewport({
244 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
245 });
246​
247 try {
248 await vi.waitFor(() => {
249 expect(environmentA.terminal.open).toHaveBeenCalledTimes(1);
250 });
251​
252 await mounted.rerender({
253 threadRef: scopeThreadRef("environment-b" as never, THREAD_ID),
254 });
255​
256 await vi.waitFor(() => {
257 expect(environmentB.terminal.open).toHaveBeenCalledTimes(1);
258 });
259 expect(terminalDisposeSpy).toHaveBeenCalledTimes(1);
260 } finally {
261 await mounted.cleanup();
262 }
263 });
264​
265 it("does not reopen the terminal when the scoped thread reference values stay the same", async () => {
266 const environment = createEnvironmentApi();
267 environmentApiById.set("environment-a", environment);
268​
269 const mounted = await mountTerminalViewport({
270 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
271 });
272​
273 try {
274 await vi.waitFor(() => {
275 expect(environment.terminal.open).toHaveBeenCalledTimes(1);
276 });
277​
278 await mounted.rerender({
279 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
280 });
281​
282 await vi.waitFor(() => {
283 expect(environment.terminal.open).toHaveBeenCalledTimes(1);
284 });
285 expect(terminalDisposeSpy).not.toHaveBeenCalled();
286 } finally {
287 await mounted.cleanup();
288 }
289 });
290​
291 it("uses the drawer surface colors for the terminal theme", async () => {
292 const environment = createEnvironmentApi();
293 environmentApiById.set("environment-a", environment);
294​
295 const mounted = await mountTerminalViewport({
296 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
297 drawerBackgroundColor: "rgb(24, 28, 36)",
298 drawerTextColor: "rgb(228, 232, 240)",
299 });
300​
301 try {
302 await vi.waitFor(() => {
303 expect(terminalConstructorSpy).toHaveBeenCalledTimes(1);
304 });
305​
306 expect(terminalConstructorSpy).toHaveBeenCalledWith(
307 expect.objectContaining({
308 theme: expect.objectContaining({
309 background: "rgb(24, 28, 36)",
310 foreground: "rgb(228, 232, 240)",
311 }),
312 }),
313 );
314 } finally {
315 await mounted.cleanup();
316 }
317 });
318});