|
| 1 | +import { MultiSearchReplaceDiffStrategy } from "../multi-search-replace" |
| 2 | + |
| 3 | +describe("HTML entity handling", () => { |
| 4 | + let strategy: MultiSearchReplaceDiffStrategy |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + strategy = new MultiSearchReplaceDiffStrategy() |
| 8 | + }) |
| 9 | + |
| 10 | + it("should distinguish between HTML entities and their literal characters", async () => { |
| 11 | + const originalContent = `.FilterBatch<int>(batch => batch.Count == 3) |
| 12 | +.MapBatch<int, int>(batch => batch.Sum())` |
| 13 | + |
| 14 | + const diffContent = ` |
| 15 | +<<<<<<< SEARCH |
| 16 | +.FilterBatch<int>(batch => batch.Count == 3) |
| 17 | +======= |
| 18 | +.FilterBatch<int>(batch => batch.Count == 3) |
| 19 | +>>>>>>> REPLACE |
| 20 | +
|
| 21 | +<<<<<<< SEARCH |
| 22 | +.MapBatch<int, int>(batch => batch.Sum()) |
| 23 | +======= |
| 24 | +.MapBatch<int, int>(batch => batch.Sum()) |
| 25 | +>>>>>>> REPLACE` |
| 26 | + |
| 27 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 28 | + expect(result.success).toBe(true) |
| 29 | + if (result.success) { |
| 30 | + expect(result.content).toBe(`.FilterBatch<int>(batch => batch.Count == 3) |
| 31 | +.MapBatch<int, int>(batch => batch.Sum())`) |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + it("should not treat < and < as identical in search/replace comparison", async () => { |
| 36 | + const originalContent = `public List<string> GetItems() { |
| 37 | + return new List<string>(); |
| 38 | +}` |
| 39 | + |
| 40 | + const diffContent = ` |
| 41 | +<<<<<<< SEARCH |
| 42 | +public List<string> GetItems() { |
| 43 | + return new List<string>(); |
| 44 | +} |
| 45 | +======= |
| 46 | +public List<string> GetItems() { |
| 47 | + return new List<string>(); |
| 48 | +} |
| 49 | +>>>>>>> REPLACE` |
| 50 | + |
| 51 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 52 | + expect(result.success).toBe(true) |
| 53 | + if (result.success) { |
| 54 | + expect(result.content).toBe(`public List<string> GetItems() { |
| 55 | + return new List<string>(); |
| 56 | +}`) |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + it("should handle mixed HTML entities correctly", async () => { |
| 61 | + const originalContent = `<div class="container"> |
| 62 | + <p>Hello & welcome</p> |
| 63 | +</div>` |
| 64 | + |
| 65 | + const diffContent = ` |
| 66 | +<<<<<<< SEARCH |
| 67 | +<div class="container"> |
| 68 | + <p>Hello & welcome</p> |
| 69 | +</div> |
| 70 | +======= |
| 71 | +<div class="container"> |
| 72 | + <p>Hello & welcome</p> |
| 73 | +</div> |
| 74 | +>>>>>>> REPLACE` |
| 75 | + |
| 76 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 77 | + expect(result.success).toBe(true) |
| 78 | + if (result.success) { |
| 79 | + expect(result.content).toBe(`<div class="container"> |
| 80 | + <p>Hello & welcome</p> |
| 81 | +</div>`) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + it("should reject when search and replace are identical (including HTML entities)", async () => { |
| 86 | + const originalContent = `function test<T>() { |
| 87 | + return value; |
| 88 | +}` |
| 89 | + |
| 90 | + // Both search and replace have the same content (literal angle brackets) |
| 91 | + const diffContent = ` |
| 92 | +<<<<<<< SEARCH |
| 93 | +function test<T>() { |
| 94 | + return value; |
| 95 | +} |
| 96 | +======= |
| 97 | +function test<T>() { |
| 98 | + return value; |
| 99 | +} |
| 100 | +>>>>>>> REPLACE` |
| 101 | + |
| 102 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 103 | + expect(result.success).toBe(false) |
| 104 | + if (!result.success && result.error) { |
| 105 | + expect(result.error).toContain("Search and replace content are identical") |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + it("should handle apostrophes and quotes with HTML entities", async () => { |
| 110 | + const originalContent = `const message = 'It's a "test" message';` |
| 111 | + |
| 112 | + const diffContent = ` |
| 113 | +<<<<<<< SEARCH |
| 114 | +const message = 'It's a "test" message'; |
| 115 | +======= |
| 116 | +const message = 'It\'s a "test" message'; |
| 117 | +>>>>>>> REPLACE` |
| 118 | + |
| 119 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 120 | + expect(result.success).toBe(true) |
| 121 | + if (result.success) { |
| 122 | + expect(result.content).toBe(`const message = 'It\'s a "test" message';`) |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + it("should handle C# generics with escaped HTML entities", async () => { |
| 127 | + const originalContent = `var dict = new Dictionary<string, List<int>>(); |
| 128 | +dict.Add("key", new List<int> { 1, 2, 3 });` |
| 129 | + |
| 130 | + const diffContent = ` |
| 131 | +<<<<<<< SEARCH |
| 132 | +var dict = new Dictionary<string, List<int>>(); |
| 133 | +dict.Add("key", new List<int> { 1, 2, 3 }); |
| 134 | +======= |
| 135 | +var dict = new Dictionary<string, List<int>>(); |
| 136 | +dict.Add("key", new List<int> { 1, 2, 3 }); |
| 137 | +>>>>>>> REPLACE` |
| 138 | + |
| 139 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 140 | + expect(result.success).toBe(true) |
| 141 | + if (result.success) { |
| 142 | + expect(result.content).toBe(`var dict = new Dictionary<string, List<int>>(); |
| 143 | +dict.Add("key", new List<int> { 1, 2, 3 });`) |
| 144 | + } |
| 145 | + }) |
| 146 | + |
| 147 | + it("should handle the exact issue from bug report", async () => { |
| 148 | + const originalContent = ` .FilterBatch<int>(batch => batch.Count == 3) |
| 149 | + .MapBatch<int, int>(batch => batch.Sum())` |
| 150 | + |
| 151 | + // This is the exact diff that was failing before the fix |
| 152 | + const diffContent = ` |
| 153 | +<<<<<<< SEARCH |
| 154 | + .FilterBatch<int>(batch => batch.Count == 3) |
| 155 | +======= |
| 156 | + .FilterBatch<int>(batch => batch.Count == 3) |
| 157 | +>>>>>>> REPLACE |
| 158 | +
|
| 159 | +<<<<<<< SEARCH |
| 160 | + .MapBatch<int, int>(batch => batch.Sum()) |
| 161 | +======= |
| 162 | + .MapBatch<int, int>(batch => batch.Sum()) |
| 163 | +>>>>>>> REPLACE` |
| 164 | + |
| 165 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 166 | + expect(result.success).toBe(true) |
| 167 | + if (result.success) { |
| 168 | + expect(result.content).toBe(` .FilterBatch<int>(batch => batch.Count == 3) |
| 169 | + .MapBatch<int, int>(batch => batch.Sum())`) |
| 170 | + } |
| 171 | + }) |
| 172 | +}) |
0 commit comments