import { execFileSync } from "node:child_process";
import { expect } from "e2e";

import { test, WEB } from "./t3.ts";

test("Azure shorthand remotes list the real open pull request", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Azure scope evidence");
  const org = process.env.AZURE_DEVOPS_TEST_ORG!;
  const name = process.env.AZURE_DEVOPS_TEST_PROJECT!;
  const organization = new URL(org).pathname.split("/").filter(Boolean)[0]!;
  const remotes = [
    ["modern-shorthand", `${org}/_git/${name}`],
    ["legacy-shorthand", `https://${organization}.visualstudio.com/_git/${name}`],
    ["legacy-collection-shorthand", `https://${organization}.visualstudio.com/DefaultCollection/_git/${name}`],
  ];
  execFileSync("git", ["remote", "add", "origin", remotes[0]![1]!], { cwd: project });
  await browser.setViewport({ width: 1280, height: 800 });
  const thread = await t3.thread({ project, title: "Azure scope evidence", messages: ["Ready"] });
  await t3.open(thread);
  const visible: boolean[] = [];
  for (const [label, remote] of remotes) {
    execFileSync("git", ["remote", "set-url", "origin", remote!], { cwd: project });
    await t3.open("/pull-requests?host=dev.azure.com");
    await screen.getByText(/Evidence 44465 shorthand remote|Could not load pull requests|Nothing under these filters|No pull requests/, { exact: false }).first().waitFor({ timeout: 60_000 });
    await app.screenshot(label!);
    visible.push(await screen.getByText("Evidence 44465 shorthand remote", { exact: true }).isVisible());
  }
  expect(visible, "The real Azure pull request is visible for every shorthand remote").toEqual([true, true, true]);
});
