// Prerequisite: SSH config entry `Host github-work` with `HostName github.com`.
// The scenario uses real GitHub CLI authentication and makes no provider stubs.
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("SCP plus username resolves the SSH alias like git username", WEB, async ({t3, app, browser, screen}) => {
  const remote = (user: string) => ({
    ".git/config": `[remote "origin"]\n  url = ${user}@github-work:pingdotgg/t3code.git\n  fetch = +refs/heads/*:refs/remotes/origin/*\n`,
  });
  const control = await t3.project("control-git", remote("git"));
  const plus = await t3.project("plus-user", remote("alice+work"));
  const controlThread = await t3.thread({project: control, title: "Control SSH remote", messages: ["Start"]});
  const plusThread = await t3.thread({project: plus, title: "Plus username SSH remote", messages: ["Start"]});
  await browser.setViewport({width:1280,height:800});
  for (const [thread, label] of [[controlThread, "control-git"], [plusThread, "plus-user"]] as const) {
    await t3.open(thread);
    await screen.getByText("first fixture turn complete", {exact:true}).waitFor();
    await app.screenshot(label + "-repository");
    await screen.getByRole("button", /^New thread in /).tap();
    await browser.locator("[data-draft-project-trigger]").waitFor();
    await browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first().click();
    await screen.getByPlaceholder("Search refs...").fill("#44717");
    await screen.getByRole("option", /Checkout (?:pull|change) request/i).tap();
    const dialog = screen.getByRole("dialog", /Checkout (?:pull|change) request/i);
    await dialog.getByRole("textbox").waitFor();
    await browser.locator('[role="dialog"]').filter({hasText:/SCP-style remotes with \+ in the username resolve like git@ remotes|No unknown source control provider is registered/}).waitFor();
    await dialog.getByText(/Resolving (?:pull|change) request\.\.\./).waitFor({state:"hidden"});
    await app.screenshot(label + "-pull-request");
    await expect(dialog).toContainText("SCP-style remotes with + in the username resolve like git@ remotes");
    await browser.keyboard.press("Escape");
  }
});
