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

test("Select all in a rendered Markdown attachment selects only its document", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Attachment selection", messages: ["first"] });
  await t3.open(thread);
  await screen.getByRole("textbox").fill(" ");
  await browser.evaluate(() => {
    const input = document.querySelector<HTMLInputElement>('input[type="file"]');
    if (!input) throw new Error("No attachment file input");
    const files = new DataTransfer();
    files.items.add(new File(["# Release notes\n\nRendered attachment body text for the select-all check.\n\nSecond paragraph stays in the document.\n"], "notes.md", { type: "text/markdown" }));
    input.files = files.files;
    input.dispatchEvent(new Event("change", { bubbles: true }));
    return null;
  });
  await screen.getByText("notes.md", { exact: true }).tap();
  await screen.getByText("Rendered attachment body text for the select-all check.", { exact: true }).tap();
  await browser.keyboard.press("Control+a");
  await app.screenshot("rendered-document-after-control-a");
  const selected = await browser.evaluate(() => window.getSelection()?.toString() ?? "");
  expect(selected.trim()).toBe("Release notes\nRendered attachment body text for the select-all check.\n\nSecond paragraph stays in the document.");
});
