/// <reference lib="dom" />
import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Ctrl+K brings the command palette above an open branch picker", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("branch-picker-demo");
  for (const branch of ["feature/search", "feature/settings", "fix/layout", "release/next", "docs/guide", "feature/sidebar", "fix/focus", "release/stable"]) {
    execFileSync("git", ["branch", branch], { cwd: project });
  }
  const thread = await t3.thread({ project, title: "Branch picker layering", messages: ["Start"] });
  await t3.open(thread);
  await browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first().click();
  await screen.getByPlaceholder("Search refs...").waitFor();
  await screen.getByRole("option", /feature\/\s*search/).waitFor();
  await app.screenshot("branch-picker-open");
  await browser.keyboard.press("Control+k");
  const palette = screen.getByRole("dialog", "Command palette");
  await palette.getByRole("combobox").waitFor();
  await browser.keyboard.type("aaaa");
  await palette.getByText("No matching commands, projects, or threads.", { exact: true }).waitFor();
  await app.screenshot("palette-over-branch-picker");
  const pickerVisibleWithPalette = await screen.getByPlaceholder("Search refs...").isVisible();
  await browser.keyboard.press("Escape");
  await palette.waitFor({ state: "hidden" });
  await app.screenshot("after-escape");
  expect(pickerVisibleWithPalette, "The branch picker closes when Ctrl+K opens the focused command palette").toBe(false);
  expect(await screen.getByPlaceholder("Search refs...").isVisible(), "Escape must not reveal the old branch picker").toBe(false);
});
