Elffuss BraidHub

kikocis/continue public

1d6a8dcaaf43 · 14.0 KiBRawPermalink
1import * as fs from "fs";
2import * as path from "path";
3
4import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
5
6import { multiEditTool, markFileAsRead } from "./multiEdit.js";
7
8// Mock the dependencies
9vi.mock("../telemetry/telemetryService.js");
10vi.mock("../telemetry/utils.js", () => ({
11 calculateLinesOfCodeDiff: vi.fn().mockReturnValue({ added: 1, removed: 0 }),
12 getLanguageFromFilePath: vi.fn().mockReturnValue("javascript"),
13}));
14vi.mock("./writeFile.js", () => ({
15 generateDiff: vi.fn().mockReturnValue("mocked diff"),
16}));
17vi.mock("fs", async () => {
18 const actual = await vi.importActual("fs");
19 return {
20 ...actual,
21 existsSync: vi.fn(),
22 readFileSync: vi.fn(),
23 writeFileSync: vi.fn(),
24 unlinkSync: vi.fn(),
25 };
26});
27
28describe("multiEditTool", () => {
29 const testFilePath = "/tmp/test-multi-edit-file.txt";
30 const originalContent = "Hello world\nThis is a test file\nGoodbye world";
31
32 beforeEach(() => {
33 vi.clearAllMocks();
34 // Mock fs functions to simulate file operations
35 vi.mocked(fs.existsSync).mockReturnValue(true);
36 vi.mocked(fs.readFileSync).mockReturnValue(originalContent);
37 vi.mocked(fs.writeFileSync).mockImplementation(() => {});
38 });
39
40 afterEach(() => {
41 vi.clearAllMocks();
42 });
43
44 describe("preprocess", () => {
45 it("should throw error if file has not been read", async () => {
46 const args = {
47 file_path: testFilePath,
48 edits: [
49 {
50 old_string: "Hello world",
51 new_string: "Hi there",
52 },
53 ],
54 };
55
56 await expect(multiEditTool.preprocess!(args)).rejects.toThrow(
57 `You must use the Read tool to read ${testFilePath} before editing it.`,
58 );
59 });
60
61 it("should throw error if file does not exist", async () => {
62 const nonExistentFile = "/tmp/non-existent-file.txt";
63 markFileAsRead(nonExistentFile);
64 vi.mocked(fs.existsSync).mockReturnValue(false);
65
66 const args = {
67 file_path: nonExistentFile,
68 edits: [
69 {
70 old_string: "Hello",
71 new_string: "Hi",
72 },
73 ],
74 };
75
76 await expect(multiEditTool.preprocess!(args)).rejects.toThrow(
77 `File ${nonExistentFile} does not exist`,
78 );
79 });
80
81 it("should throw error if edits array is empty", async () => {
82 markFileAsRead(testFilePath);
83
84 const args = {
85 file_path: testFilePath,
86 edits: [],
87 };
88
89 await expect(multiEditTool.preprocess!(args)).rejects.toThrow(
90 "edits array is required and must contain at least one edit",
91 );
92 });
93
94 it("should throw error if edit has missing old_string", async () => {
95 markFileAsRead(testFilePath);
96
97 const args = {
98 file_path: testFilePath,
99 edits: [
100 {
101 old_string: undefined as any,
102 new_string: "Hi there",
103 },
104 ],
105 };
106
107 await expect(multiEditTool.preprocess!(args)).rejects.toThrow(
108 "Edit 1: old_string is required",
109 );
110 });
111
112 it("should throw error if edit has missing new_string", async () => {
113 markFileAsRead(testFilePath);
114
115 const args = {
116 file_path: testFilePath,
117 edits: [
118 {
119 old_string: "Hello world",
120 new_string: undefined as any,
121 },
122 ],
123 };
124
125 await expect(multiEditTool.preprocess!(args)).rejects.toThrow(
126 "Edit 1: new_string is required",
127 );
128 });
129
130 it("should throw error if old_string and new_string are the same", async () => {
131 markFileAsRead(testFilePath);
132
133 const args = {
134 file_path: testFilePath,
135 edits: [
136 {
137 old_string: "Hello world",
138 new_string: "Hello world",
139 },
140 ],
141 };
142
143 await expect(multiEditTool.preprocess!(args)).rejects.toThrow(
144 "Edit 1: old_string and new_string must be different",
145 );
146 });
147
148 it("should throw error if old_string is not found", async () => {
149 markFileAsRead(testFilePath);
150 vi.mocked(fs.readFileSync).mockReturnValue("Different content");
151
152 const args = {
153 file_path: testFilePath,
154 edits: [
155 {
156 old_string: "Not found",
157 new_string: "Hi there",
158 },
159 ],
160 };
161
162 await expect(multiEditTool.preprocess!(args)).rejects.toThrow(
163 'Edit 1: String not found in file: "Not found"',
164 );
165 });
166
167 it("should throw error if old_string appears multiple times and replace_all is false", async () => {
168 markFileAsRead(testFilePath);
169
170 const args = {
171 file_path: testFilePath,
172 edits: [
173 {
174 old_string: "world",
175 new_string: "universe",
176 replace_all: false,
177 },
178 ],
179 };
180
181 await expect(multiEditTool.preprocess!(args)).rejects.toThrow(
182 'Edit 1: String "world" appears 2 times in the file. Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences.',
183 );
184 });
185
186 it("should successfully preprocess single edit", async () => {
187 markFileAsRead(testFilePath);
188
189 const args = {
190 file_path: testFilePath,
191 edits: [
192 {
193 old_string: "Hello world",
194 new_string: "Hi there",
195 },
196 ],
197 };
198
199 const result = await multiEditTool.preprocess!(args);
200
201 expect(result.args).toEqual({
202 file_path: testFilePath,
203 newContent: "Hi there\nThis is a test file\nGoodbye world",
204 originalContent: originalContent,
205 isCreatingNewFile: false,
206 editCount: 1,
207 });
208 expect(result.preview).toHaveLength(2);
209 expect(result.preview?.[0]).toEqual({
210 type: "text",
211 content: "Will apply 1 edit to modify /tmp/test-multi-edit-file.txt:",
212 });
213 expect(result.preview?.[1]).toEqual({
214 type: "diff",
215 content: "mocked diff",
216 });
217 });
218
219 it("should successfully preprocess multiple edits", async () => {
220 markFileAsRead(testFilePath);
221
222 const args = {
223 file_path: testFilePath,
224 edits: [
225 {
226 old_string: "Hello world",
227 new_string: "Hi there",
228 },
229 {
230 old_string: "Goodbye world",
231 new_string: "See you later",
232 },
233 ],
234 };
235
236 const result = await multiEditTool.preprocess!(args);
237
238 expect(result.args.newContent).toBe(
239 "Hi there\nThis is a test file\nSee you later",
240 );
241 expect(result.args.editCount).toBe(2);
242 expect(result.preview?.[0]?.content).toContain("Will apply 2 edits");
243 });
244
245 it("should successfully preprocess replace_all edit", async () => {
246 markFileAsRead(testFilePath);
247
248 const args = {
249 file_path: testFilePath,
250 edits: [
251 {
252 old_string: "world",
253 new_string: "universe",
254 replace_all: true,
255 },
256 ],
257 };
258
259 const result = await multiEditTool.preprocess!(args);
260
261 expect(result.args.newContent).toBe(
262 "Hello universe\nThis is a test file\nGoodbye universe",
263 );
264 });
265
266 it("should handle new file creation with empty old_string", async () => {
267 const newFilePath = "/tmp/new-file.txt";
268 vi.mocked(fs.existsSync).mockImplementation((path) => {
269 // Parent directory exists, but file doesn't
270 if (path === "/tmp") return true;
271 if (path === newFilePath) return false;
272 return true;
273 });
274
275 const args = {
276 file_path: newFilePath,
277 edits: [
278 {
279 old_string: "",
280 new_string: "New file content\nSecond line",
281 },
282 ],
283 };
284
285 const result = await multiEditTool.preprocess!(args);
286
287 expect(result.args.newContent).toBe("New file content\nSecond line");
288 expect(result.args.isCreatingNewFile).toBe(true);
289 expect(result.preview?.[0]?.content).toContain("create");
290 });
291
292 it("should apply edits sequentially", async () => {
293 markFileAsRead(testFilePath);
294
295 const args = {
296 file_path: testFilePath,
297 edits: [
298 {
299 old_string: "Hello world",
300 new_string: "Hi universe",
301 },
302 {
303 old_string: "Hi universe",
304 new_string: "Greetings cosmos",
305 },
306 ],
307 };
308
309 const result = await multiEditTool.preprocess!(args);
310
311 expect(result.args.newContent).toBe(
312 "Greetings cosmos\nThis is a test file\nGoodbye world",
313 );
314 });
315 });
316
317 describe("run", () => {
318 it("should successfully write file and return success message", async () => {
319 const newContent = "Hi there\nThis is a test file\nGoodbye world";
320 const args = {
321 file_path: testFilePath,
322 newContent,
323 originalContent: originalContent,
324 isCreatingNewFile: false,
325 editCount: 1,
326 };
327
328 const result = await multiEditTool.run(args);
329
330 expect(result).toBe(
331 `Successfully edited ${testFilePath} with 1 edit\nDiff:\nmocked diff`,
332 );
333 expect(fs.writeFileSync).toHaveBeenCalledWith(
334 testFilePath,
335 newContent,
336 "utf-8",
337 );
338 });
339
340 it("should return correct message for multiple edits", async () => {
341 const args = {
342 file_path: testFilePath,
343 newContent: "New content",
344 originalContent: originalContent,
345 isCreatingNewFile: false,
346 editCount: 3,
347 };
348
349 const result = await multiEditTool.run(args);
350
351 expect(result).toBe(
352 `Successfully edited ${testFilePath} with 3 edits\nDiff:\nmocked diff`,
353 );
354 });
355
356 it("should return correct message for new file creation", async () => {
357 const args = {
358 file_path: "/tmp/new-file.txt",
359 newContent: "New file content",
360 originalContent: "",
361 isCreatingNewFile: true,
362 editCount: 1,
363 };
364
365 const result = await multiEditTool.run(args);
366
367 expect(result).toBe(
368 `Successfully created /tmp/new-file.txt with 1 edit\nDiff:\nmocked diff`,
369 );
370 });
371
372 it("should throw error if file write fails", async () => {
373 vi.mocked(fs.writeFileSync).mockImplementation(() => {
374 throw new Error("Write failed");
375 });
376
377 const args = {
378 file_path: testFilePath,
379 newContent: "new content",
380 originalContent: originalContent,
381 isCreatingNewFile: false,
382 editCount: 1,
383 };
384
385 await expect(multiEditTool.run(args)).rejects.toThrow(
386 `Error: failed to edit ${testFilePath}: Write failed`,
387 );
388 });
389 });
390
391 describe("relative path handling", () => {
392 it("should convert relative paths to absolute paths", async () => {
393 const relativePath = "test-file.txt";
394 const absolutePath = path.resolve(process.cwd(), relativePath);
395 markFileAsRead(absolutePath);
396
397 const args = {
398 file_path: relativePath,
399 edits: [
400 {
401 old_string: "Hello world",
402 new_string: "Hi there",
403 },
404 ],
405 };
406
407 const result = await multiEditTool.preprocess!(args);
408
409 expect(result.args.file_path).toBe(absolutePath);
410 expect(result.args.newContent).toBe(
411 "Hi there\nThis is a test file\nGoodbye world",
412 );
413 });
414
415 it("should handle relative paths with subdirectories", async () => {
416 const relativePath = "src/components/test.js";
417 const absolutePath = path.resolve(process.cwd(), relativePath);
418 markFileAsRead(absolutePath);
419
420 const args = {
421 file_path: relativePath,
422 edits: [
423 {
424 old_string: "Hello world",
425 new_string: "Hi there",
426 },
427 ],
428 };
429
430 const result = await multiEditTool.preprocess!(args);
431
432 expect(result.args.file_path).toBe(absolutePath);
433 });
434
435 it("should handle relative paths with ../ patterns", async () => {
436 const relativePath = "../test-file.txt";
437 const absolutePath = path.resolve(process.cwd(), relativePath);
438 markFileAsRead(absolutePath);
439
440 const args = {
441 file_path: relativePath,
442 edits: [
443 {
444 old_string: "Hello world",
445 new_string: "Hi there",
446 },
447 ],
448 };
449
450 const result = await multiEditTool.preprocess!(args);
451
452 expect(result.args.file_path).toBe(absolutePath);
453 });
454
455 it("should leave absolute paths unchanged", async () => {
456 const absolutePath = "/tmp/absolute-file.txt";
457 markFileAsRead(absolutePath);
458
459 const args = {
460 file_path: absolutePath,
461 edits: [
462 {
463 old_string: "Hello world",
464 new_string: "Hi there",
465 },
466 ],
467 };
468
469 const result = await multiEditTool.preprocess!(args);
470
471 expect(result.args.file_path).toBe(absolutePath);
472 });
473
474 it("should handle relative path for new file creation", async () => {
475 const relativePath = "new-file.txt";
476 const absolutePath = path.resolve(process.cwd(), relativePath);
477
478 vi.mocked(fs.existsSync).mockImplementation((path) => {
479 // Parent directory (cwd) exists, but file doesn't
480 if (path === process.cwd()) return true;
481 if (path === absolutePath) return false;
482 return true;
483 });
484
485 const args = {
486 file_path: relativePath,
487 edits: [
488 {
489 old_string: "",
490 new_string: "New file content",
491 },
492 ],
493 };
494
495 const result = await multiEditTool.preprocess!(args);
496
497 expect(result.args.file_path).toBe(absolutePath);
498 expect(result.args.newContent).toBe("New file content");
499 expect(result.args.isCreatingNewFile).toBe(true);
500 });
501 });
502
503 describe("markFileAsRead", () => {
504 it("should allow editing after marking file as read", async () => {
505 markFileAsRead(testFilePath);
506
507 const args = {
508 file_path: testFilePath,
509 edits: [
510 {
511 old_string: "Hello world",
512 new_string: "Hi there",
513 },
514 ],
515 };
516
517 const result = await multiEditTool.preprocess!(args);
518 expect(result.args.newContent).toContain("Hi there");
519 });
520 });
521});