import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { run } from "./shell.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("Pasted negated scalar qualifiers preserve matching GitHub PRs", WEB, async ({ t3, app, screen, browser }) => {
  const seed = await t3.project("Search control");
  const repository = `${seed}/../pr-search-fixture`;
  await runPromise(run("git", ["clone", "--quiet", seed, repository]));
  await runPromise(run("git", ["remote", "set-url", "origin", "https://github.com/only21mil-fixtures/pr-watch-filter-fixture.git"], { cwd: repository }));
  const added = await runPromise(makeState(T3_HOME).cli(["project", "add", repository]));
  const id = /Added project (\S+) /.exec(added.stdout)?.[1];
  if (!id) throw new Error("Fixture project was not added");
  t3.trackProject(id);
  await t3.open(`/pull-requests?selectedProjectId=${id}`);
  const title = "Fixture: PR media preview after tab-indented fence";
  await screen.getByText(title, { exact: true }).waitFor();
  const search = screen.getByPlaceholder("Search pull requests, or label:bug");
  const searchFor = async (query: string) => {
    await search.fill(query);
    await search.press("Enter");
    await browser.locator('[data-slot="input-group"]:has(input[aria-label="Search pull requests"]) svg[role="status"]').waitFor({ state: "hidden" });
  };
  const failures: unknown[] = [];
  await searchFor("Fixture");
  await screen.getByText(title, { exact: true }).waitFor();
  await app.screenshot("control-fixture-search");
  await searchFor("-author:app/dependabot Fixture");
  try {
    await expect(screen.getByText(title, { exact: true })).toBeVisible();
  } catch (error) {
    failures.push(error);
  } finally {
    await app.screenshot("negated-author-search");
  }
  await searchFor("-review:approved -checks:passing -status:failure Fixture");
  try {
    await expect(screen.getByText(title, { exact: true })).toBeVisible();
  } catch (error) {
    failures.push(error);
  } finally {
    await app.screenshot("negated-review-checks-status-search");
  }
  if (failures.length > 0) throw failures[0];
});
