| 1 | import { findSearchMatch } from "./findSearchMatch"; |
| 2 | |
| 3 | describe("Exact matches", () => { |
| 4 | it("should find exact match at the beginning of file", () => { |
| 5 | const fileContent = `function hello() { |
| 6 | console.log("world"); |
| 7 | }`; |
| 8 | const searchContent = `function hello() { |
| 9 | console.log("world"); |
| 10 | }`; |
| 11 | |
| 12 | const result = findSearchMatch(fileContent, searchContent); |
| 13 | |
| 14 | expect(result).toEqual({ |
| 15 | startIndex: 0, |
| 16 | endIndex: fileContent.length, |
| 17 | strategyName: "exactMatch", |
| 18 | }); |
| 19 | }); |
| 20 | |
| 21 | it("should find exact match in the middle of file", () => { |
| 22 | const fileContent = `const a = 1; |
| 23 | function hello() { |
| 24 | console.log("world"); |
| 25 | } |
| 26 | const b = 2;`; |
| 27 | const searchContent = `function hello() { |
| 28 | console.log("world"); |
| 29 | }`; |
| 30 | |
| 31 | const result = findSearchMatch(fileContent, searchContent); |
| 32 | |
| 33 | expect(result).toEqual({ |
| 34 | startIndex: 13, // After "const a = 1;\n" |
| 35 | endIndex: 13 + searchContent.length, |
| 36 | strategyName: "exactMatch", |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | it("should find exact match at the end of file", () => { |
| 41 | const fileContent = `const a = 1; |
| 42 | function hello() { |
| 43 | console.log("world"); |
| 44 | }`; |
| 45 | const searchContent = `function hello() { |
| 46 | console.log("world"); |
| 47 | }`; |
| 48 | |
| 49 | const result = findSearchMatch(fileContent, searchContent); |
| 50 | |
| 51 | expect(result).toEqual({ |
| 52 | startIndex: 13, // After "const a = 1;\n" |
| 53 | endIndex: fileContent.length, |
| 54 | strategyName: "exactMatch", |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | it("should find exact match with single line", () => { |
| 59 | const fileContent = `const a = 1; |
| 60 | const b = 2; |
| 61 | const c = 3;`; |
| 62 | const searchContent = `const b = 2;`; |
| 63 | |
| 64 | const result = findSearchMatch(fileContent, searchContent); |
| 65 | |
| 66 | expect(result).toEqual({ |
| 67 | startIndex: 13, // After "const a = 1;\n" |
| 68 | endIndex: 13 + searchContent.length, |
| 69 | strategyName: "exactMatch", |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | it("should find exact match with whitespace preserved", () => { |
| 74 | const fileContent = `function test() { |
| 75 | const x = 1; |
| 76 | const y = 2; |
| 77 | }`; |
| 78 | const searchContent = ` const x = 1; |
| 79 | const y = 2;`; |
| 80 | |
| 81 | const result = findSearchMatch(fileContent, searchContent); |
| 82 | |
| 83 | // The actual indexOf result is 18, not 17 |
| 84 | expect(result).toEqual({ |
| 85 | startIndex: 18, |
| 86 | endIndex: 18 + searchContent.length, |
| 87 | strategyName: "exactMatch", |
| 88 | }); |
| 89 | }); |
| 90 | }); |
| 91 | |
| 92 | describe("Trimmed matches", () => { |
| 93 | it("should find trimmed match when exact match fails due to leading newlines", () => { |
| 94 | const fileContent = `function hello() { |
| 95 | console.log("world"); |
| 96 | }`; |
| 97 | const searchContent = `\n\nfunction hello() { |
| 98 | console.log("world"); |
| 99 | }\n\n`; |
| 100 | |
| 101 | const result = findSearchMatch(fileContent, searchContent); |
| 102 | |
| 103 | expect(result).toEqual({ |
| 104 | startIndex: 0, |
| 105 | endIndex: fileContent.length, // Uses trimmed length |
| 106 | strategyName: "trimmedMatch", |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | it("should find trimmed match when exact match fails due to trailing newlines", () => { |
| 111 | const fileContent = `const a = 1; |
| 112 | function hello() { |
| 113 | return "world"; |
| 114 | } |
| 115 | const b = 2;`; |
| 116 | const searchContent = `\nfunction hello() { |
| 117 | return "world"; |
| 118 | }\n`; |
| 119 | |
| 120 | const result = findSearchMatch(fileContent, searchContent); |
| 121 | |
| 122 | // The function finds the exact match first (at index 12), not the trimmed match |
| 123 | expect(result).toEqual({ |
| 124 | startIndex: 12, |
| 125 | endIndex: 12 + searchContent.length, |
| 126 | strategyName: "exactMatch", |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | it("should find trimmed match for single line with extra whitespace", () => { |
| 131 | const fileContent = `const a = 1; |
| 132 | const b = 2; |
| 133 | const c = 3;`; |
| 134 | const searchContent = `\n const b = 2; \n`; |
| 135 | |
| 136 | const result = findSearchMatch(fileContent, searchContent); |
| 137 | |
| 138 | const trimmedSearchContent = searchContent.trim(); |
| 139 | const expectedStart = fileContent.indexOf(trimmedSearchContent); |
| 140 | |
| 141 | expect(result).toEqual({ |
| 142 | startIndex: expectedStart, |
| 143 | endIndex: expectedStart + trimmedSearchContent.length, |
| 144 | strategyName: "trimmedMatch", |
| 145 | }); |
| 146 | }); |
| 147 | }); |
| 148 | |
| 149 | describe("Empty search content", () => { |
| 150 | it("should match at beginning for empty search content", () => { |
| 151 | const fileContent = `function hello() { |
| 152 | console.log("world"); |
| 153 | }`; |
| 154 | const searchContent = ""; |
| 155 | |
| 156 | const result = findSearchMatch(fileContent, searchContent); |
| 157 | |
| 158 | expect(result).toEqual({ |
| 159 | startIndex: 0, |
| 160 | endIndex: 0, |
| 161 | strategyName: "emptySearch", |
| 162 | }); |
| 163 | }); |
| 164 | |
| 165 | it("should match at beginning for whitespace-only search content", () => { |
| 166 | const fileContent = `function hello() { |
| 167 | console.log("world"); |
| 168 | }`; |
| 169 | const searchContent = "\n\n \t \n"; |
| 170 | |
| 171 | const result = findSearchMatch(fileContent, searchContent); |
| 172 | |
| 173 | expect(result).toEqual({ |
| 174 | startIndex: 0, |
| 175 | endIndex: 0, |
| 176 | strategyName: "emptySearch", |
| 177 | }); |
| 178 | }); |
| 179 | |
| 180 | it("should match at beginning for empty file and empty search", () => { |
| 181 | const fileContent = ""; |
| 182 | const searchContent = ""; |
| 183 | |
| 184 | const result = findSearchMatch(fileContent, searchContent); |
| 185 | |
| 186 | expect(result).toEqual({ |
| 187 | startIndex: 0, |
| 188 | endIndex: 0, |
| 189 | strategyName: "emptySearch", |
| 190 | }); |
| 191 | }); |
| 192 | }); |
| 193 | |
| 194 | describe("No matches", () => { |
| 195 | it("should return null when search content is not found", () => { |
| 196 | const fileContent = `function hello() { |
| 197 | console.log("world"); |
| 198 | }`; |
| 199 | const searchContent = `function goodbye() { |
| 200 | console.log("world"); |
| 201 | }`; |
| 202 | |
| 203 | const result = findSearchMatch(fileContent, searchContent); |
| 204 | |
| 205 | expect(result?.strategyName).toEqual("jaroWinklerFuzzyMatch"); |
| 206 | }); |
| 207 | |
| 208 | it("should return only fuzzy result when trimmed search content is not found", () => { |
| 209 | const fileContent = `function hello() { |
| 210 | console.log("world"); |
| 211 | }`; |
| 212 | const searchContent = `\n\nfunction goodbye() { |
| 213 | console.log("world"); |
| 214 | }\n\n`; |
| 215 | |
| 216 | const result = findSearchMatch(fileContent, searchContent); |
| 217 | |
| 218 | expect(result?.strategyName).toEqual("jaroWinklerFuzzyMatch"); |
| 219 | }); |
| 220 | }); |
| 221 | |
| 222 | describe("Edge cases", () => { |
| 223 | it("should handle empty file with non-empty search", () => { |
| 224 | const fileContent = ""; |
| 225 | const searchContent = "function hello() {}"; |
| 226 | |
| 227 | const result = findSearchMatch(fileContent, searchContent); |
| 228 | |
| 229 | expect(result).toBeNull(); |
| 230 | }); |
| 231 | |
| 232 | it("should handle very large content", () => { |
| 233 | const repeatedLine = "const x = 1;\n"; |
| 234 | const fileContent = |
| 235 | repeatedLine.repeat(1000) + |
| 236 | "const target = 2;\n" + |
| 237 | repeatedLine.repeat(1000); |
| 238 | const searchContent = "const target = 2;"; |
| 239 | |
| 240 | const result = findSearchMatch(fileContent, searchContent); |
| 241 | |
| 242 | const expectedStart = repeatedLine.length * 1000; |
| 243 | expect(result).toEqual({ |
| 244 | startIndex: expectedStart, |
| 245 | endIndex: expectedStart + searchContent.length, |
| 246 | strategyName: "exactMatch", |
| 247 | }); |
| 248 | }); |
| 249 | |
| 250 | it("should handle special characters and symbols", () => { |
| 251 | const fileContent = `const regex = /[a-zA-Z]+/g; |
| 252 | const symbols = !@#$%^&*(); |
| 253 | const unicode = "馃殌 Hello 涓栫晫";`; |
| 254 | const searchContent = `const symbols = !@#$%^&*();`; |
| 255 | |
| 256 | const result = findSearchMatch(fileContent, searchContent); |
| 257 | |
| 258 | const expectedStart = fileContent.indexOf(searchContent); |
| 259 | expect(result).toEqual({ |
| 260 | startIndex: expectedStart, |
| 261 | endIndex: expectedStart + searchContent.length, |
| 262 | strategyName: "exactMatch", |
| 263 | }); |
| 264 | }); |
| 265 | |
| 266 | it("should prefer exact match over trimmed match", () => { |
| 267 | const fileContent = `function test() { |
| 268 | const x = 1; |
| 269 | } |
| 270 | |
| 271 | function test() { |
| 272 | const x = 1; |
| 273 | }`; |
| 274 | const searchContent = `function test() { |
| 275 | const x = 1; |
| 276 | }`; |
| 277 | |
| 278 | const result = findSearchMatch(fileContent, searchContent); |
| 279 | |
| 280 | // Should find the first exact match, not a trimmed version |
| 281 | expect(result).toEqual({ |
| 282 | startIndex: 0, |
| 283 | endIndex: searchContent.length, |
| 284 | strategyName: "exactMatch", |
| 285 | }); |
| 286 | }); |
| 287 | |
| 288 | it("should handle multiple occurrences and return first match", () => { |
| 289 | const fileContent = `const a = 1; |
| 290 | const b = 2; |
| 291 | const a = 1;`; |
| 292 | const searchContent = `const a = 1;`; |
| 293 | |
| 294 | const result = findSearchMatch(fileContent, searchContent); |
| 295 | |
| 296 | expect(result).toEqual({ |
| 297 | startIndex: 0, |
| 298 | endIndex: searchContent.length, |
| 299 | strategyName: "exactMatch", |
| 300 | }); |
| 301 | }); |
| 302 | }); |
| 303 | |
| 304 | describe("Whitespace-ignored matches", () => { |
| 305 | it("should match code with different indentation", () => { |
| 306 | const fileContent = `function example() { |
| 307 | const x = 1; |
| 308 | const y = 2; |
| 309 | return x + y; |
| 310 | }`; |
| 311 | const searchContent = `const x=1; |
| 312 | const y=2; |
| 313 | return x+y;`; |
| 314 | |
| 315 | const result = findSearchMatch(fileContent, searchContent); |
| 316 | |
| 317 | expect(result).not.toBeNull(); |
| 318 | expect(result!.startIndex).toBe(20); // After "function example() {\n" |
| 319 | expect(result!.endIndex).toBe(72); // End of " return x + y;" |
| 320 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 321 | |
| 322 | // Verify the matched content contains the original whitespace |
| 323 | const matchedContent = fileContent.substring( |
| 324 | result!.startIndex, |
| 325 | result!.endIndex, |
| 326 | ); |
| 327 | expect(matchedContent).toBe(` |
| 328 | const x = 1; |
| 329 | const y = 2; |
| 330 | return x + y;`); |
| 331 | }); |
| 332 | |
| 333 | it("should match single line with different spacing", () => { |
| 334 | const fileContent = `const a = 1; |
| 335 | const b = 2; |
| 336 | const c = 3;`; |
| 337 | const searchContent = `const b=2;`; |
| 338 | |
| 339 | const result = findSearchMatch(fileContent, searchContent); |
| 340 | |
| 341 | expect(result).not.toBeNull(); |
| 342 | expect(result!.startIndex).toBe(12); // After "const a = 1;" |
| 343 | expect(result!.endIndex).toBe(32); // End of "const b = 2;" |
| 344 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 345 | |
| 346 | // Verify the matched content preserves original spacing |
| 347 | const matchedContent = fileContent.substring( |
| 348 | result!.startIndex, |
| 349 | result!.endIndex, |
| 350 | ); |
| 351 | expect(matchedContent).toBe(` |
| 352 | const b = 2;`); |
| 353 | }); |
| 354 | |
| 355 | it("should match with tabs vs spaces", () => { |
| 356 | const fileContent = `function test() { |
| 357 | \tif (condition) { |
| 358 | \t\treturn true; |
| 359 | \t} |
| 360 | }`; |
| 361 | const searchContent = `if (condition) { |
| 362 | return true; |
| 363 | }`; |
| 364 | |
| 365 | const result = findSearchMatch(fileContent, searchContent); |
| 366 | |
| 367 | expect(result).not.toBeNull(); |
| 368 | expect(result!.startIndex).toBe(17); // After "function test() {" |
| 369 | expect(result!.endIndex).toBe(53); // End of "\t}" |
| 370 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 371 | |
| 372 | // Verify the matched content preserves original tabs |
| 373 | const matchedContent = fileContent.substring( |
| 374 | result!.startIndex, |
| 375 | result!.endIndex, |
| 376 | ); |
| 377 | expect(matchedContent).toBe(` |
| 378 | \tif (condition) { |
| 379 | \t\treturn true; |
| 380 | \t}`); |
| 381 | }); |
| 382 | |
| 383 | it("should match complex whitespace patterns", () => { |
| 384 | const fileContent = `const obj = { |
| 385 | key1 : 'value1', |
| 386 | key2 : 'value2', |
| 387 | key3 : 'value3' |
| 388 | };`; |
| 389 | const searchContent = `key1:'value1', |
| 390 | key2:'value2', |
| 391 | key3:'value3'`; |
| 392 | |
| 393 | const result = findSearchMatch(fileContent, searchContent); |
| 394 | |
| 395 | expect(result).not.toBeNull(); |
| 396 | expect(result!.startIndex).toBe(13); // After "const obj = {" |
| 397 | expect(result!.endIndex).toBe(83); // End of " key3 : 'value3'" |
| 398 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 399 | |
| 400 | // Verify the matched content preserves original formatting |
| 401 | const matchedContent = fileContent.substring( |
| 402 | result!.startIndex, |
| 403 | result!.endIndex, |
| 404 | ); |
| 405 | expect(matchedContent).toBe(` |
| 406 | key1 : 'value1', |
| 407 | key2 : 'value2', |
| 408 | key3 : 'value3'`); |
| 409 | }); |
| 410 | |
| 411 | it("should handle newlines and mixed whitespace", () => { |
| 412 | const fileContent = `function calc() { |
| 413 | |
| 414 | |
| 415 | let result = 0; |
| 416 | |
| 417 | result += 5; |
| 418 | |
| 419 | |
| 420 | return result; |
| 421 | }`; |
| 422 | const searchContent = `let result=0;result+=5;return result;`; |
| 423 | |
| 424 | const result = findSearchMatch(fileContent, searchContent); |
| 425 | |
| 426 | expect(result).not.toBeNull(); |
| 427 | expect(result!.startIndex).toBe(17); // After "function calc() {\n\n" |
| 428 | expect(result!.endIndex).toBe(78); // End of " return result;" |
| 429 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 430 | |
| 431 | // Verify the matched content includes all original whitespace |
| 432 | const matchedContent = fileContent.substring( |
| 433 | result!.startIndex, |
| 434 | result!.endIndex, |
| 435 | ); |
| 436 | expect(matchedContent).toBe(` |
| 437 | |
| 438 | |
| 439 | let result = 0; |
| 440 | |
| 441 | result += 5; |
| 442 | |
| 443 | |
| 444 | return result;`); |
| 445 | }); |
| 446 | |
| 447 | it("should prefer exact match over whitespace-ignored match", () => { |
| 448 | const fileContent = `const x=1; |
| 449 | const x = 1;`; |
| 450 | const searchContent = `const x=1;`; |
| 451 | |
| 452 | const result = findSearchMatch(fileContent, searchContent); |
| 453 | |
| 454 | // Should find the exact match first (at index 0), not the whitespace-ignored match |
| 455 | expect(result).toEqual({ |
| 456 | startIndex: 0, |
| 457 | endIndex: 10, |
| 458 | strategyName: "exactMatch", |
| 459 | }); |
| 460 | }); |
| 461 | |
| 462 | it("should handle empty content after whitespace removal", () => { |
| 463 | const fileContent = `function test() { |
| 464 | return 1; |
| 465 | }`; |
| 466 | const searchContent = `\n\n \t \n`; |
| 467 | |
| 468 | const result = findSearchMatch(fileContent, searchContent); |
| 469 | |
| 470 | // Should match at beginning for empty/whitespace-only content |
| 471 | expect(result).toEqual({ |
| 472 | startIndex: 0, |
| 473 | endIndex: 0, |
| 474 | strategyName: "emptySearch", |
| 475 | }); |
| 476 | }); |
| 477 | |
| 478 | it("should handle complex code blocks with different formatting", () => { |
| 479 | const fileContent = `class Example { |
| 480 | constructor(name) { |
| 481 | this.name = name; |
| 482 | this.value = 0; |
| 483 | } |
| 484 | |
| 485 | getValue() { |
| 486 | return this.value; |
| 487 | } |
| 488 | }`; |
| 489 | const searchContent = `constructor(name){ |
| 490 | this.name=name; |
| 491 | this.value=0; |
| 492 | }`; |
| 493 | |
| 494 | const result = findSearchMatch(fileContent, searchContent); |
| 495 | |
| 496 | expect(result).not.toBeNull(); |
| 497 | expect(result!.startIndex).toBe(15); // After "class Example {" |
| 498 | expect(result!.endIndex).toBe(95); // End of " }" |
| 499 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 500 | |
| 501 | // Verify the matched content preserves original formatting |
| 502 | const matchedContent = fileContent.substring( |
| 503 | result!.startIndex, |
| 504 | result!.endIndex, |
| 505 | ); |
| 506 | expect(matchedContent).toBe(` |
| 507 | constructor(name) { |
| 508 | this.name = name; |
| 509 | this.value = 0; |
| 510 | }`); |
| 511 | }); |
| 512 | |
| 513 | it("should return null when no close match exists", () => { |
| 514 | const fileContent = `function hello() { |
| 515 | console.log("world"); |
| 516 | }`; |
| 517 | const searchContent = `this has nothing to do with the other option`; |
| 518 | |
| 519 | const result = findSearchMatch(fileContent, searchContent); |
| 520 | |
| 521 | expect(result).toBeNull(); |
| 522 | }); |
| 523 | |
| 524 | it("should handle multiple potential matches and return first", () => { |
| 525 | const fileContent = `const a=1; |
| 526 | const a = 1; |
| 527 | const a = 1;`; |
| 528 | const searchContent = `const a=1;`; |
| 529 | |
| 530 | const result = findSearchMatch(fileContent, searchContent); |
| 531 | |
| 532 | // Should find the first exact match |
| 533 | expect(result).toEqual({ |
| 534 | startIndex: 0, |
| 535 | endIndex: 10, |
| 536 | strategyName: "exactMatch", |
| 537 | }); |
| 538 | }); |
| 539 | }); |
| 540 | |
| 541 | describe("Real-world scenarios", () => { |
| 542 | it("should match typical function replacement", () => { |
| 543 | const fileContent = `class Calculator { |
| 544 | constructor() { |
| 545 | this.result = 0; |
| 546 | } |
| 547 | |
| 548 | reset() { |
| 549 | this.result = 0; |
| 550 | return this; |
| 551 | } |
| 552 | |
| 553 | add(value) { |
| 554 | this.result += value; |
| 555 | return this; |
| 556 | } |
| 557 | }`; |
| 558 | const searchContent = ` reset() { |
| 559 | this.result = 0; |
| 560 | return this; |
| 561 | }`; |
| 562 | |
| 563 | const result = findSearchMatch(fileContent, searchContent); |
| 564 | |
| 565 | const expectedStart = fileContent.indexOf(searchContent); |
| 566 | expect(result).toEqual({ |
| 567 | startIndex: expectedStart, |
| 568 | endIndex: expectedStart + searchContent.length, |
| 569 | strategyName: "exactMatch", |
| 570 | }); |
| 571 | }); |
| 572 | |
| 573 | it("should match import statement", () => { |
| 574 | const fileContent = `import React from 'react'; |
| 575 | import { useState, useEffect } from 'react'; |
| 576 | import './App.css'; |
| 577 | |
| 578 | function App() { |
| 579 | return <div>Hello World</div>; |
| 580 | }`; |
| 581 | const searchContent = `import { useState, useEffect } from 'react';`; |
| 582 | |
| 583 | const result = findSearchMatch(fileContent, searchContent); |
| 584 | |
| 585 | const expectedStart = fileContent.indexOf(searchContent); |
| 586 | expect(result).toEqual({ |
| 587 | startIndex: expectedStart, |
| 588 | endIndex: expectedStart + searchContent.length, |
| 589 | strategyName: "exactMatch", |
| 590 | }); |
| 591 | }); |
| 592 | |
| 593 | it("should match comment block", () => { |
| 594 | const fileContent = `/** |
| 595 | * This is a comment |
| 596 | * that spans multiple lines |
| 597 | */ |
| 598 | function test() { |
| 599 | // Single line comment |
| 600 | return true; |
| 601 | }`; |
| 602 | const searchContent = `/** |
| 603 | * This is a comment |
| 604 | * that spans multiple lines |
| 605 | */`; |
| 606 | |
| 607 | const result = findSearchMatch(fileContent, searchContent); |
| 608 | |
| 609 | expect(result).toEqual({ |
| 610 | startIndex: 0, |
| 611 | endIndex: searchContent.length, |
| 612 | strategyName: "exactMatch", |
| 613 | }); |
| 614 | }); |
| 615 | |
| 616 | describe("Jaro-Winkler fuzzy matching", () => { |
| 617 | describe("Basic fuzzy matching", () => { |
| 618 | it("should find fuzzy match for similar strings", () => { |
| 619 | const fileContent = `function calculateSum(a, b) { |
| 620 | return a + b; |
| 621 | }`; |
| 622 | const searchContent = `function calculateSum(x, y) { |
| 623 | return x + y; |
| 624 | }`; |
| 625 | |
| 626 | const result = findSearchMatch(fileContent, searchContent); |
| 627 | |
| 628 | expect(result).not.toBeNull(); |
| 629 | }); |
| 630 | |
| 631 | it("should find fuzzy match with minor typos", () => { |
| 632 | const fileContent = `const message = "Hello World";`; |
| 633 | const searchContent = `const mesage = "Hello World";`; // Missing 's' in 'message' |
| 634 | |
| 635 | const result = findSearchMatch(fileContent, searchContent); |
| 636 | |
| 637 | expect(result).not.toBeNull(); |
| 638 | expect(result?.startIndex).toBe(0); |
| 639 | }); |
| 640 | |
| 641 | it("should find fuzzy match with different variable names", () => { |
| 642 | const fileContent = `let userAge = 25; |
| 643 | let userName = "John";`; |
| 644 | const searchContent = `let age = 25; |
| 645 | let name = "John";`; |
| 646 | |
| 647 | const result = findSearchMatch(fileContent, searchContent); |
| 648 | |
| 649 | expect(result).not.toBeNull(); |
| 650 | }); |
| 651 | |
| 652 | it("should find fuzzy match for similar function signature", () => { |
| 653 | const fileContent = `function processUserData(userData) { |
| 654 | validateInput(userData); |
| 655 | return formatOutput(userData); |
| 656 | }`; |
| 657 | const searchContent = `function processData(data) { |
| 658 | validateInput(data); |
| 659 | return formatOutput(data); |
| 660 | }`; |
| 661 | |
| 662 | const result = findSearchMatch(fileContent, searchContent); |
| 663 | |
| 664 | expect(result).not.toBeNull(); |
| 665 | }); |
| 666 | }); |
| 667 | |
| 668 | describe("Multi-line fuzzy matching", () => { |
| 669 | it("should find fuzzy match for multi-line blocks", () => { |
| 670 | const fileContent = `class Calculator { |
| 671 | constructor() { |
| 672 | this.result = 0; |
| 673 | } |
| 674 | |
| 675 | add(value) { |
| 676 | this.result += value; |
| 677 | return this; |
| 678 | } |
| 679 | }`; |
| 680 | const searchContent = `class Calculator { |
| 681 | constructor() { |
| 682 | this.value = 0; |
| 683 | } |
| 684 | |
| 685 | add(num) { |
| 686 | this.value += num; |
| 687 | return this; |
| 688 | } |
| 689 | }`; |
| 690 | |
| 691 | const result = findSearchMatch(fileContent, searchContent); |
| 692 | |
| 693 | expect(result).not.toBeNull(); |
| 694 | }); |
| 695 | |
| 696 | it("should find fuzzy match for partial blocks", () => { |
| 697 | const fileContent = `function processData(input) { |
| 698 | const validated = validateInput(input); |
| 699 | const processed = transformData(validated); |
| 700 | const result = formatOutput(processed); |
| 701 | return result; |
| 702 | }`; |
| 703 | const searchContent = `const validated = validateInput(input); |
| 704 | const processed = transformData(validated);`; |
| 705 | |
| 706 | const result = findSearchMatch(fileContent, searchContent); |
| 707 | |
| 708 | expect(result).not.toBeNull(); |
| 709 | }); |
| 710 | }); |
| 711 | |
| 712 | describe("Edge cases with fuzzy matching", () => { |
| 713 | it("should handle empty search content with fuzzy matching enabled", () => { |
| 714 | const fileContent = `function test() {}`; |
| 715 | const searchContent = ""; |
| 716 | |
| 717 | const result = findSearchMatch(fileContent, searchContent); |
| 718 | |
| 719 | expect(result).toEqual({ |
| 720 | startIndex: 0, |
| 721 | endIndex: 0, |
| 722 | strategyName: "emptySearch", |
| 723 | }); |
| 724 | }); |
| 725 | |
| 726 | it("should handle empty file content with fuzzy matching enabled", () => { |
| 727 | const fileContent = ""; |
| 728 | const searchContent = "function test() {}"; |
| 729 | |
| 730 | const result = findSearchMatch(fileContent, searchContent); |
| 731 | |
| 732 | expect(result).toBeNull(); |
| 733 | }); |
| 734 | |
| 735 | it("should prefer exact match over fuzzy match", () => { |
| 736 | const fileContent = `function test() { return true; } |
| 737 | function test() { return false; }`; |
| 738 | const searchContent = `function test() { return true; }`; |
| 739 | |
| 740 | const result = findSearchMatch(fileContent, searchContent); |
| 741 | |
| 742 | expect(result).not.toBeNull(); |
| 743 | expect(result?.startIndex).toBe(0); |
| 744 | }); |
| 745 | |
| 746 | it("should handle very short strings", () => { |
| 747 | const fileContent = `a b c`; |
| 748 | const searchContent = `a c`; |
| 749 | |
| 750 | const result = findSearchMatch(fileContent, searchContent); |
| 751 | |
| 752 | // Should not match due to low similarity |
| 753 | expect(result).toBeNull(); |
| 754 | }); |
| 755 | |
| 756 | it("should handle special characters in fuzzy matching", () => { |
| 757 | const fileContent = `const regex = /[a-zA-Z]+/g; |
| 758 | const symbols = !@#$%^&*();`; |
| 759 | const searchContent = `const regex = /[a-zA-Z0-9]+/g; |
| 760 | const symbols = !@#$%^&*();`; |
| 761 | |
| 762 | const result = findSearchMatch(fileContent, searchContent); |
| 763 | |
| 764 | expect(result).not.toBeNull(); |
| 765 | }); |
| 766 | }); |
| 767 | }); |
| 768 | }); |