contentHorizontalScroll.test.ts 7.8 KB

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