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

const TITLE = "fix(server): fast worktree checkout no longer breaks submodules under submodule.recurse=true";
test("Loaded PRs survive non-adjacent partial search terms", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("Search proof");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], {cwd: project}));
  await t3.open("/pull-requests?involvement=all&state=open");
  await screen.getByText(TITLE, {exact:true}).waitFor({timeout:60000});
  await app.screenshot("loaded-pr");
  const input = screen.getByRole("searchbox", "Search pull requests");
  const settled = (query: string) => browser.evaluate((query) => {
    const input = document.querySelector('[role="searchbox"]');
    return new URLSearchParams(location.search).get("q") === query && input !== null && !input.closest('[data-slot="input-group"]')?.querySelector('[role="status"]');
  }, query);
  await input.fill("submod");
  await input.press("Enter");
  await expect.poll(() => settled("submod")).toBe(true);
  await screen.getByText(TITLE, {exact:true}).waitFor();
  await app.screenshot("single-partial-word");
  await input.fill("submod checko");
  await input.press("Enter");
  await expect.poll(() => settled("submod checko")).toBe(true);
  await app.screenshot("multi-word-partial-search");
  await expect(screen.getByText(TITLE, {exact:true})).toBeVisible();
});
