Elffuss BraidHub

kikocis/continue public

main · 10.0 KiBRawPermalink
1import { render } from "ink-testing-library";
2import React from "react";
3import { vi } from "vitest";
4
5import type {
6 ToolCallState,
7 ChatHistoryItem,
8} from "../../../../../core/index.js";
9import { ToolPermissionSelector } from "../components/ToolPermissionSelector.js";
10
11describe("TUIChat - Tool Permission Tests", () => {
12 beforeEach(() => {
13 vi.useFakeTimers();
14 });
15
16 afterEach(() => {
17 vi.clearAllTimers();
18 vi.useRealTimers();
19 });
20 it("shows ToolPermissionSelector with correct content", async () => {
21 const handleResponse = vi.fn();
22
23 const { lastFrame } = render(
24 <ToolPermissionSelector
25 toolName="Edit"
26 toolArgs={{
27 file_path: "/path/to/test.txt",
28 old_string: "old content",
29 new_string: "new content",
30 }}
31 requestId="test-request-123"
32 onResponse={handleResponse}
33 />,
34 );
35
36 await vi.advanceTimersByTimeAsync(50);
37
38 const frame = lastFrame();
39
40 // Verify ToolPermissionSelector is shown
41 expect(frame).toContain("Edit");
42 expect(frame).toContain("Would you like to continue?");
43 expect(frame).toContain("Continue");
44 expect(frame).toContain("Continue + don't ask again");
45 expect(frame).toContain("No, and tell Continue what to do differently");
46 expect(frame).toContain("(tab)");
47 expect(frame).toContain("(shift+tab)");
48 expect(frame).toContain("(esc)");
49 // Verify old Cancel option is not shown
50 expect(frame).not.toContain("Cancel");
51 });
52
53 it("handles keyboard shortcuts for permission approval and rejection", async () => {
54 const handleResponse = vi.fn();
55
56 const { stdin } = render(
57 <ToolPermissionSelector
58 toolName="Write"
59 toolArgs={{
60 file_path: "/path/to/new-file.txt",
61 content: "Hello, world!",
62 }}
63 requestId="test-request-123"
64 onResponse={handleResponse}
65 />,
66 );
67
68 await vi.advanceTimersByTimeAsync(50);
69
70 // Test Tab key for approval
71 stdin.write("\t");
72
73 await vi.advanceTimersByTimeAsync(50);
74
75 expect(handleResponse).toHaveBeenCalledWith(
76 "test-request-123",
77 true,
78 false,
79 false,
80 );
81 });
82
83 it("handles escape key for rejection", async () => {
84 const handleResponse = vi.fn();
85
86 const { stdin } = render(
87 <ToolPermissionSelector
88 toolName="Bash"
89 toolArgs={{
90 command: "rm -rf /",
91 }}
92 requestId="test-request-456"
93 onResponse={handleResponse}
94 />,
95 );
96
97 await vi.advanceTimersByTimeAsync(50);
98
99 // Test escape key for rejection
100 stdin.write("\x1b"); // ESC key
101
102 await vi.advanceTimersByTimeAsync(50);
103
104 expect(handleResponse).toHaveBeenCalledWith(
105 "test-request-456",
106 false,
107 false,
108 true,
109 );
110 });
111
112 it("handles 'n' key for rejection", async () => {
113 const handleResponse = vi.fn();
114
115 const { stdin } = render(
116 <ToolPermissionSelector
117 toolName="Delete"
118 toolArgs={{
119 path: "/important/file.txt",
120 }}
121 requestId="test-request-789"
122 onResponse={handleResponse}
123 />,
124 );
125
126 await vi.advanceTimersByTimeAsync(50);
127
128 // Test 'n' key for rejection
129 stdin.write("n");
130
131 await vi.advanceTimersByTimeAsync(50);
132
133 expect(handleResponse).toHaveBeenCalledWith(
134 "test-request-789",
135 false,
136 false,
137 true,
138 );
139 });
140
141 it("handles arrow key navigation in permission selector", async () => {
142 const handleResponse = vi.fn();
143
144 const { lastFrame, stdin } = render(
145 <ToolPermissionSelector
146 toolName="Read"
147 toolArgs={{
148 file_path: "/path/to/secret.txt",
149 }}
150 requestId="test-request-123"
151 onResponse={handleResponse}
152 />,
153 );
154
155 await vi.advanceTimersByTimeAsync(50);
156
157 // Initial state should show "> Continue" selected
158 const frame = lastFrame();
159 expect(frame).toMatch(/>\s+Continue/);
160
161 // Test that pressing Enter on default selection triggers approval
162 stdin.write("\r");
163 await vi.advanceTimersByTimeAsync(50);
164
165 expect(handleResponse).toHaveBeenCalledWith(
166 "test-request-123",
167 true,
168 undefined,
169 undefined,
170 );
171 });
172
173 it("handles shift+tab key for policy creation", async () => {
174 const handleResponse = vi.fn();
175
176 const { stdin } = render(
177 <ToolPermissionSelector
178 toolName="Bash"
179 toolArgs={{
180 command: "ls -la",
181 }}
182 requestId="test-request-policy"
183 onResponse={handleResponse}
184 />,
185 );
186
187 await vi.advanceTimersByTimeAsync(50);
188
189 // Test shift+tab key for approval with policy creation
190 // In terminal, shift+tab is typically represented as "\x1b[Z"
191 stdin.write("\x1b[Z");
192
193 await vi.advanceTimersByTimeAsync(50);
194
195 expect(handleResponse).toHaveBeenCalledWith(
196 "test-request-policy",
197 true,
198 true,
199 false,
200 );
201 });
202
203 it("shows the new 'No, and tell Continue what to do differently' option in the UI", async () => {
204 const handleResponse = vi.fn();
205
206 const { lastFrame } = render(
207 <ToolPermissionSelector
208 toolName="Edit"
209 toolArgs={{
210 file_path: "/path/to/test.txt",
211 old_string: "old content",
212 new_string: "new content",
213 }}
214 requestId="test-request-stop"
215 onResponse={handleResponse}
216 />,
217 );
218
219 await vi.advanceTimersByTimeAsync(50);
220
221 // Verify the new option exists in the rendered output
222 const frame = lastFrame();
223 expect(frame).toContain("No, and tell Continue what to do differently");
224
225 // Verify all 3 options are present
226 expect(frame).toContain("Continue");
227 expect(frame).toContain("Continue + don't ask again");
228 expect(frame).toContain("No, and tell Continue what to do differently");
229 // Verify old Cancel option is not shown
230 expect(frame).not.toContain("Cancel");
231 });
232
233 it("triggers stop stream when escape key is pressed", async () => {
234 // This test ensures the actual ToolPermissionSelector component correctly handles the escape key
235 const handleResponse = vi.fn();
236
237 const { stdin } = render(
238 <ToolPermissionSelector
239 toolName="Edit"
240 toolArgs={{
241 file_path: "/path/to/test.txt",
242 old_string: "old content",
243 new_string: "new content",
244 }}
245 requestId="test-request-escape"
246 onResponse={handleResponse}
247 />,
248 );
249
250 await vi.advanceTimersByTimeAsync(50);
251
252 // Press escape key to trigger the "No, and tell Continue what to do differently" option
253 stdin.write("\x1b"); // ESC key
254
255 await vi.advanceTimersByTimeAsync(50);
256
257 // Verify the escape key triggers the stopStream behavior
258 expect(handleResponse).toHaveBeenCalledWith(
259 "test-request-escape",
260 false, // approved = false (rejection)
261 false, // createPolicy = false
262 true, // stopStream = true (this is the new behavior)
263 );
264 });
265
266 it("triggers stop stream when 'n' key is pressed", async () => {
267 // This test ensures the 'n' key also triggers the new stop stream behavior
268 const handleResponse = vi.fn();
269
270 const { stdin } = render(
271 <ToolPermissionSelector
272 toolName="Write"
273 toolArgs={{
274 file_path: "/path/to/new-file.txt",
275 content: "New content",
276 }}
277 requestId="test-request-n-key"
278 onResponse={handleResponse}
279 />,
280 );
281
282 await vi.advanceTimersByTimeAsync(50);
283
284 // Press 'n' key to trigger the "No, and tell Continue what to do differently" option
285 stdin.write("n");
286
287 await vi.advanceTimersByTimeAsync(50);
288
289 // Verify the 'n' key triggers the stopStream behavior
290 expect(handleResponse).toHaveBeenCalledWith(
291 "test-request-n-key",
292 false, // approved = false (rejection)
293 false, // createPolicy = false
294 true, // stopStream = true (this is the new behavior)
295 );
296 });
297
298 it("shows tool result with red dot and 'Cancelled by user' message", async () => {
299 // Import the components we need for this test
300 const { ToolResultSummary } = await import("../ToolResultSummary.js");
301 const { MemoizedMessage } = await import(
302 "../components/MemoizedMessage.js"
303 );
304
305 // Test the ToolResultSummary component directly
306 const { lastFrame: summaryFrame } = render(
307 <ToolResultSummary toolName="Edit" content="Permission denied by user" />,
308 );
309
310 const summary = summaryFrame();
311 expect(summary).toContain("Cancelled by user");
312
313 // Test the MemoizedMessage component with a proper tool result in ChatHistoryItem
314 const toolCallState: ToolCallState = {
315 toolCallId: "test-tool-call",
316 toolCall: {
317 id: "test-tool-call",
318 type: "function",
319 function: {
320 name: "Edit",
321 arguments: JSON.stringify({ file_path: "test.txt" }),
322 },
323 },
324 status: "errored", // Set to errored to show red dot
325 parsedArgs: { file_path: "test.txt" },
326 output: [
327 {
328 name: "tool_result",
329 content: "Permission denied by user",
330 description: "Tool execution denied",
331 },
332 ],
333 };
334
335 const historyItem: ChatHistoryItem = {
336 message: {
337 role: "assistant",
338 content: "",
339 toolCalls: [
340 {
341 id: "test-tool-call",
342 function: {
343 name: "Edit",
344 arguments: JSON.stringify({ file_path: "test.txt" }),
345 },
346 type: "function",
347 },
348 ],
349 },
350 contextItems: [],
351 toolCallStates: [toolCallState],
352 };
353
354 const { lastFrame: messageFrame } = render(
355 <MemoizedMessage item={historyItem} index={0} />,
356 );
357
358 await vi.advanceTimersByTimeAsync(100);
359
360 const messageOutput = messageFrame();
361
362 // Verify the red dot appears (for failed tool call)
363 expect(messageOutput).toContain("●");
364
365 // Verify the tool name appears
366 expect(messageOutput).toContain("Edit");
367
368 // The MemoizedMessage component correctly identifies this as a failure
369 // because the tool result includes "Permission denied"
370 // This causes the red dot to be shown
371 });
372});