patchDiffPreview.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. countPatchFileDiffs,
  4. getPlainTextPatchFallbackLines,
  5. getPlainTextPatchPreviewLines,
  6. parseTruncatedMarkerOmittedLineCount,
  7. } from "@/lib/patchDiffPreview.js";
  8. describe("getPlainTextPatchFallbackLines", () => {
  9. it("falls back for created files whose unknown suffix resolves to plain text", () => {
  10. const patch = [
  11. "--- /dev/null",
  12. "+++ b/test.123",
  13. "@@ -0,0 +1,1 @@",
  14. "+123",
  15. ].join("\n");
  16. expect(getPlainTextPatchFallbackLines(patch)).toEqual(["+123"]);
  17. });
  18. it("falls back for created structured files so expansion never renders blank", () => {
  19. const patch = [
  20. "--- /dev/null",
  21. "+++ b/demo.json",
  22. "@@ -0,0 +1,1 @@",
  23. "+{}",
  24. ].join("\n");
  25. expect(getPlainTextPatchFallbackLines(patch)).toEqual(["+{}"]);
  26. });
  27. it("falls back for oversized patches to keep UI responsive", () => {
  28. const changedLines = Array.from(
  29. { length: 1_300 },
  30. (_, index) => `+line-${index + 1}`,
  31. );
  32. const patch = [
  33. "--- a/demo.json",
  34. "+++ b/demo.json",
  35. "@@ -0,0 +1,1300 @@",
  36. ...changedLines,
  37. ].join("\n");
  38. const fallback = getPlainTextPatchFallbackLines(patch);
  39. expect(fallback).toBeTruthy();
  40. expect(fallback?.[0]).toBe("+line-1");
  41. const truncatedMarkerLine = fallback?.find(
  42. (line) => parseTruncatedMarkerOmittedLineCount(line) !== null,
  43. );
  44. expect(truncatedMarkerLine).toBeTruthy();
  45. expect(parseTruncatedMarkerOmittedLineCount(truncatedMarkerLine ?? "")).toBe(
  46. 501,
  47. );
  48. expect(fallback?.at(-1)).toBe("+line-1300");
  49. expect(fallback?.length).toBe(800);
  50. });
  51. it("falls back when hunk line numbers are deep in large files", () => {
  52. const patch = [
  53. "--- a/demo.json",
  54. "+++ b/demo.json",
  55. "@@ -1499,3 +1499,3 @@",
  56. " line-1499",
  57. "-old",
  58. "+new",
  59. " line-1501",
  60. ].join("\n");
  61. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  62. " line-1499",
  63. "-old",
  64. "+new",
  65. " line-1501",
  66. ]);
  67. });
  68. it("falls back for multi-file patches because PatchDiff only supports one file", () => {
  69. const patch = [
  70. "diff --git a/one.ts b/one.ts",
  71. "--- a/one.ts",
  72. "+++ b/one.ts",
  73. "@@ -1 +1 @@",
  74. "-old",
  75. "+new",
  76. "diff --git a/two.ts b/two.ts",
  77. "--- a/two.ts",
  78. "+++ b/two.ts",
  79. "@@ -1 +1 @@",
  80. "-before",
  81. "+after",
  82. ].join("\n");
  83. expect(countPatchFileDiffs(patch)).toBe(2);
  84. expect(getPlainTextPatchFallbackLines(patch)).toEqual(
  85. expect.arrayContaining(["+new", "+after"]),
  86. );
  87. });
  88. it("falls back for hunk-only patches without file headers", () => {
  89. const patch = ["@@ -1 +1 @@", "-old", "+new"].join("\n");
  90. expect(countPatchFileDiffs(patch)).toBe(0);
  91. expect(getPlainTextPatchFallbackLines(patch)).toEqual(["-old", "+new"]);
  92. });
  93. it("falls back for apply_patch payloads instead of sending them to PatchDiff", () => {
  94. const patch = [
  95. "*** Begin Patch",
  96. "*** Update File: demo.ts",
  97. "@@",
  98. "-old",
  99. "+new",
  100. "*** End Patch",
  101. ].join("\n");
  102. expect(countPatchFileDiffs(patch)).toBe(0);
  103. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  104. "-old",
  105. "+new",
  106. "*** End Patch",
  107. ]);
  108. });
  109. it("does not treat hunk text that starts with file headers as new files", () => {
  110. const patch = [
  111. "--- a/readme.md",
  112. "+++ b/readme.md",
  113. "@@ -1,3 +1,3 @@",
  114. " title",
  115. "-before",
  116. "+--- not a file header",
  117. " context",
  118. ].join("\n");
  119. expect(countPatchFileDiffs(patch)).toBe(1);
  120. expect(getPlainTextPatchFallbackLines(patch)).toBeNull();
  121. });
  122. it("falls back when the renderer mistakes a deleted SQL comment for another file", () => {
  123. // Bugfix 回归:unified diff 的删除标记 `-` 与 SQL 注释 `--` 拼接后会成为 `--- `。
  124. // @pierre/diffs 会把它误认成第二个文件头;自有计数仍为 1 时也不能继续交给 PatchDiff。
  125. const patch = [
  126. "--- a/schema.sql",
  127. "+++ b/schema.sql",
  128. "@@ -1,2 +1,2 @@",
  129. "--- old table comment",
  130. "+-- new table comment",
  131. " SELECT 1;",
  132. ].join("\n");
  133. const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
  134. try {
  135. expect(countPatchFileDiffs(patch)).toBe(1);
  136. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  137. "--- old table comment",
  138. "+-- new table comment",
  139. " SELECT 1;",
  140. ]);
  141. // Bugfix 回归:解析失败是正常降级控制流,不能把解析出的文件或代码内容写进控制台。
  142. expect(consoleError).not.toHaveBeenCalled();
  143. } finally {
  144. consoleError.mockRestore();
  145. }
  146. });
  147. it("counts multi-file patches without diff --git headers (ZCT-202606-183BF216)", () => {
  148. // Bugfix 复现:模型通过 Edit/Write 工具一次性改多个文件,产出的 unified diff
  149. // 没有 `diff --git` 前缀(只有 --- a/ +++ b/),且 hunk 头声明的行数比实际 body 多 1。
  150. // 多出的配额会把下一文件的 "--- a/x"('-' 开头算 deletion)和 "+++ b/x"('+' 开头
  151. // 算 addition)吞进当前 hunk,导致后续文件头被 consumeHunkBody 误消费,3 个文件被计为 1。
  152. // 误判为单文件后不降级 → 整个多文件 patch 被送往 @pierre/diffs 的 PatchDiff →
  153. // 抛 "Provided patch must contain exactly 1 file diff" → 整页错误边界。
  154. const patch = [
  155. "--- a/IProviderAppService.java",
  156. "+++ b/IProviderAppService.java",
  157. "@@ -5,3 +5,3 @@",
  158. " import lombok.RequiredArgsConstructor;",
  159. "-import com.pnm.old.DeprecatedService;",
  160. "+import com.pnm.new.NetworkTierAssembler;",
  161. "--- a/ProviderAppServiceImpl.java",
  162. "+++ b/ProviderAppServiceImpl.java",
  163. "@@ -30,3 +30,3 @@",
  164. " @Autowired",
  165. "- private OldRepo oldRepo;",
  166. "+ private NetworkTierJpaRepository networkTierJpaRepo;",
  167. "--- a/ProviderMgmtController.java",
  168. "+++ b/ProviderMgmtController.java",
  169. "@@ -95,2 +95,2 @@",
  170. " @Override",
  171. "- public Response<Void> old() { return null; }",
  172. "+ public Response<List<NetworkTierDTO>> listNetworkTiers() { return ok(); }",
  173. ].join("\n");
  174. expect(countPatchFileDiffs(patch)).toBe(3);
  175. // 多文件 patch 必须走轻量高亮 diff(剥掉 +/-/@@ 协议 marker、保留增删配色与语法高亮),
  176. // 绝不能整体交给 @pierre/diffs 的 PatchDiff——它只支持单文件,会抛 "exactly 1 file diff"。
  177. expect(getPlainTextPatchFallbackLines(patch)).not.toBeNull();
  178. });
  179. it("does not split a hunk when only a lone --- line appears in the body", () => {
  180. // 边界守卫:正文里真实出现以 `--- ` 开头的行(但下一行不是 `+++ `),
  181. // 不构成文件头对,必须仍按单文件计数,不能被新的提前结束逻辑误伤。
  182. const patch = [
  183. "--- a/doc.md",
  184. "+++ b/doc.md",
  185. "@@ -1,3 +1,3 @@",
  186. " heading",
  187. "-old text",
  188. "+--- a dash-prefixed sentence, not a header",
  189. " trailing",
  190. ].join("\n");
  191. expect(countPatchFileDiffs(patch)).toBe(1);
  192. });
  193. it("counts two-file patches without diff --git headers when hunk counts are exact", () => {
  194. // 即使 hunk 行数与正文完全吻合(无 off-by-one),相邻文件头也应被正确识别为新文件。
  195. const patch = [
  196. "--- a/first.ts",
  197. "+++ b/first.ts",
  198. "@@ -1,2 +1,2 @@",
  199. " keep",
  200. "-old",
  201. "+new",
  202. "--- a/second.ts",
  203. "+++ b/second.ts",
  204. "@@ -1,2 +1,2 @@",
  205. " keep",
  206. "-before",
  207. "+after",
  208. ].join("\n");
  209. expect(countPatchFileDiffs(patch)).toBe(2);
  210. // 同样走轻量高亮 diff,而非 PatchDiff。
  211. expect(getPlainTextPatchFallbackLines(patch)).not.toBeNull();
  212. });
  213. it("can always build lightweight plain-text preview lines", () => {
  214. const patch = [
  215. "--- a/demo.json",
  216. "+++ b/demo.json",
  217. "@@ -2,3 +2,3 @@",
  218. " line-2",
  219. "-old",
  220. "+new",
  221. " line-4",
  222. ].join("\n");
  223. expect(getPlainTextPatchPreviewLines(patch)).toEqual([
  224. " line-2",
  225. "-old",
  226. "+new",
  227. " line-4",
  228. ]);
  229. });
  230. it("falls back for deleted plain text files and strips patch metadata", () => {
  231. const patch = [
  232. "--- a/demo.txt",
  233. "+++ /dev/null",
  234. "@@ -1,1 +0,0 @@",
  235. "-hello",
  236. "",
  237. ].join("\n");
  238. expect(getPlainTextPatchFallbackLines(patch)).toEqual(["-hello"]);
  239. });
  240. it("keeps created-file content lines whose text starts with plus markers", () => {
  241. const patch = [
  242. "--- /dev/null",
  243. "+++ b/demo.txt",
  244. "@@ -0,0 +1,3 @@",
  245. "+++ keep me",
  246. "++++ keep me too",
  247. "+plain",
  248. ].join("\n");
  249. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  250. "+++ keep me",
  251. "++++ keep me too",
  252. "+plain",
  253. ]);
  254. });
  255. it("keeps deleted-file content lines whose text starts with minus markers", () => {
  256. const patch = [
  257. "--- a/demo.txt",
  258. "+++ /dev/null",
  259. "@@ -1,3 +0,0 @@",
  260. "--- keep me",
  261. "---- keep me too",
  262. "-plain",
  263. ].join("\n");
  264. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  265. "--- keep me",
  266. "---- keep me too",
  267. "-plain",
  268. ]);
  269. });
  270. it("falls back for rename-only patches without hunks", () => {
  271. const patch = [
  272. "diff --git a/old.ts b/new.ts",
  273. "similarity index 100%",
  274. "rename from old.ts",
  275. "rename to new.ts",
  276. "",
  277. ].join("\n");
  278. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  279. "similarity index 100%",
  280. "rename from old.ts",
  281. "rename to new.ts",
  282. ]);
  283. });
  284. it("falls back for mode-only patches without hunks", () => {
  285. const patch = [
  286. "diff --git a/script.sh b/script.sh",
  287. "old mode 100644",
  288. "new mode 100755",
  289. "",
  290. ].join("\n");
  291. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  292. "old mode 100644",
  293. "new mode 100755",
  294. ]);
  295. });
  296. it("falls back for package manager lockfiles", () => {
  297. const patch = [
  298. "diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml",
  299. "--- a/pnpm-lock.yaml",
  300. "+++ b/pnpm-lock.yaml",
  301. "@@ -1,3 +1,3 @@",
  302. " lockfileVersion: '9.0'",
  303. "-pkg: 1.0.0",
  304. "+pkg: 1.0.1",
  305. ].join("\n");
  306. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  307. " lockfileVersion: '9.0'",
  308. "-pkg: 1.0.0",
  309. "+pkg: 1.0.1",
  310. ]);
  311. });
  312. it("falls back for shell script patches in packaged builds", () => {
  313. const patch = [
  314. "diff --git a/scripts/publish-release.sh b/scripts/publish-release.sh",
  315. "--- a/scripts/publish-release.sh",
  316. "+++ b/scripts/publish-release.sh",
  317. "@@ -1,3 +1,4 @@",
  318. " #!/usr/bin/env bash",
  319. " set -euo pipefail",
  320. "+pnpm build",
  321. " echo done",
  322. ].join("\n");
  323. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  324. " #!/usr/bin/env bash",
  325. " set -euo pipefail",
  326. "+pnpm build",
  327. " echo done",
  328. ]);
  329. });
  330. it("falls back for modified Gradle patches opened from edit file chips", () => {
  331. const patch = [
  332. "diff --git a/test.gradle b/test.gradle",
  333. "--- a/test.gradle",
  334. "+++ b/test.gradle",
  335. "@@ -1,3 +1,3 @@",
  336. " plugins {",
  337. "- id 'java'",
  338. "+ id 'application'",
  339. " }",
  340. ].join("\n");
  341. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  342. " plugins {",
  343. "- id 'java'",
  344. "+ id 'application'",
  345. " }",
  346. ]);
  347. });
  348. it("falls back for Gradle patches with unified diff timestamps", () => {
  349. const patch = [
  350. "diff --git a/test.gradle b/test.gradle",
  351. "--- a/test.gradle\t2026-05-13 16:00:00",
  352. "+++ b/test.gradle\t2026-05-13 16:01:00",
  353. "@@ -1,3 +1,3 @@",
  354. " plugins {",
  355. "- id 'java'",
  356. "+ id 'application'",
  357. " }",
  358. ].join("\n");
  359. expect(getPlainTextPatchFallbackLines(patch)).toEqual([
  360. " plugins {",
  361. "- id 'java'",
  362. "+ id 'application'",
  363. " }",
  364. ]);
  365. });
  366. });