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

const MESSAGE = "Tags [WIP] and [done]";

test("Copied brackets stay prose with Render math enabled", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("bracket-copy");
  const thread = await t3.thread({ project, title: "Bracket prose", messages: ["Hello"] });
  await t3.open("/settings/appearance");
  const math = screen.getByRole("switch", "Render math", { exact: true });
  await math.waitFor();
  if ((await math.getAttribute("aria-checked")) !== "true") await math.tap();
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.tap();
  await browser.keyboard.type(MESSAGE);
  await browser.keyboard.press("Enter");
  await screen.getByText("second fixture turn complete").waitFor();
  await browser.evaluate(() => {
    const paragraph = Array.from(document.querySelectorAll(".user-message-markdown p")).find(p => p.textContent === "Tags [WIP] and [done]");
    if (!paragraph) throw new Error("No bracketed user paragraph");
    const range = document.createRange();
    range.selectNodeContents(paragraph);
    window.getSelection()?.removeAllRanges();
    window.getSelection()?.addRange(range);
    return null;
  });
  await browser.keyboard.press("ControlOrMeta+C");
  await composer.tap();
  await browser.keyboard.press("ControlOrMeta+V");
  await app.screenshot("pasted-brackets");
  await browser.keyboard.press("Enter");
  await screen.getByText("first fixture turn complete", { exact: true }).nth(1).waitFor();
  await app.screenshot("sent-brackets");
  await expect(screen.getByText(MESSAGE, { exact: true })).toHaveCount(2);
});
