chatMediaAttachmentPreviewDialog.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /** @vitest-environment jsdom */
  2. import { createElement, type ReactNode } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ChatMediaAttachmentPreviewDialog } from "@/ChatMediaAttachmentPreviewDialog.js";
  6. vi.mock("@/components/ui/dialog.js", () => ({
  7. Dialog: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  8. DialogContent: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  9. DialogDescription: ({ children }: { children: ReactNode }) =>
  10. createElement("div", null, children),
  11. DialogHeader: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  12. DialogTitle: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  13. }));
  14. vi.mock("@/components/ui/pdf-viewer.js", () => ({
  15. PdfViewer: ({ labels }: { labels?: Record<string, string> }) =>
  16. createElement(
  17. "div",
  18. {
  19. "data-testid": "pdf-viewer",
  20. "data-labels": JSON.stringify(labels ?? {}),
  21. },
  22. "pdf",
  23. ),
  24. }));
  25. vi.mock("@/i18n/IntlProvider.js", () => ({
  26. useZCodeIntl: () => ({
  27. intl: { formatMessage: ({ id }: { id: string }) => id },
  28. }),
  29. }));
  30. afterEach(cleanup);
  31. describe("ChatMediaAttachmentPreviewDialog", () => {
  32. it("renders topic history as readable original text without interpreting markup", () => {
  33. const view = render(
  34. createElement(ChatMediaAttachmentPreviewDialog, {
  35. attachment: {
  36. filename: "topic-history.txt",
  37. mediaType: "text/plain",
  38. textContent: "Alice | ou_a\n<script>history</script>",
  39. },
  40. open: true,
  41. onOpenChange: vi.fn(),
  42. }),
  43. );
  44. expect(view.container.querySelector("pre")?.textContent).toBe(
  45. "Alice | ou_a\n<script>history</script>",
  46. );
  47. expect(view.container.querySelector("script")).toBeNull();
  48. });
  49. it("loads every video MIME through the native inline player without autoplay", () => {
  50. const view = render(
  51. createElement(ChatMediaAttachmentPreviewDialog, {
  52. attachment: {
  53. filename: "demo.mov",
  54. mediaType: "video/quicktime",
  55. url: "blob:video-preview",
  56. },
  57. open: true,
  58. onOpenChange: vi.fn(),
  59. }),
  60. );
  61. const video = view.container.querySelector("video");
  62. expect(video).not.toBeNull();
  63. expect(video?.hasAttribute("controls")).toBe(true);
  64. expect(video?.hasAttribute("playsinline")).toBe(true);
  65. expect(video?.hasAttribute("autoplay")).toBe(false);
  66. expect(video?.getAttribute("src")).toBe("blob:video-preview");
  67. expect(screen.getByRole("status").textContent).toBe("chat.attachments.preview.videoLoading");
  68. fireEvent.loadedMetadata(video!);
  69. expect(screen.queryByRole("status")).toBeNull();
  70. expect(video?.className).not.toContain("invisible");
  71. });
  72. it("shows a non-blocking format notice when the native player cannot decode the video", () => {
  73. const onOpenChange = vi.fn();
  74. const view = render(
  75. createElement(ChatMediaAttachmentPreviewDialog, {
  76. attachment: {
  77. filename: "unsupported.mov",
  78. mediaType: "video/quicktime",
  79. url: "blob:unsupported-video",
  80. },
  81. open: true,
  82. onOpenChange,
  83. }),
  84. );
  85. const video = view.container.querySelector("video");
  86. fireEvent.error(video!);
  87. expect(screen.getByRole("alert").textContent).toBe("chat.attachments.preview.videoUnsupported");
  88. expect(view.container.querySelector("video")).toBeNull();
  89. expect(onOpenChange).not.toHaveBeenCalled();
  90. view.rerender(
  91. createElement(ChatMediaAttachmentPreviewDialog, {
  92. attachment: {
  93. filename: "unsupported.mov",
  94. mediaType: "video/quicktime",
  95. url: "blob:unsupported-video",
  96. },
  97. open: false,
  98. onOpenChange,
  99. }),
  100. );
  101. expect(view.container.querySelector("video")).not.toBeNull();
  102. });
  103. it("preserves the existing image preview branch", () => {
  104. const view = render(
  105. createElement(ChatMediaAttachmentPreviewDialog, {
  106. attachment: {
  107. filename: "screen.png",
  108. mediaType: "image/png",
  109. url: "blob:image-preview",
  110. },
  111. open: true,
  112. onOpenChange: vi.fn(),
  113. }),
  114. );
  115. expect(screen.getByRole("img", { name: "screen.png" }).getAttribute("src")).toBe(
  116. "blob:image-preview",
  117. );
  118. expect(view.container.querySelector("video")).toBeNull();
  119. });
  120. it("passes the complete localized PDF control label set to PdfViewer", async () => {
  121. render(
  122. createElement(ChatMediaAttachmentPreviewDialog, {
  123. attachment: {
  124. filename: "report.pdf",
  125. mediaType: "application/pdf",
  126. url: "blob:pdf-preview",
  127. },
  128. open: true,
  129. onOpenChange: vi.fn(),
  130. }),
  131. );
  132. const viewer = await screen.findByTestId("pdf-viewer");
  133. const labels = JSON.parse(viewer.getAttribute("data-labels") ?? "{}");
  134. for (const key of [
  135. "loading",
  136. "loadError",
  137. "noData",
  138. "previousPage",
  139. "nextPage",
  140. "pageInput",
  141. "zoomIn",
  142. "zoomOut",
  143. ]) {
  144. expect(labels[key]).toBeTruthy();
  145. }
  146. expect(labels.previousPage).toBe("codeViewer.pdf.previousPage");
  147. expect(labels.zoomIn).toBe("codeViewer.pdf.zoomIn");
  148. });
  149. });