markdownImageGallery.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { render, waitFor } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import {
  6. clampMarkdownImagePreviewOffset,
  7. MarkdownImage,
  8. normalizeConsecutiveMarkdownImageBlocks,
  9. } from "@/components/ai-elements/markdown-image.js";
  10. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  11. import { rewriteMarkdownArtifactImageSources } from "@zcode/shared";
  12. import { sanitizeImageSourceForLog } from "@/components/ai-elements/image-preview-dialog.js";
  13. import {
  14. imageThumbnailClassName,
  15. imageThumbnailTriggerClassName,
  16. } from "@/components/ai-elements/image-thumbnail-gallery.js";
  17. afterEach(() => vi.restoreAllMocks());
  18. describe("artifact-backed Markdown images", () => {
  19. it("reads an authorized session artifact instead of passing its custom URI to img", async () => {
  20. const ref = "zcode-artifact://session-1/cua-shot";
  21. const rewritten = rewriteMarkdownArtifactImageSources(`![shot](${ref})`);
  22. const src = rewritten.match(/!\[shot\]\(([^)]+)\)/u)?.[1] ?? "";
  23. const readAttachment = vi.fn().mockResolvedValue({
  24. bytes: new Uint8Array([1, 2, 3]),
  25. mediaType: "image/png",
  26. });
  27. vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:cua-shot");
  28. const revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
  29. const rendered = render(
  30. createElement(
  31. ZCodeIntlProvider,
  32. { initialLocale: "zh-CN" },
  33. createElement(MarkdownImage, {
  34. alt: "shot",
  35. src,
  36. sessionId: "session-1",
  37. readAttachment,
  38. }),
  39. ),
  40. );
  41. await waitFor(() =>
  42. expect(rendered.container.querySelector("img")?.getAttribute("src")).toBe("blob:cua-shot"),
  43. );
  44. expect(readAttachment).toHaveBeenCalledWith({ sessionId: "session-1", ref });
  45. expect(rendered.container.innerHTML).not.toContain("zcode-artifact://");
  46. rendered.unmount();
  47. expect(revoke).toHaveBeenCalledWith("blob:cua-shot");
  48. });
  49. });
  50. describe("shared image thumbnail styles", () => {
  51. it("单图使用半宽、高度上限和 xl 圆角", () => {
  52. expect(imageThumbnailTriggerClassName).toContain("max-w-1/2");
  53. expect(imageThumbnailClassName).toContain("max-h-90");
  54. expect(imageThumbnailClassName).toContain("rounded-xl");
  55. expect(imageThumbnailClassName).toContain("object-cover");
  56. });
  57. });
  58. describe("sanitizeImageSourceForLog", () => {
  59. it("清除远程图片 URL 的身份、签名参数和 fragment", () => {
  60. const sanitized = sanitizeImageSourceForLog(
  61. "https://user:pass@example.com/a.png?token=secret#fragment",
  62. );
  63. expect(sanitized).toBe("https://example.com/a.png");
  64. expect(sanitized).not.toMatch(/user|pass|token|secret|fragment/u);
  65. });
  66. it("不把 Base64 图片内容写入日志", () => {
  67. expect(sanitizeImageSourceForLog("data:image/png;base64,SECRET")).toBe("data-url");
  68. });
  69. });
  70. describe("normalizeConsecutiveMarkdownImageBlocks", () => {
  71. it("只移除连续纯图片块之间的空行", () => {
  72. expect(
  73. normalizeConsecutiveMarkdownImageBlocks(
  74. "intro\n\n![one](one.png)\n\n![two](two.png)\n\ncaption",
  75. ),
  76. ).toBe("intro\n\n![one](one.png)\n![two](two.png)\n\ncaption");
  77. });
  78. it("不改写代码围栏中的图片语法", () => {
  79. const markdown = "```md\n![one](one.png)\n\n![two](two.png)\n```";
  80. expect(normalizeConsecutiveMarkdownImageBlocks(markdown)).toBe(markdown);
  81. });
  82. it.each([
  83. ["四反引号不会被三个反引号关闭", "````md\n```\n![one](one.png)\n\n![two](two.png)\n````"],
  84. ["四波浪号不会被三个波浪号关闭", "~~~~md\n~~~\n![one](one.png)\n\n![two](two.png)\n~~~~"],
  85. ["未闭合围栏保持到文末", "```md\n![one](one.png)\n\n![two](two.png)"],
  86. ["带 info string 的 opening fence", "````markdown\n![one](one.png)\n\n![two](two.png)\n````"],
  87. ])("%s", (_name, markdown) => {
  88. expect(normalizeConsecutiveMarkdownImageBlocks(markdown)).toBe(markdown);
  89. });
  90. });
  91. describe("clampMarkdownImagePreviewOffset", () => {
  92. it("图片未超出画布时保持居中", () => {
  93. expect(
  94. clampMarkdownImagePreviewOffset(
  95. { x: 80, y: -60 },
  96. 1,
  97. { width: 400, height: 300 },
  98. { width: 800, height: 600 },
  99. ),
  100. ).toEqual({ x: 0, y: 0 });
  101. });
  102. it("图片放大后限制拖拽边界", () => {
  103. expect(
  104. clampMarkdownImagePreviewOffset(
  105. { x: 500, y: -500 },
  106. 2,
  107. { width: 600, height: 400 },
  108. { width: 800, height: 600 },
  109. ),
  110. ).toEqual({ x: 200, y: -100 });
  111. });
  112. });