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

test("A sent file mention preserves a literal ampersand entity and opens the selected file", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("demo", {
    "docs/R&copy;.md": "LITERAL-AMP-COPY FILE: this is docs/R&copy;.md\n",
    "docs/R©.md": "WRONG TARGET: this is docs/R©.md\n",
  });
  const thread = await t3.thread({ project, title: "Literal ampersand file mention", messages: ["Show me around"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.pressSequentially("@R");
  await screen.getByRole("option", "R&copy;.md", { exact: false }).tap();
  await composer.pressSequentially(" please read this file");
  await browser.keyboard.press("Enter");
  const link = screen.getByRole("link", /^R(?:&copy;|©)\.md$/);
  await link.waitFor();
  const label = await link.textContent();
  await screen.getByText("second fixture turn complete", { exact: true }).waitFor();
  await app.screenshot("sent-file-mention");
  await link.tap();
  const editorText = () => browser.evaluate(() => {
    for (const host of document.querySelectorAll("*")) {
      const content = host.shadowRoot?.querySelector("[data-content]");
      if (content) return content.textContent ?? "";
    }
    return "";
  });
  try {
    await expect.poll(editorText).toContain("LITERAL-AMP-COPY FILE");
    expect(label).toBe("R&copy;.md");
  } finally {
    await app.screenshot("opened-mentioned-file");
  }
});
