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

test("Search excerpts ignore interior SHA matches and keep prefix matches", 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("dead 7dead52bb8");
  await composer.press("Enter");
  await screen.getByText("second fixture turn complete", { exact: false }).waitFor();
  const search = screen.getByRole("combobox", "Search threads");
  const marks = () => browser.evaluate<string[]>('Array.from(document.querySelectorAll(\'[role="listbox"] mark\'), el => el.textContent ?? "")');
  await search.fill("7dead");
  await screen.getByRole("listbox", "Thread search results").getByText("7dead", { exact: true }).waitFor();
  const prefixMarks = await marks();
  await app.screenshot("sha-prefix");
  await search.fill("dead");
  await screen.getByRole("listbox", "Thread search results").getByText("dead", { exact: true }).first().waitFor();
  try {
    expect(prefixMarks).toEqual(["7dead"]);
    await expect.poll(marks).toEqual(["dead"]);
  } finally {
    await app.screenshot("standalone-dead");
  }
});
