/// <reference lib="dom" />
import { expect } from "e2e";
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";

const repository = "juliusmarminge/r46560repro-deletion_scheduled-149122608/pull/123";
const mr = `https://gitlab.com/${repository}/-/merge_requests/1`;

test("Nested GitLab merge request opens the full repository and actual MR", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("nested-gitlab-proof");
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git("remote", "add", "origin", `https://gitlab.com/${repository}.git`);
  await git("fetch", "origin", "main", "feature");
  await git("checkout", "-B", "feature", "origin/feature");
  const thread = await t3.thread({ project, title: "Nested GitLab MR routing", messages: ["Start"] });
  await browser.setViewport({ width: 1440, height: 900 });
  await t3.open(thread);
  await screen.getByRole("textbox").fill(`Nested GitLab MR: ${mr} and GitHub control: https://github.com/pingdotgg/t3code/pull/46560/files`);
  await screen.getByRole("button", "Submit message", { exact: true }).tap();
  await browser.locator(`a[href="${mr}"]`).first().click();
  await browser.locator('[data-right-panel-surface-content]').getByText(/^(Could not load pull requests|No comments yet\.)$/).first().waitFor();
  await app.screenshot("nested-gitlab-mr-opened");
  await expect(browser.locator('[data-right-panel-tabbar] button[aria-pressed="true"]')).toHaveText("#1");
  await expect(browser.locator('[data-right-panel-surface-content]')).toContainText("Repro MR");
});
