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

test("A shared label name includes organization and repository pull requests", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("widgets");
  execFileSync("git", ["remote", "add", "origin", "http://127.0.0.1:33024/acme/widgets.git"], { cwd: project });
  const thread = await t3.thread({ project, title: "Forgejo label filter proof", messages: ["Ready"] });
  await browser.setViewport({ width: 1440, height: 900 });
  await t3.open(thread);
  await t3.open("/pull-requests?host=127.0.0.1%3A33024");
  await screen.getByText("PR without labels", { exact: true }).waitFor({ timeout: 60000 });
  await browser.locator('button[aria-label="Refresh pull requests"]:not(:disabled)').waitFor();
  await app.screenshot("all-four-pull-requests");
  await screen.getByRole("button", /^Filters/).tap();
  await screen.getByRole("menuitem", /^Labels/).tap();
  await screen.getByRole("menuitemcheckbox", /^bug/).waitFor({ timeout: 60000 });
  await app.screenshot("label-suggestions");
  const unusedSuggested = await screen.getByRole("menuitemcheckbox", /^org-unused/).count();
  await screen.getByRole("menuitemcheckbox", /^shared/).tap();
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Escape");
  await screen.getByText("PR with REPO label shared", { exact: true }).waitFor({ timeout: 60000 });
  await screen.getByText("PR without labels", { exact: true }).waitFor({ state: "hidden", timeout: 60000 });
  await browser.locator('button[aria-label="Refresh pull requests"]:not(:disabled)').waitFor();
  await app.screenshot("shared-name-filter");
  await expect(screen.getByText("PR with ORG label shared", { exact: true })).toBeVisible();
  expect(unusedSuggested).toBe(1);
});
