import { expect } from "e2e";
import { execFileSync } from "node:child_process";
import { test, WEB } from "./t3.ts";

test("Picking main after an outside checkout switches the actual checkout", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("branch-demo");
  const git = (...args: string[]) => execFileSync("git", ["-C", project, ...args], { encoding: "utf8" }).trim();
  for (const branch of ["feature/a", "feature/b", "feature/c", "release/x"]) git("branch", branch);
  const thread = await t3.thread({ project, title: "Branch checkout proof", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("region", "Version Control").getByRole("combobox").tap();
  await screen.getByRole("option", "main", { exact: false }).waitFor({ state: "visible" });
  git("switch", "feature/a");
  await app.screenshot("outside-checkout-picker");
  await screen.getByRole("option", "main", { exact: false }).tap();
  await browser.keyboard.press("Escape");
  try {
    await expect.poll(() => git("branch", "--show-current")).toBe("main");
  } finally {
    await app.screenshot("after-picking-main");
  }
});
