Elffuss BraidHub

kikocis/continue public

fb08fd5cc0fe · 15.0 KiBRawPermalink
1import { EditOperation } from "core/tools/definitions/multiEdit";
2import { describe, expect, it } from "vitest";
3import {
4 EMPTY_NON_FIRST_EDIT_MESSAGE,
5 FOUND_MULTIPLE_FIND_STRINGS_ERROR,
6 performFindAndReplace,
7 trimEmptyLines,
8 validateCreatingForMultiEdit,
9 validateSingleEdit,
10} from "./findAndReplaceUtils";
11
12describe("performFindAndReplace", () => {
13 describe("normal cases", () => {
14 it("should replace single occurrence by default", () => {
15 const content = "Hello world\nThis is a test file\nGoodbye";
16 const result = performFindAndReplace(content, "Hello", "Hi");
17
18 expect(result).toBe("Hi world\nThis is a test file\nGoodbye");
19 });
20
21 it("should replace single occurrence when replaceAll is false", () => {
22 const content = "Hello world\nHello again";
23 const result = performFindAndReplace(
24 content,
25 "Hello world",
26 "Hi world",
27 false,
28 );
29
30 expect(result).toBe("Hi world\nHello again");
31 });
32
33 it("should replace all occurrences when replaceAll is true", () => {
34 const content = "Hello world\nHello again\nHello there";
35 const result = performFindAndReplace(content, "Hello", "Hi", true);
36
37 expect(result).toBe("Hi world\nHi again\nHi there");
38 });
39
40 it("should handle empty new string (deletion)", () => {
41 const content = "Hello world";
42 const result = performFindAndReplace(content, "Hello ", "");
43
44 expect(result).toBe("world");
45 });
46
47 it("should handle empty old string replacement (insertion at start)", () => {
48 const content = "";
49 const result = performFindAndReplace(content, "", "Hello world");
50
51 expect(result).toBe("Hello world");
52 });
53
54 it("should preserve whitespace and indentation", () => {
55 const content =
56 "function test() {\n const value = 'old';\n return value;\n}";
57 const result = performFindAndReplace(
58 content,
59 " const value = 'old';",
60 " const value = 'new';",
61 );
62
63 expect(result).toBe(
64 "function test() {\n const value = 'new';\n return value;\n}",
65 );
66 });
67
68 it("should handle multiline strings", () => {
69 const content = "line1\nline2\nline3";
70 const result = performFindAndReplace(
71 content,
72 "line1\nline2",
73 "newline1\nnewline2",
74 );
75
76 expect(result).toBe("newline1\nnewline2\nline3");
77 });
78 });
79
80 describe("special character handling", () => {
81 it("should handle regex special characters in old string", () => {
82 const content = "const regex = /[a-z]+/g;";
83 const result = performFindAndReplace(content, "/[a-z]+/g", "/[A-Z]+/g");
84
85 expect(result).toBe("const regex = /[A-Z]+/g;");
86 });
87
88 it("should handle dollar signs in strings", () => {
89 const content = 'const text = "Hello $world"';
90 const result = performFindAndReplace(
91 content,
92 '"Hello $world"',
93 '"Hi $universe"',
94 );
95
96 expect(result).toBe('const text = "Hi $universe"');
97 });
98
99 it("should handle backslashes", () => {
100 const content = 'const path = "C:\\Users\\test"';
101 const result = performFindAndReplace(
102 content,
103 "C:\\Users\\test",
104 "D:\\Users\\test",
105 );
106
107 expect(result).toBe('const path = "D:\\Users\\test"');
108 });
109
110 it("should handle parentheses", () => {
111 const content = "function test() { return 'old'; }";
112 const result = performFindAndReplace(
113 content,
114 "() { return 'old'; }",
115 "() { return 'new'; }",
116 );
117
118 expect(result).toBe("function test() { return 'new'; }");
119 });
120
121 it("should handle square brackets", () => {
122 const content = "const arr = [1, 2, 3];";
123 const result = performFindAndReplace(content, "[1, 2, 3]", "[4, 5, 6]");
124
125 expect(result).toBe("const arr = [4, 5, 6];");
126 });
127 });
128
129 describe("replaceAll behavior", () => {
130 it("should replace all occurrences including partial matches", () => {
131 const content = "test testing tested test";
132 const result = performFindAndReplace(content, "test", "exam", true);
133
134 expect(result).toBe("exam examing examed exam");
135 });
136
137 it("should replace all occurrences with empty string", () => {
138 const content = "remove this remove that remove everything";
139 const result = performFindAndReplace(content, "remove ", "", true);
140
141 expect(result).toBe("this that everything");
142 });
143
144 it("should handle overlapping patterns correctly", () => {
145 const content = "aaaa";
146 const result = performFindAndReplace(content, "aa", "b", true);
147
148 expect(result).toBe("bb");
149 });
150 });
151
152 describe("error cases", () => {
153 it("should throw error when string not found", () => {
154 const content = "Hello world";
155
156 expect(() => {
157 performFindAndReplace(content, "xyz", "abc");
158 }).toThrow('string not found in file: "xyz"');
159 });
160
161 it("should throw error with edit context when string not found", () => {
162 const content = "Hello world";
163
164 expect(() => {
165 performFindAndReplace(content, "xyz", "abc", false, 2);
166 }).toThrow('edit at index 2: string not found in file: "xyz"');
167 });
168
169 it("should throw error when multiple occurrences exist and replaceAll is false", () => {
170 const content = "Hello world\nHello again\nHello there";
171
172 expect(() => {
173 performFindAndReplace(content, "Hello", "Hi", false);
174 }).toThrow(
175 `String "Hello" appears 3 times in the file. ${FOUND_MULTIPLE_FIND_STRINGS_ERROR}`,
176 );
177 });
178
179 it("should throw error with edit context for multiple occurrences", () => {
180 const content = "test test test";
181
182 expect(() => {
183 performFindAndReplace(content, "test", "exam", false, 1);
184 }).toThrow(
185 `edit at index 1: String "test" appears 3 times in the file. ${FOUND_MULTIPLE_FIND_STRINGS_ERROR}`,
186 );
187 });
188 });
189
190 describe("edge cases", () => {
191 it("should handle single character replacement", () => {
192 const content = "a b c";
193 const result = performFindAndReplace(content, "b", "x");
194
195 expect(result).toBe("a x c");
196 });
197
198 it("should handle very long strings", () => {
199 const longString = "a".repeat(10000);
200 const content = `start${longString}end`;
201 const result = performFindAndReplace(content, longString, "short");
202
203 expect(result).toBe("startshortend");
204 });
205
206 it("should handle strings at beginning of content", () => {
207 const content = "start middle end";
208 const result = performFindAndReplace(content, "start", "begin");
209
210 expect(result).toBe("begin middle end");
211 });
212
213 it("should handle strings at end of content", () => {
214 const content = "start middle end";
215 const result = performFindAndReplace(content, "end", "finish");
216
217 expect(result).toBe("start middle finish");
218 });
219
220 it("should handle entire content replacement", () => {
221 const content = "replace everything";
222 const result = performFindAndReplace(
223 content,
224 "replace everything",
225 "new content",
226 );
227
228 expect(result).toBe("new content");
229 });
230 });
231});
232
233describe("validateSingleEdit", () => {
234 describe("valid inputs", () => {
235 it("should not throw for valid non-empty strings", () => {
236 expect(() => {
237 validateSingleEdit("old", "new");
238 }).not.toThrow();
239 });
240
241 it("should allow empty old_string for file creation", () => {
242 expect(() => {
243 validateSingleEdit("", "new content");
244 }).not.toThrow();
245 });
246
247 it("should allow empty new_string for deletion", () => {
248 expect(() => {
249 validateSingleEdit("delete me", "");
250 }).not.toThrow();
251 });
252
253 it("should allow multiline strings", () => {
254 expect(() => {
255 validateSingleEdit("line1\nline2", "newline1\nnewline2");
256 }).not.toThrow();
257 });
258
259 it("should allow special characters", () => {
260 expect(() => {
261 validateSingleEdit("/[a-z]+/g", "/[A-Z]+/g");
262 }).not.toThrow();
263 });
264 });
265
266 describe("invalid inputs", () => {
267 it("should throw error when old_string is null", () => {
268 expect(() => {
269 validateSingleEdit(null as any, "new");
270 }).toThrow("old_string is required");
271 });
272
273 it("should throw error when old_string is undefined", () => {
274 expect(() => {
275 validateSingleEdit(undefined as any, "new");
276 }).toThrow("old_string is required");
277 });
278
279 it("should throw error when new_string is undefined", () => {
280 expect(() => {
281 validateSingleEdit("old", undefined as any);
282 }).toThrow("new_string is required");
283 });
284
285 it("should throw error when old_string and new_string are identical", () => {
286 expect(() => {
287 validateSingleEdit("same", "same");
288 }).toThrow("old_string and new_string must be different");
289 });
290
291 it("should throw error when both are empty strings", () => {
292 expect(() => {
293 validateSingleEdit("", "");
294 }).toThrow("old_string and new_string must be different");
295 });
296 });
297
298 describe("error messages with context", () => {
299 it("should include edit number in error messages when index provided", () => {
300 expect(() => {
301 validateSingleEdit(null as any, "new", 2);
302 }).toThrow("edit at index 2: old_string is required");
303 });
304
305 it("should include edit number for new_string errors", () => {
306 expect(() => {
307 validateSingleEdit("old", undefined as any, 0);
308 }).toThrow("edit at index 0: new_string is required");
309 });
310
311 it("should include edit number for identical strings error", () => {
312 expect(() => {
313 validateSingleEdit("same", "same", 4);
314 }).toThrow(
315 "edit at index 4: old_string and new_string must be different",
316 );
317 });
318
319 it("should not include context when index not provided", () => {
320 expect(() => {
321 validateSingleEdit("same", "same");
322 }).toThrow("old_string and new_string must be different");
323 });
324 });
325});
326
327describe("validateCreatingForMultiEdit", () => {
328 describe("single edit scenarios", () => {
329 it("should return true for creating with single edit", () => {
330 const edits: EditOperation[] = [
331 { old_string: "", new_string: "new file content" },
332 ];
333
334 const isCreating = validateCreatingForMultiEdit(edits);
335 expect(isCreating).toBe(true);
336 });
337
338 it("should return false for editing with single edit", () => {
339 const edits: EditOperation[] = [{ old_string: "old", new_string: "new" }];
340
341 const isCreating = validateCreatingForMultiEdit(edits);
342 expect(isCreating).toBe(false);
343 });
344 });
345
346 describe("multiple edit scenarios - editing existing file", () => {
347 it("should return false for multiple edits on existing file", () => {
348 const edits: EditOperation[] = [
349 { old_string: "old1", new_string: "new1" },
350 { old_string: "old2", new_string: "new2" },
351 { old_string: "old3", new_string: "new3" },
352 ];
353
354 const isCreating = validateCreatingForMultiEdit(edits);
355 expect(isCreating).toBe(false);
356 });
357
358 it("should allow multiple valid edits on existing file", () => {
359 const edits: EditOperation[] = [
360 { old_string: "import old", new_string: "import new" },
361 { old_string: "function old()", new_string: "function new()" },
362 { old_string: "return old;", new_string: "return new;" },
363 ];
364
365 expect(() => validateCreatingForMultiEdit(edits)).not.toThrow();
366 expect(validateCreatingForMultiEdit(edits)).toBe(false);
367 });
368 });
369
370 describe("error cases", () => {
371 it("should throw error when trying to make subsequent edits on file creation", () => {
372 const edits: EditOperation[] = [
373 { old_string: "", new_string: "new file content" },
374 { old_string: "old", new_string: "new" },
375 ];
376
377 expect(() => {
378 validateCreatingForMultiEdit(edits);
379 }).toThrow("cannot make subsequent edits on a file you are creating");
380 });
381
382 it("should throw error when empty old_string appears in non-first edit", () => {
383 const edits: EditOperation[] = [
384 { old_string: "old1", new_string: "new1" },
385 { old_string: "", new_string: "new2" },
386 ];
387
388 expect(() => {
389 validateCreatingForMultiEdit(edits);
390 }).toThrow(`edit at index 1: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`);
391 });
392
393 it("should throw error with correct edit number for empty old_string", () => {
394 const edits: EditOperation[] = [
395 { old_string: "old1", new_string: "new1" },
396 { old_string: "old2", new_string: "new2" },
397 { old_string: "", new_string: "new3" },
398 ];
399
400 expect(() => {
401 validateCreatingForMultiEdit(edits);
402 }).toThrow(`edit at index 2: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`);
403 });
404
405 it("should throw error for multiple empty old_strings in subsequent edits", () => {
406 const edits: EditOperation[] = [
407 { old_string: "old1", new_string: "new1" },
408 { old_string: "", new_string: "new2" },
409 { old_string: "", new_string: "new3" },
410 ];
411
412 expect(() => {
413 validateCreatingForMultiEdit(edits);
414 }).toThrow(`edit at index 1: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`);
415 });
416 });
417
418 describe("edge cases", () => {
419 it("should handle empty edits array gracefully", () => {
420 const edits: EditOperation[] = [];
421
422 // This should not throw, but would likely cause issues elsewhere
423 // The function doesn't explicitly handle empty arrays
424 expect(() => {
425 validateCreatingForMultiEdit(edits);
426 }).toThrow(); // Will throw because edits[0] is undefined
427 });
428
429 it("should handle complex edit operations", () => {
430 const edits: EditOperation[] = [
431 {
432 old_string: "function oldFunction() {\n return 'old';\n}",
433 new_string: "function newFunction() {\n return 'new';\n}",
434 replace_all: false,
435 },
436 {
437 old_string: "const OLD_CONSTANT",
438 new_string: "const NEW_CONSTANT",
439 replace_all: true,
440 },
441 ];
442
443 expect(() => validateCreatingForMultiEdit(edits)).not.toThrow();
444 expect(validateCreatingForMultiEdit(edits)).toBe(false);
445 });
446
447 it("should handle edits with replace_all flags", () => {
448 const edits: EditOperation[] = [
449 { old_string: "old", new_string: "new", replace_all: true },
450 { old_string: "test", new_string: "exam", replace_all: false },
451 ];
452
453 expect(() => validateCreatingForMultiEdit(edits)).not.toThrow();
454 expect(validateCreatingForMultiEdit(edits)).toBe(false);
455 });
456 });
457});
458
459describe("trimEmptyLines", () => {
460 it("trims leading empty lines from start", () => {
461 const input = ["", "", "a", "", "b"];
462 const result = trimEmptyLines({ lines: input, fromEnd: false });
463 expect(result).toEqual(["a", "", "b"]);
464 });
465
466 it("trims trailing empty lines from end", () => {
467 const input = ["a", "", "b", "", ""];
468 const result = trimEmptyLines({ lines: input, fromEnd: true });
469 expect(result).toEqual(["a", "", "b"]);
470 });
471
472 it("returns empty array when all lines are empty", () => {
473 const input = ["", "", ""];
474 const result = trimEmptyLines({ lines: input, fromEnd: false });
475 expect(result).toEqual([]);
476 });
477});