patch.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 a file move", () => {
  23. expect(
  24. parse("*** Begin Patch\n*** Update File: old.txt\n*** Move to: new.txt\n@@\n-old\n+new\n*** End Patch"),
  25. ).toEqual([
  26. {
  27. type: "update",
  28. path: "old.txt",
  29. movePath: "new.txt",
  30. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }],
  31. },
  32. ])
  33. })
  34. test("identifies the missing patch boundary", () => {
  35. expect(() => parse("This is not a valid patch")).toThrow("The first line of the patch must be '*** Begin Patch'")
  36. expect(() => parse("*** Begin Patch\n*** Add File: add.txt\n+added")).toThrow(
  37. "The last line of the patch must be '*** End Patch'",
  38. )
  39. expect(() => parse("extra\n*** Begin Patch\n*** End Patch")).toThrow(
  40. "The first line of the patch must be '*** Begin Patch'",
  41. )
  42. expect(() => parse("*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\nextra")).toThrow(
  43. "The last line of the patch must be '*** End Patch'",
  44. )
  45. })
  46. test("allows whitespace after the end marker", () => {
  47. expect(parse("*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\n \t\n")).toEqual([
  48. { type: "add", path: "add.txt", contents: "added" },
  49. ])
  50. })
  51. test("strips a heredoc wrapper", () => {
  52. expect(parse("cat <<'EOF'\n*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\nEOF")).toEqual([
  53. { type: "add", path: "add.txt", contents: "added" },
  54. ])
  55. })
  56. test("strips a heredoc wrapper without cat", () => {
  57. expect(parse("<<EOF\n*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\nEOF")).toEqual([
  58. { type: "add", path: "add.txt", contents: "added" },
  59. ])
  60. })
  61. test("parses a whitespace-padded hunk header", () => {
  62. expect(parse("*** Begin Patch\n *** Update File: foo.txt\n@@\n-old\n+new\n*** End Patch")).toEqual([
  63. {
  64. type: "update",
  65. path: "foo.txt",
  66. movePath: undefined,
  67. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }],
  68. },
  69. ])
  70. })
  71. test("parses leading and trailing whitespace around patch markers", () => {
  72. expect(parse(" *** Begin Patch\n*** Update File: file.txt\n@@\n-one\n+two\n*** End Patch ")).toEqual([
  73. {
  74. type: "update",
  75. path: "file.txt",
  76. movePath: undefined,
  77. chunks: [{ oldLines: ["one"], newLines: ["two"], changeContext: undefined, endOfFile: undefined }],
  78. },
  79. ])
  80. })
  81. test("parses whitespace on the inner sides of patch marker lines", () => {
  82. expect(parse("*** Begin Patch \n*** Update File: file.txt\n@@\n-one\n+two\n *** End Patch")).toEqual([
  83. {
  84. type: "update",
  85. path: "file.txt",
  86. movePath: undefined,
  87. chunks: [{ oldLines: ["one"], newLines: ["two"], changeContext: undefined, endOfFile: undefined }],
  88. },
  89. ])
  90. })
  91. test("strips one carriage return from CRLF patch lines", () => {
  92. 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([
  93. {
  94. type: "update",
  95. path: "file.txt",
  96. movePath: undefined,
  97. chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }],
  98. },
  99. ])
  100. })
  101. test("preserves an extra carriage return in CRLF patch lines", () => {
  102. 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([
  103. {
  104. type: "update",
  105. path: "file.txt",
  106. movePath: undefined,
  107. chunks: [{ oldLines: ["old\r"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }],
  108. },
  109. ])
  110. })
  111. test("preserves the end-of-file marker", () => {
  112. expect(
  113. parse(
  114. "*** Begin Patch\n*** Update File: file.txt\n@@\n+quux\n*** End of File\n\n*** End Patch",
  115. ),
  116. ).toEqual([
  117. {
  118. type: "update",
  119. path: "file.txt",
  120. movePath: undefined,
  121. chunks: [{ oldLines: [], newLines: ["quux"], changeContext: undefined, endOfFile: true }],
  122. },
  123. ])
  124. })
  125. test("derives fuzzy line updates while preserving BOM", () => {
  126. const update = Patch.derive("update.txt", [{ oldLines: [" old "], newLines: ["new"] }], "\uFEFFold\n")
  127. expect(update).toEqual({ content: "new\n", bom: true })
  128. expect(Patch.joinBom(update.content, update.bom)).toBe("\uFEFFnew\n")
  129. })
  130. test("derives multiple update chunks", () => {
  131. expect(
  132. Patch.derive(
  133. "update.txt",
  134. [
  135. { oldLines: ["line 2"], newLines: ["LINE 2"] },
  136. { oldLines: ["line 4"], newLines: ["LINE 4"] },
  137. ],
  138. "line 1\nline 2\nline 3\nline 4\n",
  139. ).content,
  140. ).toBe("line 1\nLINE 2\nline 3\nLINE 4\n")
  141. })
  142. test("updates empty files and adds a trailing newline", () => {
  143. expect(Patch.derive("empty.txt", [{ oldLines: [], newLines: ["First line"] }], "").content).toBe("First line\n")
  144. expect(Patch.derive("no-newline.txt", [{ oldLines: ["old"], newLines: ["new"] }], "old").content).toBe("new\n")
  145. })
  146. test("disambiguates updates with change context", () => {
  147. expect(
  148. Patch.derive(
  149. "update.txt",
  150. [{ oldLines: ["x=10"], newLines: ["x=11"], changeContext: "fn b" }],
  151. "fn a\nx=10\nfn b\nx=10\n",
  152. ).content,
  153. ).toBe("fn a\nx=10\nfn b\nx=11\n")
  154. })
  155. test("matches leading, trailing, and Unicode punctuation differences", () => {
  156. expect(Patch.derive("leading.txt", [{ oldLines: ["line"], newLines: ["next"] }], " line\n").content).toBe("next\n")
  157. expect(Patch.derive("trailing.txt", [{ oldLines: ["line"], newLines: ["next"] }], "line \n").content).toBe(
  158. "next\n",
  159. )
  160. expect(
  161. Patch.derive("unicode.txt", [{ oldLines: ['He said "hello"'], newLines: ['He said "hi"'] }], "He said “hello”\n")
  162. .content,
  163. ).toBe('He said "hi"\n')
  164. })
  165. test("matches Unicode minus signs and spaces", () => {
  166. expect(
  167. Patch.derive("minus.txt", [{ oldLines: ["value - 1"], newLines: ["value - 2"] }], "value − 1\n")
  168. .content,
  169. ).toBe("value - 2\n")
  170. const spaces = ["\u00A0", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006", "\u2007", "\u2008", "\u2009", "\u200A", "\u202F", "\u205F", "\u3000"]
  171. spaces.forEach(
  172. (space) => {
  173. expect(
  174. Patch.derive(
  175. "spaces.txt",
  176. [{ oldLines: ["hello world"], newLines: ["hello there"] }],
  177. `hello${space}world\n`,
  178. ).content,
  179. ).toBe("hello there\n")
  180. },
  181. )
  182. })
  183. test("does not normalize ellipses", () => {
  184. expect(() =>
  185. Patch.derive("ellipsis.txt", [{ oldLines: ["wait..."], newLines: ["done"] }], "wait…\n"),
  186. ).toThrow("Failed to find expected lines")
  187. })
  188. test("prefers a later exact match over an earlier normalized match", () => {
  189. expect(
  190. Patch.derive(
  191. "quotes.txt",
  192. [{ oldLines: ['He said "hello"'], newLines: ['He said "goodbye"'] }],
  193. 'He said “hello”\nmiddle\nHe said "hello"\n',
  194. ).content,
  195. ).toBe('He said “hello”\nmiddle\nHe said "goodbye"\n')
  196. })
  197. test("matches EOF-anchored chunks from the end", () => {
  198. expect(
  199. Patch.derive(
  200. "update.txt",
  201. [{ oldLines: ["marker", "end"], newLines: ["marker changed", "end"], endOfFile: true }],
  202. "marker\nmiddle\nmarker\nend\n",
  203. ).content,
  204. ).toBe("marker\nmiddle\nmarker changed\nend\n")
  205. })
  206. test("does not fall back to a non-EOF match", () => {
  207. expect(() =>
  208. Patch.derive(
  209. "update.txt",
  210. [{ oldLines: ["marker", "end"], newLines: ["changed", "end"], endOfFile: true }],
  211. "marker\nend\nmiddle\n",
  212. ),
  213. ).toThrow("Failed to find expected lines")
  214. })
  215. test("matches V1 lenient parsing of malformed hunk bodies", () => {
  216. expect(parse("*** Begin Patch\n*** Add File: add.txt\nmissing plus\n*** End Patch")).toEqual([
  217. { type: "add", path: "add.txt", contents: "" },
  218. ])
  219. expect(parse("*** Begin Patch\n*** Update File: update.txt\n*** End Patch")).toEqual([
  220. { type: "update", path: "update.txt", movePath: undefined, chunks: [] },
  221. ])
  222. expect(parse("*** Begin Patch\n*** Delete File: delete.txt\nunexpected body\n*** End Patch")).toEqual([
  223. { type: "delete", path: "delete.txt" },
  224. ])
  225. })
  226. })