Browse Source

feat: add tests for the git fallback strategy

Daniel Riccio 1 year ago
parent
commit
82a0ffe67a

+ 37 - 1
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")
+	})
+})

+ 1 - 1
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<EditResult> {
+export async function applyGitFallback(hunk: Hunk, content: string[]): Promise<EditResult> {
 	let tmpDir: tmp.DirResult | undefined
 
 	try {