Elffuss BraidHub

kikocis/continue public

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