/// <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 pause = () => browser.evaluate(async () => { await new Promise(resolve=>setTimeout(resolve,1500)); return null; });
  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 pause();
    await app.screenshot(`palette-${height}-open`);
    const full=await bounds();
    await palette.getByRole("combobox").fill("> Settings");
    await pause();
    await app.screenshot(`palette-${height}-filtered`);
    const filtered=await bounds();
    results.push({height,full,filtered});
    await browser.keyboard.press("Escape");
  }
  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));
});
