patch.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. import { describe, expect, test } from "bun:test"
  2. import { Patch } from "@opencode-ai/util/patch"
  3. import { Result } from "effect"
  4. const parse = (input: string) => Result.getOrThrow(Patch.parse(input))
  5. describe("Patch", () => {
  6. test("parses add, update, and delete hunks", () => {
  7. expect(
  8. parse(
  9. "*** Begin Patch\n*** Add File: add.txt\n+added\n*** Update File: update.txt\n@@ section\n-old\n+new\n*** Delete File: delete.txt\n*** End Patch",
  10. ),
  11. ).toEqual([
  12. { type: "add", path: "add.txt", contents: "added" },
  13. {
  14. type: "update",
  15. path: "update.txt",
  16. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: "section", endOfFile: undefined }],
  17. movePath: undefined,
  18. },
  19. { type: "delete", path: "delete.txt" },
  20. ])
  21. })
  22. test("parses an empty patch", () => {
  23. expect(parse("*** Begin Patch\n*** End Patch")).toEqual([])
  24. })
  25. test("ignores a Codex environment preamble", () => {
  26. expect(
  27. parse("*** Begin Patch\n*** Environment ID: remote\n*** Add File: file.txt\n+content\n*** End Patch"),
  28. ).toEqual([{ type: "add", path: "file.txt", contents: "content" }])
  29. })
  30. test("parses an update followed by an add", () => {
  31. expect(
  32. parse("*** Begin Patch\n*** Update File: update.txt\n@@\n+line\n*** Add File: add.txt\n+content\n*** End Patch"),
  33. ).toEqual([
  34. {
  35. type: "update",
  36. path: "update.txt",
  37. movePath: undefined,
  38. chunks: [{ oldLines: [], newLines: ["line"], changeContext: undefined }],
  39. },
  40. { type: "add", path: "add.txt", contents: "content" },
  41. ])
  42. })
  43. test("parses a file move", () => {
  44. expect(
  45. parse("*** Begin Patch\n*** Update File: old.txt\n*** Move to: new.txt\n@@\n-old\n+new\n*** End Patch"),
  46. ).toEqual([
  47. {
  48. type: "update",
  49. path: "old.txt",
  50. movePath: "new.txt",
  51. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }],
  52. },
  53. ])
  54. })
  55. test("identifies the missing patch boundary", () => {
  56. expect(() => parse("This is not a valid patch")).toThrow("The first line of the patch must be '*** Begin Patch'")
  57. expect(() => parse("*** Begin Patch\n*** Add File: add.txt\n+added")).toThrow(
  58. "The last line of the patch must be '*** End Patch'",
  59. )
  60. expect(() => parse("extra\n*** Begin Patch\n*** End Patch")).toThrow(
  61. "The first line of the patch must be '*** Begin Patch'",
  62. )
  63. expect(() => parse("*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\nextra")).toThrow(
  64. "The last line of the patch must be '*** End Patch'",
  65. )
  66. })
  67. test("allows whitespace after the end marker", () => {
  68. expect(parse("*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\n \t\n")).toEqual([
  69. { type: "add", path: "add.txt", contents: "added" },
  70. ])
  71. })
  72. test("strips a heredoc wrapper", () => {
  73. expect(parse("cat <<'EOF'\n*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\nEOF")).toEqual([
  74. { type: "add", path: "add.txt", contents: "added" },
  75. ])
  76. })
  77. test("strips a heredoc wrapper without cat", () => {
  78. expect(parse("<<EOF\n*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\nEOF")).toEqual([
  79. { type: "add", path: "add.txt", contents: "added" },
  80. ])
  81. })
  82. test("strips quoted heredoc wrappers", () => {
  83. const patch = "*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch"
  84. expect(parse(`<<'EOF'\n${patch}\nEOF`)).toEqual([{ type: "add", path: "add.txt", contents: "added" }])
  85. expect(parse(`<<\"EOF\"\n${patch}\nEOF`)).toEqual([{ type: "add", path: "add.txt", contents: "added" }])
  86. })
  87. test("rejects malformed heredoc wrappers", () => {
  88. const patch = "*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch"
  89. expect(() => parse(`<<\"EOF'\n${patch}\nEOF`)).toThrow("The first line of the patch must be '*** Begin Patch'")
  90. expect(() => parse("<<EOF\n*** Begin Patch\n*** Add File: add.txt\n+added\nEOF")).toThrow(
  91. "The last line of the patch must be '*** End Patch'",
  92. )
  93. })
  94. test("parses a whitespace-padded hunk header", () => {
  95. expect(parse("*** Begin Patch\n *** Update File: foo.txt\n@@\n-old\n+new\n*** End Patch")).toEqual([
  96. {
  97. type: "update",
  98. path: "foo.txt",
  99. movePath: undefined,
  100. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }],
  101. },
  102. ])
  103. })
  104. test("parses leading and trailing whitespace around patch markers", () => {
  105. expect(parse(" *** Begin Patch\n*** Update File: file.txt\n@@\n-one\n+two\n*** End Patch ")).toEqual([
  106. {
  107. type: "update",
  108. path: "file.txt",
  109. movePath: undefined,
  110. chunks: [{ oldLines: ["one"], newLines: ["two"], changeContext: undefined, endOfFile: undefined }],
  111. },
  112. ])
  113. })
  114. test("parses whitespace on the inner sides of patch marker lines", () => {
  115. expect(parse("*** Begin Patch \n*** Update File: file.txt\n@@\n-one\n+two\n *** End Patch")).toEqual([
  116. {
  117. type: "update",
  118. path: "file.txt",
  119. movePath: undefined,
  120. chunks: [{ oldLines: ["one"], newLines: ["two"], changeContext: undefined, endOfFile: undefined }],
  121. },
  122. ])
  123. })
  124. test("parses relative and absolute hunk paths", () => {
  125. expect(
  126. parse(
  127. "*** Begin Patch\n*** Add File: relative.txt\n+content\n*** Delete File: /tmp/delete.txt\n*** Update File: /tmp/update.txt\n@@\n-old\n+new\n*** End Patch",
  128. ),
  129. ).toEqual([
  130. { type: "add", path: "relative.txt", contents: "content" },
  131. { type: "delete", path: "/tmp/delete.txt" },
  132. {
  133. type: "update",
  134. path: "/tmp/update.txt",
  135. movePath: undefined,
  136. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined }],
  137. },
  138. ])
  139. })
  140. test("strips one carriage return from CRLF patch lines", () => {
  141. expect(parse("*** Begin Patch\r\n*** Update File: file.txt\r\n@@\r\n-old\r\n+new\r\n*** End Patch\r\n")).toEqual([
  142. {
  143. type: "update",
  144. path: "file.txt",
  145. movePath: undefined,
  146. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }],
  147. },
  148. ])
  149. })
  150. test("preserves an extra carriage return in CRLF patch lines", () => {
  151. expect(parse("*** Begin Patch\r\n*** Update File: file.txt\r\n@@\r\n-old\r\r\n+new\r\n*** End Patch\r\n")).toEqual([
  152. {
  153. type: "update",
  154. path: "file.txt",
  155. movePath: undefined,
  156. chunks: [{ oldLines: ["old\r"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }],
  157. },
  158. ])
  159. })
  160. test("preserves the end-of-file marker", () => {
  161. expect(parse("*** Begin Patch\n*** Update File: file.txt\n@@\n+quux\n*** End of File\n\n*** End Patch")).toEqual([
  162. {
  163. type: "update",
  164. path: "file.txt",
  165. movePath: undefined,
  166. chunks: [{ oldLines: [], newLines: ["quux"], changeContext: undefined, endOfFile: true }],
  167. },
  168. ])
  169. })
  170. test("allows an end-of-file marker before an explicit chunk", () => {
  171. expect(parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n@@\n-old\n+new\n*** End Patch")).toEqual(
  172. [
  173. {
  174. type: "update",
  175. path: "file.txt",
  176. movePath: undefined,
  177. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined }],
  178. },
  179. ],
  180. )
  181. })
  182. test("allows an end-of-file marker before an implicit chunk and move", () => {
  183. expect(parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n-old\n+new\n*** End Patch")).toEqual([
  184. {
  185. type: "update",
  186. path: "file.txt",
  187. movePath: undefined,
  188. chunks: [{ oldLines: ["old"], newLines: ["new"] }],
  189. },
  190. ])
  191. expect(
  192. parse(
  193. "*** Begin Patch\n*** Update File: old.txt\n*** End of File\n*** Move to: new.txt\n@@\n-old\n+new\n*** End Patch",
  194. ),
  195. ).toEqual([
  196. {
  197. type: "update",
  198. path: "old.txt",
  199. movePath: "new.txt",
  200. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined }],
  201. },
  202. ])
  203. })
  204. test("derives fuzzy line updates while preserving BOM", () => {
  205. const update = Patch.derive("update.txt", [{ oldLines: [" old "], newLines: ["new"] }], "\uFEFFold\n")
  206. expect(update).toEqual({ content: "new\n", bom: true })
  207. expect(Patch.joinBom(update.content, update.bom)).toBe("\uFEFFnew\n")
  208. })
  209. test("derives multiple update chunks", () => {
  210. expect(
  211. Patch.derive(
  212. "update.txt",
  213. [
  214. { oldLines: ["line 2"], newLines: ["LINE 2"] },
  215. { oldLines: ["line 4"], newLines: ["LINE 4"] },
  216. ],
  217. "line 1\nline 2\nline 3\nline 4\n",
  218. ).content,
  219. ).toBe("line 1\nLINE 2\nline 3\nLINE 4\n")
  220. })
  221. test("appends a pure-addition chunk to a nonempty file", () => {
  222. expect(
  223. Patch.derive("update.txt", [{ oldLines: [], newLines: ["added 1", "added 2"] }], "line 1\nline 2\n").content,
  224. ).toBe("line 1\nline 2\nadded 1\nadded 2\n")
  225. })
  226. test("applies a pure-addition chunk after an earlier replacement", () => {
  227. expect(
  228. Patch.derive(
  229. "update.txt",
  230. [
  231. { oldLines: [], newLines: ["after-context", "second-line"] },
  232. { oldLines: ["line1", "line2", "line3"], newLines: ["line1", "line2-replacement"] },
  233. ],
  234. "line1\nline2\nline3\n",
  235. ).content,
  236. ).toBe("line1\nline2-replacement\nafter-context\nsecond-line\n")
  237. })
  238. test("applies a deletion-only update chunk", () => {
  239. expect(
  240. Patch.derive(
  241. "update.txt",
  242. [{ oldLines: ["line1", "line2", "line3"], newLines: ["line1", "line3"] }],
  243. "line1\nline2\nline3\n",
  244. ).content,
  245. ).toBe("line1\nline3\n")
  246. })
  247. test("updates empty files and adds a trailing newline", () => {
  248. expect(Patch.derive("empty.txt", [{ oldLines: [], newLines: ["First line"] }], "").content).toBe("First line\n")
  249. expect(Patch.derive("no-newline.txt", [{ oldLines: ["old"], newLines: ["new"] }], "old").content).toBe("new\n")
  250. })
  251. test("disambiguates updates with change context", () => {
  252. expect(
  253. Patch.derive(
  254. "update.txt",
  255. [{ oldLines: ["x=10"], newLines: ["x=11"], changeContext: "fn b" }],
  256. "fn a\nx=10\nfn b\nx=10\n",
  257. ).content,
  258. ).toBe("fn a\nx=10\nfn b\nx=11\n")
  259. })
  260. test("matches leading, trailing, and Unicode punctuation differences", () => {
  261. expect(Patch.derive("leading.txt", [{ oldLines: ["line"], newLines: ["next"] }], " line\n").content).toBe("next\n")
  262. expect(Patch.derive("trailing.txt", [{ oldLines: ["line"], newLines: ["next"] }], "line \n").content).toBe(
  263. "next\n",
  264. )
  265. expect(
  266. Patch.derive("unicode.txt", [{ oldLines: ['He said "hello"'], newLines: ['He said "hi"'] }], "He said “hello”\n")
  267. .content,
  268. ).toBe('He said "hi"\n')
  269. })
  270. test("matches Unicode minus signs and spaces", () => {
  271. expect(
  272. Patch.derive("minus.txt", [{ oldLines: ["value - 1"], newLines: ["value - 2"] }], "value − 1\n").content,
  273. ).toBe("value - 2\n")
  274. const spaces = [
  275. "\u00A0",
  276. "\u2002",
  277. "\u2003",
  278. "\u2004",
  279. "\u2005",
  280. "\u2006",
  281. "\u2007",
  282. "\u2008",
  283. "\u2009",
  284. "\u200A",
  285. "\u202F",
  286. "\u205F",
  287. "\u3000",
  288. ]
  289. spaces.forEach((space) => {
  290. expect(
  291. Patch.derive("spaces.txt", [{ oldLines: ["hello world"], newLines: ["hello there"] }], `hello${space}world\n`)
  292. .content,
  293. ).toBe("hello there\n")
  294. })
  295. })
  296. test("does not normalize ellipses", () => {
  297. expect(() => Patch.derive("ellipsis.txt", [{ oldLines: ["wait..."], newLines: ["done"] }], "wait…\n")).toThrow(
  298. "Failed to find expected lines",
  299. )
  300. })
  301. test("prefers a later exact match over an earlier normalized match", () => {
  302. expect(
  303. Patch.derive(
  304. "quotes.txt",
  305. [{ oldLines: ['He said "hello"'], newLines: ['He said "goodbye"'] }],
  306. 'He said “hello”\nmiddle\nHe said "hello"\n',
  307. ).content,
  308. ).toBe('He said “hello”\nmiddle\nHe said "goodbye"\n')
  309. })
  310. test("matches EOF-anchored chunks from the end", () => {
  311. expect(
  312. Patch.derive(
  313. "update.txt",
  314. [{ oldLines: ["marker", "end"], newLines: ["marker changed", "end"], endOfFile: true }],
  315. "marker\nmiddle\nmarker\nend\n",
  316. ).content,
  317. ).toBe("marker\nmiddle\nmarker changed\nend\n")
  318. })
  319. test("does not fall back to a non-EOF match", () => {
  320. expect(() =>
  321. Patch.derive(
  322. "update.txt",
  323. [{ oldLines: ["marker", "end"], newLines: ["changed", "end"], endOfFile: true }],
  324. "marker\nend\nmiddle\n",
  325. ),
  326. ).toThrow("Failed to find expected lines")
  327. })
  328. test("identifies a missing blank line", () => {
  329. expect(() => Patch.derive("update.txt", [{ oldLines: [""], newLines: ["added"] }], "content\n")).toThrow(
  330. "Failed to find an expected blank line in update.txt",
  331. )
  332. })
  333. test("parses an update without an explicit first chunk header", () => {
  334. expect(parse("*** Begin Patch\n*** Update File: file.txt\n import foo\n+bar\n*** End Patch")).toEqual([
  335. {
  336. type: "update",
  337. path: "file.txt",
  338. movePath: undefined,
  339. chunks: [{ oldLines: ["import foo"], newLines: ["import foo", "bar"] }],
  340. },
  341. ])
  342. })
  343. test("keeps indented update markers as context lines", () => {
  344. expect(
  345. parse(
  346. "*** Begin Patch\n*** Update File: a.txt\n@@\n-old a\n+new a\n *** Update File: b.txt\n@@\n-old b\n+new b\n*** End Patch",
  347. ),
  348. ).toEqual([
  349. {
  350. type: "update",
  351. path: "a.txt",
  352. movePath: undefined,
  353. chunks: [
  354. {
  355. oldLines: ["old a", "*** Update File: b.txt"],
  356. newLines: ["new a", "*** Update File: b.txt"],
  357. changeContext: undefined,
  358. },
  359. { oldLines: ["old b"], newLines: ["new b"], changeContext: undefined },
  360. ],
  361. },
  362. ])
  363. })
  364. test("keeps indented move and EOF markers as context lines", () => {
  365. expect(
  366. parse(
  367. "*** Begin Patch\n*** Update File: file.txt\n@@\n before\n *** Move to: moved.txt\n *** End of File\n*** End Patch",
  368. ),
  369. ).toEqual([
  370. {
  371. type: "update",
  372. path: "file.txt",
  373. movePath: undefined,
  374. chunks: [
  375. {
  376. oldLines: ["before", "*** Move to: moved.txt", "*** End of File"],
  377. newLines: ["before", "*** Move to: moved.txt", "*** End of File"],
  378. changeContext: undefined,
  379. },
  380. ],
  381. },
  382. ])
  383. })
  384. test("preserves update context indentation", () => {
  385. expect(parse("*** Begin Patch\n*** Update File: file.txt\n@@ section\n-old\n+new\n*** End Patch")).toEqual([
  386. {
  387. type: "update",
  388. path: "file.txt",
  389. movePath: undefined,
  390. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: " section" }],
  391. },
  392. ])
  393. })
  394. test("preserves bare empty update lines as context", () => {
  395. expect(
  396. parse("*** Begin Patch\n*** Update File: file.txt\n@@\n context before\n\n context after\n*** End Patch"),
  397. ).toEqual([
  398. {
  399. type: "update",
  400. path: "file.txt",
  401. movePath: undefined,
  402. chunks: [
  403. {
  404. oldLines: ["context before", "", "context after"],
  405. newLines: ["context before", "", "context after"],
  406. changeContext: undefined,
  407. },
  408. ],
  409. },
  410. ])
  411. })
  412. test("rejects invalid add and delete lines", () => {
  413. expect(() => parse("*** Begin Patch\n*** Add File: file.txt\nbad\n*** End Patch")).toThrow(
  414. "Invalid hunk at line 3: Invalid Add File line for 'file.txt': expected a line starting with '+', got 'bad'",
  415. )
  416. expect(() => parse("*** Begin Patch\n*** Delete File: file.txt\nbad\n*** End Patch")).toThrow(
  417. "Invalid hunk at line 3: Unexpected line after Delete File 'file.txt': 'bad'. Delete hunks do not contain body lines",
  418. )
  419. expect(() =>
  420. parse("*** Begin Patch\n*** Delete File: file.txt\n*** Frobnicate File: next.txt\n*** End Patch"),
  421. ).toThrow("Invalid hunk at line 3: '*** Frobnicate File: next.txt' is not a valid hunk header")
  422. })
  423. test("rejects an empty update hunk", () => {
  424. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n*** End Patch")).toThrow(
  425. "Invalid hunk at line 2: Update file hunk for path 'file.txt' is empty",
  426. )
  427. expect(() =>
  428. parse(
  429. "*** Begin Patch\n*** Update File: old.txt\n*** Move to: new.txt\n*** Delete File: other.txt\n*** End Patch",
  430. ),
  431. ).toThrow("Invalid hunk at line 2: Update file hunk for path 'old.txt' is empty")
  432. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n*** End Patch")).toThrow(
  433. "Invalid hunk at line 2: Update file hunk for path 'file.txt' is empty",
  434. )
  435. })
  436. test("rejects an empty update chunk", () => {
  437. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** End Patch")).toThrow(
  438. "Invalid hunk at line 4: Update hunk does not contain any lines",
  439. )
  440. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** End of File\n*** End Patch")).toThrow(
  441. "Invalid hunk at line 4: Update hunk does not contain any lines",
  442. )
  443. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n@@\n-old\n+new\n*** End Patch")).toThrow(
  444. "Invalid hunk at line 4: Unexpected line found in update hunk: '@@'",
  445. )
  446. expect(() =>
  447. parse(
  448. "*** Begin Patch\n*** Update File: file.txt\n@@\n*** Update File: other.txt\n@@\n-old\n+new\n*** End Patch",
  449. ),
  450. ).toThrow("Invalid hunk at line 4: Unexpected line found in update hunk: '*** Update File: other.txt'")
  451. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\nbad\n*** End Patch")).toThrow(
  452. "Invalid hunk at line 4: Unexpected line found in update hunk: 'bad'",
  453. )
  454. })
  455. test("rejects an invalid update line", () => {
  456. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n-old\nbad\n*** End Patch")).toThrow(
  457. "Invalid hunk at line 5: Expected update hunk to start with a @@ context marker, got: 'bad'",
  458. )
  459. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@foo\n*** End Patch")).toThrow(
  460. "Invalid hunk at line 3: Unexpected line found in update hunk: '@@foo'",
  461. )
  462. expect(() =>
  463. parse("*** Begin Patch\n*** Update File: file.txt\n@@\n-old\n*** Frobnicate File: foo\n*** End Patch"),
  464. ).toThrow(
  465. "Invalid hunk at line 5: Expected update hunk to start with a @@ context marker, got: '*** Frobnicate File: foo'",
  466. )
  467. })
  468. test("rejects invalid and pathless hunk headers", () => {
  469. expect(() => parse("*** Begin Patch\n*** Frobnicate File: foo\n*** End Patch")).toThrow(
  470. "Invalid hunk at line 2: '*** Frobnicate File: foo' is not a valid hunk header",
  471. )
  472. expect(() => parse("*** Begin Patch\n*** Add File:\n*** End Patch")).toThrow(
  473. "Invalid hunk at line 2: '*** Add File:' is not a valid hunk header",
  474. )
  475. for (const header of ["*** Add File: ", "*** Delete File: ", "*** Update File: "]) {
  476. expect(() => parse(`*** Begin Patch\n${header}\n*** End Patch`)).toThrow(
  477. `Invalid hunk at line 2: '${header.trim()}' is not a valid hunk header`,
  478. )
  479. }
  480. expect(() =>
  481. parse("*** Begin Patch\n*** Update File: old.txt\n*** Move to: \n@@\n-old\n+new\n*** End Patch"),
  482. ).toThrow("Invalid hunk at line 3: Move destination for 'old.txt' must not be empty")
  483. })
  484. })