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

test("Project icon search stays at the same screen position while typing and clearing", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Icon picker demo");
  const thread = await t3.thread({ project, title: "Icon picker demonstration", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("combobox", "Projects").tap();
  await screen.getByRole("option", "Icon picker demo", { exact: true }).hover();
  await browser.locator('button[title="Project settings for Icon picker demo"]').tap();
  await screen.getByRole("button", "Choose a project icon", { exact: true }).tap();
  await screen.getByRole("button", "Icons", { exact: true }).tap();
  const search = screen.getByRole("searchbox", "Search Lucide icons");
  await search.tap();
  const position = () => browser.evaluate(() => {
    const input = document.querySelector('[role="searchbox"][aria-label="Search Lucide icons"]')!;
    return { top: input.getBoundingClientRect().top, focused: document.activeElement === input };
  });
  await app.screenshot("01-empty-query");
  const initial = await position();
  await search.pressSequentially("folder-code");
  await app.screenshot("02-folder-code");
  const matching = await position();
  await search.fill("zzzzzzzz");
  await screen.getByText("No icons found.", { exact: true }).waitFor();
  await app.screenshot("03-no-matches");
  const empty = await position();
  await search.fill("");
  await app.screenshot("04-cleared-query");
  const cleared = await position();
  expect([matching, empty, cleared]).toEqual([initial, initial, initial]);
});
