/**
 * Keyboard / command palette tests — open, commands, filtering, close.
 */

describe("Command palette", () => {
  it("should open via Ctrl+K", async () => {
    await browser.keys(["Control", "k"]);
    await browser.pause(400);

    const backdrop = await $(".palette-backdrop");
    if (await backdrop.isExisting()) {
      const display = await backdrop.getCSSProperty("display");
      expect(display.value).not.toBe("none");
    }
  });

  it("should show the palette panel with input", async () => {
    const panel = await $(".palette-panel");
    if (await panel.isExisting()) {
      expect(await panel.isDisplayed()).toBe(true);
    }

    const input = await $(".palette-input");
    if (await input.isExisting()) {
      expect(await input.isDisplayed()).toBe(true);
    }
  });

  it("should list 18 commands", async () => {
    const items = await $$(".palette-item");
    expect(items.length).toBe(18);
  });

  it("should show command labels and shortcuts", async () => {
    const labels = await $$(".cmd-label");
    expect(labels.length).toBeGreaterThan(0);

    const shortcuts = await $$(".cmd-shortcut");
    expect(shortcuts.length).toBeGreaterThan(0);
  });

  it("should filter commands on text input", async () => {
    const input = await $(".palette-input");
    if (!(await input.isExisting())) return;

    await input.setValue("terminal");
    await browser.pause(200);

    const items = await $$(".palette-item");
    // Should have fewer than 18 after filtering
    expect(items.length).toBeLessThan(18);
    expect(items.length).toBeGreaterThan(0);
  });

  it("should close on Escape key", async () => {
    await browser.keys("Escape");
    await browser.pause(300);

    const backdrop = await $(".palette-backdrop");
    if (await backdrop.isExisting()) {
      const display = await backdrop.getCSSProperty("display");
      expect(display.value).toBe("none");
    }
  });
});
