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

ThreadTerminalDrawer.browser.tsx

320 lines8.0 KB

1import "../index.css";
2​
3import { scopeThreadRef } from "@workbench/client-runtime";
4import { ThreadId } from "@workbench/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 keybindings={[]}
181 />,
182 { container: host },
183 );
184​
185 return {
186 rerender: async (nextProps: { threadRef: ReturnType<typeof scopeThreadRef> }) => {
187 await screen.rerender(
188 <TerminalViewport
189 threadRef={nextProps.threadRef}
190 threadId={THREAD_ID}
191 terminalId="default"
192 terminalLabel="Terminal"
193 cwd="/repo/project"
194 onSessionExited={() => undefined}
195 onAddTerminalContext={() => undefined}
196 focusRequestId={0}
197 autoFocus={false}
198 resizeEpoch={0}
199 drawerHeight={320}
200 keybindings={[]}
201 />,
202 );
203 },
204 cleanup: async () => {
205 await screen.unmount();
206 drawer.remove();
207 },
208 };
210​
211describe("TerminalViewport", () => {
212 afterEach(() => {
213 environmentApiById.clear();
214 readEnvironmentApiMock.mockClear();
215 readLocalApiMock.mockClear();
216 terminalConstructorSpy.mockClear();
217 terminalDisposeSpy.mockClear();
218 fitAddonFitSpy.mockClear();
219 fitAddonLoadSpy.mockClear();
220 });
221​
222 it("does not create a terminal when APIs are unavailable", async () => {
223 readEnvironmentApiMock.mockReturnValueOnce(undefined);
224 readLocalApiMock.mockReturnValueOnce(undefined);
225​
226 const mounted = await mountTerminalViewport({
227 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
228 });
229​
230 try {
231 await vi.waitFor(() => {
232 expect(terminalConstructorSpy).not.toHaveBeenCalled();
233 });
234 } finally {
235 await mounted.cleanup();
236 }
237 });
238​
239 it("reopens the terminal when the scoped thread reference changes", async () => {
240 const environmentA = createEnvironmentApi();
241 const environmentB = createEnvironmentApi();
242 environmentApiById.set("environment-a", environmentA);
243 environmentApiById.set("environment-b", environmentB);
244​
245 const mounted = await mountTerminalViewport({
246 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
247 });
248​
249 try {
250 await vi.waitFor(() => {
251 expect(environmentA.terminal.open).toHaveBeenCalledTimes(1);
252 });
253​
254 await mounted.rerender({
255 threadRef: scopeThreadRef("environment-b" as never, THREAD_ID),
256 });
257​
258 await vi.waitFor(() => {
259 expect(environmentB.terminal.open).toHaveBeenCalledTimes(1);
260 });
261 expect(terminalDisposeSpy).toHaveBeenCalledTimes(1);
262 } finally {
263 await mounted.cleanup();
264 }
265 });
266​
267 it("does not reopen the terminal when the scoped thread reference values stay the same", async () => {
268 const environment = createEnvironmentApi();
269 environmentApiById.set("environment-a", environment);
270​
271 const mounted = await mountTerminalViewport({
272 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
273 });
274​
275 try {
276 await vi.waitFor(() => {
277 expect(environment.terminal.open).toHaveBeenCalledTimes(1);
278 });
279​
280 await mounted.rerender({
281 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
282 });
283​
284 await vi.waitFor(() => {
285 expect(environment.terminal.open).toHaveBeenCalledTimes(1);
286 });
287 expect(terminalDisposeSpy).not.toHaveBeenCalled();
288 } finally {
289 await mounted.cleanup();
290 }
291 });
292​
293 it("uses the drawer surface colors for the terminal theme", async () => {
294 const environment = createEnvironmentApi();
295 environmentApiById.set("environment-a", environment);
296​
297 const mounted = await mountTerminalViewport({
298 threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
299 drawerBackgroundColor: "rgb(24, 28, 36)",
300 drawerTextColor: "rgb(228, 232, 240)",
301 });
302​
303 try {
304 await vi.waitFor(() => {
305 expect(terminalConstructorSpy).toHaveBeenCalledTimes(1);
306 });
307​
308 expect(terminalConstructorSpy).toHaveBeenCalledWith(
309 expect.objectContaining({
310 theme: expect.objectContaining({
311 background: "rgb(24, 28, 36)",
312 foreground: "rgb(228, 232, 240)",
313 }),
314 }),
315 );
316 } finally {
317 await mounted.cleanup();
318 }
319 });
320});