|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { MultiFileSearchReplaceDiffStrategy } from "../multi-file-search-replace" |
| 3 | + |
| 4 | +describe("MultiFileSearchReplaceDiffStrategy - 8-character marker support", () => { |
| 5 | + it("should handle 8 '<' characters in SEARCH marker (PR #9456 use case)", async () => { |
| 6 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 7 | + const originalContent = "line 1\nline 2\nline 3" |
| 8 | + |
| 9 | + const diff = `<<<<<<<< SEARCH |
| 10 | +:start_line:1 |
| 11 | +------- |
| 12 | +line 1 |
| 13 | +======= |
| 14 | +modified line 1 |
| 15 | +>>>>>>>> REPLACE` |
| 16 | + |
| 17 | + const result = await strategy.applyDiff(originalContent, diff) |
| 18 | + |
| 19 | + expect(result.success).toBe(true) |
| 20 | + if (result.success) { |
| 21 | + expect(result.content).toBe("modified line 1\nline 2\nline 3") |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + it("should handle 7 '<' characters in SEARCH marker (standard)", async () => { |
| 26 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 27 | + const originalContent = "line 1\nline 2\nline 3" |
| 28 | + |
| 29 | + const diff = `<<<<<<< SEARCH |
| 30 | +:start_line:1 |
| 31 | +------- |
| 32 | +line 1 |
| 33 | +======= |
| 34 | +modified line 1 |
| 35 | +>>>>>>> REPLACE` |
| 36 | + |
| 37 | + const result = await strategy.applyDiff(originalContent, diff) |
| 38 | + |
| 39 | + expect(result.success).toBe(true) |
| 40 | + if (result.success) { |
| 41 | + expect(result.content).toBe("modified line 1\nline 2\nline 3") |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + it("should handle 8 '>' characters in REPLACE marker", async () => { |
| 46 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 47 | + const originalContent = "line 1\nline 2\nline 3" |
| 48 | + |
| 49 | + const diff = `<<<<<<< SEARCH |
| 50 | +:start_line:2 |
| 51 | +------- |
| 52 | +line 2 |
| 53 | +======= |
| 54 | +modified line 2 |
| 55 | +>>>>>>>> REPLACE` |
| 56 | + |
| 57 | + const result = await strategy.applyDiff(originalContent, diff) |
| 58 | + |
| 59 | + expect(result.success).toBe(true) |
| 60 | + if (result.success) { |
| 61 | + expect(result.content).toBe("line 1\nmodified line 2\nline 3") |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + it("should handle optional '<' at end of REPLACE marker", async () => { |
| 66 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 67 | + const originalContent = "line 1\nline 2\nline 3" |
| 68 | + |
| 69 | + const diff = `<<<<<<< SEARCH |
| 70 | +:start_line:3 |
| 71 | +------- |
| 72 | +line 3 |
| 73 | +======= |
| 74 | +modified line 3 |
| 75 | +>>>>>>> REPLACE<` |
| 76 | + |
| 77 | + const result = await strategy.applyDiff(originalContent, diff) |
| 78 | + |
| 79 | + expect(result.success).toBe(true) |
| 80 | + if (result.success) { |
| 81 | + expect(result.content).toBe("line 1\nline 2\nmodified line 3") |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + it("should handle mixed 7 and 8 character markers in same diff", async () => { |
| 86 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 87 | + const originalContent = "line 1\nline 2\nline 3" |
| 88 | + |
| 89 | + const diff = `<<<<<<<< SEARCH |
| 90 | +:start_line:1 |
| 91 | +------- |
| 92 | +line 1 |
| 93 | +======= |
| 94 | +modified line 1 |
| 95 | +>>>>>>> REPLACE |
| 96 | +
|
| 97 | +<<<<<<< SEARCH |
| 98 | +:start_line:3 |
| 99 | +------- |
| 100 | +line 3 |
| 101 | +======= |
| 102 | +modified line 3 |
| 103 | +>>>>>>>> REPLACE` |
| 104 | + |
| 105 | + const result = await strategy.applyDiff(originalContent, diff) |
| 106 | + |
| 107 | + expect(result.success).toBe(true) |
| 108 | + if (result.success) { |
| 109 | + expect(result.content).toBe("modified line 1\nline 2\nmodified line 3") |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + it("should reject markers with too many characters (9+)", async () => { |
| 114 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 115 | + const originalContent = "line 1\nline 2\nline 3" |
| 116 | + |
| 117 | + const diff = `<<<<<<<<< SEARCH |
| 118 | +:start_line:1 |
| 119 | +------- |
| 120 | +line 1 |
| 121 | +======= |
| 122 | +modified line 1 |
| 123 | +>>>>>>> REPLACE` |
| 124 | + |
| 125 | + const result = await strategy.applyDiff(originalContent, diff) |
| 126 | + |
| 127 | + expect(result.success).toBe(false) |
| 128 | + if (!result.success) { |
| 129 | + expect(result.error).toContain("Diff block is malformed") |
| 130 | + } |
| 131 | + }) |
| 132 | + |
| 133 | + it("should reject markers with too few characters (6-)", async () => { |
| 134 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 135 | + const originalContent = "line 1\nline 2\nline 3" |
| 136 | + |
| 137 | + const diff = `<<<<<< SEARCH |
| 138 | +:start_line:1 |
| 139 | +------- |
| 140 | +line 1 |
| 141 | +======= |
| 142 | +modified line 1 |
| 143 | +>>>>>>> REPLACE` |
| 144 | + |
| 145 | + const result = await strategy.applyDiff(originalContent, diff) |
| 146 | + |
| 147 | + expect(result.success).toBe(false) |
| 148 | + if (!result.success) { |
| 149 | + expect(result.error).toContain("Diff block is malformed") |
| 150 | + } |
| 151 | + }) |
| 152 | + |
| 153 | + it("should handle validation with 8 character markers", () => { |
| 154 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 155 | + |
| 156 | + const diff = `<<<<<<<< SEARCH |
| 157 | +:start_line:1 |
| 158 | +------- |
| 159 | +content |
| 160 | +======= |
| 161 | +new content |
| 162 | +>>>>>>>> REPLACE` |
| 163 | + |
| 164 | + const result = strategy["validateMarkerSequencing"](diff) |
| 165 | + |
| 166 | + expect(result.success).toBe(true) |
| 167 | + }) |
| 168 | + |
| 169 | + it("should detect merge conflict with 8 character prefix", () => { |
| 170 | + const strategy = new MultiFileSearchReplaceDiffStrategy() |
| 171 | + |
| 172 | + const diff = `<<<<<<<< SEARCH |
| 173 | +:start_line:1 |
| 174 | +------- |
| 175 | +content |
| 176 | +<<<<<<<< HEAD |
| 177 | +conflict content |
| 178 | +======= |
| 179 | +new content |
| 180 | +>>>>>>>> REPLACE` |
| 181 | + |
| 182 | + const result = strategy["validateMarkerSequencing"](diff) |
| 183 | + |
| 184 | + expect(result.success).toBe(false) |
| 185 | + if (!result.success) { |
| 186 | + expect(result.error).toContain("merge conflict") |
| 187 | + } |
| 188 | + }) |
| 189 | +}) |
0 commit comments