conversationShareInputImages.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import type { ConversationRow } from "@zcode/shared/zcode-protocol-v4";
  5. import { ConversationShareReadonlyTimeline } from "@/v4/ConversationShareReadonlyTimeline.js";
  6. const base = { turnId: "turn-1", productTurnId: "product-1", createdAt: 1 };
  7. const rows: ConversationRow[] = [
  8. {
  9. ...base,
  10. rowId: 1,
  11. createdAtSeq: 1,
  12. kind: "turnHeader",
  13. origin: "userInput",
  14. state: "completedSuccess",
  15. startedAt: 1,
  16. },
  17. {
  18. ...base,
  19. rowId: 2,
  20. createdAtSeq: 2,
  21. kind: "userInput",
  22. origin: "realUser",
  23. text: "Describe this image",
  24. attachments: [
  25. {
  26. ref: "zcode-artifact://share/image-1",
  27. previewRef: "zcode-artifact://share/thumb-1",
  28. fileName: "photo.png",
  29. mime: "image/png",
  30. bytes: 120,
  31. },
  32. {
  33. ref: "zcode-artifact://share/text-1",
  34. fileName: "notes.txt",
  35. mime: "text/plain",
  36. bytes: 20,
  37. },
  38. ],
  39. },
  40. ];
  41. const original = "https://resources.example.com/photo.png";
  42. function render(urls: ReadonlyMap<string, string>) {
  43. return renderToStaticMarkup(
  44. createElement(ConversationShareReadonlyTimeline, { rows, artifactUrls: urls }),
  45. );
  46. }
  47. describe("shared user input images", () => {
  48. it("previews the image while opening the original and keeping ordinary file pills", () => {
  49. const html = render(
  50. new Map([
  51. ["image-1", original],
  52. ["thumb-1", "https://resources.example.com/thumb.png"],
  53. ["text-1", "https://resources.example.com/notes.txt"],
  54. ]),
  55. );
  56. expect(html).toMatch(/<img[^>]*src="https:\/\/resources.example.com\/thumb.png"/u);
  57. expect(html).toContain('alt="photo.png"');
  58. expect(html).toContain(`href="${original}"`);
  59. expect(html).toContain('data-v4-user-input-attachment-pill="true"');
  60. expect(html).toContain("notes.txt");
  61. });
  62. it("uses the original image when there is no separate thumbnail URL", () => {
  63. expect(render(new Map([["image-1", original]]))).toMatch(
  64. /<img[^>]*src="https:\/\/resources.example.com\/photo.png"/u,
  65. );
  66. });
  67. it("keeps unavailable images as file metadata without a broken image", () => {
  68. const html = render(new Map());
  69. expect(html).not.toContain("<img");
  70. expect(html).toContain("photo.png");
  71. expect(html).toContain('aria-disabled="true"');
  72. });
  73. });