import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("Thread search ignores the app prefix on flat temporary branches", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("connect");
  const { cli } = makeState(T3_HOME);
  for (const [branch, title] of [
    ["t3/abcd1234", "Add search"],
    ["t3/cleanup-payloads", "Add search"],
    ["t3-abcd1234", "Add search"],
    ["feature/search", "Other work"],
    ["feature/notes", "T3 Connect release notes"],
  ]) {
    await runPromise(cli(["thread", "start", project, "Start", "--title", title!, "--worktree", "main", "--branch", branch!, "--provider", "devReplay", "--model", "multi-turn", "--wait", "--json"]));
  }
  await t3.open();
  await screen.getByRole("combobox", "Search threads").waitFor({ state: "visible" });
  await browser.keyboard.press("Control+k");
  const dialog = screen.getByRole("dialog");
  const input = dialog.getByRole("combobox");
  await input.fill("t3");
  await dialog.getByText("T3 Connect release notes", { exact: true }).waitFor({ state: "visible" });
  await dialog.getByText("Searching messages…", { exact: true }).waitFor({ state: "hidden" });
  await dialog.getByRole("listbox").swipe({ direction: "up", momentum: "slow" });
  await app.screenshot("search-t3");
  await input.fill("t3 connect");
  await dialog.getByText("T3 Connect release notes", { exact: true }).waitFor({ state: "visible" });
  await dialog.getByText("Searching messages…", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("search-t3-connect");
  await expect(dialog.getByText("Add search", { exact: true })).toHaveCount(0);
});
