import { expect } from "e2e";
import { mkdir } from "node:fs/promises";
import { test, WEB } from "./t3.ts";

test("Forgejo clones a pasted SSH URL including its user and port", WEB, async ({ t3, app, screen, browser }) => {
  const destination = `${process.env.ISSUE_37428_FIXTURE_DIR}/runs/${Date.now()}/repo`;
  await mkdir(destination.slice(0, -5), { recursive: true });
  await t3.project("demo");
  await t3.open("/settings/source-control");
  await screen.getByRole("switch", "Offer Forgejo / Gitea when adding or publishing a project").tap();
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Control+k");
  await screen.getByRole("combobox", { visible: true }).fill("Add project");
  await screen.getByText("Add project", { exact: true, visible: true }).tap();
  await screen.getByText("Forgejo / Gitea repository", { exact: true, visible: true }).tap();
  await screen.getByPlaceholder("Enter Forgejo / Gitea repository (owner/repo)").fill("ssh://forgejo@localhost:2237/owner/repo.git");
  await app.screenshot("pasted-ssh-url");
  await browser.keyboard.press("Enter");
  await screen.getByText("Repository", { exact: true, visible: true }).waitFor();
  await screen.getByPlaceholder("Enter path (e.g. ~/projects/my-app)").fill(destination);
  await browser.keyboard.press("Enter");
  await screen.getByRole("dialog", "Command palette").waitFor({ state: "hidden" });
  await screen.getByText("What should we work on?", { exact: true, visible: true }).waitFor();
  await screen.getByText("Cloning repo", { exact: true, visible: true }).waitFor({ state: "hidden" });
  await app.screenshot("clone-outcome");
  await expect(screen.getByText("Failed to clone repo", { exact: true, visible: true })).not.toBeVisible();
});
