/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Pasting a mixed chat message keeps its text and picture", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("Clipboard demo");
  await t3.open();
  const composer = screen.getByRole("textbox");
  await composer.tap();
  await browser.evaluate(async () => {
    const canvas = new OffscreenCanvas(32, 32);
    const context = canvas.getContext("2d")!;
    context.fillStyle = "#228be6";
    context.fillRect(0, 0, 32, 32);
    context.fillStyle = "#ffffff";
    context.fillRect(8, 8, 16, 16);
    const file = new File([await canvas.convertToBlob({ type: "image/png" })], "image.png", { type: "image/png" });
    const clipboardData = new DataTransfer();
    clipboardData.setData("text/plain", "Look at this screenshot");
    clipboardData.setData("text/html", '<div>Look at this screenshot</div><div><img src="file:///tmp/qq/image.png"></div>');
    clipboardData.items.add(file);
    const editor = document.querySelector('[contenteditable="true"]');
    if (!editor) throw new Error("No composer editor");
    editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }));
    return null;
  });
  await app.screenshot("mixed-message-pasted");
  await expect(composer).toContainText("Look at this screenshot");
  await expect(screen.getByRole("img", "image.png")).toBeVisible();
});
