Elffuss BraidHub

kikocis/continue public

main · 10.5 KiBRawPermalink
1import { screen, waitFor } from "@testing-library/react";
2import { ToolCallState } from "core";
3import { renderWithProviders } from "../../util/test/render";
4import { UnifiedTerminalCommand } from "./UnifiedTerminal";
5
6const MOCK_COMMAND = "npm test";
7const MOCK_OUTPUT = `Running tests...
8✓ Test 1 passed
9✓ Test 2 passed
10✗ Test 3 failed
11 Error: Expected true but got false
12
13Test Results:
14 2 passed, 1 failed
15 Total: 3 tests`;
16
17const LONG_OUTPUT = Array.from({ length: 25 }, (_, i) => `Line ${i + 1}`).join(
18 "\n",
19);
20const MOCK_TOOL_CALL_ID = "terminal-call-123";
21
22const MOCK_ANSI_OUTPUT = `\u001b[32m✓\u001b[0m Test passed
23\u001b[31m✗\u001b[0m Test failed
24\u001b[1mBold text\u001b[0m
25\u001b[4mUnderlined text\u001b[0m`;
26
27const MOCK_OUTPUT_WITH_LINKS = `Build successful!
28Visit https://example.com for more info
29Check www.github.com/user/repo
30Error logs at file:///tmp/error.log`;
31
32// Mock the redux hooks
33const mockDispatch = vi.fn();
34const mockSelector = vi.fn();
35vi.mock("../../redux/hooks", () => ({
36 useAppDispatch: () => mockDispatch,
37 useAppSelector: () => mockSelector,
38}));
39
40beforeEach(() => {
41 vi.clearAllMocks();
42});
43
44describe("UnifiedTerminalCommand", () => {
45 test("renders basic terminal command without output", async () => {
46 const { user } = await renderWithProviders(
47 <UnifiedTerminalCommand command={MOCK_COMMAND} status="completed" />,
48 );
49
50 // Should show the command
51 expect(screen.getByText(`$ ${MOCK_COMMAND}`)).toBeInTheDocument();
52
53 // Should show terminal header
54 expect(screen.getByText("Terminal")).toBeInTheDocument();
55
56 // Should show completed status icon (green dot)
57 const terminalContainer = screen.getByTestId("terminal-container");
58 expect(terminalContainer).toBeInTheDocument();
59 });
60
61 test("renders terminal command with output", async () => {
62 const { container } = await renderWithProviders(
63 <UnifiedTerminalCommand
64 command={MOCK_COMMAND}
65 output={MOCK_OUTPUT}
66 status="completed"
67 />,
68 );
69
70 // Should show the command and output
71 expect(screen.getByText(`$ ${MOCK_COMMAND}`)).toBeInTheDocument();
72
73 // Check that the output content exists in the container
74 expect(container.textContent).toMatch(/Test 1 passed/);
75 expect(container.textContent).toMatch(/Test 3 failed/);
76 });
77
78 test("shows running state with blinking cursor", async () => {
79 const mockToolCallState: ToolCallState = {
80 status: "calling",
81 toolCallId: MOCK_TOOL_CALL_ID,
82 toolCall: {
83 id: MOCK_TOOL_CALL_ID,
84 type: "function",
85 function: {
86 name: "terminal",
87 arguments: "{}",
88 },
89 },
90 parsedArgs: {},
91 };
92
93 const { user } = await renderWithProviders(
94 <UnifiedTerminalCommand
95 command={MOCK_COMMAND}
96 status="running"
97 toolCallState={mockToolCallState}
98 toolCallId={MOCK_TOOL_CALL_ID}
99 />,
100 );
101
102 // Should show running status
103 expect(screen.getByText("Running")).toBeInTheDocument();
104
105 // Should show move to background link
106 expect(screen.getByText("Move to background")).toBeInTheDocument();
107 });
108
109 test("can expand and collapse terminal", async () => {
110 const { user, container } = await renderWithProviders(
111 <UnifiedTerminalCommand
112 command={MOCK_COMMAND}
113 output={MOCK_OUTPUT}
114 status="completed"
115 />,
116 );
117
118 // Should initially be expanded and show output
119 expect(container.textContent).toMatch(/Test 1 passed/);
120
121 // Find and click the collapse chevron (SVG icon)
122 const chevron = container.querySelector(
123 'svg[class*="cursor-pointer"], .cursor-pointer svg',
124 );
125 expect(chevron).toBeInTheDocument();
126
127 if (chevron) {
128 await user.click(chevron);
129
130 // After collapsing, output should not be visible
131 await waitFor(() => {
132 expect(container.textContent).not.toMatch(/Test 1 passed/);
133 });
134 }
135 });
136
137 test("shows collapsible output for long content", async () => {
138 const { user, container } = await renderWithProviders(
139 <UnifiedTerminalCommand
140 command={MOCK_COMMAND}
141 output={LONG_OUTPUT}
142 status="completed"
143 displayLines={15}
144 />,
145 );
146
147 // Should show the "+X more lines" indicator
148 expect(screen.getByText(/\+\d+ more lines/)).toBeInTheDocument();
149
150 // Should initially show only some of the last lines
151 expect(container.textContent).toMatch(/Line 25/);
152
153 // Click to expand using the "+X more lines" button
154 const expandButton = screen.getByText(/\+\d+ more lines/);
155 await user.click(expandButton);
156
157 // Should now show all lines
158 await waitFor(() => {
159 expect(container.textContent).toMatch(/Line 1/);
160 });
161
162 // Should show collapse option
163 expect(screen.getByText(/Collapse/)).toBeInTheDocument();
164 });
165
166 test("renders ANSI colors and formatting", async () => {
167 const { container } = await renderWithProviders(
168 <UnifiedTerminalCommand
169 command={MOCK_COMMAND}
170 output={MOCK_ANSI_OUTPUT}
171 status="completed"
172 />,
173 );
174
175 // Should render ANSI content (check the container text since ANSI splits across elements)
176 expect(container.textContent).toMatch(/Test passed/);
177 expect(container.textContent).toMatch(/Test failed/);
178 expect(container.textContent).toMatch(/Bold text/);
179 expect(container.textContent).toMatch(/Underlined text/);
180
181 // Verify ANSI processing created styled spans
182 const styledSpans = container.querySelectorAll('span[class*="sc-esYiGF"]');
183 expect(styledSpans.length).toBeGreaterThan(0);
184 });
185
186 test("handles links in output", async () => {
187 const { container } = await renderWithProviders(
188 <UnifiedTerminalCommand
189 command={MOCK_COMMAND}
190 output={MOCK_OUTPUT_WITH_LINKS}
191 status="completed"
192 />,
193 );
194
195 // Should contain the link text in the output
196 expect(container.textContent).toMatch(/https:\/\/example\.com/);
197 expect(container.textContent).toMatch(/www\.github\.com/);
198
199 // Note: Link detection might create actual <a> tags or just display the URLs
200 // The important thing is the URLs are visible in the output
201 });
202
203 test("shows copy and run in terminal buttons when not running", async () => {
204 const { container } = await renderWithProviders(
205 <UnifiedTerminalCommand
206 command={MOCK_COMMAND}
207 output={MOCK_OUTPUT}
208 status="completed"
209 />,
210 );
211
212 // Look for toolbar elements - they may be hidden by responsive classes but exist in DOM
213 const toolbarArea = container.querySelector('[class*="gap-2.5"]');
214 expect(toolbarArea).toBeInTheDocument();
215
216 // Should contain SVG icons for copy/run operations
217 const svgIcons = container.querySelectorAll("svg");
218 expect(svgIcons.length).toBeGreaterThan(0);
219
220 // Should show "Run" text (may be hidden on small screens)
221 expect(container.textContent).toMatch(/Run/);
222 });
223
224 test("handles move to background action", async () => {
225 const mockToolCallState: ToolCallState = {
226 status: "calling",
227 toolCallId: MOCK_TOOL_CALL_ID,
228 toolCall: {
229 id: MOCK_TOOL_CALL_ID,
230 type: "function",
231 function: {
232 name: "terminal",
233 arguments: "{}",
234 },
235 },
236 parsedArgs: {},
237 };
238
239 const { user } = await renderWithProviders(
240 <UnifiedTerminalCommand
241 command={MOCK_COMMAND}
242 status="running"
243 toolCallState={mockToolCallState}
244 toolCallId={MOCK_TOOL_CALL_ID}
245 />,
246 );
247
248 // Find and click move to background link
249 const moveToBackgroundLink = screen.getByText("Move to background");
250 await user.click(moveToBackgroundLink);
251
252 // Should dispatch the move to background action
253 expect(mockDispatch).toHaveBeenCalled();
254 });
255
256 test("shows different status types correctly", async () => {
257 // Test completed status
258 const { rerender, container } = await renderWithProviders(
259 <UnifiedTerminalCommand
260 command={MOCK_COMMAND}
261 status="completed"
262 statusMessage="Command completed successfully"
263 />,
264 );
265
266 expect(container.textContent).toMatch(/Command completed successfully/);
267
268 // Test failed status
269 rerender(
270 <UnifiedTerminalCommand
271 command={MOCK_COMMAND}
272 status="failed"
273 statusMessage="Command failed with exit code 1"
274 />,
275 );
276
277 expect(container.textContent).toMatch(/Command failed with exit code 1/);
278
279 // Test background status
280 rerender(
281 <UnifiedTerminalCommand
282 command={MOCK_COMMAND}
283 status="background"
284 statusMessage="Command moved to background"
285 />,
286 );
287
288 expect(container.textContent).toMatch(/Command moved to background/);
289 });
290
291 test("handles empty output gracefully", async () => {
292 const { container } = await renderWithProviders(
293 <UnifiedTerminalCommand
294 command={MOCK_COMMAND}
295 output=""
296 status="completed"
297 />,
298 );
299
300 // Should show command but no output section
301 expect(screen.getByText(`$ ${MOCK_COMMAND}`)).toBeInTheDocument();
302
303 // Should not show any output content
304 const outputElements = container.querySelectorAll(
305 "pre code > div:not(:first-child)",
306 );
307 expect(outputElements.length).toBe(0);
308 });
309
310 test("processes terminal content correctly for line limiting", async () => {
311 const { container } = await renderWithProviders(
312 <UnifiedTerminalCommand
313 command={MOCK_COMMAND}
314 output={LONG_OUTPUT}
315 status="completed"
316 displayLines={10}
317 />,
318 );
319
320 // Should show correct number of hidden lines
321 expect(screen.getByText(/\+15 more lines/)).toBeInTheDocument();
322
323 // Should show the last lines
324 expect(container.textContent).toMatch(/Line 25/);
325
326 // Should not initially show the first lines (Line 1-15 should be hidden)
327 // The component shows last 10 lines (16-25) with a +15 more lines indicator
328 expect(container.textContent).toMatch(/Line 16/);
329 });
330
331 test("clicking on collapsible output toggles expansion", async () => {
332 const { user, container } = await renderWithProviders(
333 <UnifiedTerminalCommand
334 command={MOCK_COMMAND}
335 output={LONG_OUTPUT}
336 status="completed"
337 displayLines={10}
338 />,
339 );
340
341 // Click on the "+15 more lines" button to expand
342 const expandButton = screen.getByText(/\+15 more lines/);
343 await user.click(expandButton);
344
345 // Should expand and show all content
346 await waitFor(() => {
347 expect(container.textContent).toMatch(/Line 1/);
348 });
349
350 // Should show collapse option after expansion
351 expect(screen.getByText(/Collapse/)).toBeInTheDocument();
352 });
353});