feat: add tests for the git fallback strategy

This commit is contained in:
Daniel Riccio
2025-01-17 02:04:34 -05:00
parent 5a35d7fb2a
commit 82a0ffe67a
2 changed files with 38 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
import { applyContextMatching, applyDMP } from "../edit-strategies" import { applyContextMatching, applyDMP, applyGitFallback } from "../edit-strategies"
import { Hunk } from "../types" import { Hunk } from "../types"
const testCases = [ 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")
})
})

View File

@@ -155,7 +155,7 @@ export function applyDMP(
} }
// Git fallback strategy that works with full content // 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 let tmpDir: tmp.DirResult | undefined
try { try {