contentHorizontalScroll.test.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { GitPaneChangeCard } from "@/GitPaneChangeCard.js";
  5. import { LightweightDiffPreview } from "@/components/ui/lightweight-diff-preview.js";
  6. import { PatchFallbackContent } from "@/previewPanePatchFallbackContent.js";
  7. vi.mock("@pierre/diffs/react", () => ({
  8. MultiFileDiff: ({
  9. className,
  10. disableWorkerPool,
  11. }: {
  12. className?: string;
  13. disableWorkerPool?: boolean;
  14. }) =>
  15. createElement("div", {
  16. className,
  17. "data-disable-worker-pool": String(Boolean(disableWorkerPool)),
  18. "data-testid": "multi-file-diff",
  19. }),
  20. PatchDiff: ({
  21. className,
  22. disableWorkerPool,
  23. }: {
  24. className?: string;
  25. disableWorkerPool?: boolean;
  26. }) =>
  27. createElement("div", {
  28. className,
  29. "data-disable-worker-pool": String(Boolean(disableWorkerPool)),
  30. "data-testid": "patch-diff",
  31. }),
  32. }));
  33. vi.mock("@/i18n/IntlProvider.js", () => ({
  34. useZCodeIntl: () => ({
  35. intl: {
  36. formatMessage: ({ id }: { id: string }) => id,
  37. },
  38. }),
  39. }));
  40. const codePreviewSettings = {
  41. diffSeparatorStyle: "compact",
  42. darkTheme: "github-dark",
  43. fontSizePx: 12,
  44. lightTheme: "github-light",
  45. showLineNumbers: true,
  46. wrapLongLines: false,
  47. } as const;
  48. function createGitChangeCardElement(diffState: Record<string, unknown>) {
  49. // Bugfix: GitPaneChangeCard 现在由 GitPane 注入右键菜单文案和文件动作;
  50. // 裸渲染单测也必须补齐这些依赖,否则会在 diff 内容断言前崩溃。
  51. return createElement(GitPaneChangeCard, {
  52. change: {
  53. added: 1,
  54. deleted: false,
  55. modified: true,
  56. removed: 1,
  57. status: "modified",
  58. path: "/workspace/demo.ts",
  59. workspaceRelativePath: "demo.ts",
  60. },
  61. contextMenuLabels: {
  62. copyPath: "Copy path",
  63. revealInFileManager: "Open in file manager",
  64. revealInFileTree: "Reveal in file tree",
  65. },
  66. diffState,
  67. isDiffLoading: false,
  68. isExpanded: true,
  69. canRevealInFileManager: true,
  70. codePreviewSettings,
  71. resolvedTheme: "light",
  72. onCopyPath: vi.fn(),
  73. onOpenChange: vi.fn(),
  74. onRevealInFileManager: vi.fn(),
  75. onRevealInFileTree: vi.fn(),
  76. });
  77. }
  78. describe("content horizontal scrolling", () => {
  79. it("keeps patch preview pane content scrollable in both axes", () => {
  80. const html = renderToStaticMarkup(
  81. createElement(PatchFallbackContent, {
  82. patch: "diff --git a/demo.ts b/demo.ts\n@@ -1 +1 @@\n-foo\n+bar",
  83. codePreviewSettings,
  84. resolvedTheme: "light",
  85. }),
  86. );
  87. expect(html).toContain("data-diff-viewer");
  88. expect(html).toContain("overflow-auto");
  89. expect(html).not.toContain("overflow-hidden");
  90. });
  91. it("does not opt patch preview pane rendering out of the diff worker pool", () => {
  92. const html = renderToStaticMarkup(
  93. createElement(PatchFallbackContent, {
  94. patch: "diff --git a/demo.ts b/demo.ts\n@@ -1 +1 @@\n-foo\n+bar",
  95. codePreviewSettings,
  96. resolvedTheme: "light",
  97. }),
  98. );
  99. expect(html).toContain('data-disable-worker-pool="false"');
  100. });
  101. it("falls back to plain text when previewing a multi-file patch", () => {
  102. const html = renderToStaticMarkup(
  103. createElement(PatchFallbackContent, {
  104. patch: [
  105. "diff --git a/demo.ts b/demo.ts",
  106. "--- a/demo.ts",
  107. "+++ b/demo.ts",
  108. "@@ -1 +1 @@",
  109. "-foo",
  110. "+bar",
  111. "diff --git a/other.ts b/other.ts",
  112. "--- a/other.ts",
  113. "+++ b/other.ts",
  114. "@@ -1 +1 @@",
  115. "-old",
  116. "+new",
  117. ].join("\n"),
  118. codePreviewSettings,
  119. resolvedTheme: "light",
  120. }),
  121. );
  122. expect(html).toContain("data-patch-plain-text-preview");
  123. expect(html).toContain("data-lightweight-diff-preview");
  124. expect(html).toContain("overflow-auto");
  125. expect(html).toContain("bar");
  126. expect(html).toContain("new");
  127. expect(html).not.toContain("+bar");
  128. expect(html).not.toContain("+new");
  129. expect(html).not.toContain('data-testid="patch-diff"');
  130. });
  131. it("keeps syntax metadata on created-file patch fallbacks", () => {
  132. const html = renderToStaticMarkup(
  133. createElement(PatchFallbackContent, {
  134. patch: [
  135. "--- /dev/null",
  136. "+++ b/pomodoro.html",
  137. "@@ -0,0 +1,3 @@",
  138. "+<!DOCTYPE html>",
  139. "+<html>",
  140. "+</html>",
  141. ].join("\n"),
  142. codePreviewSettings,
  143. resolvedTheme: "light",
  144. sourcePath: "/workspace/pomodoro.html",
  145. }),
  146. );
  147. expect(html).toContain("data-patch-plain-text-preview");
  148. expect(html).toContain('data-lightweight-diff-highlight-language="html"');
  149. expect(html).toContain('data-lightweight-diff-highlight-theme="github-light"');
  150. expect(html).toContain('data-lightweight-diff-highlighted="false"');
  151. expect(html).toContain("&lt;!DOCTYPE html&gt;");
  152. expect(html).not.toContain("+&lt;!DOCTYPE html&gt;");
  153. });
  154. it("allows expanded Git change card diffs to scroll horizontally", () => {
  155. const html = renderToStaticMarkup(
  156. createGitChangeCardElement({
  157. availability: "patch",
  158. patch: null,
  159. beforeContent: "foo\n",
  160. afterContent: "bar\n",
  161. path: "/workspace/demo.ts",
  162. summary: null,
  163. }),
  164. );
  165. expect(html).toContain("overflow-x-auto overflow-y-hidden");
  166. });
  167. it("keeps small Git change card diffs on the rich before/after renderer", () => {
  168. const html = renderToStaticMarkup(
  169. createGitChangeCardElement({
  170. availability: "patch",
  171. patch: "--- a/demo.ts\n+++ b/demo.ts\n@@ -1 +1 @@\n-foo\n+bar",
  172. beforeContent: "foo\n",
  173. afterContent: "bar\n",
  174. path: "/workspace/demo.ts",
  175. summary: null,
  176. }),
  177. );
  178. expect(html).toContain('data-testid="multi-file-diff"');
  179. expect(html).not.toContain('data-testid="patch-diff"');
  180. expect(html).not.toContain("data-git-plain-text-diff-preview");
  181. });
  182. it("renders the Git patch when a complete before/after pair is unavailable", () => {
  183. const html = renderToStaticMarkup(
  184. createGitChangeCardElement({
  185. availability: "patch",
  186. patch: "--- a/demo.ts\n+++ b/demo.ts\n@@ -1 +1,2 @@\n foo\n+bar",
  187. beforeContent: null,
  188. afterContent: null,
  189. path: "/workspace/demo.ts",
  190. summary: null,
  191. }),
  192. );
  193. expect(html).toContain('data-testid="patch-diff"');
  194. expect(html).not.toContain('data-testid="multi-file-diff"');
  195. expect(html).not.toContain("git.diff.unavailableTitle");
  196. });
  197. it("keeps last-turn new-file snapshots on the rich renderer when no patch exists", () => {
  198. const html = renderToStaticMarkup(
  199. createGitChangeCardElement({
  200. availability: "patch",
  201. patch: null,
  202. beforeContent: null,
  203. afterContent: "export const created = true;\n",
  204. path: "/workspace/demo.ts",
  205. summary: null,
  206. }),
  207. );
  208. expect(html).toContain('data-testid="multi-file-diff"');
  209. expect(html).not.toContain('data-testid="patch-diff"');
  210. });
  211. it("renders large Git change card diffs from patch input", () => {
  212. const beforeContent = Array.from(
  213. { length: 1_300 },
  214. (_, index) => `line-${index + 1}`,
  215. ).join("\n");
  216. const afterContent = beforeContent.replace("line-10", "line-10 changed");
  217. const html = renderToStaticMarkup(
  218. createGitChangeCardElement({
  219. availability: "patch",
  220. patch: [
  221. "--- a/demo.ts",
  222. "+++ b/demo.ts",
  223. "@@ -8,5 +8,5 @@",
  224. " line-8",
  225. " line-9",
  226. "-line-10",
  227. "+line-10 changed",
  228. " line-11",
  229. ].join("\n"),
  230. beforeContent,
  231. afterContent,
  232. path: "/workspace/demo.ts",
  233. summary: null,
  234. }),
  235. );
  236. expect(html).toContain('data-testid="patch-diff"');
  237. expect(html).not.toContain('data-testid="multi-file-diff"');
  238. });
  239. it("renders expensive Git patch fallbacks as lightweight text", () => {
  240. const afterLines = Array.from(
  241. { length: 1_300 },
  242. (_, index) => `+ "line-${index + 1}": true,`,
  243. );
  244. const html = renderToStaticMarkup(
  245. createGitChangeCardElement({
  246. availability: "patch",
  247. patch: [
  248. "--- /dev/null",
  249. "+++ b/demo.json",
  250. "@@ -0,0 +1,1300 @@",
  251. ...afterLines,
  252. ].join("\n"),
  253. beforeContent: "",
  254. afterContent: afterLines.map((line) => line.slice(1)).join("\n"),
  255. path: "/workspace/demo.json",
  256. summary: null,
  257. }),
  258. );
  259. expect(html).toContain("data-git-plain-text-diff-preview");
  260. expect(html).toContain("data-lightweight-diff-preview");
  261. expect(html).toContain("&quot;line-1&quot;: true,");
  262. expect(html).not.toContain("+ &quot;line-1&quot;: true,");
  263. expect(html).not.toContain('data-testid="patch-diff"');
  264. expect(html).not.toContain('data-testid="multi-file-diff"');
  265. });
  266. it("keeps lightweight diff row backgrounds on one shared scroll width", () => {
  267. const html = renderToStaticMarkup(
  268. createElement(LightweightDiffPreview, {
  269. codePreviewSettings,
  270. lines: [
  271. "-short",
  272. "+a very long changed line that should define the shared scroll width",
  273. "+short",
  274. ],
  275. }),
  276. );
  277. expect(html).toContain("data-lightweight-diff-scroll-content");
  278. expect(html).toContain(
  279. "min-w-full font-mono leading-relaxed text-foreground w-max",
  280. );
  281. expect(html).toContain("flex min-w-full w-full");
  282. expect(html).not.toContain("flex min-w-full w-max");
  283. });
  284. });