attachmentsPreview.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. Attachment,
  6. AttachmentInfo,
  7. AttachmentPreview,
  8. AttachmentRemove,
  9. Attachments,
  10. } from "@/components/ai-elements/attachments.js";
  11. function renderAttachmentWithOpen(openable: boolean) {
  12. return renderToStaticMarkup(
  13. createElement(
  14. Attachments,
  15. { variant: "inline" },
  16. createElement(
  17. Attachment,
  18. {
  19. data: {
  20. id: "screenshot",
  21. type: "file",
  22. filename: "screenshot.png",
  23. mediaType: "image/png",
  24. url: "blob:zcode-preview",
  25. },
  26. onOpen: openable ? () => undefined : undefined,
  27. openLabel: "Open image preview",
  28. },
  29. "screenshot.png",
  30. ),
  31. ),
  32. );
  33. }
  34. describe("Attachment preview affordance", () => {
  35. it("renders uploaded image attachments as keyboard-openable preview triggers", () => {
  36. const html = renderAttachmentWithOpen(true);
  37. expect(html).toContain('role="button"');
  38. expect(html).toContain('tabindex="0"');
  39. expect(html).toContain('aria-label="Open image preview"');
  40. expect(html).toContain("cursor-pointer");
  41. });
  42. it("does not add button semantics when no preview action is provided", () => {
  43. const html = renderAttachmentWithOpen(false);
  44. expect(html).not.toContain('role="button"');
  45. expect(html).not.toContain('tabindex="0"');
  46. expect(html).toContain("cursor-default");
  47. expect(html).not.toContain("cursor-pointer");
  48. });
  49. it("renders the shared play affordance over grid video thumbnails", () => {
  50. const html = renderToStaticMarkup(
  51. createElement(
  52. Attachment,
  53. {
  54. data: {
  55. id: "video",
  56. type: "file",
  57. filename: "demo.mov",
  58. mediaType: "video/quicktime",
  59. url: "blob:video-preview",
  60. },
  61. variant: "grid",
  62. },
  63. createElement(AttachmentPreview),
  64. ),
  65. );
  66. expect(html).toContain('data-attachment-video-play-overlay="true"');
  67. expect(html).toContain("rounded-full");
  68. expect(html).toContain("bg-black/55");
  69. expect(html).toContain("backdrop-blur-sm");
  70. expect(html).toContain("pointer-events-none");
  71. });
  72. it("renders inline clipboard text attachments as a single line with description", () => {
  73. const html = renderToStaticMarkup(
  74. createElement(
  75. Attachments,
  76. { variant: "inline" },
  77. createElement(
  78. Attachment,
  79. {
  80. data: {
  81. description: "128 行",
  82. displayName: "粘贴文本",
  83. id: "pasted-text",
  84. filename: "pasted-text-20260627-153012.txt",
  85. mediaType: "text/plain",
  86. sourceKind: "clipboard-text",
  87. type: "file",
  88. url: "",
  89. },
  90. },
  91. createElement(AttachmentInfo),
  92. ),
  93. ),
  94. );
  95. expect(html).toContain("粘贴文本");
  96. expect(html).toContain("128 行");
  97. expect(html).not.toContain("18 KB");
  98. expect(html).not.toContain("pasted-text-20260627-153012.txt");
  99. });
  100. it("renders inline remove action as a side-pane-style trailing overlay", () => {
  101. const html = renderToStaticMarkup(
  102. createElement(
  103. Attachments,
  104. { variant: "inline" },
  105. createElement(
  106. Attachment,
  107. {
  108. data: {
  109. id: "pasted-text",
  110. filename: "pasted-text.txt",
  111. mediaType: "text/plain",
  112. type: "file",
  113. url: "",
  114. },
  115. onRemove: () => undefined,
  116. },
  117. createElement(AttachmentInfo),
  118. createElement(AttachmentRemove, { label: "移除附件" }),
  119. ),
  120. ),
  121. );
  122. expect(html).toContain("absolute inset-y-0 right-0");
  123. expect(html).toContain("bg-gradient-to-r from-transparent");
  124. expect(html).toContain("pointer-events-auto");
  125. expect(html).toContain('aria-label="移除附件"');
  126. });
  127. });