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

test("Sent composer mentions open punctuation paths", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("punctuation-demo", {
    "C#/Program.cs": "// Punctuation file proof\nclass Program {}\n",
    "docs/R&D/notes.md": "Research and development proof\n",
    "icon@2x.png": new Uint8Array(Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=", "base64")),
  });
  const thread = await t3.thread({ project, title: "Punctuation file mentions", messages: ["Ready"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox", "Message Punctuation file mentions");
  const opened: string[] = [];
  for (const [query, name, content] of [
    ["Program", "Program.cs", "class Program"],
    ["notes", "notes.md", "Research and development proof"],
    ["icon", "icon@2x.png", "image"],
  ]) {
    await composer.pressSequentially(`Review @${query}`);
    await screen.getByRole("option", name, { exact: false }).tap();
    await composer.press("Enter");
    const link = screen.getByRole("link", new RegExp(`^${name.replaceAll(".", "\\.")}`));
    await link.waitFor();
    await link.tap();
    let didOpen = false;
    try {
      await expect.poll(() => browser.evaluate(({ content, name }) => {
        if (content === "image") return [...document.querySelectorAll("img")].some(img => img.alt.includes(name));
        for (const host of document.querySelectorAll("*")) {
          const text = host.shadowRoot?.querySelector("[data-content]")?.textContent;
          if (text?.includes(content)) return true;
        }
        return false;
      }, { content, name })).toBe(true);
      didOpen = true;
    } catch {}
    opened.push(`${name}:${didOpen}`);
    await app.screenshot(`sent-${query}-file-panel`);
  }
  expect(opened).toEqual(["Program.cs:true", "notes.md:true", "icon@2x.png:true"]);
});
