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

test("Removing the only project leaves Home instead of a missing thread", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("server");
  const thread = await t3.thread({ project, title: "Greeting", messages: ["hello"], model: "showcase" });
  await t3.open(thread);
  await screen.getByText("hello", { exact: true }).waitFor({ timeout: 30000 });
  await screen.getByRole("button", "Settings").tap();
  await screen.getByRole("button", "Projects", { exact: true }).tap();
  await screen.getByRole("button", "Project scope: All projects").tap();
  await screen.getByRole("menuitemradio").filter({ hasText: "server" }).tap();
  await screen.getByRole("button", "Remove project", { exact: true }).tap();
  await screen.getByRole("button", "Confirm", { exact: true }).tap();
  await browser.locator("[role=alertdialog]").waitFor({ state: "detached" });
  await screen.getByRole("button", "Close", { exact: true }).tap();
  await browser.locator("[role=dialog]").waitFor({ state: "detached" });
  await screen.getByText("No projects yet", { exact: true }).waitFor();
  await app.screenshot("only-project-removed");
  await expect(screen.getByText("Add a project to start your first thread.", { exact: true })).toBeVisible();
});
