previewPane.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. getPreviewPaneSafeErrorMessage,
  4. resolvePreviewPaneImageFileReference,
  5. getPreviewPaneDisplayOptions,
  6. isPreviewPaneFileTooLargeError,
  7. isPptxPreviewFileTooLarge,
  8. isPreviewPaneMissingFileError,
  9. shouldToastPptxReferenceFileMissing,
  10. resolvePptxPreviewReadErrorMessage,
  11. resolvePreviewPaneImageSource,
  12. resolvePreviewPaneMediaSource,
  13. resolveMediaPlaybackErrorMessageId,
  14. resolvePreviewPanePdfSource,
  15. resolvePreviewPanePptxSource,
  16. resolvePreviewPaneTextFileResult,
  17. shouldShowPreviewPaneHeaderDivider,
  18. } from "@/PreviewPane.js";
  19. import { createDiffSourceFilePreviewSource, getDiffSourceFileTarget } from "@/lib/codeViewer.js";
  20. import { createWorkspaceFileComposerMention } from "@/lib/workspaceFileDrag.js";
  21. import { getPreviewPaneErrorTextClass } from "@/previewPaneContent.js";
  22. describe("PreviewPane helpers", () => {
  23. it.each([
  24. ["/project", "/outside/drawing.png"],
  25. ["/project", "/project-other/drawing.png"],
  26. ["C:\\project", "C:\\outside\\drawing.png"],
  27. ])("keeps an outside image's absolute reference for %s", (workspacePath, path) => {
  28. const reference = resolvePreviewPaneImageFileReference(
  29. { type: "image", title: "drawing.png", mediaType: "image/png", path, workspacePath },
  30. workspacePath,
  31. workspacePath,
  32. );
  33. expect(reference?.relativePath).toBe(path);
  34. expect(createWorkspaceFileComposerMention(reference!, workspacePath).value).toBe(path);
  35. });
  36. it("routes remote image references only to the matching workspace draft", () => {
  37. const source = {
  38. type: "image" as const,
  39. title: "drawing.png",
  40. path: "/project/drawing.png",
  41. mediaType: "image/png",
  42. workspacePath: "/project",
  43. workspaceIdentity: "ssh://host-a/project",
  44. };
  45. expect(
  46. resolvePreviewPaneImageFileReference(source, "/project", "ssh://host-a/project"),
  47. ).toEqual({
  48. type: "file",
  49. name: "drawing.png",
  50. path: source.path,
  51. relativePath: "drawing.png",
  52. workspacePath: "/project",
  53. workspaceIdentity: "ssh://host-a/project",
  54. });
  55. expect(
  56. resolvePreviewPaneImageFileReference(source, "/project", "ssh://host-b/project"),
  57. ).toBeUndefined();
  58. expect(resolvePreviewPaneImageFileReference(source, "/project", "/project")).toBeUndefined();
  59. expect(resolvePreviewPaneImageFileReference(source, "/project", undefined)).toBeUndefined();
  60. });
  61. it("keeps local path fallback and rejects content without a source path", () => {
  62. expect(
  63. resolvePreviewPaneImageFileReference(
  64. { type: "file", title: "drawing.svg", path: "/project/drawing.svg" },
  65. "/project",
  66. "/project",
  67. ),
  68. ).toMatchObject({ relativePath: "drawing.svg", workspacePath: "/project" });
  69. expect(
  70. resolvePreviewPaneImageFileReference(
  71. {
  72. type: "file",
  73. title: "drawing.png",
  74. path: "/project/drawing.png",
  75. workspacePath: "/project",
  76. },
  77. "/other",
  78. "/other",
  79. ),
  80. ).toMatchObject({ path: "/project/drawing.png", relativePath: "drawing.png" });
  81. expect(
  82. resolvePreviewPaneImageFileReference(
  83. {
  84. type: "file",
  85. title: "drawing.png",
  86. path: "/project/drawing.png",
  87. workspacePath: "/project",
  88. },
  89. "/remote",
  90. "ssh://host/remote",
  91. ),
  92. ).toBeUndefined();
  93. expect(
  94. resolvePreviewPaneImageFileReference(
  95. { type: "text", title: "inline.svg", content: "<svg/>", language: "xml" },
  96. "/project",
  97. "/project",
  98. ),
  99. ).toBeUndefined();
  100. });
  101. it.each([
  102. [3, "codeViewer.mediaUnsupported"],
  103. [4, "codeViewer.mediaUnsupported"],
  104. [2, "codeViewer.mediaLoadFailed"],
  105. [1, "codeViewer.mediaLoadFailed"],
  106. [undefined, "codeViewer.mediaLoadFailed"],
  107. ])("classifies media error code %s", (code, expectedMessageId) => {
  108. expect(resolveMediaPlaybackErrorMessageId(code)).toBe(expectedMessageId);
  109. });
  110. it.each([
  111. ["clip.mp4", "video", "video/mp4"],
  112. ["clip.mov", "video", "video/quicktime"],
  113. ["clip.webm", "video", "video/webm"],
  114. ["clip.m4v", "video", "video/x-m4v"],
  115. ["song.mp3", "audio", "audio/mpeg"],
  116. ["song.wav", "audio", "audio/wav"],
  117. ["song.m4a", "audio", "audio/mp4"],
  118. ["song.ogg", "audio", "audio/ogg"],
  119. ["song.opus", "audio", "audio/opus"],
  120. ["song.flac", "audio", "audio/flac"],
  121. ["song.weba", "audio", "audio/webm"],
  122. ])("upgrades %s to a media source", (name, kind, mediaType) => {
  123. expect(
  124. resolvePreviewPaneMediaSource({
  125. type: "file",
  126. title: name,
  127. path: `/workspace/${name}`,
  128. }),
  129. ).toMatchObject({
  130. type: "media",
  131. kind,
  132. mediaType,
  133. path: `/workspace/${name}`,
  134. });
  135. });
  136. it.each(["report.docx", "report.doc", "table.xlsx", "table.xls", "slides.pptx", "paper.pdf"])(
  137. "shows the document header divider for %s",
  138. (path) => {
  139. expect(
  140. shouldShowPreviewPaneHeaderDivider({
  141. type: "file",
  142. title: path,
  143. path: `/workspace/${path}`,
  144. }),
  145. ).toBe(true);
  146. },
  147. );
  148. it("does not add the document header divider to ordinary previews", () => {
  149. expect(
  150. shouldShowPreviewPaneHeaderDivider({
  151. type: "file",
  152. title: "app.ts",
  153. path: "/workspace/app.ts",
  154. }),
  155. ).toBe(false);
  156. });
  157. it("uses media preview for raster image file sources", () => {
  158. expect(
  159. resolvePreviewPaneImageSource({
  160. type: "file",
  161. title: "Lark_Suite_logo_2022.png.png",
  162. path: "/workspace/Lark_Suite_logo_2022.png.png",
  163. }),
  164. ).toEqual({
  165. type: "image",
  166. title: "Lark_Suite_logo_2022.png.png",
  167. path: "/workspace/Lark_Suite_logo_2022.png.png",
  168. mediaType: "image/png",
  169. });
  170. });
  171. it("keeps svg file sources out of media preview so source mode still works", () => {
  172. expect(
  173. resolvePreviewPaneImageSource({
  174. type: "file",
  175. title: "logo.svg",
  176. path: "/workspace/logo.svg",
  177. }),
  178. ).toBeNull();
  179. });
  180. it("upgrades pdf file sources to the dedicated pdf preview", () => {
  181. expect(
  182. resolvePreviewPanePdfSource({
  183. type: "file",
  184. title: "report.pdf",
  185. path: "/workspace/report.pdf",
  186. }),
  187. ).toEqual({
  188. type: "pdf",
  189. title: "report.pdf",
  190. path: "/workspace/report.pdf",
  191. });
  192. expect(
  193. resolvePreviewPanePdfSource({
  194. type: "file",
  195. title: "REPORT.PDF",
  196. path: "/workspace/REPORT.PDF",
  197. }),
  198. ).toEqual({
  199. type: "pdf",
  200. title: "REPORT.PDF",
  201. path: "/workspace/REPORT.PDF",
  202. });
  203. });
  204. it("passes through pdf sources and rejects non-pdf sources", () => {
  205. const pdfSource = {
  206. type: "pdf" as const,
  207. title: "report.pdf",
  208. path: "/workspace/report.pdf",
  209. };
  210. expect(resolvePreviewPanePdfSource(pdfSource)).toBe(pdfSource);
  211. expect(
  212. resolvePreviewPanePdfSource({
  213. type: "file",
  214. title: "index.ts",
  215. path: "/workspace/index.ts",
  216. }),
  217. ).toBeNull();
  218. expect(resolvePreviewPanePdfSource(null)).toBeNull();
  219. });
  220. it("hides the more menu for pdf previews", () => {
  221. // pdf 是专用只读预览,没有源码视图,也不该出现自动换行开关
  222. expect(
  223. getPreviewPaneDisplayOptions({
  224. type: "file",
  225. title: "report.pdf",
  226. path: "/workspace/report.pdf",
  227. }).hasMoreMenu,
  228. ).toBe(false);
  229. expect(
  230. getPreviewPaneDisplayOptions({
  231. type: "pdf",
  232. title: "report.pdf",
  233. path: "/workspace/report.pdf",
  234. }).hasMoreMenu,
  235. ).toBe(false);
  236. });
  237. it("upgrades pptx file sources and preserves workspace scope", () => {
  238. const source = {
  239. type: "file" as const,
  240. title: "ROADMAP.PPTX",
  241. path: "/workspace/ROADMAP.PPTX",
  242. workspacePath: "/workspace",
  243. workspaceIdentity: "ssh:demo:/workspace",
  244. workspaceRemoteSessionId: "remote-1",
  245. };
  246. expect(resolvePreviewPanePptxSource(source)).toEqual({
  247. ...source,
  248. type: "pptx",
  249. });
  250. });
  251. it("passes through pptx sources and rejects non-pptx sources", () => {
  252. const pptxSource = {
  253. type: "pptx" as const,
  254. title: "slides.pptx",
  255. path: "/workspace/slides.pptx",
  256. };
  257. expect(resolvePreviewPanePptxSource(pptxSource)).toBe(pptxSource);
  258. expect(
  259. resolvePreviewPanePptxSource({
  260. type: "file",
  261. title: "legacy.ppt",
  262. path: "/workspace/legacy.ppt",
  263. }),
  264. ).toBeNull();
  265. expect(resolvePreviewPanePptxSource(null)).toBeNull();
  266. });
  267. it("only requests a missing-file toast for reference-driven PPTX navigation", () => {
  268. const missingError = Object.assign(new Error("missing"), { code: "ENOENT" });
  269. expect(
  270. shouldToastPptxReferenceFileMissing(
  271. {
  272. type: "pptx",
  273. title: "slides.pptx",
  274. path: "/workspace/slides.pptx",
  275. referenceNavigation: {
  276. requestId: "navigation-1",
  277. pageIndex: 2,
  278. expectedSourceFingerprint: `sha256:${"a".repeat(64)}`,
  279. },
  280. },
  281. missingError,
  282. ),
  283. ).toBe(true);
  284. expect(
  285. shouldToastPptxReferenceFileMissing(
  286. {
  287. type: "pptx",
  288. title: "slides.pptx",
  289. path: "/workspace/slides.pptx",
  290. },
  291. missingError,
  292. ),
  293. ).toBe(false);
  294. });
  295. it("hides the more menu for pptx previews", () => {
  296. expect(
  297. getPreviewPaneDisplayOptions({
  298. type: "file",
  299. title: "slides.pptx",
  300. path: "/workspace/slides.pptx",
  301. }).hasMoreMenu,
  302. ).toBe(false);
  303. expect(
  304. getPreviewPaneDisplayOptions({
  305. type: "pptx",
  306. title: "slides.pptx",
  307. path: "/workspace/slides.pptx",
  308. }).hasMoreMenu,
  309. ).toBe(false);
  310. });
  311. it("enforces the 64 MB PPTX preview source limit", () => {
  312. expect(isPptxPreviewFileTooLarge(64 * 1024 * 1024)).toBe(false);
  313. expect(isPptxPreviewFileTooLarge(64 * 1024 * 1024 + 1)).toBe(true);
  314. });
  315. it("hides the more menu when a preview source has no display options", () => {
  316. expect(
  317. getPreviewPaneDisplayOptions({
  318. type: "image",
  319. title: "logo.png",
  320. path: "/workspace/logo.png",
  321. mediaType: "image/png",
  322. }).hasMoreMenu,
  323. ).toBe(false);
  324. expect(
  325. getPreviewPaneDisplayOptions({
  326. type: "patch",
  327. title: "changes.patch",
  328. patch: "diff --git a/a b/a",
  329. }).hasMoreMenu,
  330. ).toBe(false);
  331. expect(
  332. getPreviewPaneDisplayOptions({
  333. type: "multi-file-diff",
  334. title: "changes.patch",
  335. path: "/workspace/app.ts",
  336. beforeContent: "const value = 1;",
  337. afterContent: "const value = 2;",
  338. }).hasMoreMenu,
  339. ).toBe(false);
  340. expect(
  341. getPreviewPaneDisplayOptions({
  342. type: "file",
  343. title: "logo.png",
  344. path: "/workspace/logo.png",
  345. }).hasMoreMenu,
  346. ).toBe(false);
  347. expect(
  348. getPreviewPaneDisplayOptions({
  349. type: "file",
  350. title: "report.xlsx",
  351. path: "/workspace/report.xlsx",
  352. }).hasMoreMenu,
  353. ).toBe(false);
  354. expect(
  355. getPreviewPaneDisplayOptions({
  356. type: "file",
  357. title: "proposal.docx",
  358. path: "/workspace/proposal.docx",
  359. }).hasMoreMenu,
  360. ).toBe(false);
  361. expect(
  362. getPreviewPaneDisplayOptions({
  363. type: "file",
  364. title: "legacy.doc",
  365. path: "/workspace/legacy.doc",
  366. }).hasMoreMenu,
  367. ).toBe(false);
  368. });
  369. it("shows the more menu when a preview source has display options", () => {
  370. expect(
  371. getPreviewPaneDisplayOptions({
  372. type: "text",
  373. title: "README.md",
  374. path: "/workspace/README.md",
  375. content: "# Title",
  376. language: "markdown",
  377. }).hasMoreMenu,
  378. ).toBe(true);
  379. expect(
  380. getPreviewPaneDisplayOptions({
  381. type: "file",
  382. title: "logo.svg",
  383. path: "/workspace/logo.svg",
  384. }).hasMoreMenu,
  385. ).toBe(true);
  386. expect(
  387. getPreviewPaneDisplayOptions({
  388. type: "text",
  389. title: "index.ts",
  390. content: "export {};",
  391. language: "typescript",
  392. }).hasMoreMenu,
  393. ).toBe(true);
  394. });
  395. it("keeps code-review sources in code mode with only the wrap control", () => {
  396. const options = getPreviewPaneDisplayOptions({
  397. type: "code-review",
  398. title: "README.md",
  399. path: "/workspace/README.md",
  400. workspacePath: "/workspace",
  401. review: {
  402. requestId: "review-1",
  403. title: "Review title",
  404. body: "Review body",
  405. },
  406. });
  407. expect(options).toEqual({
  408. canToggleCodeWrap: true,
  409. canToggleMarkdownView: false,
  410. canToggleSvgView: false,
  411. hasMoreMenu: true,
  412. });
  413. });
  414. it("shows the wrap option after svg is switched to code mode", () => {
  415. const svgSource = {
  416. type: "file" as const,
  417. title: "logo.svg",
  418. path: "/workspace/logo.svg",
  419. };
  420. expect(
  421. getPreviewPaneDisplayOptions(svgSource, {
  422. svgViewMode: "preview",
  423. }).canToggleCodeWrap,
  424. ).toBe(false);
  425. expect(
  426. getPreviewPaneDisplayOptions(svgSource, {
  427. svgViewMode: "code",
  428. }).canToggleCodeWrap,
  429. ).toBe(true);
  430. });
  431. it("detects missing-file preview errors without depending on platform error objects", () => {
  432. expect(
  433. isPreviewPaneMissingFileError(
  434. new Error(
  435. "ENOENT: no such file or directory, stat 'D:\\CODE\\Demo_OCR\\OCR4-Ollama-Bundle\\paper.md'",
  436. ),
  437. ),
  438. ).toBe(true);
  439. expect(isPreviewPaneMissingFileError({ code: "ENOENT" })).toBe(true);
  440. expect(isPreviewPaneMissingFileError(new Error("permission denied"))).toBe(false);
  441. });
  442. it("detects the bounded binary preview size error for localized UI copy", () => {
  443. expect(
  444. isPreviewPaneFileTooLargeError(new Error("File is too large to preview: 26214401 bytes")),
  445. ).toBe(true);
  446. expect(isPreviewPaneFileTooLargeError(new Error("permission denied"))).toBe(false);
  447. });
  448. it("only treats truncated text as too large and preserves binary detection", () => {
  449. const truncatedText = {
  450. path: "/workspace/large.log",
  451. content: "partial",
  452. offset: 0,
  453. bytesRead: 256 * 1024,
  454. totalBytes: 256 * 1024 + 1,
  455. truncated: true,
  456. isBinary: false,
  457. };
  458. const truncatedBinary = {
  459. ...truncatedText,
  460. content: "",
  461. isBinary: true,
  462. };
  463. expect(resolvePreviewPaneTextFileResult(truncatedText)).toEqual({
  464. filePreview: null,
  465. fileTooLarge: true,
  466. });
  467. expect(resolvePreviewPaneTextFileResult(truncatedBinary)).toEqual({
  468. filePreview: truncatedBinary,
  469. fileTooLarge: false,
  470. });
  471. });
  472. it("maps the old remote PPTX 8 MB limit without exposing its absolute path", () => {
  473. const sourcePath = "/srv/private/customer/slides.pptx";
  474. const message = resolvePptxPreviewReadErrorMessage(
  475. new Error(`File is too large to preview: ${sourcePath}`),
  476. {
  477. sourcePath,
  478. fileMissingMessage: "missing",
  479. legacyFileTooLargeMessage: "legacy 8 MB limit",
  480. incompleteFileMessage: "incomplete",
  481. },
  482. );
  483. expect(message).toBe("legacy 8 MB limit");
  484. expect(message).not.toContain("/srv/private/customer");
  485. });
  486. it("keeps only the file name when another PPTX read error contains an absolute path", () => {
  487. expect(
  488. getPreviewPaneSafeErrorMessage(
  489. new Error("Permission denied: D:\\private\\customer\\slides.pptx"),
  490. "D:\\private\\customer\\slides.pptx",
  491. ),
  492. ).toBe("Permission denied: slides.pptx");
  493. });
  494. it("renders missing-file preview errors as tertiary helper text", () => {
  495. const missingFileMessage = "文件不存在,或当前环境无法访问该路径。";
  496. expect(getPreviewPaneErrorTextClass(missingFileMessage, missingFileMessage)).toBe(
  497. "text-foreground-subtlest",
  498. );
  499. expect(getPreviewPaneErrorTextClass("permission denied", missingFileMessage)).toBe(
  500. "text-destructive",
  501. );
  502. });
  503. it("builds a normal file preview source from diff preview sources", () => {
  504. expect(
  505. createDiffSourceFilePreviewSource(
  506. {
  507. type: "multi-file-diff",
  508. title: "demo.ts",
  509. path: "/workspace/demo.ts",
  510. beforeContent: "const value = 1;",
  511. afterContent: "const value = 2;",
  512. workspaceIdentity: "ssh:test:/workspace",
  513. workspaceRemoteSessionId: "remote-1",
  514. },
  515. "/workspace",
  516. ),
  517. ).toEqual({
  518. type: "file",
  519. title: "demo.ts",
  520. path: "/workspace/demo.ts",
  521. workspacePath: "/workspace",
  522. workspaceIdentity: "ssh:test:/workspace",
  523. workspaceRemoteSessionId: "remote-1",
  524. });
  525. expect(
  526. createDiffSourceFilePreviewSource(
  527. {
  528. type: "patch",
  529. title: "Diff",
  530. patch: [
  531. "diff --git a/src/demo.ts b/src/demo.ts",
  532. "--- a/src/demo.ts",
  533. "+++ b/src/demo.ts",
  534. "@@ -1 +1 @@",
  535. "-old",
  536. "+new",
  537. ].join("\n"),
  538. },
  539. "/workspace",
  540. ),
  541. ).toEqual({
  542. type: "file",
  543. title: "demo.ts",
  544. path: "/workspace/src/demo.ts",
  545. workspacePath: "/workspace",
  546. });
  547. });
  548. it("uses deletion patch headers but refuses relative paths without workspace scope", () => {
  549. const deletionPatch = [
  550. "diff --git a/src/deleted.ts b/src/deleted.ts",
  551. "--- a/src/deleted.ts",
  552. "+++ /dev/null",
  553. "@@ -1 +0,0 @@",
  554. "-old",
  555. ].join("\n");
  556. expect(
  557. getDiffSourceFileTarget({
  558. type: "patch",
  559. title: "Diff",
  560. patch: deletionPatch,
  561. }),
  562. ).toBe("src/deleted.ts");
  563. expect(
  564. createDiffSourceFilePreviewSource({
  565. type: "patch",
  566. title: "Diff",
  567. patch: deletionPatch,
  568. }),
  569. ).toBeNull();
  570. });
  571. });