import { expect } from "e2e";
import * as Effect from "effect/Effect";
import { T3_HOME } from "../e2e.config.ts";
import { SECOND_HOME, startSecondServer } from "./second-server.ts";
import { rpcClient, runPromise, test, WEB } from "./t3.ts";

test("SCP usernames containing @ are excluded from clone hosts", WEB, async ({ t3, app, screen, browser }) => {
  const cases = [
    { name: "gitlab-repo", remote: "alice@corp.com@gitlab.example.com:team/repo.git", path: "~/gitlab.example.com/team/repo", https: "https://gitlab.example.com/team/repo.git" },
    { name: "azure-repo", remote: "alice@corp.com@ssh.dev.azure.com:v3/org/proj/repo", path: "~/ssh.dev.azure.com/v3/org/proj/repo", https: "https://dev.azure.com/org/proj/_git/repo" },
  ];
  for (const item of cases) await t3.project(item.name, { ".git/config": `[remote "origin"]\nurl = ${item.remote}\n` });
  const second = startSecondServer(app.baseUrl ?? "");
  try {
    const { host, token } = await second.ready;
    for (const [home, origin] of [[T3_HOME, app.baseUrl ?? ""], [SECOND_HOME, host]]) {
      await runPromise(Effect.scoped(Effect.gen(function* () {
        const client = yield* rpcClient(home!, origin!);
        yield* client["server.updateSettings"]({ patch: { ghqCloneLayout: true, addProjectBaseDirectory: "~" } });
      })));
    }
    await browser.setViewport({ width: 1440, height: 1000 });
    await t3.open("/settings/connections");
    await screen.getByRole("button", "Add environment").tap();
    const pairing = screen.getByRole("dialog");
    await pairing.getByPlaceholder("backend.example.com").fill(host);
    await pairing.getByPlaceholder("PAIRCODE").fill(token);
    await pairing.getByRole("button", "Add environment").tap();
    await screen.getByRole("button", "List", { exact: true }).tap();
    await screen.getByRole("button", "More actions for Scenario").waitFor();
    const values: Array<{path: string; https: string}> = [];
    for (const item of cases) {
      await app.open("/");
      const picker = browser.locator("[data-draft-project-trigger]");
      await picker.tap();
      await screen.getByRole("option", item.name, { exact: true }).tap();
      await picker.getByText(item.name, { exact: true }).waitFor();
      await screen.getByRole("combobox", "Run on", { exact: true }).tap();
      await screen.getByRole("option", "Add to another machine", { exact: true }).tap();
      const dialog = screen.getByRole("dialog", "Add to another machine");
      await dialog.getByRole("button", "HTTPS", { exact: true }).tap();
      await app.screenshot(`${item.name}-https-and-folder`);
      values.push(await browser.evaluate(() => {
        const dialog = document.querySelector('[role="dialog"]')!;
        return { path: (dialog.querySelector('input') as HTMLInputElement).value, https: Array.from(dialog.querySelectorAll('span')).map(n => n.textContent ?? '').find(s => s.startsWith('https://')) ?? '' };
      }));
      await dialog.getByRole("button", "Cancel", { exact: true }).tap();
    }
    await expect(values).toEqual(cases.map(item => ({ path: item.path, https: item.https })));
  } finally { await second.stop(); }
});
