patch.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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(
  162. parse(
  163. "*** Begin Patch\n*** Update File: file.txt\n@@\n+quux\n*** End of File\n\n*** End Patch",
  164. ),
  165. ).toEqual([
  166. {
  167. type: "update",
  168. path: "file.txt",
  169. movePath: undefined,
  170. chunks: [{ oldLines: [], newLines: ["quux"], changeContext: undefined, endOfFile: true }],
  171. },
  172. ])
  173. })
  174. test("allows an end-of-file marker before an explicit chunk", () => {
  175. expect(
  176. parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n@@\n-old\n+new\n*** End Patch"),
  177. ).toEqual([
  178. {
  179. type: "update",
  180. path: "file.txt",
  181. movePath: undefined,
  182. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined }],
  183. },
  184. ])
  185. })
  186. test("allows an end-of-file marker before an implicit chunk and move", () => {
  187. expect(parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n-old\n+new\n*** End Patch")).toEqual([
  188. {
  189. type: "update",
  190. path: "file.txt",
  191. movePath: undefined,
  192. chunks: [{ oldLines: ["old"], newLines: ["new"] }],
  193. },
  194. ])
  195. expect(
  196. parse(
  197. "*** Begin Patch\n*** Update File: old.txt\n*** End of File\n*** Move to: new.txt\n@@\n-old\n+new\n*** End Patch",
  198. ),
  199. ).toEqual([
  200. {
  201. type: "update",
  202. path: "old.txt",
  203. movePath: "new.txt",
  204. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined }],
  205. },
  206. ])
  207. })
  208. test("derives fuzzy line updates while preserving BOM", () => {
  209. const update = Patch.derive("update.txt", [{ oldLines: [" old "], newLines: ["new"] }], "\uFEFFold\n")
  210. expect(update).toEqual({ content: "new\n", bom: true })
  211. expect(Patch.joinBom(update.content, update.bom)).toBe("\uFEFFnew\n")
  212. })
  213. test("derives multiple update chunks", () => {
  214. expect(
  215. Patch.derive(
  216. "update.txt",
  217. [
  218. { oldLines: ["line 2"], newLines: ["LINE 2"] },
  219. { oldLines: ["line 4"], newLines: ["LINE 4"] },
  220. ],
  221. "line 1\nline 2\nline 3\nline 4\n",
  222. ).content,
  223. ).toBe("line 1\nLINE 2\nline 3\nLINE 4\n")
  224. })
  225. test("appends a pure-addition chunk to a nonempty file", () => {
  226. expect(Patch.derive("update.txt", [{ oldLines: [], newLines: ["added 1", "added 2"] }], "line 1\nline 2\n").content).toBe(
  227. "line 1\nline 2\nadded 1\nadded 2\n",
  228. )
  229. })
  230. test("applies a pure-addition chunk after an earlier replacement", () => {
  231. expect(
  232. Patch.derive(
  233. "update.txt",
  234. [
  235. { oldLines: [], newLines: ["after-context", "second-line"] },
  236. { oldLines: ["line1", "line2", "line3"], newLines: ["line1", "line2-replacement"] },
  237. ],
  238. "line1\nline2\nline3\n",
  239. ).content,
  240. ).toBe("line1\nline2-replacement\nafter-context\nsecond-line\n")
  241. })
  242. test("applies a deletion-only update chunk", () => {
  243. expect(
  244. Patch.derive(
  245. "update.txt",
  246. [{ oldLines: ["line1", "line2", "line3"], newLines: ["line1", "line3"] }],
  247. "line1\nline2\nline3\n",
  248. ).content,
  249. ).toBe("line1\nline3\n")
  250. })
  251. test("updates empty files and adds a trailing newline", () => {
  252. expect(Patch.derive("empty.txt", [{ oldLines: [], newLines: ["First line"] }], "").content).toBe("First line\n")
  253. expect(Patch.derive("no-newline.txt", [{ oldLines: ["old"], newLines: ["new"] }], "old").content).toBe("new\n")
  254. })
  255. test("disambiguates updates with change context", () => {
  256. expect(
  257. Patch.derive(
  258. "update.txt",
  259. [{ oldLines: ["x=10"], newLines: ["x=11"], changeContext: "fn b" }],
  260. "fn a\nx=10\nfn b\nx=10\n",
  261. ).content,
  262. ).toBe("fn a\nx=10\nfn b\nx=11\n")
  263. })
  264. test("matches leading, trailing, and Unicode punctuation differences", () => {
  265. expect(Patch.derive("leading.txt", [{ oldLines: ["line"], newLines: ["next"] }], " line\n").content).toBe("next\n")
  266. expect(Patch.derive("trailing.txt", [{ oldLines: ["line"], newLines: ["next"] }], "line \n").content).toBe(
  267. "next\n",
  268. )
  269. expect(
  270. Patch.derive("unicode.txt", [{ oldLines: ['He said "hello"'], newLines: ['He said "hi"'] }], "He said “hello”\n")
  271. .content,
  272. ).toBe('He said "hi"\n')
  273. })
  274. test("matches Unicode minus signs and spaces", () => {
  275. expect(
  276. Patch.derive("minus.txt", [{ oldLines: ["value - 1"], newLines: ["value - 2"] }], "value − 1\n")
  277. .content,
  278. ).toBe("value - 2\n")
  279. const spaces = ["\u00A0", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006", "\u2007", "\u2008", "\u2009", "\u200A", "\u202F", "\u205F", "\u3000"]
  280. spaces.forEach(
  281. (space) => {
  282. expect(
  283. Patch.derive(
  284. "spaces.txt",
  285. [{ oldLines: ["hello world"], newLines: ["hello there"] }],
  286. `hello${space}world\n`,
  287. ).content,
  288. ).toBe("hello there\n")
  289. },
  290. )
  291. })
  292. test("does not normalize ellipses", () => {
  293. expect(() =>
  294. Patch.derive("ellipsis.txt", [{ oldLines: ["wait..."], newLines: ["done"] }], "wait…\n"),
  295. ).toThrow("Failed to find expected lines")
  296. })
  297. test("prefers a later exact match over an earlier normalized match", () => {
  298. expect(
  299. Patch.derive(
  300. "quotes.txt",
  301. [{ oldLines: ['He said "hello"'], newLines: ['He said "goodbye"'] }],
  302. 'He said “hello”\nmiddle\nHe said "hello"\n',
  303. ).content,
  304. ).toBe('He said “hello”\nmiddle\nHe said "goodbye"\n')
  305. })
  306. test("matches EOF-anchored chunks from the end", () => {
  307. expect(
  308. Patch.derive(
  309. "update.txt",
  310. [{ oldLines: ["marker", "end"], newLines: ["marker changed", "end"], endOfFile: true }],
  311. "marker\nmiddle\nmarker\nend\n",
  312. ).content,
  313. ).toBe("marker\nmiddle\nmarker changed\nend\n")
  314. })
  315. test("does not fall back to a non-EOF match", () => {
  316. expect(() =>
  317. Patch.derive(
  318. "update.txt",
  319. [{ oldLines: ["marker", "end"], newLines: ["changed", "end"], endOfFile: true }],
  320. "marker\nend\nmiddle\n",
  321. ),
  322. ).toThrow("Failed to find expected lines")
  323. })
  324. test("identifies a missing blank line", () => {
  325. expect(() =>
  326. Patch.derive("update.txt", [{ oldLines: [""], newLines: ["added"] }], "content\n"),
  327. ).toThrow("Failed to find an expected blank line in update.txt")
  328. })
  329. test("parses an update without an explicit first chunk header", () => {
  330. expect(parse("*** Begin Patch\n*** Update File: file.txt\n import foo\n+bar\n*** End Patch")).toEqual([
  331. {
  332. type: "update",
  333. path: "file.txt",
  334. movePath: undefined,
  335. chunks: [{ oldLines: ["import foo"], newLines: ["import foo", "bar"] }],
  336. },
  337. ])
  338. })
  339. test("keeps indented update markers as context lines", () => {
  340. expect(
  341. parse(
  342. "*** 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",
  343. ),
  344. ).toEqual([
  345. {
  346. type: "update",
  347. path: "a.txt",
  348. movePath: undefined,
  349. chunks: [
  350. {
  351. oldLines: ["old a", "*** Update File: b.txt"],
  352. newLines: ["new a", "*** Update File: b.txt"],
  353. changeContext: undefined,
  354. },
  355. { oldLines: ["old b"], newLines: ["new b"], changeContext: undefined },
  356. ],
  357. },
  358. ])
  359. })
  360. test("keeps indented move and EOF markers as context lines", () => {
  361. expect(
  362. parse(
  363. "*** Begin Patch\n*** Update File: file.txt\n@@\n before\n *** Move to: moved.txt\n *** End of File\n*** End Patch",
  364. ),
  365. ).toEqual([
  366. {
  367. type: "update",
  368. path: "file.txt",
  369. movePath: undefined,
  370. chunks: [
  371. {
  372. oldLines: ["before", "*** Move to: moved.txt", "*** End of File"],
  373. newLines: ["before", "*** Move to: moved.txt", "*** End of File"],
  374. changeContext: undefined,
  375. },
  376. ],
  377. },
  378. ])
  379. })
  380. test("preserves update context indentation", () => {
  381. expect(parse("*** Begin Patch\n*** Update File: file.txt\n@@ section\n-old\n+new\n*** End Patch")).toEqual([
  382. {
  383. type: "update",
  384. path: "file.txt",
  385. movePath: undefined,
  386. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: " section" }],
  387. },
  388. ])
  389. })
  390. test("preserves bare empty update lines as context", () => {
  391. expect(
  392. parse("*** Begin Patch\n*** Update File: file.txt\n@@\n context before\n\n context after\n*** End Patch"),
  393. ).toEqual([
  394. {
  395. type: "update",
  396. path: "file.txt",
  397. movePath: undefined,
  398. chunks: [
  399. {
  400. oldLines: ["context before", "", "context after"],
  401. newLines: ["context before", "", "context after"],
  402. changeContext: undefined,
  403. },
  404. ],
  405. },
  406. ])
  407. })
  408. test("rejects invalid add and delete lines", () => {
  409. expect(() => parse("*** Begin Patch\n*** Add File: file.txt\nbad\n*** End Patch")).toThrow(
  410. "Invalid hunk at line 3: Invalid Add File line for 'file.txt': expected a line starting with '+', got 'bad'",
  411. )
  412. expect(() => parse("*** Begin Patch\n*** Delete File: file.txt\nbad\n*** End Patch")).toThrow(
  413. "Invalid hunk at line 3: Unexpected line after Delete File 'file.txt': 'bad'. Delete hunks do not contain body lines",
  414. )
  415. expect(() =>
  416. parse("*** Begin Patch\n*** Delete File: file.txt\n*** Frobnicate File: next.txt\n*** End Patch"),
  417. ).toThrow("Invalid hunk at line 3: '*** Frobnicate File: next.txt' is not a valid hunk header")
  418. })
  419. test("rejects an empty update hunk", () => {
  420. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n*** End Patch")).toThrow(
  421. "Invalid hunk at line 2: Update file hunk for path 'file.txt' is empty",
  422. )
  423. expect(() =>
  424. parse(
  425. "*** Begin Patch\n*** Update File: old.txt\n*** Move to: new.txt\n*** Delete File: other.txt\n*** End Patch",
  426. ),
  427. ).toThrow("Invalid hunk at line 2: Update file hunk for path 'old.txt' is empty")
  428. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n*** End Patch")).toThrow(
  429. "Invalid hunk at line 2: Update file hunk for path 'file.txt' is empty",
  430. )
  431. })
  432. test("rejects an empty update chunk", () => {
  433. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** End Patch")).toThrow(
  434. "Invalid hunk at line 4: Update hunk does not contain any lines",
  435. )
  436. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** End of File\n*** End Patch")).toThrow(
  437. "Invalid hunk at line 4: Update hunk does not contain any lines",
  438. )
  439. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n@@\n-old\n+new\n*** End Patch")).toThrow(
  440. "Invalid hunk at line 4: Unexpected line found in update hunk: '@@'",
  441. )
  442. expect(() =>
  443. parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** Update File: other.txt\n@@\n-old\n+new\n*** End Patch"),
  444. ).toThrow("Invalid hunk at line 4: Unexpected line found in update hunk: '*** Update File: other.txt'")
  445. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\nbad\n*** End Patch")).toThrow(
  446. "Invalid hunk at line 4: Unexpected line found in update hunk: 'bad'",
  447. )
  448. })
  449. test("rejects an invalid update line", () => {
  450. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n-old\nbad\n*** End Patch")).toThrow(
  451. "Invalid hunk at line 5: Expected update hunk to start with a @@ context marker, got: 'bad'",
  452. )
  453. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@foo\n*** End Patch")).toThrow(
  454. "Invalid hunk at line 3: Unexpected line found in update hunk: '@@foo'",
  455. )
  456. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n-old\n*** Frobnicate File: foo\n*** End Patch")).toThrow(
  457. "Invalid hunk at line 5: Expected update hunk to start with a @@ context marker, got: '*** Frobnicate File: foo'",
  458. )
  459. })
  460. test("rejects invalid and pathless hunk headers", () => {
  461. expect(() => parse("*** Begin Patch\n*** Frobnicate File: foo\n*** End Patch")).toThrow(
  462. "Invalid hunk at line 2: '*** Frobnicate File: foo' is not a valid hunk header",
  463. )
  464. expect(() => parse("*** Begin Patch\n*** Add File:\n*** End Patch")).toThrow(
  465. "Invalid hunk at line 2: '*** Add File:' is not a valid hunk header",
  466. )
  467. for (const header of ["*** Add File: ", "*** Delete File: ", "*** Update File: "]) {
  468. expect(() => parse(`*** Begin Patch\n${header}\n*** End Patch`)).toThrow(
  469. `Invalid hunk at line 2: '${header.trim()}' is not a valid hunk header`,
  470. )
  471. }
  472. expect(() =>
  473. parse("*** Begin Patch\n*** Update File: old.txt\n*** Move to: \n@@\n-old\n+new\n*** End Patch"),
  474. ).toThrow("Invalid hunk at line 3: Move destination for 'old.txt' must not be empty")
  475. })
  476. })