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

test("Command palette opens lower and keeps its height while filtering", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("Palette demo", {"sample.ts":"export const sample = 1;\n", "icon.svg":'<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32"><rect width="32" height="32" fill="blue"/></svg>'});
  const thread = await t3.thread({project,title:"Palette work",messages:["Start"]});
  await browser.setViewport({width:1440,height:900});
  await t3.open(thread);
  await screen.getByText("Start",{exact:true}).waitFor({timeout:30000});
  const bounds = () => browser.evaluate(() => {
    const r=document.querySelector('[data-slot="command-dialog-popup"]')!.getBoundingClientRect();
    return {top:Math.round(r.top),height:Math.round(r.height)};
  });
  const results = [];
  for (const height of [900,620]) {
    await browser.setViewport({width:1440,height});
    await browser.keyboard.press("Control+k");
    const palette=screen.getByRole("dialog","Command palette");
    await palette.getByRole("combobox").fill(">");
    await screen.getByText("Restart agent session",{exact:true}).waitFor();
    await app.screenshot(`palette-${height}-open`);
    const full=await bounds();
    await palette.getByRole("combobox").fill("> Settings");
    await app.screenshot(`palette-${height}-filtered`);
    const filtered=await bounds();
    results.push({height,full,filtered});
    await browser.keyboard.press("Escape");
  }
  for (const height of [900,620]) {
    await browser.setViewport({width:1440,height});
    await browser.keyboard.press("Control+p");
    await screen.getByRole("dialog","File picker").getByRole("combobox").fill("sample");
    await screen.getByRole("option",/sample.ts/).waitFor();
    await app.screenshot(`files-${height}`);
    await browser.keyboard.press("Escape");
    await browser.keyboard.press("Control+Shift+f");
    await screen.getByRole("dialog","Search project contents").getByRole("combobox").fill("sample");
    await screen.getByRole("dialog","Search project contents").getByText("sample.ts",{exact:true}).waitFor();
    await app.screenshot(`content-${height}`);
    await browser.keyboard.press("Escape");
  }
  await t3.open("/settings/projects");
  await screen.getByRole("button",/^Palette demo /).tap();
  await screen.getByRole("button","Choose a project icon file").tap();
  await screen.getByRole("dialog","Choose project icon").waitFor();
  for (const height of [900,620]) {
    await browser.setViewport({width:1440,height});
    await app.screenshot(`icon-${height}`);
  }
  expect(results[0].full.top).toBe(225);
  expect(results[1].full.top).toBe(138);
  expect(results[0].full.height).toBe(420);
  expect(results[1].full.height).toBe(420);
  expect(results.map(r=>r.filtered.top)).toEqual(results.map(r=>r.full.top));
});
