Elffuss BraidHub

kikocis/continue public

8eb3ae7b8d16 · 9.7 KiBRawPermalink
1import {
2 parseAllSearchReplaceBlocks,
3 parseSearchReplaceBlock,
4} from "./parseSearchReplaceBlock";
5
6describe("parseSearchReplaceBlock", () => {
7 describe("Complete blocks", () => {
8 it("should parse a complete basic search/replace block", () => {
9 const content = `------- SEARCH
10reset() {
11this.result = 0;
12return this;
13}
14=======
15// Resets the calculator result to 0 and returns the instance for method chaining
16reset() {
17this.result = 0;
18return this;
19}
20+++++++ REPLACE`;
21
22 const result = parseSearchReplaceBlock(content);
23
24 expect(result.isComplete).toBe(true);
25 expect(result.searchContent).toBe(`reset() {
26this.result = 0;
27return this;
28}`);
29 expect(result.replaceContent)
30 .toBe(`// Resets the calculator result to 0 and returns the instance for method chaining
31reset() {
32this.result = 0;
33return this;
34}`);
35 expect(result.error).toBeUndefined();
36 });
37
38 it("should handle different marker lengths", () => {
39 const content = `------- SEARCH
40old content
41=========
42new content
43+++++++++++ REPLACE`;
44
45 const result = parseSearchReplaceBlock(content);
46
47 expect(result.isComplete).toBe(true);
48 expect(result.searchContent).toBe("old content");
49 expect(result.replaceContent).toBe("new content");
50 });
51
52 it("should handle empty search content", () => {
53 const content = `------- SEARCH
54=======
55new content
56+++++++ REPLACE`;
57
58 const result = parseSearchReplaceBlock(content);
59
60 expect(result.isComplete).toBe(true);
61 expect(result.searchContent).toBe("");
62 expect(result.replaceContent).toBe("new content");
63 });
64
65 it("should handle empty replace content (deletion)", () => {
66 const content = `------- SEARCH
67old content to delete
68=======
69+++++++ REPLACE`;
70
71 const result = parseSearchReplaceBlock(content);
72
73 expect(result.isComplete).toBe(true);
74 expect(result.searchContent).toBe("old content to delete");
75 expect(result.replaceContent).toBe("");
76 });
77
78 it("should handle both empty search and replace content", () => {
79 const content = `------- SEARCH
80=======
81+++++++ REPLACE`;
82
83 const result = parseSearchReplaceBlock(content);
84
85 expect(result.isComplete).toBe(true);
86 expect(result.searchContent).toBe("");
87 expect(result.replaceContent).toBe("");
88 });
89 });
90
91 describe("Incomplete blocks", () => {
92 it("should handle incomplete search block", () => {
93 const content = `------- SEARCH
94some content`;
95
96 const result = parseSearchReplaceBlock(content);
97
98 expect(result.isComplete).toBe(false);
99 expect(result.searchContent).toBe("some content");
100 expect(result.replaceContent).toBe("");
101 expect(result.error).toBeUndefined();
102 });
103
104 it("should handle incomplete replace block", () => {
105 const content = `------- SEARCH
106old content
107=======
108new content`;
109
110 const result = parseSearchReplaceBlock(content);
111
112 expect(result.isComplete).toBe(false);
113 expect(result.searchContent).toBe("old content");
114 expect(result.replaceContent).toBe("new content");
115 expect(result.error).toBeUndefined();
116 });
117 });
118
119 describe("Error cases", () => {
120 it("should error on search end without start", () => {
121 const content = `=======
122some content
123+++++++ REPLACE`;
124
125 const result = parseSearchReplaceBlock(content);
126
127 expect(result.isComplete).toBe(false);
128 expect(result.error).toBe(
129 "Found search block end marker without matching start marker",
130 );
131 });
132
133 it("should error on replace end without replace start", () => {
134 const content = `+++++++ REPLACE`;
135
136 const result = parseSearchReplaceBlock(content);
137
138 expect(result.isComplete).toBe(false);
139 expect(result.error).toBe(
140 "Found replace block end marker without matching replace start marker",
141 );
142 });
143 });
144
145 describe("Regex pattern validation", () => {
146 it("should match various search start patterns", () => {
147 const patterns = ["--- SEARCH", "------- SEARCH", "------------- SEARCH"];
148
149 patterns.forEach((pattern) => {
150 const content = `${pattern}
151content
152=======
153replacement
154+++++++ REPLACE`;
155
156 const result = parseSearchReplaceBlock(content);
157 expect(result.isComplete).toBe(true);
158 });
159 });
160
161 it("should match various search end patterns", () => {
162 const patterns = ["===", "=======", "=============="];
163
164 patterns.forEach((pattern) => {
165 const content = `------- SEARCH
166content
167${pattern}
168replacement
169+++++++ REPLACE`;
170
171 const result = parseSearchReplaceBlock(content);
172 expect(result.isComplete).toBe(true);
173 });
174 });
175
176 it("should match various replace end patterns", () => {
177 const patterns = [
178 "+++ REPLACE",
179 "+++++++ REPLACE",
180 "+++++++++++ REPLACE",
181 ];
182
183 patterns.forEach((pattern) => {
184 const content = `------- SEARCH
185content
186=======
187replacement
188${pattern}`;
189
190 const result = parseSearchReplaceBlock(content);
191 expect(result.isComplete).toBe(true);
192 });
193 });
194
195 it("should not match invalid patterns", () => {
196 const invalidPatterns = [
197 "-- SEARCH", // Too few dashes
198 "SEARCH", // No markers
199 "== SEARCH", // Wrong markers
200 "=== REPLACE", // Wrong end marker
201 "++++ SEARCH", // Wrong markers for search
202 ];
203
204 invalidPatterns.forEach((pattern) => {
205 const content = `${pattern}
206content`;
207
208 const result = parseSearchReplaceBlock(content);
209 expect(result.isComplete).toBe(false);
210 expect(result.searchContent).toBe(""); // Should not enter search mode
211 });
212 });
213 });
214});
215
216describe("parseAllSearchReplaceBlocks", () => {
217 it("should parse multiple complete blocks", () => {
218 const content = `------- SEARCH
219old code 1
220=======
221new code 1
222+++++++ REPLACE
223
224------- SEARCH
225old code 2
226=======
227new code 2
228+++++++ REPLACE`;
229
230 const blocks = parseAllSearchReplaceBlocks(content);
231
232 expect(blocks).toHaveLength(2);
233 expect(blocks[0].searchContent).toBe("old code 1");
234 expect(blocks[0].replaceContent).toBe("new code 1");
235 expect(blocks[1].searchContent).toBe("old code 2");
236 expect(blocks[1].replaceContent).toBe("new code 2");
237 });
238
239 it("should handle blocks with different content", () => {
240 const content = `------- SEARCH
241function old() {
242 return 1;
243}
244=======
245function new() {
246 return 2;
247}
248+++++++ REPLACE
249
250------- SEARCH
251const x = 'old';
252=======
253const x = 'new';
254+++++++ REPLACE`;
255
256 const blocks = parseAllSearchReplaceBlocks(content);
257
258 expect(blocks).toHaveLength(2);
259 expect(blocks[0].searchContent).toBe(`function old() {
260 return 1;
261}`);
262 expect(blocks[0].replaceContent).toBe(`function new() {
263 return 2;
264}`);
265 expect(blocks[1].searchContent).toBe(`const x = 'old';`);
266 expect(blocks[1].replaceContent).toBe(`const x = 'new';`);
267 });
268
269 it("should handle deletion blocks (empty replace content)", () => {
270 const content = `------- SEARCH
271code to keep
272=======
273modified code
274+++++++ REPLACE
275
276------- SEARCH
277code to delete
278=======
279+++++++ REPLACE`;
280
281 const blocks = parseAllSearchReplaceBlocks(content);
282
283 expect(blocks).toHaveLength(2);
284 expect(blocks[0].searchContent).toBe("code to keep");
285 expect(blocks[0].replaceContent).toBe("modified code");
286 expect(blocks[1].searchContent).toBe("code to delete");
287 expect(blocks[1].replaceContent).toBe(""); // Deletion
288 });
289
290 it("should handle insertion blocks (empty search content)", () => {
291 const content = `------- SEARCH
292existing code
293=======
294modified existing code
295+++++++ REPLACE
296
297------- SEARCH
298=======
299new code to insert
300+++++++ REPLACE`;
301
302 const blocks = parseAllSearchReplaceBlocks(content);
303
304 expect(blocks).toHaveLength(2);
305 expect(blocks[0].searchContent).toBe("existing code");
306 expect(blocks[0].replaceContent).toBe("modified existing code");
307 expect(blocks[1].searchContent).toBe(""); // Insertion
308 expect(blocks[1].replaceContent).toBe("new code to insert");
309 });
310
311 it("should skip incomplete blocks", () => {
312 const content = `------- SEARCH
313incomplete block
314
315------- SEARCH
316complete block
317=======
318replacement
319+++++++ REPLACE`;
320
321 const blocks = parseAllSearchReplaceBlocks(content);
322
323 expect(blocks).toHaveLength(1);
324 expect(blocks[0].searchContent).toBe("complete block");
325 expect(blocks[0].replaceContent).toBe("replacement");
326 });
327
328 it("should handle single block", () => {
329 const content = `------- SEARCH
330single block
331=======
332replacement
333+++++++ REPLACE`;
334
335 const blocks = parseAllSearchReplaceBlocks(content);
336
337 expect(blocks).toHaveLength(1);
338 expect(blocks[0].searchContent).toBe("single block");
339 expect(blocks[0].replaceContent).toBe("replacement");
340 });
341
342 it("should return empty array when no complete blocks found", () => {
343 const content = `------- SEARCH
344incomplete
345=======
346missing replace marker`;
347
348 const blocks = parseAllSearchReplaceBlocks(content);
349
350 expect(blocks).toHaveLength(0);
351 });
352
353 it("should handle blocks with extra content between them", () => {
354 const content = `Some other content here
355
356------- SEARCH
357block 1
358=======
359replacement 1
360+++++++ REPLACE
361
362More content in between
363
364------- SEARCH
365block 2
366=======
367replacement 2
368+++++++ REPLACE
369
370Trailing content`;
371
372 const blocks = parseAllSearchReplaceBlocks(content);
373
374 expect(blocks).toHaveLength(2);
375 expect(blocks[0].searchContent).toBe("block 1");
376 expect(blocks[0].replaceContent).toBe("replacement 1");
377 expect(blocks[1].searchContent).toBe("block 2");
378 expect(blocks[1].replaceContent).toBe("replacement 2");
379 });
380
381 it("should throw error for malformed blocks", () => {
382 const content = `------- SEARCH
383content
384+++++++ REPLACE`; // Missing =======
385
386 expect(() => parseAllSearchReplaceBlocks(content)).toThrow(
387 "Found replace block end marker without matching replace start marker",
388 );
389 });
390});