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

test("terminal font preview lets Escape and app shortcuts through", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Keyboard demo");
  const thread = await t3.thread({ project, title: "Keyboard demo", messages: ["hello"], model: "default" });
  await t3.open(thread);
  await t3.open("/settings/appearance");
  const preview = screen.getByLabel("Terminal font preview");
  await preview.tap();
  await screen.getByLabel("Terminal input").focus();
  await browser.keyboard.type("hello");
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().filter(a => a.effect?.getComputedTiming().iterations !== Infinity).map(a => a.finished.catch(() => undefined))); return null; });
  await app.screenshot("ordinary-preview-input");
  await browser.keyboard.press("Escape");
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().filter(a => a.effect?.getComputedTiming().iterations !== Infinity).map(a => a.finished.catch(() => undefined))); return null; });
  await app.screenshot("after-escape-in-preview");
  const escapeClosedSettings = await browser.evaluate(() => !location.pathname.includes("/settings/"));

  await t3.open("/settings/appearance");
  await screen.getByLabel("Terminal font preview").tap();
  await screen.getByLabel("Terminal input").focus();
  await browser.keyboard.type("hello");
  await browser.keyboard.press("Control+k");
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().filter(a => a.effect?.getComputedTiming().iterations !== Infinity).map(a => a.finished.catch(() => undefined))); return null; });
  await app.screenshot("after-ctrl-k-in-preview");
  const paletteOpened = await browser.evaluate(() => [...document.querySelectorAll('[role="dialog"]')].some(el => el.getAttribute("aria-label") === "Command palette" || el.textContent?.includes("Search commands, projects, and threads")));
  await expect(screen.getByRole("dialog", "Command palette", { exact: true })).toBeVisible();
  expect(escapeClosedSettings).toBe(true);
  expect(paletteOpened).toBe(true);
});
