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

test("ArrowUp keeps the selected content result below its sticky file header", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 1280, height: 800 });
  const lines = (name: string) => Array.from({ length: 16 }, (_, i) => `export const ${name}${i + 1} = ${i + 1};`).join("\n");
  const project = await t3.project("proj", { "alpha.ts": lines("alpha"), "beta.ts": lines("beta"), "gamma.ts": lines("gamma") });
  const thread = await t3.thread({ project, title: "Search example", messages: ["Ready for project search"] });
  await t3.open(thread);
  await screen.getByRole("textbox").waitFor();
  await browser.keyboard.press("Control+Shift+f");
  const search = screen.getByPlaceholder("Search in proj");
  await search.fill("export");
  await screen.getByText("48 results in 3 files").waitFor();
  for (let i = 0; i < 13; i++) await browser.keyboard.press("ArrowDown");
  for (let i = 0; i < 10; i++) await browser.keyboard.press("ArrowUp");
  await app.screenshot("arrowup-selected-result");
  const geometry = await browser.evaluate(() => {
    const row = document.querySelector<HTMLElement>('[data-testid="project-content-search"] button.bg-accent')!;
    const header = row.closest("section")!.querySelector<HTMLElement>(".sticky")!;
    return { rowTop: row.getBoundingClientRect().top, headerBottom: header.getBoundingClientRect().bottom };
  });
  expect(geometry.rowTop).toBeGreaterThanOrEqual(geometry.headerBottom);
});
