import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { T3_HOME } from "../e2e.config.ts";
import { rpcClient, runPromise, test, WEB } from "./t3.ts";

test("Thread search indexes only visible link labels", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  // The listener starts before the replay provider's model discovery finishes.
  await runPromise(Effect.gen(function* () {
    const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
    yield* client.subscribeServerConfig({}).pipe(
      Stream.filter((event) => (event.type === "snapshot" ? event.config.providers : event.type === "providerStatuses" ? event.payload.providers : []).some((p) => p.instanceId === "devReplay" && p.models.some((m) => m.slug === "multi-turn"))),
      Stream.runHead,
    );
  }).pipe(Effect.scoped));
  const thread = await t3.thread({ project, title: "Weekly notes", messages: ["Start"] });
  const control = await t3.thread({ project, title: "Quoted control", messages: ["Start"] });
  await t3.open(control);
  await screen.getByRole("textbox").pressSequentially('Ship [the playbook](https://x.dev/b "Heron playbook")');
  await screen.getByRole("textbox").press("Enter");
  await screen.getByText("second fixture turn complete", { exact: false }).waitFor();
  await t3.open(thread);
  await screen.getByRole("textbox").pressSequentially("Deploy [the runbook](https://x.dev/a (Bittern runbook))");
  await screen.getByRole("textbox").press("Enter");
  await screen.getByText("second fixture turn complete", { exact: false }).waitFor();
  await app.screenshot("sent-link-visible-label");

  await browser.keyboard.press("Control+k");
  const palette = screen.getByRole("dialog", "Command palette");
  const query = palette.getByRole("combobox");
  const paletteSearch = async (term: string, title: string, label: string) => {
    await query.fill(term);
    await palette.getByText(title, { exact: true }).or(palette.getByText("No matching commands, projects, or threads.", { exact: true })).waitFor();
    await palette.getByText("Searching messages…", { exact: true }).waitFor({ state: "hidden" });
    await app.screenshot(label);
    return (await palette.allTextContents()).join(" ");
  };
  const hidden = await paletteSearch("Bittern", "Weekly notes", "palette-hidden-title");
  const label = await paletteSearch("runbook", "Weekly notes", "palette-visible-label");
  const quotedHidden = await paletteSearch("Heron", "Quoted control", "palette-quoted-hidden-title");
  const quotedLabel = await paletteSearch("playbook", "Quoted control", "palette-quoted-visible-label");
  await browser.keyboard.press("Escape");

  const search = screen.getByRole("combobox", "Search threads");
  await search.fill("Bittern");
  await screen.getByRole("listbox", "Thread search results").or(screen.getByText("No threads found", { exact: true })).waitFor();
  await screen.getByText("Searching thread messages…", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("sidebar-hidden-title");
  const sidebarHidden = await screen.getByRole("listbox", "Thread search results").count();
  await search.fill("runbook");
  const results = screen.getByRole("listbox", "Thread search results");
  await results.waitFor();
  await app.screenshot("sidebar-visible-label");
  const sidebarLabel = (await results.allTextContents()).join(" ");

  expect(hidden).not.toContain("Weekly notes");
  expect(label).toContain("You: Deploy the runbook");
  expect(label).not.toContain("https://x.dev");
  expect(quotedHidden).not.toContain("Quoted control");
  expect(quotedLabel).toContain("You: Ship the playbook");
  expect(sidebarHidden).toBe(0);
  expect(sidebarLabel).toContain("You: Deploy the runbook");
});
