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

test("Opening matching threads preserves sidebar search", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Search demo");
  await t3.thread({ project, title: "Orchard first", messages: ["Prepare the first example"] });
  await t3.thread({ project, title: "Orchard second", messages: ["Prepare the second example"] });
  await t3.open();
  const search = screen.getByRole("combobox", "Search threads");
  await search.waitFor({ state: "visible", timeout: 120_000 });
  await search.fill("Orchard");
  await screen.getByRole("option").first().tap();
  await screen.getByText("first fixture turn complete").waitFor({ state: "visible" });
  await app.screenshot("after-click");
  await expect.soft(search).toHaveValue("Orchard");
  await expect.soft(screen.getByRole("option")).toHaveCount(2);
  await search.fill("Orchard");
  await search.tap();
  await browser.keyboard.press("ArrowDown");
  await browser.keyboard.press("Enter");
  await screen.getByText("Prepare the first example").waitFor({ state: "visible" });
  await app.screenshot("after-enter");
  await expect.soft(search).toHaveValue("Orchard");
  await expect.soft(screen.getByRole("option")).toHaveCount(2);
  await search.fill("Orchard");
  await screen.getByRole("button", "Clear thread search").tap();
  await app.screenshot("after-clear");
  await search.fill("Orchard");
  await browser.keyboard.press("Escape");
  await app.screenshot("after-escape");
});
