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

test("Detached HEAD clears the recorded branch from the picker", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("branch-demo");
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git("branch", "feature");
  const thread = await t3.thread({ project, title: "go", model: "default", messages: ["Hello"] });
  await t3.open(thread);
  const branch = browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first();
  await branch.filter({ hasText: "main" }).waitFor();
  await git("switch", "--detach");
  await t3.open(thread);
  await screen.getByRole("button", "Git action options", { exact: true }).click();
  await screen.getByText("Detached HEAD: create and check out a branch to enable push and pull request actions.", { exact: true }).waitFor();
  await screen.getByRole("button", "Copy code", { exact: true }).waitFor();
  await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("detached-from-main");
  await browser.keyboard.press("Escape");
  await git("switch", "feature");
  await t3.open(thread);
  await branch.filter({ hasText: "feature" }).waitFor();
  await git("switch", "--detach");
  await t3.open(thread);
  await screen.getByRole("button", "Git action options", { exact: true }).click();
  await screen.getByText("Detached HEAD: create and check out a branch to enable push and pull request actions.", { exact: true }).waitFor();
  await screen.getByRole("button", "Copy code", { exact: true }).waitFor();
  await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("detached-from-feature");
  await expect(branch).toContainText("Select ref");
});
