import { expect } from "e2e";
import { test, WEB, runPromise } from "./t3.ts";
import { run } from "./shell.ts";

test("SCP IPv6 remote recognizes the branch pull request", WEB, async ({ t3, app, screen }) => {
  const seed = async (title: string, remote: string) => {
    const project = await t3.project(title);
    const git = (args: string[]) => runPromise(run("git", args, { cwd: project }));
    await git(["config", "core.sshCommand", process.env.PROOF_SSH_COMMAND!]);
    await git(["remote", "add", "origin", "http://[::1]:13917/maria/project.git"]);
    await git(["-c", "http.proxy=", "fetch", "origin"]);
    await git(["checkout", "-B", "feature", "origin/feature"]);
    await git(["remote", "set-url", "origin", remote]);
    return t3.thread({ project, title, messages: ["Check the feature branch"], model: "multi-turn" });
  };
  // Separate checkouts prevent discovery caches from retaining the control remote's result.
  const control = await seed("SSH URL control", "ssh://git@[::1]/maria/project.git");
  const scp = await seed("SCP IPv6 remote", "git@[::1]:maria/project.git");
  await t3.open(control);
  await screen.getByText("Feature PR", { exact: false }).first().waitFor({ state: "visible", timeout: 60000 });
  await screen.getByText("Syncing messages...", { exact: false }).waitFor({ state: "hidden" });
  await app.screenshot("ssh-url-control");
  await t3.open(scp);
  await screen.getByRole("heading", "SCP IPv6 remote").waitFor({ state: "visible" });
  await screen.getByText("Syncing messages...", { exact: false }).waitFor({ state: "hidden" });
  try {
    await expect(screen.getByText("Feature PR", { exact: false }).first()).toBeVisible({ timeout: 60000 });
  } finally {
    await app.screenshot("scp-ipv6-pr");
  }
});
