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

test("Keybinding search keeps focus on held Escape and ignores modified Escape", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("escape-demo");
  await t3.thread({ project, title: "Escape demo", messages: ["Hello"] });
  await t3.open("/settings/keybindings");
  const search = screen.getByRole("searchbox", "Search keybindings");
  await search.fill("terminal");
  await browser.keyboard.down("Escape");
  await app.screenshot("plain-escape-cleared");
  for (let i = 0; i < 6; i++) await browser.keyboard.down("Escape");
  await browser.keyboard.up("Escape");
  const held = await browser.evaluate(() => {
    const input = document.querySelector<HTMLInputElement>('input[placeholder="Search keybindings"]');
    return { value: input?.value, focused: document.activeElement === input };
  });
  await app.screenshot("held-escape-focus");
  await t3.open("/settings/keybindings");
  await search.fill("terminal");
  await browser.keyboard.press("Shift+Escape");
  try {
    expect(held).toEqual({ value: "", focused: true });
    await expect.poll(() => browser.evaluate(() => !location.pathname.includes("/settings/"))).toBe(true);
  } finally {
    await app.screenshot("shift-escape-closes-settings");
  }
});
