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

test("Selected host author keeps avatar across other and empty searches", WEB, async ({t3, app, browser, screen}) => {
  await t3.project("Pull request demo", {".git/config": '[remote "origin"]\nurl = https://github.com/pingdotgg/t3code.git\nfetch = +refs/heads/*:refs/remotes/origin/*\n'});
  await t3.open("/pull-requests");
  await screen.getByRole("button", /^Filters/).waitFor();
  await browser.locator('[role="status"][aria-label="Loading pull requests"]').waitFor({state:"hidden",timeout:60000});
  await screen.getByRole("button", /^Filters/).tap();
  await screen.getByRole("menuitem", /^Author/).tap();
  const search = screen.getByPlaceholder("Search authors");
  await search.fill("bowd");
  const author = screen.getByRole("menuitemradio", "bowd", {exact:true});
  await author.waitFor({timeout:60000});
  await browser.locator('[role="menuitemradio"]:has-text("bowd") img').waitFor();
  await author.tap();
  await screen.getByText("Nothing under these filters", {exact:true}).waitFor({timeout:60000});
  await app.screenshot("selected-host-author");
  await search.fill("t3");
  await screen.getByRole("menuitemradio", "t3dotgg", {exact:true}).waitFor({timeout:60000});
  await browser.locator('[role="menu"]:has(input[placeholder="Search authors"]) [role="status"]').waitFor({state:"hidden",timeout:60000});
  await app.screenshot("after-other-search");
  const otherHasAvatar = await browser.evaluate(() => !![...document.querySelectorAll('[role="menuitemradio"]')].find(e=>e.textContent?.trim()==="bowd")?.querySelector('img'));
  await search.fill("zqxjzqxj");
  await browser.locator('[role="menuitemradio"]:has-text("t3dotgg")').waitFor({state:"hidden",timeout:60000});
  await browser.locator('[role="menu"]:has(input[placeholder="Search authors"]) [role="status"]').waitFor({state:"hidden"});
  await app.screenshot("after-empty-search");
  const emptyHasAvatar = await browser.evaluate(() => !![...document.querySelectorAll('[role="menuitemradio"]')].find(e=>e.textContent?.trim()==="bowd")?.querySelector('img'));
  await expect(otherHasAvatar && emptyHasAvatar).toBe(true);
});
