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

test("Thread search excludes a parenthesized link address with an escaped label", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Bird link check", messages: ["Start"] });
  await t3.open(thread);
  await browser.evaluate(() => { localStorage.setItem("chat_thread_sidebar_width", "560"); return null; });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.pressSequentially(String.raw`Open [\_\_dunlin\_\_.py](https://dunlin.dev/wiki/Dunlin_(bird)/sandpiper) now`);
  await composer.press("Enter");
  await screen.getByText("second fixture turn complete", { exact: false }).waitFor();
  const search = screen.getByRole("combobox", "Search threads");
  await search.fill("sandpiper");
  await screen.getByText(/No threads found|You: Open/).waitFor();
  await app.screenshot("address-only-search");
  const addressResults = await browser.evaluate(() => document.querySelector('[role="listbox"]')?.textContent ?? "");
  await search.fill("__dunlin__");
  await screen.getByText("You: Open", { exact: false }).waitFor();
  await app.screenshot("displayed-label-search");
  await expect(screen.getByRole("listbox", "Thread search results")).toContainText("Open __dunlin__.py now");
  expect(addressResults).not.toContain("Bird link check");
});
