Elffuss BraidHub

kikocis/continue public

4131ae02de3e · 11.8 KiBRawPermalink
1import * as fs from "fs";
2
3import * as diff from "diff";
4import { describe, it, expect, beforeEach, vi } from "vitest";
5
6// Mock fs module
7vi.mock("fs", async () => {
8 // Use actual fs implementation for non-mocked functions
9 const actualFs = await vi.importActual("fs");
10 return {
11 ...(actualFs as any),
12 default: actualFs,
13 existsSync: vi.fn(),
14 readFileSync: vi.fn(),
15 };
16});
17
18// Mock telemetry service
19vi.mock("./src/telemetry/telemetryService.js"); // Mock diff module
20vi.mock("diff", () => ({
21 createTwoFilesPatch: vi.fn(),
22}));
23
24// Get mocked functions using vi.mocked
25const mockExistsSync = vi.mocked(fs.existsSync);
26const mockReadFileSync = vi.mocked(fs.readFileSync);
27const mockCreateTwoFilesPatch = vi.mocked(diff.createTwoFilesPatch);
28
29import { fetchTool } from "./fetch.js";
30import { listFilesTool } from "./listFiles.js";
31import { readFileTool } from "./readFile.js";
32import { runTerminalCommandTool } from "./runTerminalCommand.js";
33import { searchAndReplaceInFileTool } from "./searchAndReplace/index.js";
34import { searchCodeTool } from "./searchCode.js";
35import { viewDiffTool } from "./viewDiff.js";
36import { writeFileTool } from "./writeFile.js";
37
38describe.skip("Tool preprocess functions", () => {
39 beforeEach(() => {
40 vi.clearAllMocks();
41 // The mock functions will be configured in each test as needed
42 });
43
44 describe("fetchTool.preprocess", () => {
45 it("should return preview with URL to fetch", async () => {
46 const args = { url: "https://example.com" };
47 const result = await fetchTool.preprocess!(args);
48
49 expect(result).toEqual({
50 preview: [
51 {
52 type: "text",
53 content: "Will fetch: https://example.com",
54 },
55 ],
56 args,
57 });
58 });
59
60 it("should handle URL with timeout parameter", async () => {
61 const args = { url: "https://example.com", timeout: 5000 };
62 const result = await fetchTool.preprocess!(args);
63
64 expect(result).toEqual({
65 preview: [
66 {
67 type: "text",
68 content: "Will fetch: https://example.com",
69 },
70 ],
71 args,
72 });
73 });
74 });
75
76 describe("listFilesTool.preprocess", () => {
77 it("should return preview with directory path when directory arg present", async () => {
78 const args = { dirpath: "some/path" };
79 const result = await listFilesTool.preprocess!(args);
80
81 expect(result).toEqual({
82 args,
83 preview: [
84 {
85 type: "text",
86 content: "Will list files in: some/path",
87 },
88 ],
89 });
90 });
91
92 it("should show current directory when no directory arg", async () => {
93 const args = {};
94 const result = await listFilesTool.preprocess!(args);
95
96 expect(result).toEqual({
97 args,
98 preview: [
99 {
100 type: "text",
101 content: "Will list files in current directory",
102 },
103 ],
104 });
105 });
106 });
107
108 describe("readFileTool.preprocess", () => {
109 it("should return formatted tool call preview", async () => {
110 const args = { filepath: "path/to/file.txt" };
111 const result = await readFileTool.preprocess!(args);
112
113 expect(result).toEqual({
114 args,
115 preview: [
116 {
117 type: "text",
118 content: "Will read path/to/file.txt",
119 },
120 ],
121 });
122 });
123 });
124
125 describe("runTerminalCommandTool.preprocess", () => {
126 it("should return preview with full command when short", async () => {
127 const args = { command: "ls -la" };
128 const result = await runTerminalCommandTool.preprocess!(args);
129
130 expect(result).toEqual({
131 args,
132 preview: [
133 {
134 type: "text",
135 content: "Will run: ls -la",
136 },
137 ],
138 });
139 });
140
141 it("should truncate long commands", async () => {
142 const longCommand = "a".repeat(100);
143 const args = { command: longCommand };
144 const result = await runTerminalCommandTool.preprocess!(args);
145
146 expect(result).toEqual({
147 args,
148 preview: [
149 {
150 type: "text",
151 content: `Will run: ${"a".repeat(60)}...`,
152 },
153 ],
154 });
155 });
156 });
157
158 describe("searchCodeTool.preprocess", () => {
159 it("should return preview with full pattern when short", async () => {
160 const args = { pattern: "searchTerm" };
161 const result = await searchCodeTool.preprocess!(args);
162
163 expect(result).toEqual({
164 args,
165 preview: [
166 {
167 type: "text",
168 content: 'Will search for: "searchTerm"',
169 },
170 ],
171 });
172 });
173
174 it("should truncate long patterns", async () => {
175 const longPattern = "a".repeat(100);
176 const args = { pattern: longPattern };
177 const result = await searchCodeTool.preprocess!(args);
178
179 expect(result).toEqual({
180 args,
181 preview: [
182 {
183 type: "text",
184 content: `Will search for: "${"a".repeat(50)}..."`,
185 },
186 ],
187 });
188 });
189 });
190
191 describe("viewDiffTool.preprocess", () => {
192 it("should return preview indicating git diff will be shown", async () => {
193 const args = { path: "/repo/path" };
194 const result = await viewDiffTool.preprocess!(args);
195
196 expect(result).toEqual({
197 args,
198 preview: [
199 {
200 type: "text",
201 content: "Will show git diff",
202 },
203 ],
204 });
205 });
206
207 it("should handle empty args", async () => {
208 const args = {};
209 const result = await viewDiffTool.preprocess!(args);
210
211 expect(result).toEqual({
212 args,
213 preview: [
214 {
215 type: "text",
216 content: "Will show git diff",
217 },
218 ],
219 });
220 });
221 });
222
223 describe("writeFileTool.preprocess", () => {
224 it("should show diff preview when file exists", async () => {
225 // Setup mocks for this test
226 mockExistsSync.mockReturnValue(true);
227 mockReadFileSync.mockReturnValue("old content");
228
229 const args = { filepath: "path/to/file.txt", content: "new content" };
230 const result = await writeFileTool.preprocess!(args);
231
232 expect(mockExistsSync).toHaveBeenCalled();
233 expect(mockReadFileSync).toHaveBeenCalled();
234 expect(mockCreateTwoFilesPatch).toHaveBeenCalled();
235
236 expect(result).toEqual({
237 args,
238 preview: [
239 {
240 type: "text",
241 content: "Preview of changes:",
242 },
243 {
244 type: "diff",
245 content: "mock diff content",
246 },
247 ],
248 });
249 });
250
251 it("should show new file content preview when file doesn't exist", async () => {
252 mockExistsSync.mockReturnValue(false);
253
254 const args = {
255 filepath: "path/to/file.txt",
256 content: "line 1\nline 2\nline 3\nline 4",
257 };
258 const result = await writeFileTool.preprocess!(args);
259
260 expect(result).toEqual({
261 args,
262 preview: [
263 {
264 type: "text",
265 content: "New file content:",
266 },
267 {
268 type: "text",
269 content: "line 1",
270 paddingLeft: 2,
271 },
272 {
273 type: "text",
274 content: "line 2",
275 paddingLeft: 2,
276 },
277 {
278 type: "text",
279 content: "line 3",
280 paddingLeft: 2,
281 },
282 {
283 type: "text",
284 content: "... (1 more lines)",
285 },
286 ],
287 });
288 });
289
290 it("should handle empty lines in new file preview", async () => {
291 mockExistsSync.mockReturnValue(false);
292
293 const args = {
294 filepath: "path/to/file.txt",
295 content: "line 1\n\nline 3",
296 };
297 const result = await writeFileTool.preprocess!(args);
298
299 expect(result).toEqual({
300 args,
301 preview: [
302 {
303 type: "text",
304 content: "New file content:",
305 },
306 {
307 type: "text",
308 content: "line 1",
309 paddingLeft: 2,
310 },
311 {
312 type: "text",
313 content: " ",
314 paddingLeft: 2,
315 },
316 {
317 type: "text",
318 content: "line 3",
319 paddingLeft: 2,
320 },
321 ],
322 });
323 });
324
325 it("should handle fs errors gracefully", async () => {
326 mockExistsSync.mockImplementation(() => {
327 throw new Error("Permission denied");
328 });
329
330 const args = { filepath: "path/to/file.txt", content: "new content" };
331 const result = await writeFileTool.preprocess!(args);
332
333 expect(result).toEqual({
334 args,
335 preview: [
336 {
337 type: "text",
338 content: "New file content:",
339 },
340 {
341 type: "text",
342 content: "new content",
343 paddingLeft: 2,
344 },
345 ],
346 });
347 });
348 });
349
350 describe("searchAndReplaceInFileTool.preprocess", () => {
351 it("should process simple search and replace", async () => {
352 // Setup mocks
353 mockExistsSync.mockReturnValue(true);
354 mockReadFileSync.mockReturnValue("old line 1\nold line 2\nold line 3");
355
356 const args = {
357 filepath: "path/to/file.txt",
358 diffs: [
359 `------- SEARCH
360old line 2
361=======
362new line 2
363+++++++ REPLACE`,
364 ],
365 };
366
367 const result = await searchAndReplaceInFileTool.preprocess!(args);
368
369 expect(mockExistsSync).toHaveBeenCalled();
370 expect(mockReadFileSync).toHaveBeenCalled();
371 expect(mockCreateTwoFilesPatch).toHaveBeenCalled();
372
373 expect(result.args).toEqual({
374 filepath: expect.any(String),
375 newContent: "old line 1\nnew line 2\nold line 3",
376 oldContent: "old line 1\nold line 2\nold line 3",
377 });
378
379 expect(result.preview).toEqual([
380 {
381 type: "text",
382 content: "Will make the following changes:",
383 },
384 {
385 type: "diff",
386 content: "mock diff content",
387 },
388 ]);
389 });
390
391 it("should throw error when file doesn't exist", async () => {
392 mockExistsSync.mockReturnValue(false);
393
394 const args = {
395 filepath: "/nonexistent/file.txt",
396 diffs: ["some diff"],
397 };
398
399 await expect(
400 searchAndReplaceInFileTool.preprocess!(args),
401 ).rejects.toThrowError(/file .* does not exist/);
402 });
403
404 it("should throw error when no search/replace blocks found", async () => {
405 mockExistsSync.mockReturnValue(true);
406 mockReadFileSync.mockReturnValue("file content");
407
408 const args = {
409 filepath: "path/to/file.txt",
410 diffs: ["invalid diff format"],
411 };
412
413 await expect(
414 searchAndReplaceInFileTool.preprocess!(args),
415 ).rejects.toThrowError(
416 "No complete search/replace blocks found in any diffs",
417 );
418 });
419
420 it("should throw error when search content not found", async () => {
421 mockExistsSync.mockReturnValue(true);
422 mockReadFileSync.mockReturnValue("old line 1\nold line 2\nold line 3");
423
424 const args = {
425 filepath: "path/to/file.txt",
426 diffs: [
427 `------- SEARCH
428nonexistent content
429=======
430replacement
431+++++++ REPLACE`,
432 ],
433 };
434
435 await expect(
436 searchAndReplaceInFileTool.preprocess!(args),
437 ).rejects.toThrowError(/Search content not found in block 1:/);
438 });
439
440 it("should process multiple search/replace blocks", async () => {
441 mockExistsSync.mockReturnValue(true);
442 mockReadFileSync.mockReturnValue("line 1\nline 2\nline 3\nline 4");
443
444 const args = {
445 filepath: "path/to/file.txt",
446 diffs: [
447 `------- SEARCH
448line 1
449=======
450modified line 1
451+++++++ REPLACE
452
453------- SEARCH
454line 3
455=======
456modified line 3
457+++++++ REPLACE`,
458 ],
459 };
460
461 const result = await searchAndReplaceInFileTool.preprocess!(args);
462
463 expect(result.args.newContent).toBe(
464 "modified line 1\nline 2\nmodified line 3\nline 4",
465 );
466 });
467 });
468});