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

test("Bulk mark unread excludes active runs in both sidebar modes", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Menu demonstration");
  const completed = await t3.thread({ project, title: "Completed and read", messages: ["Finish a short task"] });
  const { cli } = makeState(T3_HOME);
  const start = async (title: string) => {
    const result = await runPromise(cli(["thread", "start", project, "Keep working on the long task", "--title", title, "--provider", "devReplay", "--model", "bench-stream", "--json"]));
    return JSON.parse(result.stdout) as { threadId: string };
  };
  const first = await start("Running task Alpha");
  const second = await start("Running task Beta");
  const row = (id: string) => browser.locator(`[data-sidebar-row$=":${id}"]`);
  const results: number[] = [];
  for (const working of [true, false]) {
    await t3.open("/settings/general");
    const toggle = screen.getByRole("switch", "Working section", { exact: true });
    if (working) await toggle.check(); else await toggle.uncheck();
    await t3.open(completed);
    if (working) await browser.locator('[data-testid="sidebar-working-shelf-toggle"]').tap();
    await row(first.threadId).tap({ modifiers: ["Control"] });
    await row(completed.threadId).tap({ modifiers: ["Control"] });
    await row(second.threadId).tap({ modifiers: ["Control"] });
    await row(first.threadId).secondaryTap();
    await screen.getByRole("menuitem").first().waitFor();
    await app.screenshot(working ? "working-all-active" : "legacy-all-active");
    results.push(await screen.getByRole("menuitem", { name: "Mark unread", exact: false }).count());
    await browser.keyboard.press("Escape");
    await row(second.threadId).tap({ modifiers: ["Control"] });
    await row(completed.threadId).tap({ modifiers: ["Control"] });
    await row(first.threadId).secondaryTap();
    await screen.getByRole("menuitem").first().waitFor();
    await app.screenshot(working ? "working-mixed" : "legacy-mixed");
    results.push(await screen.getByRole("menuitem", "Mark unread (1)", { exact: true }).count());
    await browser.keyboard.press("Escape");
  }
  expect(results).toEqual([0, 1, 0, 1]);
});
