File size: 1,560 Bytes
b1df986 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import assert from 'node:assert/strict'
import test from 'node:test'
import { applyPatchSetToCode, parsePatchResponse } from '../code-retry/utils'
test('parses multiple hunks inside one patch block', () => {
const result = parsePatchResponse(`[[PATCH]]
[[SEARCH]]
const a = 1
[[REPLACE]]
const a = 2
[[SEARCH]]
const b = 1
[[REPLACE]]
const b = 2
[[END]]`)
assert.equal(result.patches.length, 2)
assert.equal(
applyPatchSetToCode('const a = 1\nconst b = 1', result),
'const a = 2\nconst b = 2'
)
})
test('parses multiple patch blocks in one response', () => {
const result = parsePatchResponse(`[[PATCH]]
[[SEARCH]]
const a = 1
[[REPLACE]]
const a = 2
[[END]]
[[PATCH]]
[[SEARCH]]
const b = 1
[[REPLACE]]
const b = 2
[[END]]`)
assert.equal(result.patches.length, 2)
assert.equal(
applyPatchSetToCode('const a = 1\nconst b = 1', result),
'const a = 2\nconst b = 2'
)
})
test('rejects a control marker nested inside SEARCH content', () => {
assert.throws(
() => parsePatchResponse(`[[PATCH]]
[[SEARCH]]
const a = 1
[[PATCH]]
[[REPLACE]]
const a = 2
[[END]]`),
/nested \[\[PATCH\]\] marker/
)
})
test('rejects a response without a PATCH block', () => {
assert.throws(
() => parsePatchResponse(`[[SEARCH]]
const a = 1
[[REPLACE]]
const a = 2
[[END]]`),
/did not contain any \[\[PATCH\]\] blocks/
)
})
test('rejects a patch that changes nothing', () => {
assert.throws(
() => parsePatchResponse(`[[PATCH]]
[[SEARCH]]
const a = 1
[[REPLACE]]
const a = 1
[[END]]`),
/produced no change/
)
})
|