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

test("Selected host-only label keeps its orange dot after search changes", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("Label filter proof");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], {cwd: project}));
  await browser.emulateMedia({ colorScheme: "dark" });
  await t3.open("/pull-requests");
  await screen.getByRole("button", "Filters", {exact:true}).waitFor({timeout:60000});
  await screen.getByRole("status", /Loading pull requests/).waitFor({state:"hidden", timeout:120000});
  await screen.getByRole("button", "Filters", {exact:true}).tap();
  await screen.getByRole("menuitem", /^Labels/).tap();
  const search = screen.getByRole("textbox", "Search labels", {exact:true});
  await search.fill("junk");
  const junk = screen.getByRole("menuitemcheckbox", "junk", {exact:true});
  await junk.waitFor({timeout:60000});
  await junk.tap();
  await screen.getByText("Nothing under these filters", {exact:true}).waitFor({timeout:120000});
  await app.screenshot("selected-junk-before-search-change");
  await search.fill("doc");
  await screen.getByRole("menuitemcheckbox", /documentation/).waitFor({timeout:60000});
  await app.screenshot("selected-junk-after-search-change");
  const color = await browser.evaluate(() => {
    const item = [...document.querySelectorAll('[role="menuitemcheckbox"]')].find(el => el.textContent?.trim() === "junk");
    const dot = item?.querySelector('.rounded-full');
    return dot ? getComputedStyle(dot).backgroundColor : null;
  });
  expect(color).toBe("rgb(244, 148, 60)");
});
