Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion sdk/src/__tests__/apply-patch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'bun:test'
import path from 'node:path'

import { createMockFs } from '@codebuff/common/testing/mocks/filesystem'
import { describe, expect, test } from 'bun:test'

import { applyPatchTool } from '../tools/apply-patch'

Expand Down Expand Up @@ -187,6 +188,31 @@ describe('applyPatchTool', () => {
expect(updated).toBe(['before', 'target', 'inserted', 'after', ''].join('\n'))
})

test('rejects a missing anchor without editing matching context elsewhere', async () => {
const original = 'function other() {\n return false\n}\n'
const cwd = path.resolve('/repo')
const file = path.join(cwd, 'src/file.ts')
const fs = createMockFs({ files: { [file]: original } })

const result = await applyPatchTool({
parameters: {
operation: {
type: 'update_file',
path: 'src/file.ts',
diff: '@@ function missing() {\n- return false\n+ return true\n',
},
},
cwd,
fs,
})

expect(result[0]).toMatchObject({
type: 'json',
value: { errorMessage: expect.stringContaining('function missing() {') },
})
expect(await fs.readFile(file, 'utf-8')).toBe(original)
})

test('applies update patch when file has CRLF line endings', async () => {
const fs = createMockFs({
files: {
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/tools/apply-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ function advanceCursorToAnchor(
break
}
}
if (!found) {
throw new Error(`Anchor not found: ${anchor}`)
}
}

return cursor
Expand Down
Loading