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

test("Command palette returns keyboard interaction to a closed select trigger", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  await t3.thread({ project, title: "Focus demonstration", messages: ["Hello"], model: "default" });
  const results: boolean[] = [];
  for (const shortcut of ["Control+k", "Control+p"]) {
    await t3.open("/settings/general");
    const trigger = screen.getByRole("combobox", "Thread notifications");
    await trigger.focus();
    await browser.keyboard.press("Space");
    await screen.getByRole("option", "Off", { exact: true }).waitFor();
    await app.screenshot(`${shortcut.endsWith("k") ? "commands" : "files"}-select-open`);
    await browser.keyboard.press(shortcut);
    await browser.locator('[data-slot="command-dialog-popup"] input').last().waitFor();
    await app.screenshot(`${shortcut.endsWith("k") ? "commands" : "files"}-palette-open`);
    await browser.keyboard.press("Escape");
    if (shortcut.endsWith("p")) await browser.keyboard.press("Escape");
    await browser.locator('[data-slot="command-dialog-popup"] input').last().waitFor({ state: "hidden" });
    results.push(await browser.evaluate(() => document.activeElement?.getAttribute("aria-label") === "Thread notifications"));
    await browser.keyboard.press("Space");
    await app.screenshot(`${shortcut.endsWith("k") ? "commands" : "files"}-space-after-close`);
    results.push(await trigger.getAttribute("aria-expanded") === "true");
  }
  expect(results).toEqual([true, true, true, true]);
});
