Skip to content

Commit 82a0ffe

Browse files
committed
feat: add tests for the git fallback strategy
1 parent 5a35d7f commit 82a0ffe

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/core/diff/strategies/new-unified/__tests__/edit-strategies.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { applyContextMatching, applyDMP } from "../edit-strategies"
1+
import { applyContextMatching, applyDMP, applyGitFallback } from "../edit-strategies"
22
import { Hunk } from "../types"
33

44
const testCases = [
@@ -257,3 +257,39 @@ describe("applyDMP", () => {
257257
})
258258
})
259259
})
260+
261+
describe("applyGitFallback", () => {
262+
it("should successfully apply changes using git operations", async () => {
263+
const hunk = {
264+
changes: [
265+
{ type: "context", content: "line1", indent: "" },
266+
{ type: "remove", content: "line2", indent: "" },
267+
{ type: "add", content: "new line2", indent: "" },
268+
{ type: "context", content: "line3", indent: "" }
269+
]
270+
} as Hunk
271+
272+
const content = ["line1", "line2", "line3"]
273+
const result = await applyGitFallback(hunk, content)
274+
275+
expect(result.result.join("\n")).toEqual("line1\nnew line2\nline3")
276+
expect(result.confidence).toBe(1)
277+
expect(result.strategy).toBe("git-fallback")
278+
})
279+
280+
it("should return original content with 0 confidence when changes cannot be applied", async () => {
281+
const hunk = {
282+
changes: [
283+
{ type: "context", content: "nonexistent", indent: "" },
284+
{ type: "add", content: "new line", indent: "" }
285+
]
286+
} as Hunk
287+
288+
const content = ["line1", "line2", "line3"]
289+
const result = await applyGitFallback(hunk, content)
290+
291+
expect(result.result).toEqual(content)
292+
expect(result.confidence).toBe(0)
293+
expect(result.strategy).toBe("git-fallback")
294+
})
295+
})

src/core/diff/strategies/new-unified/edit-strategies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export function applyDMP(
155155
}
156156

157157
// Git fallback strategy that works with full content
158-
async function applyGitFallback(hunk: Hunk, content: string[]): Promise<EditResult> {
158+
export async function applyGitFallback(hunk: Hunk, content: string[]): Promise<EditResult> {
159159
let tmpDir: tmp.DirResult | undefined
160160

161161
try {

0 commit comments

Comments
 (0)