Elffuss BraidHub

kikocis/continue public

main · 19.8 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import { findSearchMatch } from "./findSearchMatch";
3
4describe("findSearchMatch", () => {
5 describe("Empty search content", () => {
6 it("should return position 0 for empty string", () => {
7 const result = findSearchMatch("some file content", "");
8 expect(result).toEqual({
9 startIndex: 0,
10 endIndex: 0,
11 strategyName: "emptySearch",
12 });
13 });
14
15 it("should return position 0 for whitespace-only string", () => {
16 const result = findSearchMatch("some file content", " \n\t ");
17 expect(result).toEqual({
18 startIndex: 0,
19 endIndex: 0,
20 strategyName: "emptySearch",
21 });
22 });
23
24 it("should handle empty file content with empty search", () => {
25 const result = findSearchMatch("", "");
26 expect(result).toEqual({
27 startIndex: 0,
28 endIndex: 0,
29 strategyName: "emptySearch",
30 });
31 });
32 });
33
34 describe("Exact match strategy", () => {
35 it("should find exact match at beginning of file", () => {
36 const fileContent = "hello world";
37 const searchContent = "hello";
38 const result = findSearchMatch(fileContent, searchContent);
39
40 expect(result).toEqual({
41 startIndex: 0,
42 endIndex: 5,
43 strategyName: "exactMatch",
44 });
45 });
46
47 it("should find exact match in middle of file", () => {
48 const fileContent = "hello world test";
49 const searchContent = "world";
50 const result = findSearchMatch(fileContent, searchContent);
51
52 expect(result).toEqual({
53 startIndex: 6,
54 endIndex: 11,
55 strategyName: "exactMatch",
56 });
57 });
58
59 it("should find exact match at end of file", () => {
60 const fileContent = "hello world";
61 const searchContent = "world";
62 const result = findSearchMatch(fileContent, searchContent);
63
64 expect(result).toEqual({
65 startIndex: 6,
66 endIndex: 11,
67 strategyName: "exactMatch",
68 });
69 });
70
71 it("should find multi-line exact match", () => {
72 const fileContent = "line1\nline2\nline3";
73 const searchContent = "line2\nline3";
74 const result = findSearchMatch(fileContent, searchContent);
75
76 expect(result).toEqual({
77 startIndex: 6,
78 endIndex: 17,
79 strategyName: "exactMatch",
80 });
81 });
82
83 it("should handle exact match with special characters", () => {
84 const fileContent = 'function test() { return "hello"; }';
85 const searchContent = '{ return "hello"; }';
86 const result = findSearchMatch(fileContent, searchContent);
87
88 expect(result).toEqual({
89 startIndex: 16,
90 endIndex: 35,
91 strategyName: "exactMatch",
92 });
93 });
94
95 it("should find first occurrence when multiple matches exist", () => {
96 const fileContent = "test test test";
97 const searchContent = "test";
98 const result = findSearchMatch(fileContent, searchContent);
99
100 expect(result).toEqual({
101 startIndex: 0,
102 endIndex: 4,
103 strategyName: "exactMatch",
104 });
105 });
106
107 it("should handle case-sensitive matching", () => {
108 const fileContent = "Hello World";
109 const searchContent = "hello";
110 const result = findSearchMatch(fileContent, searchContent);
111
112 // Should not match due to case sensitivity, will fall through to other strategies
113 expect(result).toBeNull();
114 });
115 });
116
117 describe("Trimmed match strategy fallback", () => {
118 it("should find match when search content has leading/trailing whitespace", () => {
119 const fileContent = "hello world";
120 const searchContent = " hello ";
121 const result = findSearchMatch(fileContent, searchContent);
122
123 expect(result).toEqual({
124 startIndex: 0,
125 endIndex: 5,
126 strategyName: "trimmedMatch",
127 });
128 });
129
130 it("should find match when search content has tabs and newlines", () => {
131 const fileContent = "function test() {}";
132 const searchContent = "\t\nfunction test()\n\t";
133 const result = findSearchMatch(fileContent, searchContent);
134
135 expect(result).toEqual({
136 startIndex: 0,
137 endIndex: 15,
138 strategyName: "trimmedMatch",
139 });
140 });
141
142 it("should handle complex whitespace trimming", () => {
143 const fileContent = "const x = 5;";
144 const searchContent = " \n\t const x = 5; \t\n ";
145 const result = findSearchMatch(fileContent, searchContent);
146
147 expect(result).toEqual({
148 startIndex: 0,
149 endIndex: 12,
150 strategyName: "trimmedMatch",
151 });
152 });
153 });
154
155 describe("Whitespace ignored strategy fallback", () => {
156 it("should match content with different whitespace formatting", () => {
157 const fileContent = "function test() {\n return true;\n}";
158 const searchContent = "function test(){return true;}";
159 const result = findSearchMatch(fileContent, searchContent);
160
161 expect(result).toEqual({
162 startIndex: 0,
163 endIndex: 34,
164 strategyName: "whitespaceIgnoredMatch",
165 });
166 });
167
168 it("should handle mixed whitespace differences", () => {
169 const fileContent = "if (condition) {\n doSomething();\n}";
170 const searchContent = "if(condition){doSomething();}";
171 const result = findSearchMatch(fileContent, searchContent);
172
173 expect(result).toEqual({
174 startIndex: 0,
175 endIndex: 37,
176 strategyName: "whitespaceIgnoredMatch",
177 });
178 });
179
180 it("should match with tabs vs spaces differences", () => {
181 const fileContent = "function test() {\n\treturn 42;\n}";
182 const searchContent = "function test() {\n return 42;\n}";
183 const result = findSearchMatch(fileContent, searchContent);
184
185 expect(result).toEqual({
186 startIndex: 0,
187 endIndex: 31,
188 strategyName: "whitespaceIgnoredMatch",
189 });
190 });
191
192 it("should handle partial matches within larger content", () => {
193 const fileContent = "before\nfunction test() {\n return true;\n}\nafter";
194 const searchContent = "function test(){return true;}";
195 const result = findSearchMatch(fileContent, searchContent);
196
197 expect(result).toEqual({
198 startIndex: 7,
199 endIndex: 41,
200 strategyName: "whitespaceIgnoredMatch",
201 });
202 });
203
204 it("should recognize empty search", () => {
205 const fileContent = "some content";
206 const searchContent = " \t\n ";
207 const result = findSearchMatch(fileContent, searchContent);
208
209 expect(result).toEqual({
210 startIndex: 0,
211 endIndex: 0,
212 strategyName: "emptySearch",
213 });
214 });
215
216 it("should handle complex nested structures with whitespace differences", () => {
217 const fileContent = `{
218 "key": "value",
219 "nested": {
220 "prop": 123
221 }
222}`;
223 const searchContent = '{"key":"value","nested":{"prop":123}}';
224 const result = findSearchMatch(fileContent, searchContent);
225
226 expect(result).toEqual({
227 startIndex: 0,
228 endIndex: 55,
229 strategyName: "whitespaceIgnoredMatch",
230 });
231 });
232
233 it("should match content in middle of file with complex indentation", () => {
234 const fileContent = `import React from 'react';
235
236function MyComponent() {
237 const handleClick = () => {
238 console.log('clicked');
239 };
240 return <button onClick={handleClick}>Click me</button>;
241}
242
243export default MyComponent;`;
244 const searchContent = `const handleClick=()=>{console.log('clicked');};`;
245 const result = findSearchMatch(fileContent, searchContent);
246
247 expect(result).toEqual({
248 startIndex: 55,
249 endIndex: 115,
250 strategyName: "whitespaceIgnoredMatch",
251 });
252 });
253
254 it("should handle matches with mixed line endings and special characters", () => {
255 const fileContent = `<!DOCTYPE html>\r\n<html>\r\n <body>\r\n <div id="app">\r\n <p>Hello & goodbye</p>\r\n </div>\r\n </body>\r\n</html>`;
256 const searchContent = `<divid="app"><p>Hello&goodbye</p></div>`;
257 const result = findSearchMatch(fileContent, searchContent);
258
259 expect(result).toEqual({
260 startIndex: 39,
261 endIndex: 95,
262 strategyName: "whitespaceIgnoredMatch",
263 });
264 });
265
266 it("should match content surrounded by different types of whitespace", () => {
267 const fileContent = `\tconst data = {\n\t\tname: "John",\n\t\tage: 30,\n\t\taddress: {\n\t\t\tstreet: "Main St",\n\t\t\tcity: "NYC"\n\t\t}\n\t};`;
268 const searchContent = `address:{street:"MainSt",city:"NYC"}`;
269 const result = findSearchMatch(fileContent, searchContent);
270
271 expect(result).toEqual({
272 startIndex: 45,
273 endIndex: 96,
274 strategyName: "whitespaceIgnoredMatch",
275 });
276 });
277
278 // IMPORTANT string.length and string.slice both count UTF-16 units so emojis will count as 2 in both
279 it("should handle unicode characters with whitespace differences", () => {
280 const fileContent = `const message = {\n greeting: "Hello 👋",\n emoji: "🚀",\n unicode: "café",\n symbol: "∑"\n};`;
281 const searchContent = `emoji:"🚀",unicode:"café",symbol:"∑"`;
282 const result = findSearchMatch(fileContent, searchContent);
283
284 expect(result).toEqual({
285 startIndex: 44,
286 endIndex: 89,
287 strategyName: "whitespaceIgnoredMatch",
288 });
289 });
290
291 it("should match across multiple nested blocks with varying indentation", () => {
292 const fileContent = `class Calculator {\n add(a, b) {\n if (typeof a === 'number' && typeof b === 'number') {\n return a + b;\n }\n throw new Error('Invalid input');\n }\n\n multiply(x, y) {\n return x * y;\n }\n}`;
293 const searchContent = `if(typeofa==='number'&&typeofb==='number'){returna+b;}`;
294 const result = findSearchMatch(fileContent, searchContent);
295
296 expect(result).toEqual({
297 startIndex: 43,
298 endIndex: 132,
299 strategyName: "whitespaceIgnoredMatch",
300 });
301 });
302
303 it("should handle regex-like patterns with escaped characters", () => {
304 const fileContent = `const pattern = /\\d+\\.\\d+/g;\nconst test = \"123.456\";\nif (pattern.test(test)) {\n console.log(\"Match found\");\n}`;
305 const searchContent = `consttest="123.456";if(pattern.test(test)){console.log("Matchfound");}`;
306 const result = findSearchMatch(fileContent, searchContent);
307
308 expect(result).toEqual({
309 startIndex: 29,
310 endIndex: 110,
311 strategyName: "whitespaceIgnoredMatch",
312 });
313 });
314
315 it("should match SQL-like content with varying whitespace", () => {
316 const fileContent = `SELECT users.name,\n users.email,\n profiles.bio\nFROM users\nINNER JOIN profiles ON users.id = profiles.user_id\nWHERE users.active = true\nORDER BY users.name;`;
317 const searchContent = `SELECTusers.name,users.email,profiles.bioFROMusersINNERJOINprofiles`;
318 const result = findSearchMatch(fileContent, searchContent);
319
320 expect(result).toEqual({
321 startIndex: 0,
322 endIndex: 89,
323 strategyName: "whitespaceIgnoredMatch",
324 });
325 });
326
327 it("should handle content with form feeds, vertical tabs, and other rare whitespace", () => {
328 const fileContent = `function\fprocess\vdata(input)\u0020{\u00A0\n\treturn\u2000input.trim();\u2009\n}`;
329 const searchContent = `functionprocessdata(input){returninput.trim();}`;
330 const result = findSearchMatch(fileContent, searchContent);
331
332 expect(result).toEqual({
333 startIndex: 0,
334 endIndex: 56,
335 strategyName: "whitespaceIgnoredMatch",
336 });
337 });
338
339 it("should match content with Windows-style CRLF line endings", () => {
340 const fileContent = `@echo off\r\nset PATH=%PATH%;C:\\tools\r\nif \"%1\"==\"\" (\r\n echo Usage: script.bat filename\r\n exit /b 1\r\n)\r\necho Processing %1...\r\necho Done.`;
341 const searchContent = `if\"%1\"==\"\"(echo Usage: script.bat filename exit/b 1)`;
342 const result = findSearchMatch(fileContent, searchContent);
343
344 expect(result).toEqual({
345 startIndex: 37,
346 endIndex: 105,
347 strategyName: "whitespaceIgnoredMatch",
348 });
349 });
350
351 it("should handle deeply nested JSON with mixed formatting", () => {
352 const fileContent = `{\n \"config\": {\n \"database\": {\n \"host\": \"localhost\",\n \"port\": 5432,\n \"credentials\": {\n \"username\": \"admin\",\n \"password\": \"secret\"\n },\n \"options\": {\n \"ssl\": true,\n \"timeout\": 30000\n }\n }\n }\n}`;
353 const searchContent = `\"credentials\":{\"username\":\"admin\",\"password\":\"secret\"},\"options\":{\"ssl\":true,\"timeout\":30000}`;
354 const result = findSearchMatch(fileContent, searchContent);
355
356 expect(result).toEqual({
357 startIndex: 87,
358 endIndex: 243,
359 strategyName: "whitespaceIgnoredMatch",
360 });
361 });
362
363 it("should match XML content with attribute spacing differences", () => {
364 const fileContent = `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n <item id = \"1\" type= \"primary\" >\n <name>Test Item</name>\n <value unit=\"px\" >100</value>\n </item>\n</root>`;
365 const searchContent = `<itemid=\"1\"type=\"primary\"><name>TestItem</name><valueunit=\"px\">100</value></item>`;
366 const result = findSearchMatch(fileContent, searchContent);
367
368 expect(result).toEqual({
369 startIndex: 48,
370 endIndex: 155,
371 strategyName: "whitespaceIgnoredMatch",
372 });
373 });
374
375 it("should handle mathematical expressions with varying spacing", () => {
376 const fileContent = `const result = (a + b) * c - d / e;\nconst complex = Math.sqrt( x ** 2 + y ** 2 );\nconst formula = ( ( a + b ) * ( c - d ) ) / ( e + f );`;
377 const searchContent = `constcomplex=Math.sqrt(x**2+y**2);constformula=((a+b)*(c-d))/(e+f);`;
378 const result = findSearchMatch(fileContent, searchContent);
379
380 expect(result).toEqual({
381 startIndex: 36,
382 endIndex: 142,
383 strategyName: "whitespaceIgnoredMatch",
384 });
385 });
386
387 it("should match across comments and code with whitespace variations", () => {
388 const fileContent = `/* Multi-line comment\n with varying indentation */\nfunction processData(input) {\n // Single line comment\n const cleaned = input.trim();\n /* Another comment */\n return cleaned.toLowerCase();\n}`;
389 const searchContent = `/*Multi-linecommentwithvaryingindentation*/functionprocessData(input){//Singlelinecommentconstcleaned=input.trim();/*Anothercomment*/returncleaned.toLowerCase();}`;
390 const result = findSearchMatch(fileContent, searchContent);
391
392 expect(result).toEqual({
393 startIndex: 0,
394 endIndex: 205,
395 strategyName: "whitespaceIgnoredMatch",
396 });
397 });
398 });
399
400 describe("No match scenarios", () => {
401 it("should return null when no strategy finds a match", () => {
402 const fileContent = "hello world";
403 const searchContent = "goodbye";
404 const result = findSearchMatch(fileContent, searchContent);
405
406 expect(result).toBeNull();
407 });
408
409 it("should return null for completely different content", () => {
410 const fileContent = "function test() { return true; }";
411 const searchContent = "class MyClass { }";
412 const result = findSearchMatch(fileContent, searchContent);
413
414 expect(result).toBeNull();
415 });
416
417 it("should return null when search content is longer than file content", () => {
418 const fileContent = "short";
419 const searchContent = "this is much longer than the file content";
420 const result = findSearchMatch(fileContent, searchContent);
421
422 expect(result).toBeNull();
423 });
424 });
425
426 describe("Edge cases", () => {
427 it("should handle empty file content with non-empty search", () => {
428 const fileContent = "";
429 const searchContent = "something";
430 const result = findSearchMatch(fileContent, searchContent);
431
432 expect(result).toBeNull();
433 });
434
435 it("should handle single character matches", () => {
436 const fileContent = "a";
437 const searchContent = "a";
438 const result = findSearchMatch(fileContent, searchContent);
439
440 expect(result).toEqual({
441 startIndex: 0,
442 endIndex: 1,
443 strategyName: "exactMatch",
444 });
445 });
446
447 it("should handle unicode characters", () => {
448 const fileContent = "Hello 🌍 World";
449 const searchContent = "🌍";
450 const result = findSearchMatch(fileContent, searchContent);
451
452 expect(result).toEqual({
453 startIndex: 6,
454 endIndex: 8, // Unicode emoji takes 2 character positions
455 strategyName: "exactMatch",
456 });
457 });
458
459 it("should handle newlines and carriage returns", () => {
460 const fileContent = "line1\r\nline2\nline3";
461 const searchContent = "\r\nline2";
462 const result = findSearchMatch(fileContent, searchContent);
463
464 expect(result).toEqual({
465 startIndex: 5,
466 endIndex: 12,
467 strategyName: "exactMatch",
468 });
469 });
470
471 it("should handle very long content efficiently", () => {
472 const longContent = "a".repeat(10000) + "target" + "b".repeat(10000);
473 const searchContent = "target";
474 const result = findSearchMatch(longContent, searchContent);
475
476 expect(result).toEqual({
477 startIndex: 10000,
478 endIndex: 10006,
479 strategyName: "exactMatch",
480 });
481 });
482 });
483
484 describe("Strategy precedence", () => {
485 it("should prefer exact match over trimmed match", () => {
486 const fileContent = "hello world hello";
487 const searchContent = "hello"; // This exists exactly, so should use exactMatch
488 const result = findSearchMatch(fileContent, searchContent);
489
490 expect(result).toEqual({
491 startIndex: 0,
492 endIndex: 5,
493 strategyName: "exactMatch",
494 });
495 });
496
497 it("should prefer trimmed match over whitespace ignored match", () => {
498 // Set up content where trimmed match would work but whitespace ignored would also work
499 const fileContent = "hello world";
500 const searchContent = " hello "; // Has whitespace but trimmed version exists
501 const result = findSearchMatch(fileContent, searchContent);
502
503 expect(result).toEqual({
504 startIndex: 0,
505 endIndex: 5,
506 strategyName: "trimmedMatch",
507 });
508 });
509
510 it("should fall back through strategies in correct order", () => {
511 const fileContent = "function test() {\n return true;\n}";
512 const searchContent = " function test(){return true;} "; // Needs whitespace ignored
513 const result = findSearchMatch(fileContent, searchContent);
514
515 expect(result).toEqual({
516 startIndex: 0,
517 endIndex: 34,
518 strategyName: "whitespaceIgnoredMatch",
519 });
520 });
521 });
522
523 describe("Real-world code examples", () => {
524 it("should handle JavaScript function matching", () => {
525 const fileContent = `function calculateSum(a, b) {
526 const result = a + b;
527 return result;
528}`;
529 const searchContent = `function calculateSum(a, b) {
530 const result = a + b;
531 return result;
532}`;
533 const result = findSearchMatch(fileContent, searchContent);
534
535 expect(result).toEqual({
536 startIndex: 0,
537 endIndex: 72,
538 strategyName: "exactMatch",
539 });
540 });
541
542 it("should handle HTML tag matching with different formatting", () => {
543 const fileContent = `<div class="container">
544 <p>Hello World</p>
545</div>`;
546 const searchContent = `<div class="container"><p>Hello World</p></div>`;
547 const result = findSearchMatch(fileContent, searchContent);
548
549 expect(result).toEqual({
550 startIndex: 0,
551 endIndex: 51,
552 strategyName: "whitespaceIgnoredMatch",
553 });
554 });
555
556 it("should handle CSS rule matching", () => {
557 const fileContent = `.button {
558 background-color: blue;
559 color: white;
560}`;
561 const searchContent = `.button{background-color:blue;color:white;}`;
562 const result = findSearchMatch(fileContent, searchContent);
563
564 expect(result).toEqual({
565 startIndex: 0,
566 endIndex: 53,
567 strategyName: "whitespaceIgnoredMatch",
568 });
569 });
570 });
571});