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

test("Opening sidebar matches keeps the search available", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Search demo");
  const first = await t3.thread({ project, title: "Orchard alpha", messages: ["Start"] });
  const second = await t3.thread({ project, title: "Orchard beta", messages: ["Start"] });
  await t3.open(second);
  const search = screen.getByRole("combobox", "Search threads");
  const results = screen.getByRole("listbox", "Thread search results");
  await search.fill("Orchard");
  await results.waitFor();
  await app.screenshot("matching-threads");
  await results.getByRole("option").first().click();
  await browser.mouse.move(650, 400);
  try {
    await expect(search).toHaveValue("Orchard");
    await expect(results).toContainText("Orchard alpha");
    await expect(results).toContainText("Orchard beta");
  } finally {
    await app.screenshot("after-click");
  }
  await search.click();
  await search.press("ArrowDown");
  await search.press("Enter");
  await browser.mouse.move(650, 400);
  try {
    await expect(search).toHaveValue("Orchard");
    await expect(results).toContainText("Orchard alpha");
    await expect(results).toContainText("Orchard beta");
  } finally {
    await app.screenshot("after-keyboard");
  }
  await screen.getByRole("button", "Clear thread search").click();
  await app.screenshot("after-clear");
  await search.fill("Orchard");
  await results.waitFor();
  await search.press("Escape");
  await app.screenshot("after-escape");
});
