Elffuss BraidHub

kikocis/continue public

main · 7.3 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import { ContinueErrorReason } from "../../util/errors";
3import { executeFindAndReplace } from "./performReplace";
4
5describe("executeFindAndReplace", () => {
6 describe("normal cases", () => {
7 it("should replace single occurrence by default", () => {
8 const content = "Hello world\nThis is a test file\nGoodbye";
9 const result = executeFindAndReplace(content, "Hello", "Hi", false);
10
11 expect(result).toBe("Hi world\nThis is a test file\nGoodbye");
12 });
13
14 it("should replace single occurrence when replaceAll is false", () => {
15 const content = "Hello world\nHello again";
16 const result = executeFindAndReplace(
17 content,
18 "Hello world",
19 "Hi world",
20 false,
21 );
22
23 expect(result).toBe("Hi world\nHello again");
24 });
25
26 it("should replace all occurrences when replaceAll is true", () => {
27 const content = "Hello world\nHello again\nHello there";
28 const result = executeFindAndReplace(content, "Hello", "Hi", true);
29
30 expect(result).toBe("Hi world\nHi again\nHi there");
31 });
32
33 it("should handle empty new string (deletion)", () => {
34 const content = "Hello world";
35 const result = executeFindAndReplace(content, "Hello ", "", false);
36
37 expect(result).toBe("world");
38 });
39
40 it("should handle empty old string replacement (insertion at start)", () => {
41 const content = "";
42 const result = executeFindAndReplace(content, "", "Hello world", false);
43
44 expect(result).toBe("Hello world");
45 });
46
47 it("should preserve whitespace and indentation", () => {
48 const content =
49 "function test() {\n const value = 'old';\n return value;\n}";
50 const result = executeFindAndReplace(
51 content,
52 " const value = 'old';",
53 " const value = 'new';",
54 false,
55 );
56
57 expect(result).toBe(
58 "function test() {\n const value = 'new';\n return value;\n}",
59 );
60 });
61
62 it("should handle multiline strings", () => {
63 const content = "line1\nline2\nline3";
64 const result = executeFindAndReplace(
65 content,
66 "line1\nline2",
67 "newline1\nnewline2",
68 false,
69 );
70
71 expect(result).toBe("newline1\nnewline2\nline3");
72 });
73 });
74
75 describe("special character handling", () => {
76 it("should handle regex special characters in old string", () => {
77 const content = "const regex = /[a-z]+/g;";
78 const result = executeFindAndReplace(
79 content,
80 "/[a-z]+/g",
81 "/[A-Z]+/g",
82 false,
83 );
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 = executeFindAndReplace(
91 content,
92 '"Hello $world"',
93 '"Hi $universe"',
94 false,
95 );
96
97 expect(result).toBe('const text = "Hi $universe"');
98 });
99
100 it("should handle backslashes", () => {
101 const content = 'const path = "C:\\Users\\test"';
102 const result = executeFindAndReplace(
103 content,
104 "C:\\Users\\test",
105 "D:\\Users\\test",
106 false,
107 );
108
109 expect(result).toBe('const path = "D:\\Users\\test"');
110 });
111
112 it("should handle parentheses", () => {
113 const content = "function test() { return 'old'; }";
114 const result = executeFindAndReplace(
115 content,
116 "() { return 'old'; }",
117 "() { return 'new'; }",
118 false,
119 );
120
121 expect(result).toBe("function test() { return 'new'; }");
122 });
123
124 it("should handle square brackets", () => {
125 const content = "const arr = [1, 2, 3];";
126 const result = executeFindAndReplace(
127 content,
128 "[1, 2, 3]",
129 "[4, 5, 6]",
130 false,
131 );
132
133 expect(result).toBe("const arr = [4, 5, 6];");
134 });
135 });
136
137 describe("replaceAll behavior", () => {
138 it("should replace all occurrences including partial matches", () => {
139 const content = "test testing tested test";
140 const result = executeFindAndReplace(content, "test", "exam", true);
141
142 expect(result).toBe("exam examing examed exam");
143 });
144
145 it("should replace all occurrences with empty string", () => {
146 const content = "remove this remove that remove everything";
147 const result = executeFindAndReplace(content, "remove ", "", true);
148
149 expect(result).toBe("this that everything");
150 });
151
152 it("should handle overlapping patterns correctly", () => {
153 const content = "aaaa";
154 const result = executeFindAndReplace(content, "aa", "b", true);
155
156 expect(result).toBe("bb");
157 });
158 });
159
160 describe("error cases", () => {
161 it("should throw error when string not found", () => {
162 const content = "Hello world";
163
164 expect(() => {
165 executeFindAndReplace(content, "xyz", "abc", false);
166 }).toThrowError(
167 expect.objectContaining({
168 reason: ContinueErrorReason.FindAndReplaceOldStringNotFound,
169 }),
170 );
171 });
172
173 it("should throw error with edit context when string not found", () => {
174 const content = "Hello world";
175
176 expect(() => {
177 executeFindAndReplace(content, "xyz", "abc", false, 2);
178 }).toThrowError(
179 expect.objectContaining({
180 reason: ContinueErrorReason.FindAndReplaceOldStringNotFound,
181 }),
182 );
183 });
184
185 it("should throw error when multiple occurrences exist and replaceAll is false", () => {
186 const content = "Hello world\nHello again\nHello there";
187
188 expect(() => {
189 executeFindAndReplace(content, "Hello", "Hi", false);
190 }).toThrowError(
191 expect.objectContaining({
192 reason: ContinueErrorReason.FindAndReplaceMultipleOccurrences,
193 }),
194 );
195 });
196
197 it("should throw error with edit context for multiple occurrences", () => {
198 const content = "test test test";
199
200 expect(() => {
201 executeFindAndReplace(content, "test", "exam", false, 1);
202 }).toThrowError(
203 expect.objectContaining({
204 reason: ContinueErrorReason.FindAndReplaceMultipleOccurrences,
205 }),
206 );
207 });
208 });
209
210 describe("edge cases", () => {
211 it("should handle single character replacement", () => {
212 const content = "a b c";
213 const result = executeFindAndReplace(content, "b", "x", false);
214
215 expect(result).toBe("a x c");
216 });
217
218 it("should handle very long strings", () => {
219 const longString = "a".repeat(10000);
220 const content = `start${longString}end`;
221 const result = executeFindAndReplace(content, longString, "short", false);
222
223 expect(result).toBe("startshortend");
224 });
225
226 it("should handle strings at beginning of content", () => {
227 const content = "start middle end";
228 const result = executeFindAndReplace(content, "start", "begin", false);
229
230 expect(result).toBe("begin middle end");
231 });
232
233 it("should handle strings at end of content", () => {
234 const content = "start middle end";
235 const result = executeFindAndReplace(content, "end", "finish", false);
236
237 expect(result).toBe("start middle finish");
238 });
239
240 it("should handle entire content replacement", () => {
241 const content = "replace everything";
242 const result = executeFindAndReplace(
243 content,
244 "replace everything",
245 "new content",
246 false,
247 );
248
249 expect(result).toBe("new content");
250 });
251 });
252});