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

test("Typed viewer aliases select the canonical author and clearing the author removes the typed qualifier", WEB, async ({t3, app, browser, screen}) => {
  await t3.project("Author filter proof");
  await t3.open("/pull-requests");
  const search = screen.getByRole("searchbox", "Search pull requests");
  const observations: {query:string; selected:string|null; after:string|null}[] = [];
  for (const query of ["author:@me", "author:@ME", "author:ME", "author:me", "author:@me author:me", "author:me author:@me", "author:OctoCat"]) {
    await search.fill(query);
    await search.press("Enter");
    await screen.getByText(/Nothing matches/).waitFor();
    await screen.getByRole("button", /^Filters/).tap();
    await screen.getByRole("menuitem", /^Author/).hover();
    await screen.getByRole("menuitemradio", "Anyone", {exact:true}).waitFor();
    const selected = await browser.locator('[role="menuitemradio"][aria-checked="true"] .truncate').textContent();
    await app.screenshot(`author-${query.replaceAll(/[^a-zA-Z0-9]/g,"-")}-menu`);
    await screen.getByRole("menuitemradio", "Anyone", {exact:true}).tap();
    await browser.keyboard.press("Escape");
    await browser.keyboard.press("Escape");
    await screen.getByText("No pull requests", {exact:true}).waitFor();
    const after = await search.getAttribute("value");
    await app.screenshot(`author-${query.replaceAll(/[^a-zA-Z0-9]/g,"-")}-after-anyone`);
    observations.push({query,selected,after});
  }
  expect(observations.map(({query,selected}) => ({query,selected:selected?.trim()}))).toEqual([
    ...["author:@me", "author:@ME", "author:ME", "author:me", "author:@me author:me", "author:me author:@me"].map(query=>({query,selected:"me"})),
    {query:"author:OctoCat",selected:"OctoCat"}
  ]);
});
