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

test("Project search skips settled and snoozed threads", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("acme-demo");
  const alpha = await t3.thread({ project, title: "Alpha", messages: ["Alpha history"] });
  const bravo = await t3.thread({ project, title: "Bravo", messages: ["Bravo history"] });
  await t3.open(bravo);
  const row = (id: string) => browser.locator(`[data-thread-item$=":${id}"]`);
  const openProject = async () => {
    await browser.keyboard.press("Control+k");
    await screen.getByRole("combobox").fill("acme-demo");
    await screen.getByRole("option").filter({ hasText: "acme-demo" }).first().tap();
    await browser.locator('input[placeholder="Search commands, projects, and threads..."]').waitFor({ state: "hidden" });
  };
  await row(bravo.threadId).hover();
  await row(bravo.threadId).getByRole("button", "Settle thread").tap();
  await openProject();
  await app.screenshot("settled-project-result");
  const settledPath = await browser.evaluate(() => location.pathname);

  await t3.open(bravo);
  await screen.getByRole("button", "Un-settle", { exact: true }).tap();
  await t3.open(alpha);
  await row(bravo.threadId).hover();
  await row(bravo.threadId).getByRole("button", "Snooze thread").tap();
  await screen.getByRole("menuitem").filter({ hasText: "Tomorrow" }).tap();
  await openProject();
  await app.screenshot("snoozed-project-result");
  const snoozedPath = await browser.evaluate(() => location.pathname);
  expect([decodeURIComponent(settledPath.split("/").at(-1)!), decodeURIComponent(snoozedPath.split("/").at(-1)!)]).toEqual([alpha.threadId, alpha.threadId]);
});
