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

test("Selecting a cached CURRENT branch rechecks Git", IOS, async ({ t3, app, screen }) => {
  const project = await t3.project("branch-demo");
  const git = (...args: string[]) => execFileSync("git", args, { cwd: project, encoding: "utf8" }).trim();
  git("checkout", "-b", "feature/a");
  await t3.open();
  await screen.getByRole("button", "New chat").tap();
  await screen.getByRole("button", "branch-demo").last().tap();
  const newWorktree = screen.getByRole("button", "New worktree");
  if (await newWorktree.isVisible()) await newWorktree.tap();
  await screen.getByRole("button", "Branch: feature/a").tap();
  await app.screenshot("cached-current-branch");
  git("checkout", "main");
  console.log("External checkout: " + git("branch", "--show-current"));
  await screen.getByRole("radio", "feature/a, CURRENT").tap();
  await screen.getByRole("button", "Branch: feature/a").waitFor({ state: "visible" });
  await app.screenshot("selected-branch");
  const actual = git("branch", "--show-current");
  console.log("Composer feature/a; Git branch: " + actual);
  expect(actual).toBe("feature/a");
});
