import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Decomposed label search keeps the composed loaded label", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("Accent filter proof");
  const git = (...args: string[]) => execFileSync("git", args, {cwd:project, stdio:"pipe"});
  git("remote", "add", "origin", "https://dev.azure.com/t3code-factory-test/t3-factory-test/_git/t3-factory-test");
  git("-c", 'credential.helper=!f() { echo username=pat; echo "password=$AZURE_DEVOPS_EXT_PAT"; }; f', "fetch", "origin", "main");
  git("reset", "--hard", "FETCH_HEAD");
  await t3.open("/pull-requests");
  await screen.getByText("Accent filter proof", {exact:true}).last().waitFor({timeout:60000});
  await screen.getByRole("button", /^Filters/).tap();
  await screen.getByRole("menuitem", /^Labels/).hover();
  const search = screen.getByPlaceholder("Search labels");
  await screen.getByRole("menuitemcheckbox", /amélioration/).waitFor();
  await search.fill("amélioration");
  await screen.getByRole("menuitemcheckbox", /amélioration/).waitFor();
  await new Promise(resolve => setTimeout(resolve, 5000));
  await app.screenshot("composed-label-control");
  await search.fill("ame\u0301lioration");
  await browser.locator('input[placeholder="Search labels"]:not([aria-busy="true"])').waitFor();
  // Observe the completed host search, rather than capturing the debounce interval.
  await new Promise(resolve => setTimeout(resolve, 5000));
  await app.screenshot("decomposed-label-result");
  await expect(screen.getByRole("menuitemcheckbox", /amélioration/)).toBeVisible();
});
