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

test("Team-requested pull requests remain visible in Reviewing", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("trpc");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/trpc/trpc.git"], { cwd: project }));
  await t3.open("/pull-requests?repository=trpc%2Ftrpc&involvement=all&state=open");
  const target = screen.getByText("fix(server): accept binary inputs in the Fastify plugin", { exact: true });
  await target.waitFor({ timeout: 90_000 });
  await browser.evaluate(() => {
    [...document.querySelectorAll("button")].find(el => el.textContent?.includes("fix(server): accept binary inputs in the Fastify plugin"))?.scrollIntoView({ block: "center" });
    return null;
  });
  await screen.getByRole("button", "Refresh pull requests", { disabled: false }).waitFor({ timeout: 90_000 });
  await app.screenshot("all-team-review-control");
  await app.open("/pull-requests?repository=trpc%2Ftrpc&involvement=reviewing&state=open");
  await screen.getByRole("button", "Refresh pull requests").tap();
  await screen.getByRole("button", "Refresh pull requests", { disabled: false }).waitFor({ timeout: 90_000 });
  try {
    await expect(target).toBeVisible({ timeout: 30_000 });
  } finally {
    await app.screenshot("reviewing-team-request");
    await screen.getByRole("button", /^Filters/).tap();
    await screen.getByRole("menuitem", /^Involvement/).tap();
    await app.screenshot("reviewing-selected-filter");
  }
});
