|
| 1 | +import { MultiSearchReplaceDiffStrategy } from "../multi-search-replace" |
| 2 | + |
| 3 | +describe("null edit detection", () => { |
| 4 | + let strategy: MultiSearchReplaceDiffStrategy |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + strategy = new MultiSearchReplaceDiffStrategy() |
| 8 | + }) |
| 9 | + |
| 10 | + it("should detect and reject null edits (identical search and replace)", async () => { |
| 11 | + const originalContent = 'function hello() {\n console.log("hello")\n}\n' |
| 12 | + const diffContent = [ |
| 13 | + "<<<<<<< SEARCH", |
| 14 | + "function hello() {", |
| 15 | + ' console.log("hello")', |
| 16 | + "}", |
| 17 | + "=======", |
| 18 | + "function hello() {", |
| 19 | + ' console.log("hello")', |
| 20 | + "}", |
| 21 | + ">>>>>>> REPLACE", |
| 22 | + ].join("\n") |
| 23 | + |
| 24 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 25 | + expect(result.success).toBe(false) |
| 26 | + if (!result.success && result.failParts && result.failParts.length > 0) { |
| 27 | + const firstError = result.failParts[0] |
| 28 | + if (!firstError.success && firstError.error) { |
| 29 | + expect(firstError.error).toContain("NULL EDIT DETECTED") |
| 30 | + expect(firstError.error).toContain("Search and replace content are identical") |
| 31 | + expect(firstError.error).toContain("This is a common issue with AI models (especially Gemini)") |
| 32 | + expect(firstError.error).toContain("Model hallucination") |
| 33 | + } |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + it("should detect null edits in multi-block diffs", async () => { |
| 38 | + const originalContent = |
| 39 | + 'function hello() {\n console.log("hello")\n}\nfunction world() {\n return "world"\n}' |
| 40 | + const diffContent = [ |
| 41 | + "<<<<<<< SEARCH", |
| 42 | + "function hello() {", |
| 43 | + ' console.log("hello")', |
| 44 | + "}", |
| 45 | + "=======", |
| 46 | + "function hello() {", |
| 47 | + ' console.log("hello world")', |
| 48 | + "}", |
| 49 | + ">>>>>>> REPLACE", |
| 50 | + "", |
| 51 | + "<<<<<<< SEARCH", |
| 52 | + "function world() {", |
| 53 | + ' return "world"', |
| 54 | + "}", |
| 55 | + "=======", |
| 56 | + "function world() {", |
| 57 | + ' return "world"', |
| 58 | + "}", |
| 59 | + ">>>>>>> REPLACE", |
| 60 | + ].join("\n") |
| 61 | + |
| 62 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 63 | + // Should partially succeed but report the null edit |
| 64 | + expect(result.success).toBe(true) |
| 65 | + if (result.success) { |
| 66 | + expect(result.failParts).toBeDefined() |
| 67 | + if (result.failParts && result.failParts[0] && !result.failParts[0].success) { |
| 68 | + expect(result.failParts[0].error).toContain("NULL EDIT DETECTED") |
| 69 | + } |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + it("should detect null edits with line numbers", async () => { |
| 74 | + const originalContent = "function test() {\n return true;\n}\n" |
| 75 | + const diffContent = [ |
| 76 | + "<<<<<<< SEARCH", |
| 77 | + ":start_line:1", |
| 78 | + "-------", |
| 79 | + "function test() {", |
| 80 | + " return true;", |
| 81 | + "}", |
| 82 | + "=======", |
| 83 | + "function test() {", |
| 84 | + " return true;", |
| 85 | + "}", |
| 86 | + ">>>>>>> REPLACE", |
| 87 | + ].join("\n") |
| 88 | + |
| 89 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 90 | + expect(result.success).toBe(false) |
| 91 | + if (!result.success && result.failParts && result.failParts.length > 0) { |
| 92 | + const firstError = result.failParts[0] |
| 93 | + if (!firstError.success && firstError.error) { |
| 94 | + expect(firstError.error).toContain("NULL EDIT DETECTED") |
| 95 | + expect(firstError.error).toContain("phantom") |
| 96 | + expect(firstError.error).toContain("Ensure the REPLACE block contains the actual modified content") |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + it("should not trigger null edit detection for legitimate empty replacements (deletions)", async () => { |
| 102 | + const originalContent = "function test() {\n // Remove this comment\n return true;\n}\n" |
| 103 | + const diffContent = ["<<<<<<< SEARCH", " // Remove this comment", "=======", ">>>>>>> REPLACE"].join("\n") |
| 104 | + |
| 105 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 106 | + expect(result.success).toBe(true) |
| 107 | + // Should not contain null edit error |
| 108 | + if (!result.success && result.error) { |
| 109 | + expect(result.error).not.toContain("NULL EDIT DETECTED") |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + it("should not trigger for actual changes even if similar", async () => { |
| 114 | + const originalContent = "function test() {\n return true;\n}\n" |
| 115 | + const diffContent = [ |
| 116 | + "<<<<<<< SEARCH", |
| 117 | + "function test() {", |
| 118 | + " return true;", |
| 119 | + "}", |
| 120 | + "=======", |
| 121 | + "function test() {", |
| 122 | + " return false;", |
| 123 | + "}", |
| 124 | + ">>>>>>> REPLACE", |
| 125 | + ].join("\n") |
| 126 | + |
| 127 | + const result = await strategy.applyDiff(originalContent, diffContent) |
| 128 | + expect(result.success).toBe(true) |
| 129 | + // Should not contain null edit error |
| 130 | + if (!result.success && result.error) { |
| 131 | + expect(result.error).not.toContain("NULL EDIT DETECTED") |
| 132 | + } |
| 133 | + }) |
| 134 | +}) |
0 commit comments