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

test("Settings project scope matches canonically equivalent accents", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Préprod");
  for (let i = 0; i < 6; i++) await t3.project(`Project ${i}`);
  await t3.thread({ project, title: "Scope search demo", messages: ["Hello"], model: "showcase" });
  await t3.open("/settings/projects");
  await screen.getByRole("combobox", "Project scope: All projects", { exact: true }).tap();
  const search = screen.getByPlaceholder("Search projects", { exact: true });
  await search.waitFor();
  const insert = async (query: string) => {
    await browser.evaluate((value) => {
      const input = document.querySelector('input[placeholder="Search projects"]') as HTMLInputElement;
      input.focus();
      input.select();
      document.execCommand("insertText", false, value);
      if (input.value !== value) throw new Error("Query code points changed");
    }, query);
  };
  await insert("Préprod");
  await screen.getByRole("option", "Préprod", { exact: true }).waitFor();
  await app.screenshot("precomposed-control");
  await insert("Pre\u0301prod");
  try {
    await expect(screen.getByRole("option", "Préprod", { exact: true })).toBeVisible();
  } finally {
    await app.screenshot("decomposed-query");
  }
  await screen.getByRole("option", "Préprod", { exact: true }).tap();
  await expect(screen.getByRole("combobox", "Project scope: Préprod", { exact: true })).toBeVisible();
});
