import { expect } from "e2e";
import { test, WEB, runPromise } from "./t3.ts";
import { run } from "./shell.ts";

test("Reference suggestions keep complete titles with combining marks", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("reference-demo");
  await runPromise(run("git", ["remote", "add", "origin", "https://gitlab.com/julius54/factory-44511-readable-proof.git"], { cwd: project }));
  const thread = await t3.thread({ project, title: "Reference completion", messages: ["Ready"] });
  await t3.open(thread);
  await t3.open("/pull-requests?repository=julius54%2Ffactory-44511-readable-proof&number=1&host=gitlab.com&selectedUrl=https%3A%2F%2Fgitlab.com%2Fjulius54%2Ffactory-44511-readable-proof%2F-%2Fmerge_requests%2F1");
  await screen.getByRole("button", "Comment on merge request", { exact: true }).tap();
  const composer = screen.getByRole("combobox", "Comment on this merge request", { exact: true });
  const results: boolean[] = [];
  const capture = async (label: string, expected: string | null) => {
    if (expected) {
      // Wait for the real forge lookup on controls; completed queries may have no popup on base.
      const open = await browser.evaluate(() => document.querySelector('textarea[aria-label="Comment on this merge request"]')?.getAttribute("aria-expanded") === "true");
      if (label.includes("control") || label.includes("bare") || open) {
        await screen.getByRole("option").filter({ hasText: expected === "café menu" ? "cafe\u0301 menu" : expected }).first().waitFor();
      }
    }
    await screen.getByText("Searching...", { exact: true }).waitFor({ state: "hidden" });
    await screen.getByText("Searching users...", { exact: true }).waitFor({ state: "hidden" });
    await browser.evaluate(async () => { await document.fonts.ready; await Promise.all(document.getAnimations().map(a => a.finished.catch(() => {}))); return null; });
    await app.screenshot(label);
    if (label.includes("complete")) results.push(await browser.evaluate((text) => {
      const input = document.querySelector('textarea[aria-label="Comment on this merge request"]');
      const options = [...document.querySelectorAll('[role="option"]')].map(e => e.textContent ?? "");
      return text === null ? input?.getAttribute("aria-expanded") === "false" : input?.getAttribute("aria-expanded") === "true" && options.some(s => s.normalize("NFC").includes(text.normalize("NFC")));
    }, expected));
  };
  await composer.fill("#unr");
  await capture("001-unrelated-control", "unrelated bug");
  await composer.fill("#क");
  await capture("002-devanagari-issue-control", "किताब");
  await browser.keyboard.type("िताब");
  await capture("003-devanagari-issue-complete", "किताब");
  await composer.fill("see !क");
  await capture("004-devanagari-mr-control", "किताब");
  await browser.keyboard.type("िताब");
  await capture("005-devanagari-mr-complete", "किताब");
  await composer.fill("#cafe");
  await capture("006-accent-control", "café menu");
  await browser.keyboard.type("\u0301");
  await capture("007-decomposed-accent-complete", "café menu");
  await composer.fill("#");
  await capture("008-bare-issue-sigil", "किताब");
  await composer.fill("!");
  await capture("009-bare-mr-sigil", "किताब");
  await composer.fill("#\u0301");
  await capture("010-leading-combining-mark-invalid", null);
  expect(results).toEqual(Array(3).fill(true));
});
