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

ThreadTerminalDrawer.browser.tsx

315 lines7.8 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 readLocalApi: readLocalApiMock,
117}));
118​
119import { TerminalViewport } from "./ThreadTerminalDrawer";
120​
121const THREAD_ID = ThreadId.makeUnsafe("thread-terminal-browser");
122​
123function createEnvironmentApi() {
124 return {
125 terminal: {
126 open: vi.fn(async () => ({
127 threadId: THREAD_ID,
128 terminalId: "default",
129 cwd: "/repo/project",
130 worktreePath: null,
131 status: "running" as const,
132 pid: 123,
133 history: "",
134 exitCode: null,
135 exitSignal: null,
136 updatedAt: "2026-04-07T00:00:00.000Z",
137 })),
138 write: vi.fn(async () => undefined),
139 resize: vi.fn(async () => undefined),
140 },
141 };
143​
144async function mountTerminalViewport(props: {
145 threadRef: ReturnType<typeof scopeThreadRef>;
146 drawerBackgroundColor?: string;
147 drawerTextColor?: string;
148}) {
149 const drawer = document.createElement("div");
150 drawer.className = "thread-terminal-drawer";
151 if (props.drawerBackgroundColor) {
152 drawer.style.backgroundColor = props.drawerBackgroundColor;
153 }
154 if (props.drawerTextColor) {
155 drawer.style.color = props.drawerTextColor;
156 }
157​
158 const host = document.createElement("div");
159 host.style.width = "800px";
160 host.style.height = "400px";
161 drawer.append(host);
162 document.body.append(drawer);
163​
164 const screen = await render(
165 <TerminalViewport
166 threadRef={props.threadRef}
167 threadId={THREAD_ID}
168 terminalId="default"
169 terminalLabel="Terminal"
170 cwd="/repo/project"
171 onSessionExited={() => undefined}
172 onAddTerminalContext={() => undefined}
173 focusRequestId={0}
174 autoFocus={false}
175 resizeEpoch={0}
176 drawerHeight={320}
177 />,
178 { container: host },
179 );
180​
181 return {
182 rerender: async (nextProps: { threadRef: ReturnType<typeof scopeThreadRef> }) => {
183 await screen.rerender(
184 <TerminalViewport
185 threadRef={nextProps.threadRef}
186 threadId={THREAD_ID}
187 terminalId="default"
188 terminalLabel="Terminal"
189 cwd="/repo/project"
190 onSessionExited={() => undefined}
191 onAddTerminalContext={() => undefined}
192 focusRequestId={0}
193 autoFocus={false}
194 resizeEpoch={0}
195 drawerHeight={320}
196 />,
197 );
198 },
199 cleanup: async () => {
200 await screen.unmount();
201 drawer.remove();
202 },
203 };
205​
206describe("TerminalViewport", () => {
207 afterEach(() => {
208 environmentApiById.clear();
209 readEnvironmentApiMock.mockClear();
210 readLocalApiMock.mockClear();
211 terminalConstructorSpy.mockClear();
212 terminalDisposeSpy.mockClear();
213 fitAddonFitSpy.mockClear();
214 fitAddonLoadSpy.mockClear();
215 });
216​
217 it("does not create a terminal when APIs are unavailable", async () => {
218 readEnvironmentApiMock.mockReturnValueOnce(undefined);
219 readLocalApiMock.mockReturnValueOnce(undefined);
220​
221 const mounted = await mountTerminalViewport({
222 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
223 });
224​
225 try {
226 await vi.waitFor(() => {
227 expect(terminalConstructorSpy).not.toHaveBeenCalled();
228 });
229 } finally {
230 await mounted.cleanup();
231 }
232 });
233​
234 it("reopens the terminal when the scoped thread reference changes", async () => {
235 const environmentA = createEnvironmentApi();
236 const environmentB = createEnvironmentApi();
237 environmentApiById.set("environment-a", environmentA);
238 environmentApiById.set("environment-b", environmentB);
239​
240 const mounted = await mountTerminalViewport({
241 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
242 });
243​
244 try {
245 await vi.waitFor(() => {
246 expect(environmentA.terminal.open).toHaveBeenCalledTimes(1);
247 });
248​
249 await mounted.rerender({
250 threadRef: scopeThreadRef("environment-b" as never, THREAD_ID),
251 });
252​
253 await vi.waitFor(() => {
254 expect(environmentB.terminal.open).toHaveBeenCalledTimes(1);
255 });
256 expect(terminalDisposeSpy).toHaveBeenCalledTimes(1);
257 } finally {
258 await mounted.cleanup();
259 }
260 });
261​
262 it("does not reopen the terminal when the scoped thread reference values stay the same", async () => {
263 const environment = createEnvironmentApi();
264 environmentApiById.set("environment-a", environment);
265​
266 const mounted = await mountTerminalViewport({
267 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
268 });
269​
270 try {
271 await vi.waitFor(() => {
272 expect(environment.terminal.open).toHaveBeenCalledTimes(1);
273 });
274​
275 await mounted.rerender({
276 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
277 });
278​
279 await vi.waitFor(() => {
280 expect(environment.terminal.open).toHaveBeenCalledTimes(1);
281 });
282 expect(terminalDisposeSpy).not.toHaveBeenCalled();
283 } finally {
284 await mounted.cleanup();
285 }
286 });
287​
288 it("uses the drawer surface colors for the terminal theme", async () => {
289 const environment = createEnvironmentApi();
290 environmentApiById.set("environment-a", environment);
291​
292 const mounted = await mountTerminalViewport({
293 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
294 drawerBackgroundColor: "rgb(24, 28, 36)",
295 drawerTextColor: "rgb(228, 232, 240)",
296 });
297​
298 try {
299 await vi.waitFor(() => {
300 expect(terminalConstructorSpy).toHaveBeenCalledTimes(1);
301 });
302​
303 expect(terminalConstructorSpy).toHaveBeenCalledWith(
304 expect.objectContaining({
305 theme: expect.objectContaining({
306 background: "rgb(24, 28, 36)",
307 foreground: "rgb(228, 232, 240)",
308 }),
309 }),
310 );
311 } finally {
312 await mounted.cleanup();
313 }
314 });
315});