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

test("Sent file mentions preserve literal brackets and tildes", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Notes", { "notes/[TODO].md": "Todo notes", "notes/~~draft~~.md": "Draft notes" });
  const thread = await t3.thread({ project, title: "Review notes", messages: ["Start"] });
  await t3.open("/settings/appearance");
  await screen.getByRole("switch", "Render math").tap();
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.pressSequentially("Check @TODO");
  await screen.getByRole("option", "[TODO].md", { exact: false }).tap();
  await composer.pressSequentially(" and @draft");
  await screen.getByRole("option", "~~draft~~.md", { exact: false }).tap();
  await app.screenshot("composer");
  await composer.press("Enter");
  await screen.getByText("second fixture turn complete", { exact: false }).waitFor();
  await app.screenshot("sent-mentions");
  const rendered = await browser.evaluate(() => {
    const body = [...document.querySelectorAll<HTMLElement>("[data-user-message-body]")].find(el => el.innerText.includes("Check"));
    return { text: body?.innerText, math: body?.querySelectorAll(".katex, math").length, deleted: body?.querySelectorAll("del").length };
  });
  await screen.getByRole("combobox", "Search threads").fill("[TODO].md");
  await expect.poll(() => browser.evaluate(() =>
    document.body.innerText.includes("No threads found") ||
    document.querySelector('[role="listbox"][aria-label="Thread search results"]') !== null
  )).toBe(true);
  await app.screenshot("literal-filename-search");
  expect(rendered.math).toBe(0);
  expect(rendered.deleted).toBe(0);
  expect(rendered.text).toContain("[TODO].md");
  expect(rendered.text).toContain("~~draft~~.md");
  await expect(screen.getByRole("listbox", "Thread search results")).toContainText("[TODO].md");
});
