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

test("Userless Azure SSH remotes with spaces keep their provider icon", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 1280, height: 800 });
  const threads = [];
  for (const [name, path] of [["control", "MyProject/MyRepo"], ["spaced", "My Project/My Repo"]]) {
    const project = await t3.project(name);
    const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
    await git("remote", "add", "origin", `ssh.dev.azure.com:v3/Org/${path}`);
    await git("checkout", "-b", "feature");
    await git("-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "--allow-empty", "-m", "Feature commit");
    threads.push({ name, thread: await t3.thread({ project, title: `${name} Azure remote`, messages: ["first"] }) });
  }
  const icons = [];
  for (const { name, thread } of threads) {
    await t3.open(thread);
    await screen.getByRole("button", "Git action options").click({ timeout: 60000 });
    await screen.getByRole("menuitem", "Create PR", { exact: true }).waitFor();
    await app.screenshot(`${name}-git-actions`);
    icons.push(await browser.evaluate(() => [...document.querySelectorAll('[role="menuitem"]')].filter(el => el.textContent?.trim() === "Create PR").map(el => el.querySelector("svg")?.getAttribute("viewBox"))[0]));
    await browser.keyboard.press("Escape");
  }
  expect(icons[1]).toBe(icons[0]);
});
