previewPaneFileContent.test.ts 7.0 KB

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