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

test("Sidebar folder selects projects and opens Add project", WEB, async ({ t3, app, screen, browser }) => {
  const capture = async (label: string) => {
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().map((animation) => animation.finished.catch(() => {})));
    });
    await app.screenshot(label);
  };
  const alpha = await t3.project("Alpha");
  await t3.project("Beta");
  const thread = await t3.thread({ project: alpha, title: "Alpha work", messages: [] });
  await t3.open(thread);
  const trigger = screen.getByRole("combobox", /^(Projects|Filter threads by project)(:.*)?$/);
  await trigger.tap();
  await capture("project-menu");
  await expect(screen.getByRole("option", "Add project")).toBeVisible();
  await screen.getByRole("option", "Add project").tap();
  await capture("add-project-palette");
  await expect(screen.getByRole("dialog").getByRole("combobox")).toBeFocused();
  await browser.keyboard.press("Escape");
  await trigger.tap();
  await screen.getByPlaceholder("Search projects...").fill("Beta");
  await capture("search-project");
  await expect(screen.getByRole("option", "Add project")).not.toBeVisible();
  await browser.keyboard.press("Enter");
  await capture("selected-project");
  await expect(screen.getByRole("combobox", "Projects: Beta")).toBeVisible();
  await screen.getByRole("combobox", "Projects: Beta").tap();
  await screen.getByPlaceholder("Search projects...").fill("");
  await screen.getByRole("option", "All projects").tap();
  await trigger.tap();
  await browser.keyboard.press("ArrowUp");
  await browser.keyboard.press("Enter");
  await capture("keyboard-add-project");
  await expect(screen.getByRole("dialog").getByRole("combobox")).toBeFocused();
});
