/// <reference lib="dom" />
import { expect } from "e2e";
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";

test("PR checkout retains dialog focus for Enter and click while branches restore composer focus", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("focus-demo");
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git("remote", "add", "origin", "https://github.com/pingdotgg/t3code.git");
  await git("branch", "feature");
  await browser.setViewport({ width: 1280, height: 800 });
  await t3.open("/");
  const picker = browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first();
  const outcomes = [];
  for (const method of ["Enter", "click"]) {
    await picker.click();
    const search = screen.getByPlaceholder("Search refs...");
    await search.fill("#123");
    const option = screen.getByRole("option", /Checkout pull request/);
    await option.waitFor();
    if (method === "Enter") await search.press("Enter");
    else await option.tap();
    const dialog = screen.getByRole("dialog", /Checkout pull request/i);
    await dialog.waitFor();
    // Wait for the real GitHub resolution, allowing the competing focus frame to run.
    await dialog.getByText("Add Expo mobile client and wire up `dev:mobile` workflow", { exact: true }).waitFor({ timeout: 30000 });
    await browser.keyboard.type(method === "Enter" ? "456" : "789");
    outcomes.push(await browser.evaluate(() => {
      const input = document.querySelector<HTMLDivElement>('[role="dialog"]')?.querySelector<HTMLInputElement>('input');
      const composer = document.querySelector<HTMLElement>('[contenteditable="true"][role="textbox"]');
      return { value: input?.value, focused: document.activeElement === input, composer: composer?.textContent ?? "" };
    }));
    await dialog.getByText("Resolving pull request...", { exact: true }).waitFor({ state: "hidden", timeout: 30000 });
    await app.screenshot(`${method}-typed-without-clicking`);
    await dialog.getByRole("button", "Cancel", { exact: true }).tap();
    await app.screenshot(`${method}-composer-after-cancel`);
    const composer = browser.locator('[contenteditable="true"][role="textbox"]').first();
    await composer.click();
    await browser.keyboard.press("Control+a");
    await browser.keyboard.press("Backspace");
  }
  await picker.click();
  await screen.getByRole("option", /^feature\b/).tap();
  await screen.getByRole("combobox", "Branch: feature", { exact: true }).first().waitFor();
  await browser.keyboard.type("ordinary branch typing");
  const ordinaryFocused = await browser.evaluate(() => document.activeElement?.getAttribute("contenteditable") === "true");
  await app.screenshot("ordinary-branch-restores-composer");
  expect(outcomes).toEqual([
    { value: "456", focused: true, composer: "" },
    { value: "789", focused: true, composer: "" },
  ]);
  expect(ordinaryFocused).toBe(true);
});
