conversation-session-image-preview-edit.test.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {
  2. getV4ComposerAttachments,
  3. getV4ComposerText,
  4. getV4Messages,
  5. pasteV4ComposerFileAttachment,
  6. prepareV4ConversationE2E,
  7. setV4ComposerText,
  8. } from "#e2e/helpers/v4-conversation";
  9. import { countUpstreamRequests } from "#e2e/helpers/conversation-session-network";
  10. describe("IPE04 图片编辑保存到当前输入框", () => {
  11. before(async function () {
  12. this.timeout(180000);
  13. await prepareV4ConversationE2E();
  14. });
  15. it("追加 PNG,保留原附件和文字且不发送", async () => {
  16. await setV4ComposerText("E2E_IMAGE_PREVIEW_EDIT existing draft");
  17. const png = await browser.execute(() => {
  18. const canvas = document.createElement("canvas");
  19. canvas.width = 320;
  20. canvas.height = 240;
  21. const context = canvas.getContext("2d")!;
  22. context.fillStyle = "#dddddd";
  23. context.fillRect(0, 0, 320, 240);
  24. return canvas.toDataURL("image/png").split(",")[1]!;
  25. });
  26. await pasteV4ComposerFileAttachment("original.png", { mimeType: "image/png", base64: png });
  27. await browser.waitUntil(async () => (await getV4ComposerAttachments()).length === 1);
  28. const original = await getV4ComposerAttachments();
  29. const messages = await getV4Messages();
  30. const requests = await countUpstreamRequests({});
  31. await $('[data-testid^="v4-attachment-"][data-upload-status][role="button"]').click();
  32. await browser.waitUntil(
  33. () =>
  34. browser.execute(() => {
  35. const image = document.querySelector<HTMLImageElement>(
  36. "[data-image-preview-surface] img",
  37. );
  38. return image?.naturalWidth === 320 && image.getBoundingClientRect().width > 0;
  39. }),
  40. { timeout: 10000, timeoutMsg: "原图未解码,不能开始标注" },
  41. );
  42. await browser.executeAsync((done) => {
  43. const dialog = document.querySelector('[role="dialog"]');
  44. Promise.all(dialog?.getAnimations().map((animation) => animation.finished) ?? []).then(() =>
  45. done(),
  46. );
  47. });
  48. await $("[data-image-preview-annotate]").click();
  49. // 新版标注栏按需展开;保存按钮只在有标注后出现在图形下方,因此这里只保留 11-02 工具栏修订的“无橡皮擦”断言。
  50. const eraserCount = await browser.execute(
  51. () => document.querySelectorAll('[data-image-preview-tool="eraser"]').length,
  52. );
  53. expect(eraserCount).toBe(0);
  54. await $('[data-image-preview-tool="pen"]').click();
  55. const surface = await $("[data-image-preview-surface]");
  56. const location = await surface.getLocation();
  57. await browser.performActions([
  58. {
  59. type: "pointer",
  60. id: "mouse",
  61. parameters: { pointerType: "mouse" },
  62. actions: [
  63. {
  64. type: "pointerMove",
  65. duration: 0,
  66. origin: "viewport",
  67. x: Math.round(location.x + 40),
  68. y: Math.round(location.y + 40),
  69. },
  70. { type: "pointerDown", button: 0 },
  71. { type: "pointerMove", duration: 200, origin: "pointer", x: 100, y: 60 },
  72. { type: "pointerUp", button: 0 },
  73. ],
  74. },
  75. ]);
  76. await browser.releaseActions();
  77. await $("[data-image-preview-save]").click();
  78. await browser.waitUntil(async () => (await getV4ComposerAttachments()).length === 2, {
  79. timeout: 15000,
  80. timeoutMsg: "保存没有追加新图片",
  81. });
  82. expect(await getV4ComposerText()).toBe("E2E_IMAGE_PREVIEW_EDIT existing draft");
  83. const attachments = await getV4ComposerAttachments();
  84. expect(attachments[0]?.text).toBe(original[0]?.text);
  85. expect(attachments[1]?.text).toBe("original-edited.png");
  86. const savedSize = await browser.execute(async () => {
  87. const image = document.querySelector<HTMLImageElement>('img[alt="original-edited.png"]')!;
  88. await image.decode();
  89. return [image.naturalWidth, image.naturalHeight];
  90. });
  91. expect(savedSize).toEqual([320, 240]);
  92. expect(await getV4Messages()).toEqual(messages);
  93. expect(await countUpstreamRequests({})).toBe(requests);
  94. await $('[role="dialog"]').waitForExist({ reverse: true });
  95. });
  96. });