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

test("Quoted search keeps adjacent exact message matches separate", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Weekly notes", messages: ["Start"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.pressSequentially("日本日本");
  await composer.press("Enter");
  await screen.getByText("second fixture turn complete", { exact: false }).waitFor();
  await browser.keyboard.press("Escape");
  await screen.getByRole("combobox", "Search threads").fill('"日本"');
  const results = screen.getByRole("listbox", "Thread search results");
  await results.getByText("Weekly notes", { exact: true }).waitFor();
  await app.screenshot("quoted-search-result");
  await results.getByText("Weekly notes", { exact: true }).tap();
  const find = screen.getByRole("searchbox", "Find in thread");
  await find.waitFor();
  await browser.locator("[data-thread-find-bar] span[aria-hidden]").filter({ hasText: /^1\/[12]$/ }).waitFor();
  await app.screenshot("opened-thread-find");
  await find.press("Enter");
  try {
    await expect(find).toHaveValue("日本");
    await expect.poll(() => browser.evaluate<string>('document.querySelector("[data-thread-find-bar] span[aria-hidden]")?.textContent ?? ""')).toBe("2/2");
  } finally {
    await app.screenshot("next-adjacent-match");
  }
});
