Elffuss BraidHub

kikocis/continue public

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