|
| 1 | +import { MultiSearchReplaceDiffStrategy } from "../multi-search-replace" |
| 2 | + |
| 3 | +describe("MultiSearchReplaceDiffStrategy - Line Number Stripping", () => { |
| 4 | + describe("line number stripping at last line", () => { |
| 5 | + let strategy: MultiSearchReplaceDiffStrategy |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + strategy = new MultiSearchReplaceDiffStrategy() |
| 9 | + }) |
| 10 | + |
| 11 | + it("should correctly strip line numbers from the last line in SEARCH and REPLACE blocks", async () => { |
| 12 | + // This test case reproduces the issue from GitHub issue #8020 |
| 13 | + // where the last line with a line number wasn't being stripped correctly |
| 14 | + // Note: The original content should NOT have line numbers |
| 15 | + const originalContent = ` ContactRelaEnum.MS.name(), addressList, contactStatusMap, SensitiveType.ADDRESS, curSector); |
| 16 | + } |
| 17 | + } |
| 18 | + List<ContactInfoItemResp> addressInfoList = new ArrayList<>(CollectionUtils.size(repairInfoList) > 10 ? 10 |
| 19 | + : CollectionUtils.size(repairInfoList) + CollectionUtils.size(homeAddressInfoList) |
| 20 | + + CollectionUtils.size(idNoAddressInfoList) + CollectionUtils.size(workAddressInfoList) |
| 21 | + + CollectionUtils.size(personIdentityInfoList));` |
| 22 | + |
| 23 | + const diffContent = `<<<<<<< SEARCH |
| 24 | +1473 | ContactRelaEnum.MS.name(), addressList, contactStatusMap, SensitiveType.ADDRESS, curSector); |
| 25 | +1474 | } |
| 26 | +1475 | } |
| 27 | +1476 | List<ContactInfoItemResp> addressInfoList = new ArrayList<>(CollectionUtils.size(repairInfoList) > 10 ? 10 |
| 28 | +1477 | : CollectionUtils.size(repairInfoList) + CollectionUtils.size(homeAddressInfoList) |
| 29 | +1478 | + CollectionUtils.size(idNoAddressInfoList) + CollectionUtils.size(workAddressInfoList) |
| 30 | +1479 | + CollectionUtils.size(personIdentityInfoList)); |
| 31 | +======= |
| 32 | +1473 | ContactRelaEnum.MS.name(), addressList, contactStatusMap, SensitiveType.ADDRESS, curSector); |
| 33 | +1474 | } |
| 34 | +1475 | } |
| 35 | +1476 | |
| 36 | +1477 | // |
| 37 | +1478 | if (isAddressDisplayOptimizeEnabled()) { |
| 38 | +1479 | homeAddressInfoList = filterAddressesByThreeYearRule(homeAddressInfoList); |
| 39 | +1480 | personIdentityInfoList = filterAddressesByThreeYearRule(personIdentityInfoList); |
| 40 | +1481 | idNoAddressInfoList = filterAddressesByThreeYearRule(idNoAddressInfoList); |
| 41 | +1482 | workAddressInfoList = filterAddressesByThreeYearRule(workAddressInfoList); |
| 42 | +1483 | } |
| 43 | +1484 | |
| 44 | +1485 | List<ContactInfoItemResp> addressInfoList = new ArrayList<>(CollectionUtils.size(repairInfoList) > 10 ? 10 |
| 45 | +1486 | : CollectionUtils.size(repairInfoList) + CollectionUtils.size(homeAddressInfoList) |
| 46 | +1487 | + CollectionUtils.size(idNoAddressInfoList) + CollectionUtils.size(workAddressInfoList) |
| 47 | +1488 | + CollectionUtils.size(personIdentityInfoList)); |
| 48 | +>>>>>>> REPLACE` |
| 49 | + |
| 50 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 51 | + if (!result.success) { |
| 52 | + console.error("Test failed with error:", result.error || result.failParts) |
| 53 | + } |
| 54 | + expect(result.success).toBe(true) |
| 55 | + if (result.success) { |
| 56 | + const expectedContent = ` ContactRelaEnum.MS.name(), addressList, contactStatusMap, SensitiveType.ADDRESS, curSector); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // |
| 61 | + if (isAddressDisplayOptimizeEnabled()) { |
| 62 | + homeAddressInfoList = filterAddressesByThreeYearRule(homeAddressInfoList); |
| 63 | + personIdentityInfoList = filterAddressesByThreeYearRule(personIdentityInfoList); |
| 64 | + idNoAddressInfoList = filterAddressesByThreeYearRule(idNoAddressInfoList); |
| 65 | + workAddressInfoList = filterAddressesByThreeYearRule(workAddressInfoList); |
| 66 | + } |
| 67 | + |
| 68 | + List<ContactInfoItemResp> addressInfoList = new ArrayList<>(CollectionUtils.size(repairInfoList) > 10 ? 10 |
| 69 | + : CollectionUtils.size(repairInfoList) + CollectionUtils.size(homeAddressInfoList) |
| 70 | + + CollectionUtils.size(idNoAddressInfoList) + CollectionUtils.size(workAddressInfoList) |
| 71 | + + CollectionUtils.size(personIdentityInfoList));` |
| 72 | + expect(result.content).toBe(expectedContent) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + it("should handle Windows CRLF line endings with line numbers at the last line", async () => { |
| 77 | + // Test with Windows-style CRLF line endings |
| 78 | + const originalContent = "line 1\r\nline 2\r\nline 3" |
| 79 | + const diffContent = `<<<<<<< SEARCH |
| 80 | +1 | line 1 |
| 81 | +2 | line 2 |
| 82 | +3 | line 3 |
| 83 | +======= |
| 84 | +1 | line 1 |
| 85 | +2 | modified line 2 |
| 86 | +3 | line 3 |
| 87 | +>>>>>>> REPLACE` |
| 88 | + |
| 89 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 90 | + expect(result.success).toBe(true) |
| 91 | + if (result.success) { |
| 92 | + expect(result.content).toBe("line 1\r\nmodified line 2\r\nline 3") |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + it("should handle single line with line number at the end", async () => { |
| 97 | + // Simple test case for a single line with line number |
| 98 | + const originalContent = "some content here" |
| 99 | + const diffContent = `<<<<<<< SEARCH |
| 100 | +1 | some content here |
| 101 | +======= |
| 102 | +1 | modified content here |
| 103 | +>>>>>>> REPLACE` |
| 104 | + |
| 105 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 106 | + expect(result.success).toBe(true) |
| 107 | + if (result.success) { |
| 108 | + expect(result.content).toBe("modified content here") |
| 109 | + } |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
0 commit comments