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

test("SSH aliases open the project's GitLab merge request in the app", WEB, async ({ t3, app, screen }) => {
  const results: boolean[] = [];
  for (const protocol of ["git+ssh", "ssh+git", "ssh"]) {
    const project = await t3.project(`${protocol.replaceAll("+", "-")}-remote`);
    execFileSync("git", ["remote", "add", "origin", `${protocol}://git@gitlab.example.test:2222/group/repo.git`], { cwd: project });
    await t3.thread({ project, title: `${protocol} merge request`, messages: [
      `Remote: \`${protocol}://git@gitlab.example.test:2222/group/repo.git\`\n\n[Review merge request 7](https://gitlab.example.test/group/repo/-/merge_requests/7)`
    ] });
    await t3.open();
    await screen.getByRole("button", new RegExp(`^${protocol.replaceAll("+", "\\+")} merge request,`)).tap();
    await screen.getByRole("button", "Agent Remote:", { exact: false }).tap();
    await screen.getByRole("link", "Review merge request 7").tap();
    let opened = true;
    try {
      await expect(screen.getByRole("link", "Open on GitLab")).toBeVisible();
      await expect(screen.getByRole("link", "Open on GitLab")).toHaveAttribute("href", "https://gitlab.example.test/group/repo/-/merge_requests/7");
    } catch { opened = false; }
    results.push(opened);
    await app.screenshot(`${protocol.replaceAll("+", "-")}-merge-request`);
  }
  await expect(results).toEqual([true, true, true]);
});
