/// <reference lib="dom" />
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";

test("Height-capped picker layouts survive filtering", WEB, async ({t3, app, browser, screen}) => {
  await browser.setViewport({width:1280,height:900});
  const project = await t3.project("picker-demo");
  for (let i = 0; i < 32; i++) await runPromise(run("git", ["branch", `${i < 16 ? "sample" : "other"}-${String(i).padStart(2,"0")}`], {cwd:project}));
  const thread = await t3.thread({project,title:"Picker scrolling",messages:["Hello"]});
  for (let i = 0; i < 20; i++) await t3.thread({project,title:`${i < 12 ? "Sample" : "Other"} task ${String(i).padStart(2,"0")}`,messages:["Hello"]});
  const capture = async (label: string) => {
    // Hold the state so the interaction is readable in the recording, after popup transitions.
    await browser.evaluate(() => new Promise(resolve=>setTimeout(resolve,1000)));
    console.log(label, await browser.evaluate(() => [...document.querySelectorAll('[data-slot="combobox-list"], [data-slot="autocomplete-list"]')].map(list => {
      const v = list.closest('[data-slot="scroll-area-viewport"]');
      const root = v?.parentElement;
      const track = root?.querySelector('[data-slot="scroll-area-scrollbar"][data-orientation="vertical"]');
      return {count:list.querySelectorAll('[role="option"]').length, height:v?.clientHeight ?? null, scroll:v?.scrollHeight ?? null, track:track?.getBoundingClientRect().height ?? null, thumb:track?.querySelector('[data-slot="scroll-area-thumb"]')?.getBoundingClientRect().height ?? null};
    })));
    await app.screenshot(label);
  };
  const hover = async () => {
    const p = await browser.evaluate(() => {const v=document.querySelector('[data-slot="combobox-list"], [data-slot="autocomplete-list"]')!.closest('[data-slot="scroll-area-viewport"]')!;const r=v.getBoundingClientRect();return {x:r.right-12,y:r.top+40};});
    await browser.mouse.move(p.x,p.y);
  };
  await t3.open(thread);
  await screen.getByRole("button","Toggle right panel").tap();
  await screen.getByText("Diff", {exact:true}).tap();
  await screen.getByRole("button",/^Diff scope:/).tap();
  await screen.getByRole("menuitemradio","Changes",{exact:true}).tap();
  await screen.getByRole("combobox",/^Change comparison target/).tap();
  await screen.getByRole("option").first().waitFor();
  await hover(); await capture("diff-refs-capped-256");
  await screen.getByPlaceholder("Search refs...").fill("sample");
  await hover(); await capture("diff-refs-filtered");
  await browser.keyboard.press("Escape");

  await t3.open("/settings/scheduled-tasks");
  await screen.getByRole("button","New task").tap();
  await screen.getByRole("combobox","Destination",{exact:true}).tap();
  await screen.getByRole("option","One thread, continuing the conversation").tap();
  await screen.getByRole("combobox","Destination thread").tap();
  await screen.getByRole("option").first().waitFor();
  await hover(); await capture("task-destination-capped-288");
  await screen.getByRole("combobox","Search threads",{exact:true}).fill("Sample");
  await hover(); await capture("task-destination-filtered");
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Escape");

});
