patch.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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("updates empty files and adds a trailing newline", () => {
  226. expect(Patch.derive("empty.txt", [{ oldLines: [], newLines: ["First line"] }], "").content).toBe("First line\n")
  227. expect(Patch.derive("no-newline.txt", [{ oldLines: ["old"], newLines: ["new"] }], "old").content).toBe("new\n")
  228. })
  229. test("disambiguates updates with change context", () => {
  230. expect(
  231. Patch.derive(
  232. "update.txt",
  233. [{ oldLines: ["x=10"], newLines: ["x=11"], changeContext: "fn b" }],
  234. "fn a\nx=10\nfn b\nx=10\n",
  235. ).content,
  236. ).toBe("fn a\nx=10\nfn b\nx=11\n")
  237. })
  238. test("matches leading, trailing, and Unicode punctuation differences", () => {
  239. expect(Patch.derive("leading.txt", [{ oldLines: ["line"], newLines: ["next"] }], " line\n").content).toBe("next\n")
  240. expect(Patch.derive("trailing.txt", [{ oldLines: ["line"], newLines: ["next"] }], "line \n").content).toBe(
  241. "next\n",
  242. )
  243. expect(
  244. Patch.derive("unicode.txt", [{ oldLines: ['He said "hello"'], newLines: ['He said "hi"'] }], "He said “hello”\n")
  245. .content,
  246. ).toBe('He said "hi"\n')
  247. })
  248. test("matches Unicode minus signs and spaces", () => {
  249. expect(
  250. Patch.derive("minus.txt", [{ oldLines: ["value - 1"], newLines: ["value - 2"] }], "value − 1\n")
  251. .content,
  252. ).toBe("value - 2\n")
  253. const spaces = ["\u00A0", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006", "\u2007", "\u2008", "\u2009", "\u200A", "\u202F", "\u205F", "\u3000"]
  254. spaces.forEach(
  255. (space) => {
  256. expect(
  257. Patch.derive(
  258. "spaces.txt",
  259. [{ oldLines: ["hello world"], newLines: ["hello there"] }],
  260. `hello${space}world\n`,
  261. ).content,
  262. ).toBe("hello there\n")
  263. },
  264. )
  265. })
  266. test("does not normalize ellipses", () => {
  267. expect(() =>
  268. Patch.derive("ellipsis.txt", [{ oldLines: ["wait..."], newLines: ["done"] }], "wait…\n"),
  269. ).toThrow("Failed to find expected lines")
  270. })
  271. test("prefers a later exact match over an earlier normalized match", () => {
  272. expect(
  273. Patch.derive(
  274. "quotes.txt",
  275. [{ oldLines: ['He said "hello"'], newLines: ['He said "goodbye"'] }],
  276. 'He said “hello”\nmiddle\nHe said "hello"\n',
  277. ).content,
  278. ).toBe('He said “hello”\nmiddle\nHe said "goodbye"\n')
  279. })
  280. test("matches EOF-anchored chunks from the end", () => {
  281. expect(
  282. Patch.derive(
  283. "update.txt",
  284. [{ oldLines: ["marker", "end"], newLines: ["marker changed", "end"], endOfFile: true }],
  285. "marker\nmiddle\nmarker\nend\n",
  286. ).content,
  287. ).toBe("marker\nmiddle\nmarker changed\nend\n")
  288. })
  289. test("does not fall back to a non-EOF match", () => {
  290. expect(() =>
  291. Patch.derive(
  292. "update.txt",
  293. [{ oldLines: ["marker", "end"], newLines: ["changed", "end"], endOfFile: true }],
  294. "marker\nend\nmiddle\n",
  295. ),
  296. ).toThrow("Failed to find expected lines")
  297. })
  298. test("parses an update without an explicit first chunk header", () => {
  299. expect(parse("*** Begin Patch\n*** Update File: file.txt\n import foo\n+bar\n*** End Patch")).toEqual([
  300. {
  301. type: "update",
  302. path: "file.txt",
  303. movePath: undefined,
  304. chunks: [{ oldLines: ["import foo"], newLines: ["import foo", "bar"] }],
  305. },
  306. ])
  307. })
  308. test("keeps indented update markers as context lines", () => {
  309. expect(
  310. parse(
  311. "*** 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",
  312. ),
  313. ).toEqual([
  314. {
  315. type: "update",
  316. path: "a.txt",
  317. movePath: undefined,
  318. chunks: [
  319. {
  320. oldLines: ["old a", "*** Update File: b.txt"],
  321. newLines: ["new a", "*** Update File: b.txt"],
  322. changeContext: undefined,
  323. },
  324. { oldLines: ["old b"], newLines: ["new b"], changeContext: undefined },
  325. ],
  326. },
  327. ])
  328. })
  329. test("keeps indented move and EOF markers as context lines", () => {
  330. expect(
  331. parse(
  332. "*** Begin Patch\n*** Update File: file.txt\n@@\n before\n *** Move to: moved.txt\n *** End of File\n*** End Patch",
  333. ),
  334. ).toEqual([
  335. {
  336. type: "update",
  337. path: "file.txt",
  338. movePath: undefined,
  339. chunks: [
  340. {
  341. oldLines: ["before", "*** Move to: moved.txt", "*** End of File"],
  342. newLines: ["before", "*** Move to: moved.txt", "*** End of File"],
  343. changeContext: undefined,
  344. },
  345. ],
  346. },
  347. ])
  348. })
  349. test("preserves update context indentation", () => {
  350. expect(parse("*** Begin Patch\n*** Update File: file.txt\n@@ section\n-old\n+new\n*** End Patch")).toEqual([
  351. {
  352. type: "update",
  353. path: "file.txt",
  354. movePath: undefined,
  355. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: " section" }],
  356. },
  357. ])
  358. })
  359. test("preserves bare empty update lines as context", () => {
  360. expect(
  361. parse("*** Begin Patch\n*** Update File: file.txt\n@@\n context before\n\n context after\n*** End Patch"),
  362. ).toEqual([
  363. {
  364. type: "update",
  365. path: "file.txt",
  366. movePath: undefined,
  367. chunks: [
  368. {
  369. oldLines: ["context before", "", "context after"],
  370. newLines: ["context before", "", "context after"],
  371. changeContext: undefined,
  372. },
  373. ],
  374. },
  375. ])
  376. })
  377. test("rejects invalid add and delete lines", () => {
  378. expect(() => parse("*** Begin Patch\n*** Add File: file.txt\nbad\n*** End Patch")).toThrow(
  379. "Invalid hunk at line 3: 'bad' is not a valid hunk header",
  380. )
  381. expect(() => parse("*** Begin Patch\n*** Delete File: file.txt\nbad\n*** End Patch")).toThrow(
  382. "Invalid hunk at line 3: 'bad' is not a valid hunk header",
  383. )
  384. })
  385. test("rejects an empty update hunk", () => {
  386. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n*** End Patch")).toThrow(
  387. "Invalid hunk at line 2: Update file hunk for path 'file.txt' is empty",
  388. )
  389. expect(() =>
  390. parse(
  391. "*** Begin Patch\n*** Update File: old.txt\n*** Move to: new.txt\n*** Delete File: other.txt\n*** End Patch",
  392. ),
  393. ).toThrow("Invalid hunk at line 2: Update file hunk for path 'old.txt' is empty")
  394. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n*** End Patch")).toThrow(
  395. "Invalid hunk at line 2: Update file hunk for path 'file.txt' is empty",
  396. )
  397. })
  398. test("rejects an empty update chunk", () => {
  399. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** End Patch")).toThrow(
  400. "Invalid hunk at line 4: Update hunk does not contain any lines",
  401. )
  402. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** End of File\n*** End Patch")).toThrow(
  403. "Invalid hunk at line 4: Update hunk does not contain any lines",
  404. )
  405. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n@@\n-old\n+new\n*** End Patch")).toThrow(
  406. "Invalid hunk at line 4: Unexpected line found in update hunk: '@@'",
  407. )
  408. expect(() =>
  409. parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** Update File: other.txt\n@@\n-old\n+new\n*** End Patch"),
  410. ).toThrow("Invalid hunk at line 4: Unexpected line found in update hunk: '*** Update File: other.txt'")
  411. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\nbad\n*** End Patch")).toThrow(
  412. "Invalid hunk at line 4: Unexpected line found in update hunk: 'bad'",
  413. )
  414. })
  415. test("rejects an invalid update line", () => {
  416. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n-old\nbad\n*** End Patch")).toThrow(
  417. "Invalid hunk at line 5: Expected update hunk to start with a @@ context marker, got: 'bad'",
  418. )
  419. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@foo\n*** End Patch")).toThrow(
  420. "Invalid hunk at line 3: Unexpected line found in update hunk: '@@foo'",
  421. )
  422. expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n-old\n*** Frobnicate File: foo\n*** End Patch")).toThrow(
  423. "Invalid hunk at line 5: Expected update hunk to start with a @@ context marker, got: '*** Frobnicate File: foo'",
  424. )
  425. })
  426. test("rejects invalid and pathless hunk headers", () => {
  427. expect(() => parse("*** Begin Patch\n*** Frobnicate File: foo\n*** End Patch")).toThrow(
  428. "Invalid hunk at line 2: '*** Frobnicate File: foo' is not a valid hunk header",
  429. )
  430. expect(() => parse("*** Begin Patch\n*** Add File:\n*** End Patch")).toThrow(
  431. "Invalid hunk at line 2: '*** Add File:' is not a valid hunk header",
  432. )
  433. for (const header of ["*** Add File: ", "*** Delete File: ", "*** Update File: "]) {
  434. expect(() => parse(`*** Begin Patch\n${header}\n*** End Patch`)).toThrow(
  435. `Invalid hunk at line 2: '${header.trim()}' is not a valid hunk header`,
  436. )
  437. }
  438. expect(() =>
  439. parse("*** Begin Patch\n*** Update File: old.txt\n*** Move to: \n@@\n-old\n+new\n*** End Patch"),
  440. ).toThrow("Invalid hunk at line 3: '*** Move to:' is not a valid hunk header")
  441. })
  442. })