From 552ed0e98668a2efbe35009ba0d5ddaadeb81da1 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Thu, 3 Apr 2025 17:55:05 +0300 Subject: [PATCH] chore: Add more multi-block tests for apply_diff tool (#2261) --- .../__tests__/multi-search-replace.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/core/diff/strategies/__tests__/multi-search-replace.test.ts b/src/core/diff/strategies/__tests__/multi-search-replace.test.ts index 60d9f0508b..8aec76407e 100644 --- a/src/core/diff/strategies/__tests__/multi-search-replace.test.ts +++ b/src/core/diff/strategies/__tests__/multi-search-replace.test.ts @@ -76,6 +76,54 @@ function hello() { } }) + it("should replace matching content in multiple blocks", async () => { + const originalContent = 'function hello() {\n console.log("hello")\n}\n' + const diffContent = `test.ts +<<<<<<< SEARCH +function hello() { +======= +function helloWorld() { +>>>>>>> REPLACE +<<<<<<< SEARCH + console.log("hello") +======= + console.log("hello world") +>>>>>>> REPLACE` + + const result = await strategy.applyDiff(originalContent, diffContent) + expect(result.success).toBe(true) + if (result.success) { + expect(result.content).toBe('function helloWorld() {\n console.log("hello world")\n}\n') + } + }) + + it("should replace matching content in multiple blocks with line numbers", async () => { + const originalContent = 'function hello() {\n console.log("hello")\n}\n' + const diffContent = `test.ts +<<<<<<< SEARCH +:start_line:1 +:end_line:1 +------- +function hello() { +======= +function helloWorld() { +>>>>>>> REPLACE +<<<<<<< SEARCH +:start_line:2 +:end_line:2 +------- + console.log("hello") +======= + console.log("hello world") +>>>>>>> REPLACE` + + const result = await strategy.applyDiff(originalContent, diffContent) + expect(result.success).toBe(true) + if (result.success) { + expect(result.content).toBe('function helloWorld() {\n console.log("hello world")\n}\n') + } + }) + it("should match content with different surrounding whitespace", async () => { const originalContent = "\nfunction example() {\n return 42;\n}\n\n" const diffContent = `test.ts