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

test("PDF clipboard filename stages an attachment without inserting text", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("clipboard-demo");
  const thread = await t3.thread({ project, title: "PDF clipboard paste", messages: ["first"], model: "default" });
  await t3.open(thread);
  await screen.getByRole("textbox").tap();
  await browser.evaluate(() => {
    const editor = document.activeElement;
    if (!(editor instanceof HTMLElement) || !editor.isContentEditable) throw new Error("Composer not focused");
    const clipboardData = new DataTransfer();
    clipboardData.items.add(new File(['%PDF-1.4\n%%EOF\n'], 'report.pdf', { type: 'application/pdf' }));
    clipboardData.setData('text/plain', 'report.pdf');
    editor.dispatchEvent(new ClipboardEvent('paste', { clipboardData, bubbles: true, cancelable: true }));
    return null;
  });
  // A screenshot records the actual composer result before the regression assertion.
  await app.screenshot("pdf-with-filename-pasted");
  await expect(screen.getByRole("button", { name: "File attachment, report.pdf, 1 KB", exact: true })).toBeVisible();
  const text = await browser.evaluate(() => (() => {
    const editor = document.querySelector('[data-testid="composer-editor"]')?.cloneNode(true) as HTMLElement;
    editor.querySelectorAll('[contenteditable="false"]').forEach((node) => node.remove());
    return editor.textContent?.trim() ?? "";
  })());
  expect(text).toBe("");
});
