diff --git a/src/core/diff/strategies/new-unified/__tests__/edit-strategies.test.ts b/src/core/diff/strategies/new-unified/__tests__/edit-strategies.test.ts index 459445f..2ed1cc9 100644 --- a/src/core/diff/strategies/new-unified/__tests__/edit-strategies.test.ts +++ b/src/core/diff/strategies/new-unified/__tests__/edit-strategies.test.ts @@ -1,4 +1,4 @@ -import { applyContextMatching, applyDMP } from "../edit-strategies" +import { applyContextMatching, applyDMP, applyGitFallback } from "../edit-strategies" import { Hunk } from "../types" const testCases = [ @@ -257,3 +257,39 @@ describe("applyDMP", () => { }) }) }) + +describe("applyGitFallback", () => { + it("should successfully apply changes using git operations", async () => { + const hunk = { + changes: [ + { type: "context", content: "line1", indent: "" }, + { type: "remove", content: "line2", indent: "" }, + { type: "add", content: "new line2", indent: "" }, + { type: "context", content: "line3", indent: "" } + ] + } as Hunk + + const content = ["line1", "line2", "line3"] + const result = await applyGitFallback(hunk, content) + + expect(result.result.join("\n")).toEqual("line1\nnew line2\nline3") + expect(result.confidence).toBe(1) + expect(result.strategy).toBe("git-fallback") + }) + + it("should return original content with 0 confidence when changes cannot be applied", async () => { + const hunk = { + changes: [ + { type: "context", content: "nonexistent", indent: "" }, + { type: "add", content: "new line", indent: "" } + ] + } as Hunk + + const content = ["line1", "line2", "line3"] + const result = await applyGitFallback(hunk, content) + + expect(result.result).toEqual(content) + expect(result.confidence).toBe(0) + expect(result.strategy).toBe("git-fallback") + }) +}) diff --git a/src/core/diff/strategies/new-unified/edit-strategies.ts b/src/core/diff/strategies/new-unified/edit-strategies.ts index e7f3d85..0828c83 100644 --- a/src/core/diff/strategies/new-unified/edit-strategies.ts +++ b/src/core/diff/strategies/new-unified/edit-strategies.ts @@ -155,7 +155,7 @@ export function applyDMP( } // Git fallback strategy that works with full content -async function applyGitFallback(hunk: Hunk, content: string[]): Promise { +export async function applyGitFallback(hunk: Hunk, content: string[]): Promise { let tmpDir: tmp.DirResult | undefined try {