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

const mr = "https://gitlab.com/julius54/t3-routing-proof-30905/-/merge_requests/1";
test("Private GitLab MR web URL uses configured login host", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("GitLab routing proof");
  const git = (...args: string[]) => execFileSync("git", args, { cwd: project, encoding: "utf8" });
  git("remote", "add", "origin", "git@ssh.example.test:julius54/t3-routing-proof-30905.git");
  git("config", "credential.helper", "!f() { echo username=oauth2; printf password=; glab config get token --host ssh.example.test; }; f");
  git("-c", "url.https://gitlab.com/.insteadOf=git@ssh.example.test:", "fetch", "origin");
  git("reset", "--hard", "origin/main");
  git("branch", "--set-upstream-to=origin/main", "main");
  await t3.open();
  await screen.getByRole("combobox", "Branch: main").first().tap();
  await screen.getByPlaceholder("Search refs...").fill(mr);
  await screen.getByText("Checkout merge request", { exact: true }).tap();
  // Wait for the lookup to finish on both revisions before capturing its result.
  await expect(screen.getByText("Resolving merge request...", { exact: true })).not.toBeVisible({ timeout: 60000 });
  await app.screenshot("mr-url-resolution");
  await expect(screen.getByRole("button", "Local", { exact: true })).toBeEnabled({ timeout: 60000 });
  await screen.getByRole("button", "Local", { exact: true }).tap();
  await expect(screen.getByRole("combobox", "Branch: feature/mr-source").first()).toBeVisible({ timeout: 60000 });
  await app.screenshot("mr-checked-out");
});
