| 1 | import { findSearchMatch, SearchMatchResult } from "./findSearchMatch"; |
| 2 | |
| 3 | /** |
| 4 | * Helper function to perform a replacement using the match result |
| 5 | * This simulates what the actual replacement tool would do |
| 6 | */ |
| 7 | function performReplacement( |
| 8 | fileContent: string, |
| 9 | match: SearchMatchResult, |
| 10 | replacement: string, |
| 11 | ): string { |
| 12 | return ( |
| 13 | fileContent.substring(0, match.startIndex) + |
| 14 | replacement + |
| 15 | fileContent.substring(match.endIndex) |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Helper function to test a specific strategy by crafting inputs that |
| 21 | * will fail all previous strategies |
| 22 | */ |
| 23 | function testSpecificStrategy( |
| 24 | fileContent: string, |
| 25 | searchContent: string, |
| 26 | expectedStrategy: string, |
| 27 | ): SearchMatchResult | null { |
| 28 | const result = findSearchMatch(fileContent, searchContent); |
| 29 | if (result && result.strategyName !== expectedStrategy) { |
| 30 | throw new Error( |
| 31 | `Expected strategy ${expectedStrategy} but got ${result.strategyName}`, |
| 32 | ); |
| 33 | } |
| 34 | return result; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Helper function to verify that a match result correctly identifies |
| 39 | * the intended content and replacement works properly |
| 40 | */ |
| 41 | function verifyMatchAndReplacement( |
| 42 | fileContent: string, |
| 43 | searchContent: string, |
| 44 | expectedMatchedText: string, |
| 45 | replacementText: string, |
| 46 | expectedResult: string, |
| 47 | ): void { |
| 48 | const match = findSearchMatch(fileContent, searchContent); |
| 49 | |
| 50 | if (!match) { |
| 51 | throw new Error("No match found"); |
| 52 | } |
| 53 | |
| 54 | // Verify the match captures the expected text |
| 55 | const actualMatchedText = fileContent.substring( |
| 56 | match.startIndex, |
| 57 | match.endIndex, |
| 58 | ); |
| 59 | expect(actualMatchedText).toBe(expectedMatchedText); |
| 60 | |
| 61 | // Verify replacement works correctly |
| 62 | const result = performReplacement(fileContent, match, replacementText); |
| 63 | expect(result).toBe(expectedResult); |
| 64 | } |
| 65 | |
| 66 | describe("Strategy-specific tests", () => { |
| 67 | describe("exactMatch strategy", () => { |
| 68 | it("should handle exact match at various positions", () => { |
| 69 | const testCases = [ |
| 70 | { |
| 71 | name: "at start", |
| 72 | fileContent: "abc def ghi", |
| 73 | searchContent: "abc", |
| 74 | expectedStart: 0, |
| 75 | expectedEnd: 3, |
| 76 | }, |
| 77 | { |
| 78 | name: "in middle", |
| 79 | fileContent: "abc def ghi", |
| 80 | searchContent: "def", |
| 81 | expectedStart: 4, |
| 82 | expectedEnd: 7, |
| 83 | }, |
| 84 | { |
| 85 | name: "at end", |
| 86 | fileContent: "abc def ghi", |
| 87 | searchContent: "ghi", |
| 88 | expectedStart: 8, |
| 89 | expectedEnd: 11, |
| 90 | }, |
| 91 | { |
| 92 | name: "entire content", |
| 93 | fileContent: "abc def ghi", |
| 94 | searchContent: "abc def ghi", |
| 95 | expectedStart: 0, |
| 96 | expectedEnd: 11, |
| 97 | }, |
| 98 | ]; |
| 99 | |
| 100 | testCases.forEach( |
| 101 | ({ name, fileContent, searchContent, expectedStart, expectedEnd }) => { |
| 102 | const result = testSpecificStrategy( |
| 103 | fileContent, |
| 104 | searchContent, |
| 105 | "exactMatch", |
| 106 | ); |
| 107 | expect(result).not.toBeNull(); |
| 108 | expect(result!.startIndex).toBe(expectedStart); |
| 109 | expect(result!.endIndex).toBe(expectedEnd); |
| 110 | |
| 111 | // Verify the matched content |
| 112 | const matchedContent = fileContent.substring( |
| 113 | result!.startIndex, |
| 114 | result!.endIndex, |
| 115 | ); |
| 116 | expect(matchedContent).toBe(searchContent); |
| 117 | }, |
| 118 | ); |
| 119 | }); |
| 120 | |
| 121 | it("should handle multiline exact matches", () => { |
| 122 | const fileContent = `line1 |
| 123 | line2 |
| 124 | line3 |
| 125 | line4`; |
| 126 | const searchContent = `line2 |
| 127 | line3`; |
| 128 | |
| 129 | const result = testSpecificStrategy( |
| 130 | fileContent, |
| 131 | searchContent, |
| 132 | "exactMatch", |
| 133 | ); |
| 134 | expect(result).not.toBeNull(); |
| 135 | expect(result!.startIndex).toBe(6); // After "line1\n" |
| 136 | expect(result!.endIndex).toBe(17); // End of "line3" |
| 137 | |
| 138 | const matchedContent = fileContent.substring( |
| 139 | result!.startIndex, |
| 140 | result!.endIndex, |
| 141 | ); |
| 142 | expect(matchedContent).toBe(searchContent); |
| 143 | }); |
| 144 | |
| 145 | it("should handle content with special characters", () => { |
| 146 | const fileContent = `const regex = /[a-z]+/gi; |
| 147 | const str = "Hello, World!"; |
| 148 | const result = str.match(regex);`; |
| 149 | const searchContent = `const str = "Hello, World!";`; |
| 150 | |
| 151 | const result = testSpecificStrategy( |
| 152 | fileContent, |
| 153 | searchContent, |
| 154 | "exactMatch", |
| 155 | ); |
| 156 | expect(result).not.toBeNull(); |
| 157 | |
| 158 | const matchedContent = fileContent.substring( |
| 159 | result!.startIndex, |
| 160 | result!.endIndex, |
| 161 | ); |
| 162 | expect(matchedContent).toBe(searchContent); |
| 163 | }); |
| 164 | |
| 165 | it("should verify replacement works correctly for exact matches", () => { |
| 166 | verifyMatchAndReplacement( |
| 167 | "const a = 1;\nconst b = 2;\nconst c = 3;", |
| 168 | "const b = 2;", |
| 169 | "const b = 2;", |
| 170 | "const b = 42;", |
| 171 | "const a = 1;\nconst b = 42;\nconst c = 3;", |
| 172 | ); |
| 173 | }); |
| 174 | |
| 175 | it("should handle Windows-style line endings", () => { |
| 176 | const fileContent = "line1\r\nline2\r\nline3"; |
| 177 | const searchContent = "line2"; |
| 178 | |
| 179 | const result = testSpecificStrategy( |
| 180 | fileContent, |
| 181 | searchContent, |
| 182 | "exactMatch", |
| 183 | ); |
| 184 | expect(result).not.toBeNull(); |
| 185 | expect(result!.startIndex).toBe(7); // After "line1\r\n" |
| 186 | expect(result!.endIndex).toBe(12); // End of "line2" |
| 187 | }); |
| 188 | |
| 189 | it("should handle mixed line endings", () => { |
| 190 | const fileContent = "line1\nline2\r\nline3\rline4"; |
| 191 | const searchContent = "line2\r\nline3"; |
| 192 | |
| 193 | const result = testSpecificStrategy( |
| 194 | fileContent, |
| 195 | searchContent, |
| 196 | "exactMatch", |
| 197 | ); |
| 198 | expect(result).not.toBeNull(); |
| 199 | |
| 200 | const matchedContent = fileContent.substring( |
| 201 | result!.startIndex, |
| 202 | result!.endIndex, |
| 203 | ); |
| 204 | expect(matchedContent).toBe(searchContent); |
| 205 | }); |
| 206 | }); |
| 207 | |
| 208 | describe("trimmedMatch strategy", () => { |
| 209 | it("should match when only whitespace differs at edges", () => { |
| 210 | const testCases = [ |
| 211 | { |
| 212 | name: "leading spaces", |
| 213 | fileContent: "abc\n def\nghi", |
| 214 | searchContent: " def", |
| 215 | trimmedSearch: "def", |
| 216 | }, |
| 217 | { |
| 218 | name: "trailing spaces", |
| 219 | fileContent: "abc\ndef \nghi", |
| 220 | searchContent: "def ", |
| 221 | trimmedSearch: "def", |
| 222 | }, |
| 223 | { |
| 224 | name: "both leading and trailing", |
| 225 | fileContent: "abc\n def \nghi", |
| 226 | searchContent: " def ", |
| 227 | trimmedSearch: "def", |
| 228 | }, |
| 229 | { |
| 230 | name: "tabs and spaces", |
| 231 | fileContent: "abc\n\t def \t\nghi", |
| 232 | searchContent: "\t def \t", |
| 233 | trimmedSearch: "def", |
| 234 | }, |
| 235 | ]; |
| 236 | |
| 237 | testCases.forEach( |
| 238 | ({ name, fileContent, searchContent, trimmedSearch }) => { |
| 239 | const result = findSearchMatch(fileContent, searchContent); |
| 240 | expect(result).not.toBeNull(); |
| 241 | |
| 242 | // For cases where exact match exists, it should be preferred |
| 243 | if (fileContent.indexOf(searchContent) !== -1) { |
| 244 | expect(result!.strategyName).toBe("exactMatch"); |
| 245 | const matchedContent = fileContent.substring( |
| 246 | result!.startIndex, |
| 247 | result!.endIndex, |
| 248 | ); |
| 249 | expect(matchedContent).toBe(searchContent); |
| 250 | } else { |
| 251 | expect(result!.strategyName).toBe("trimmedMatch"); |
| 252 | // The match should find the trimmed content with surrounding whitespace |
| 253 | const matchedContent = fileContent.substring( |
| 254 | result!.startIndex, |
| 255 | result!.endIndex, |
| 256 | ); |
| 257 | expect(matchedContent).toContain(trimmedSearch); |
| 258 | } |
| 259 | }, |
| 260 | ); |
| 261 | }); |
| 262 | |
| 263 | it("should handle multiline content with edge whitespace", () => { |
| 264 | const fileContent = `function test() { |
| 265 | const x = 1; |
| 266 | const y = 2; |
| 267 | }`; |
| 268 | const searchContent = ` const x = 1; |
| 269 | const y = 2; `; |
| 270 | |
| 271 | const result = findSearchMatch(fileContent, searchContent); |
| 272 | expect(result).not.toBeNull(); |
| 273 | // Since there's no exact match (trailing spaces don't exist), trimmedMatch should be used |
| 274 | expect(result!.strategyName).toBe("trimmedMatch"); |
| 275 | }); |
| 276 | |
| 277 | it("should verify replacement with trimmed matches", () => { |
| 278 | const fileContent = "start\n targetContent \nend"; |
| 279 | const searchContent = " targetContent "; |
| 280 | const replacement = "newContent"; |
| 281 | |
| 282 | const match = findSearchMatch(fileContent, searchContent); |
| 283 | expect(match).not.toBeNull(); |
| 284 | expect(match!.strategyName).toBe("trimmedMatch"); |
| 285 | |
| 286 | const result = performReplacement(fileContent, match!, replacement); |
| 287 | expect(result).toBe("start\nnewContent\nend"); |
| 288 | }); |
| 289 | |
| 290 | it("should handle edge case with only whitespace", () => { |
| 291 | const fileContent = " \n \n "; |
| 292 | const searchContent = " \n "; |
| 293 | |
| 294 | const result = findSearchMatch(fileContent, searchContent); |
| 295 | expect(result).not.toBeNull(); |
| 296 | // Should find exact match since the content matches exactly |
| 297 | expect(result!.strategyName).toBe("exactMatch"); |
| 298 | }); |
| 299 | }); |
| 300 | |
| 301 | describe("whitespaceIgnoredMatch strategy", () => { |
| 302 | it("should match content with different indentation", () => { |
| 303 | const fileContent = `function test() { |
| 304 | const x = 1; |
| 305 | if (x > 0) { |
| 306 | return true; |
| 307 | } |
| 308 | }`; |
| 309 | const searchContent = `const x=1; |
| 310 | if(x>0){ |
| 311 | return true; |
| 312 | }`; |
| 313 | |
| 314 | const result = findSearchMatch(fileContent, searchContent); |
| 315 | expect(result).not.toBeNull(); |
| 316 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 317 | |
| 318 | // Verify the match covers the intended code block |
| 319 | const matchedContent = fileContent.substring( |
| 320 | result!.startIndex, |
| 321 | result!.endIndex, |
| 322 | ); |
| 323 | expect(matchedContent).toContain("const x = 1"); |
| 324 | expect(matchedContent).toContain("return true"); |
| 325 | }); |
| 326 | |
| 327 | it("should handle complex whitespace variations", () => { |
| 328 | const testCases = [ |
| 329 | { |
| 330 | name: "spaces vs tabs", |
| 331 | fileContent: "if\t(\tcondition\t)\t{\n\treturn\ttrue;\n}", |
| 332 | searchContent: "if (condition) { return true; }", |
| 333 | }, |
| 334 | { |
| 335 | name: "multiple spaces collapsed", |
| 336 | fileContent: "const result = calculate( x , y );", |
| 337 | searchContent: "const result=calculate(x,y);", |
| 338 | }, |
| 339 | { |
| 340 | name: "newlines removed", |
| 341 | fileContent: "function\ntest\n(\n)\n{\nreturn\n1\n;\n}", |
| 342 | searchContent: "function test(){return 1;}", |
| 343 | }, |
| 344 | ]; |
| 345 | |
| 346 | testCases.forEach(({ name, fileContent, searchContent }) => { |
| 347 | const result = findSearchMatch(fileContent, searchContent); |
| 348 | expect(result).not.toBeNull(); |
| 349 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 350 | |
| 351 | // Verify the entire content is matched |
| 352 | const matchedContent = fileContent.substring( |
| 353 | result!.startIndex, |
| 354 | result!.endIndex, |
| 355 | ); |
| 356 | expect(matchedContent.replace(/\s/g, "")).toBe( |
| 357 | searchContent.replace(/\s/g, ""), |
| 358 | ); |
| 359 | }); |
| 360 | }); |
| 361 | |
| 362 | it("should handle partial matches with whitespace differences", () => { |
| 363 | const fileContent = `class Example { |
| 364 | constructor(name) { |
| 365 | this.name = name; |
| 366 | this.items = []; |
| 367 | } |
| 368 | |
| 369 | addItem(item) { |
| 370 | this.items.push(item); |
| 371 | } |
| 372 | }`; |
| 373 | const searchContent = `constructor(name){this.name=name;this.items=[];}`; |
| 374 | |
| 375 | const result = findSearchMatch(fileContent, searchContent); |
| 376 | expect(result).not.toBeNull(); |
| 377 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 378 | |
| 379 | // The match should cover the constructor method |
| 380 | const matchedContent = fileContent.substring( |
| 381 | result!.startIndex, |
| 382 | result!.endIndex, |
| 383 | ); |
| 384 | expect(matchedContent).toContain("constructor"); |
| 385 | expect(matchedContent).toContain("this.items = []"); |
| 386 | }); |
| 387 | |
| 388 | it("should verify replacement with whitespace-ignored matches", () => { |
| 389 | const fileContent = `function calculate ( a , b ) { |
| 390 | return a + b ; |
| 391 | }`; |
| 392 | const searchContent = `function calculate(a,b){return a+b;}`; |
| 393 | const replacement = `function calculate(x, y) { |
| 394 | return x + y; |
| 395 | }`; |
| 396 | |
| 397 | const match = findSearchMatch(fileContent, searchContent); |
| 398 | expect(match).not.toBeNull(); |
| 399 | expect(match!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 400 | |
| 401 | const result = performReplacement(fileContent, match!, replacement); |
| 402 | expect(result).toBe(replacement); |
| 403 | }); |
| 404 | |
| 405 | it("should handle edge cases with empty content after whitespace removal", () => { |
| 406 | const fileContent = " \n\t\r\n "; |
| 407 | const searchContent = " \n "; |
| 408 | |
| 409 | const result = findSearchMatch(fileContent, searchContent); |
| 410 | // Since search content doesn't exist in file but trims to empty, should return emptySearch |
| 411 | expect(result).not.toBeNull(); |
| 412 | expect(result!.strategyName).toBe("emptySearch"); |
| 413 | }); |
| 414 | |
| 415 | it("should match correctly when multiple potential matches exist", () => { |
| 416 | const fileContent = `const a=1; |
| 417 | const a = 1 ; |
| 418 | const a = 1;`; |
| 419 | const searchContent = "const a = 1;"; |
| 420 | |
| 421 | const result = findSearchMatch(fileContent, searchContent); |
| 422 | expect(result).not.toBeNull(); |
| 423 | // Should find the exact match first (third occurrence) |
| 424 | expect(result!.strategyName).toBe("exactMatch"); |
| 425 | expect(result!.startIndex).toBe(33); // After first two lines |
| 426 | }); |
| 427 | }); |
| 428 | |
| 429 | describe("jaroWinklerFuzzyMatch strategy", () => { |
| 430 | it("should match similar code with variable name differences", () => { |
| 431 | const fileContent = `function calculateTotal(items) { |
| 432 | let sum = 0; |
| 433 | for (const item of items) { |
| 434 | sum += item.price; |
| 435 | } |
| 436 | return sum; |
| 437 | }`; |
| 438 | const searchContent = `function calculateTotal(products) { |
| 439 | let total = 0; |
| 440 | for (const product of products) { |
| 441 | total += product.price; |
| 442 | } |
| 443 | return total; |
| 444 | }`; |
| 445 | |
| 446 | const result = findSearchMatch(fileContent, searchContent); |
| 447 | expect(result).not.toBeNull(); |
| 448 | expect(result!.strategyName).toBe("jaroWinklerFuzzyMatch"); |
| 449 | }); |
| 450 | |
| 451 | it("should match code with minor typos", () => { |
| 452 | const testCases = [ |
| 453 | { |
| 454 | name: "typo in function name", |
| 455 | fileContent: "function getUserName() { return this.name; }", |
| 456 | searchContent: "function getUserNaem() { return this.name; }", |
| 457 | }, |
| 458 | { |
| 459 | name: "typo in variable", |
| 460 | fileContent: "const message = 'Hello World';", |
| 461 | searchContent: "const mesage = 'Hello World';", |
| 462 | }, |
| 463 | { |
| 464 | name: "missing character", |
| 465 | fileContent: "console.log('Debug info');", |
| 466 | searchContent: "console.log('Debug nfo');", |
| 467 | }, |
| 468 | ]; |
| 469 | |
| 470 | testCases.forEach(({ name, fileContent, searchContent }) => { |
| 471 | const result = findSearchMatch(fileContent, searchContent); |
| 472 | expect(result).not.toBeNull(); |
| 473 | expect(result!.strategyName).toBe("jaroWinklerFuzzyMatch"); |
| 474 | }); |
| 475 | }); |
| 476 | |
| 477 | it("should not match when similarity is too low", () => { |
| 478 | const fileContent = "function add(a, b) { return a + b; }"; |
| 479 | const searchContent = |
| 480 | "class Calculator { multiply(x, y) { return x * y; } }"; |
| 481 | |
| 482 | const result = findSearchMatch(fileContent, searchContent); |
| 483 | expect(result).toBeNull(); |
| 484 | }); |
| 485 | |
| 486 | it("should handle multiline fuzzy matches", () => { |
| 487 | const fileContent = `if (userIsLoggedIn) { |
| 488 | showDashboard(); |
| 489 | loadUserData(); |
| 490 | }`; |
| 491 | const searchContent = `if (isUserLoggedIn) { |
| 492 | displayDashboard(); |
| 493 | fetchUserData(); |
| 494 | }`; |
| 495 | |
| 496 | const result = findSearchMatch(fileContent, searchContent); |
| 497 | expect(result).not.toBeNull(); |
| 498 | expect(result!.strategyName).toBe("jaroWinklerFuzzyMatch"); |
| 499 | }); |
| 500 | |
| 501 | it("should verify replacement with fuzzy matches", () => { |
| 502 | const fileContent = "const userName = 'John';"; |
| 503 | const searchContent = "const usrName = 'John';"; // Typo in variable name |
| 504 | const replacement = "const userName = 'Jane';"; |
| 505 | |
| 506 | const match = findSearchMatch(fileContent, searchContent); |
| 507 | expect(match).not.toBeNull(); |
| 508 | expect(match!.strategyName).toBe("jaroWinklerFuzzyMatch"); |
| 509 | |
| 510 | const result = performReplacement(fileContent, match!, replacement); |
| 511 | expect(result).toBe(replacement); |
| 512 | }); |
| 513 | |
| 514 | it("should handle edge case with very short strings", () => { |
| 515 | const fileContent = "ab"; |
| 516 | const searchContent = "ac"; |
| 517 | |
| 518 | const result = findSearchMatch(fileContent, searchContent); |
| 519 | // Should not match due to low similarity on short strings |
| 520 | expect(result).toBeNull(); |
| 521 | }); |
| 522 | }); |
| 523 | |
| 524 | describe("Empty search content", () => { |
| 525 | it("should always return position 0 for empty search", () => { |
| 526 | const testCases = [ |
| 527 | { fileContent: "some content" }, |
| 528 | { fileContent: "" }, |
| 529 | { fileContent: "line1\nline2\nline3" }, |
| 530 | { fileContent: " spaces " }, |
| 531 | ]; |
| 532 | |
| 533 | testCases.forEach(({ fileContent }) => { |
| 534 | const result = findSearchMatch(fileContent, ""); |
| 535 | expect(result).toEqual({ |
| 536 | startIndex: 0, |
| 537 | endIndex: 0, |
| 538 | strategyName: "emptySearch", |
| 539 | }); |
| 540 | }); |
| 541 | }); |
| 542 | |
| 543 | it("should treat whitespace-only search as empty", () => { |
| 544 | const searchVariants = [" ", "\n\n", "\t\t", " \n\t "]; |
| 545 | |
| 546 | searchVariants.forEach((searchContent) => { |
| 547 | const result = findSearchMatch("test content", searchContent); |
| 548 | expect(result).toEqual({ |
| 549 | startIndex: 0, |
| 550 | endIndex: 0, |
| 551 | strategyName: "emptySearch", |
| 552 | }); |
| 553 | }); |
| 554 | }); |
| 555 | }); |
| 556 | |
| 557 | describe("Strategy precedence", () => { |
| 558 | it("should always prefer exact match over other strategies", () => { |
| 559 | const fileContent = `exact match |
| 560 | exact match with spaces |
| 561 | exactmatch`; |
| 562 | |
| 563 | // This should find exact match even though fuzzy would also work |
| 564 | const result = findSearchMatch(fileContent, "exact match"); |
| 565 | expect(result!.strategyName).toBe("exactMatch"); |
| 566 | expect(result!.startIndex).toBe(0); |
| 567 | }); |
| 568 | |
| 569 | it("should prefer trimmed match over whitespace-ignored", () => { |
| 570 | const fileContent = " content "; |
| 571 | const searchContent = " content "; |
| 572 | |
| 573 | // Should find exact match |
| 574 | const result = findSearchMatch(fileContent, searchContent); |
| 575 | expect(result!.strategyName).toBe("exactMatch"); |
| 576 | }); |
| 577 | |
| 578 | it("should prefer whitespace-ignored over fuzzy", () => { |
| 579 | const fileContent = "const x = 1;"; |
| 580 | const searchContent = "const x=1;"; |
| 581 | |
| 582 | // Should use whitespace-ignored, not fuzzy |
| 583 | const result = findSearchMatch(fileContent, searchContent); |
| 584 | expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); |
| 585 | }); |
| 586 | }); |
| 587 | |
| 588 | describe("Replacement verification tests", () => { |
| 589 | it("should correctly replace content found by each strategy", () => { |
| 590 | const testCases = [ |
| 591 | { |
| 592 | name: "exact match replacement", |
| 593 | fileContent: "const a = 1;\nconst b = 2;\nconst c = 3;", |
| 594 | searchContent: "const b = 2;", |
| 595 | replacement: "const b = 42;", |
| 596 | expectedResult: "const a = 1;\nconst b = 42;\nconst c = 3;", |
| 597 | expectedStrategy: "exactMatch", |
| 598 | }, |
| 599 | { |
| 600 | name: "trimmed match replacement", |
| 601 | fileContent: "start\n target \nend", |
| 602 | searchContent: " target ", |
| 603 | replacement: "replaced", |
| 604 | expectedResult: "start\nreplaced\nend", |
| 605 | expectedStrategy: "trimmedMatch", |
| 606 | }, |
| 607 | { |
| 608 | name: "whitespace-ignored replacement", |
| 609 | fileContent: "if ( x > 0 ) { return true; }", |
| 610 | searchContent: "if(x>0){return true;}", |
| 611 | replacement: "if (x > 0) { return false; }", |
| 612 | expectedResult: "if (x > 0) { return false; }", |
| 613 | expectedStrategy: "whitespaceIgnoredMatch", |
| 614 | }, |
| 615 | ]; |
| 616 | |
| 617 | testCases.forEach( |
| 618 | ({ |
| 619 | name, |
| 620 | fileContent, |
| 621 | searchContent, |
| 622 | replacement, |
| 623 | expectedResult, |
| 624 | expectedStrategy, |
| 625 | }) => { |
| 626 | const match = findSearchMatch(fileContent, searchContent); |
| 627 | expect(match).not.toBeNull(); |
| 628 | expect(match!.strategyName).toBe(expectedStrategy); |
| 629 | |
| 630 | const result = performReplacement(fileContent, match!, replacement); |
| 631 | expect(result).toBe(expectedResult); |
| 632 | }, |
| 633 | ); |
| 634 | }); |
| 635 | |
| 636 | it("should handle edge cases in replacement", () => { |
| 637 | // Empty replacement |
| 638 | verifyMatchAndReplacement( |
| 639 | "before target after", |
| 640 | "target", |
| 641 | "target", |
| 642 | "", |
| 643 | "before after", |
| 644 | ); |
| 645 | |
| 646 | // Replacement longer than original |
| 647 | verifyMatchAndReplacement( |
| 648 | "short", |
| 649 | "short", |
| 650 | "short", |
| 651 | "much longer replacement", |
| 652 | "much longer replacement", |
| 653 | ); |
| 654 | |
| 655 | // Multiline replacement |
| 656 | verifyMatchAndReplacement( |
| 657 | "single line", |
| 658 | "single", |
| 659 | "single", |
| 660 | "multi\nline\nreplacement", |
| 661 | "multi\nline\nreplacement line", |
| 662 | ); |
| 663 | }); |
| 664 | }); |
| 665 | |
| 666 | describe("Performance and edge cases", () => { |
| 667 | it("should handle very long content efficiently", () => { |
| 668 | const longContent = "x".repeat(10000) + "target" + "y".repeat(10000); |
| 669 | const result = findSearchMatch(longContent, "target"); |
| 670 | |
| 671 | expect(result).not.toBeNull(); |
| 672 | expect(result!.startIndex).toBe(10000); |
| 673 | expect(result!.endIndex).toBe(10006); |
| 674 | }); |
| 675 | |
| 676 | it("should handle unicode and special characters", () => { |
| 677 | const testCases = [ |
| 678 | { |
| 679 | fileContent: "const emoji = '馃殌馃専馃帀';", |
| 680 | searchContent: "馃殌馃専馃帀", |
| 681 | }, |
| 682 | { |
| 683 | fileContent: "const chinese = '浣犲ソ涓栫晫';", |
| 684 | searchContent: "浣犲ソ涓栫晫", |
| 685 | }, |
| 686 | { |
| 687 | fileContent: "const regex = /\\d+/g;", |
| 688 | searchContent: "/\\d+/g", |
| 689 | }, |
| 690 | ]; |
| 691 | |
| 692 | testCases.forEach(({ fileContent, searchContent }) => { |
| 693 | const result = findSearchMatch(fileContent, searchContent); |
| 694 | expect(result).not.toBeNull(); |
| 695 | |
| 696 | const matchedContent = fileContent.substring( |
| 697 | result!.startIndex, |
| 698 | result!.endIndex, |
| 699 | ); |
| 700 | expect(matchedContent).toContain(searchContent); |
| 701 | }); |
| 702 | }); |
| 703 | |
| 704 | it("should handle null bytes and control characters", () => { |
| 705 | const fileContent = "before\x00null\x01byte\x02after"; |
| 706 | const searchContent = "\x00null\x01byte\x02"; |
| 707 | |
| 708 | const result = findSearchMatch(fileContent, searchContent); |
| 709 | expect(result).not.toBeNull(); |
| 710 | expect(result!.strategyName).toBe("exactMatch"); |
| 711 | }); |
| 712 | }); |
| 713 | }); |