preview-pane-image-copy.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { mkdir, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { tmpdir } from "node:os";
  4. import { TID_WORKSPACE_FILE_TREE_ROW, testId } from "@zcode/shared/test-ids";
  5. import {
  6. DEFAULT_WORKSPACE,
  7. clearAppData,
  8. waitForDefaultWorkspaceReady,
  9. } from "#e2e/helpers/desktop-app";
  10. import { openProjectFileTree } from "#e2e/helpers/project-sidebar";
  11. // PIC-01/02/05:只验证文件预览和原生剪贴板,不调用模型或修改会话状态。
  12. const IMAGE_DIR = join(DEFAULT_WORKSPACE, "E2E_IMAGE_COPY");
  13. const SCREENSHOTS = process.env.E2E_IMAGE_COPY_SCREENSHOTS ?? join(tmpdir(), "zcode-image-copy");
  14. const SVG = `<svg xmlns="http://www.w3.org/2000/svg" width="720" height="440" viewBox="0 0 720 440">
  15. <rect width="720" height="440" fill="white"/>
  16. <g stroke="#334155" stroke-width="3" fill="none"><circle cx="135" cy="210" r="74"/>
  17. <path d="M135 150v60l48 28"/><circle cx="135" cy="210" r="4" fill="#334155"/>
  18. <path d="M360 128l72 41v82l-72 41-72-41v-82z" fill="#d1fae5" stroke="#059669"/>
  19. <g stroke="#6366f1"><circle cx="585" cy="210" r="76"/><circle cx="585" cy="210" r="48"/><circle cx="585" cy="210" r="20"/></g></g>
  20. <g font-family="sans-serif" font-size="18" fill="#334155" text-anchor="middle">
  21. <text x="135" y="325">Clock</text><text x="360" y="325">Hexagon</text><text x="585" y="325">Circles</text></g></svg>`;
  22. describe("右侧图片预览复制 E2E", () => {
  23. after(async () => {
  24. await browser.electron.restoreAllMocks();
  25. await clearAppData();
  26. await rm(IMAGE_DIR, { recursive: true, force: true });
  27. });
  28. it("PIC-01/02/05:SVG 与 PNG 均通过右键写入原生图片剪贴板", async function () {
  29. this.timeout(150000);
  30. await mkdir(IMAGE_DIR, { recursive: true });
  31. await mkdir(SCREENSHOTS, { recursive: true });
  32. await writeFile(join(IMAGE_DIR, "drawing.svg"), SVG);
  33. await waitForDefaultWorkspaceReady();
  34. // 原生生成 PNG,避免测试依赖外网或与平台无关的图片解码库。
  35. const png = await browser.electron.execute((electron) =>
  36. electron.nativeImage
  37. .createFromBuffer(Buffer.alloc(64 * 48 * 4, 180), { width: 64, height: 48 })
  38. .toPNG()
  39. .toString("base64"),
  40. );
  41. await writeFile(join(IMAGE_DIR, "sample.png"), Buffer.from(png, "base64"));
  42. await openProjectFileTree(DEFAULT_WORKSPACE);
  43. await clickFile(IMAGE_DIR);
  44. for (const [name, width, height] of [
  45. ["drawing.svg", 720, 440],
  46. ["sample.png", 64, 48],
  47. ] as const) {
  48. await clickFile(join(IMAGE_DIR, name));
  49. const image = $("[data-image-preview] img");
  50. await image.waitForDisplayed();
  51. await browser.waitUntil(() =>
  52. browser.execute(() => {
  53. const image = document.querySelector<HTMLImageElement>("[data-image-preview] img");
  54. return Boolean(image?.complete && image.naturalWidth);
  55. }),
  56. );
  57. await browser.electron.execute((electron) => electron.clipboard.clear());
  58. await image.click({ button: "right" });
  59. const copy = $('[role="menuitem"][data-image-preview-copy]');
  60. await copy.waitForDisplayed();
  61. await browser.waitUntil(() =>
  62. browser.execute(() =>
  63. document
  64. .querySelector('[role="menu"]')
  65. ?.getAnimations()
  66. .every((animation) => animation.playState === "finished"),
  67. ),
  68. );
  69. await browser.saveScreenshot(join(SCREENSHOTS, `${name}-context-menu.png`));
  70. await copy.click();
  71. await browser.waitUntil(
  72. async () => {
  73. const result = await browser.electron.execute(async (electron) => {
  74. // Electron 44 移除了 readImage;读取 PNG 后仍验证原始图片的非空状态和尺寸。
  75. const item = (await electron.clipboard.read()).find((entry) =>
  76. entry.types.includes("image/png"),
  77. );
  78. if (!item) return { empty: true, width: 0, height: 0 };
  79. const png = await item.getType("image/png");
  80. const image = electron.nativeImage.createFromBuffer(
  81. Buffer.from(await png.arrayBuffer()),
  82. );
  83. return { empty: image.isEmpty(), ...image.getSize() };
  84. });
  85. return !result.empty && result.width === width && result.height === height;
  86. },
  87. { timeout: 10000, timeoutMsg: `${name} 没有写入原始尺寸的剪贴板图片` },
  88. );
  89. await browser.saveScreenshot(join(SCREENSHOTS, `${name}-copied.png`));
  90. }
  91. await browser.execute(() =>
  92. document.querySelector<HTMLElement>("[data-image-preview]")?.focus(),
  93. );
  94. await browser.keys(["Shift", "F10"]);
  95. await $('[role="menuitem"][data-image-preview-copy]').waitForDisplayed();
  96. await browser.keys("Escape");
  97. });
  98. });
  99. async function clickFile(path: string) {
  100. await browser.waitUntil(
  101. () =>
  102. browser.execute(
  103. (id) => {
  104. const row = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  105. (element) => element.dataset.testid === id,
  106. );
  107. row?.click();
  108. return Boolean(row);
  109. },
  110. testId(TID_WORKSPACE_FILE_TREE_ROW, path),
  111. ),
  112. {
  113. timeout: 15000,
  114. timeoutMsg: `找不到文件树条目 ${path}`,
  115. },
  116. );
  117. }