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

test("Ctrl-click includes the open thread and respects its later deselection", WEB, async ({ t3, app, browser }) => {
  const project = await t3.project("Selection demo");
  const threads = {} as Record<string, { threadId: string }>;
  for (const title of ["Bravo", "Charlie", "Delta", "Echo"]) {
    threads[title] = await t3.thread({ project, title, messages: ["hello"] });
  }
  await t3.open(threads.Echo);
  const row = (title: string) => browser.locator(`[data-sidebar-row$="${threads[title].threadId}"]`);
  const menu = () => browser.evaluate(() => document.querySelector('[role="menu"]')?.textContent ?? "");
  await row("Echo").tap();
  for (const title of ["Delta", "Charlie", "Bravo"]) await row(title).tap({ modifiers: ["Control"] });
  await row("Bravo").secondaryTap();
  await app.screenshot("ctrl-four-threads");
  const ctrlMenu = await menu();
  await browser.keyboard.press("Escape");
  await row("Echo").tap();
  await row("Bravo").tap({ modifiers: ["Shift"] });
  await row("Bravo").secondaryTap();
  await app.screenshot("shift-four-threads");
  const shiftMenu = await menu();
  await browser.keyboard.press("Escape");
  await row("Echo").tap({ modifiers: ["Control"] });
  await row("Bravo").tap({ modifiers: ["Control"] });
  await row("Bravo").tap({ modifiers: ["Control"] });
  await row("Bravo").secondaryTap();
  await app.screenshot("open-thread-stays-deselected");
  const deselectedMenu = await menu();
  expect(ctrlMenu).toContain("Settle (4)");
  expect(shiftMenu).toContain("Settle (4)");
  expect(deselectedMenu).toContain("Settle (3)");
});
