markdownImageGallery.test.ts 3.9 KB

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