previewPaneFileContent.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
  6. import { PreviewPaneContent } from "@/previewPaneContent.js";
  7. vi.mock("@pierre/diffs/react", () => ({
  8. File: ({
  9. className,
  10. file,
  11. lineAnnotations,
  12. renderAnnotation,
  13. selectedLines,
  14. }: {
  15. className?: string;
  16. file?: { contents?: string };
  17. lineAnnotations?: unknown[];
  18. renderAnnotation?: (annotation: unknown) => unknown;
  19. selectedLines?: { start: number; end: number } | null;
  20. }) =>
  21. createElement(
  22. "diffs-container",
  23. {
  24. class: className,
  25. "data-code": file?.contents,
  26. "data-selected-lines": selectedLines
  27. ? `${selectedLines.start}-${selectedLines.end}`
  28. : "",
  29. },
  30. lineAnnotations?.map((annotation, index) =>
  31. createElement("div", { key: index }, renderAnnotation?.(annotation)),
  32. ),
  33. ),
  34. MultiFileDiff: () => createElement("diffs-multi-file"),
  35. PatchDiff: () => createElement("diffs-patch"),
  36. }));
  37. function renderFileContent(overrides: Record<string, unknown> = {}): string {
  38. return renderToStaticMarkup(
  39. createElement(
  40. ZCodeIntlProvider,
  41. { initialLocale: "en-US" },
  42. createElement(PreviewPaneContent, {
  43. source: {
  44. type: "file",
  45. title: "response.json",
  46. path: "/workspace/response.json",
  47. },
  48. filePreview: {
  49. path: "/workspace/response.json",
  50. content: "line 75\n我是中文\nline 77",
  51. offset: 0,
  52. bytesRead: 28,
  53. totalBytes: 28,
  54. truncated: false,
  55. isBinary: false,
  56. },
  57. fileTooLarge: false,
  58. loadingInitial: false,
  59. loadingImagePreview: false,
  60. imagePreview: null,
  61. loadingPdfPreview: false,
  62. pdfViewerSource: null,
  63. pdfViewerLabels: {
  64. loading: "Loading",
  65. loadError: "Load error",
  66. noData: "No data",
  67. previousPage: "Previous",
  68. nextPage: "Next",
  69. pageInput: "Page",
  70. zoomIn: "Zoom in",
  71. zoomOut: "Zoom out",
  72. },
  73. loadingOfficePreview: false,
  74. officePreview: null,
  75. officePreviewKind: null,
  76. loadingPptxPreview: false,
  77. pptxPreviewData: null,
  78. pptxReferenceSource: null,
  79. pptxViewerLabels: {
  80. loading: "Loading",
  81. loadError: "Load error",
  82. noSlides: "No slides",
  83. previousPage: "Previous",
  84. nextPage: "Next",
  85. pageInput: "Page",
  86. zoomIn: "Zoom in",
  87. zoomOut: "Zoom out",
  88. thumbnails: "Thumbnails",
  89. thumbnail: (pageNumber) => `Slide ${pageNumber}`,
  90. selectElement: "Select element",
  91. exitElementSelection: "Exit selection",
  92. aiEdit: "AI Edit",
  93. commentPlaceholder: "Add a comment",
  94. cancelAiEdit: "Cancel",
  95. addToConversation: "Add to conversation",
  96. },
  97. error: null,
  98. codePreviewSettings: DEFAULT_CODE_PREVIEW_SETTINGS,
  99. codeTheme: "github-light",
  100. resolvedTheme: "light",
  101. theme: "light",
  102. workspacePath: "/workspace",
  103. markdownViewMode: "code",
  104. svgViewMode: "code",
  105. wrapLongLines: false,
  106. codeComments: [],
  107. codeCommentLabels: {
  108. addComment: "Add comment",
  109. addCommentTooltip: "Add comment",
  110. commentPlaceholder: "Comment",
  111. submitComment: "Submit",
  112. cancelComment: "Cancel",
  113. deleteComment: "Delete",
  114. commentLine: "Line {line}",
  115. commentRange: "Lines {startLine}-{endLine}",
  116. },
  117. ...overrides,
  118. }),
  119. ),
  120. );
  121. }
  122. describe("PreviewPaneContent text files", () => {
  123. it("renders UTF-8 text as one continuous document", () => {
  124. const html = renderFileContent();
  125. expect(html.match(/<diffs-container/g)).toHaveLength(1);
  126. expect(html).toContain("line 75");
  127. expect(html).toContain("我是中文");
  128. expect(html).toContain("line 77");
  129. expect(html).toContain("overflow-auto");
  130. expect(html).not.toContain("data-preview-file-scroll-container");
  131. expect(html).not.toContain("data-scroll-mode");
  132. });
  133. it("shows a localized limit message instead of truncated text", () => {
  134. const html = renderFileContent({
  135. filePreview: null,
  136. fileTooLarge: true,
  137. });
  138. expect(html).toContain(
  139. "This file exceeds the 256 KB preview limit. Open it in another editor to view the full contents.",
  140. );
  141. expect(html).toContain("text-foreground-subtle");
  142. expect(html).not.toContain("<diffs-container");
  143. });
  144. it("keeps PPTX preview available when element-reference identity is unavailable", () => {
  145. const html = renderFileContent({
  146. source: {
  147. type: "pptx",
  148. title: "deck.pptx",
  149. path: "/workspace/deck.pptx",
  150. },
  151. filePreview: null,
  152. pptxPreviewData: new ArrayBuffer(1),
  153. pptxReferenceSource: null,
  154. });
  155. expect(html).toContain("Loading");
  156. });
  157. it("renders a valid code-review range as a read-only inline comment and source code", () => {
  158. const html = renderFileContent({
  159. source: {
  160. type: "code-review",
  161. title: "README.md",
  162. path: "/workspace/README.md",
  163. review: {
  164. requestId: "review-1",
  165. title: "Heading problem",
  166. body: "Use a more specific heading.",
  167. priority: 1,
  168. startLine: 2,
  169. endLine: 3,
  170. },
  171. },
  172. filePreview: {
  173. path: "/workspace/README.md",
  174. content: "# Heading\ntext\nmore text",
  175. offset: 0,
  176. bytesRead: 24,
  177. totalBytes: 24,
  178. truncated: false,
  179. isBinary: false,
  180. },
  181. markdownViewMode: "preview",
  182. });
  183. expect(html).toContain("<diffs-container");
  184. expect(html).toContain("Use a more specific heading.");
  185. expect(html).toContain("Lines 2-3");
  186. expect(html).toContain('data-selected-lines="2-3"');
  187. expect(html).not.toContain("Delete");
  188. });
  189. it("renders a code review without line numbers at the top without fabricating a range", () => {
  190. const html = renderFileContent({
  191. source: {
  192. type: "code-review",
  193. title: "response.json",
  194. path: "/workspace/response.json",
  195. review: {
  196. requestId: "review-no-range",
  197. title: "File review",
  198. body: "Review the whole file.",
  199. },
  200. },
  201. });
  202. expect(html).toContain("Review the whole file.");
  203. expect(html).not.toContain("Line 1");
  204. expect(html).toContain('data-selected-lines=""');
  205. });
  206. it("keeps an out-of-range review visible at the top and reports the invalid target", () => {
  207. const html = renderFileContent({
  208. source: {
  209. type: "code-review",
  210. title: "response.json",
  211. path: "/workspace/response.json",
  212. review: {
  213. requestId: "review-out-of-range",
  214. title: "Missing line",
  215. body: "This target moved.",
  216. startLine: 99,
  217. endLine: 100,
  218. },
  219. },
  220. });
  221. expect(html).toContain("This target moved.");
  222. expect(html).toContain("The target line does not exist");
  223. expect(html).toContain('data-selected-lines=""');
  224. });
  225. });