import { screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, expect, it } from "vitest"; import { ApiError } from "../src/api/client"; import { fakeApi, renderApp, samplePlayerState, unauthorized } from "./render"; describe("guest onboarding", () => { it("shows the start screen without a session; the button creates a guest and opens HQ", async () => { const api = fakeApi({ getPlayerState: unauthorized, createGuest: () => ({ playerId: "p1", state: samplePlayerState() }), }); renderApp(api, "/fleet"); const start = await screen.findByRole("button", { name: "Start your company" }); expect(screen.queryByRole("navigation", { name: "Main" })).not.toBeInTheDocument(); await userEvent.click(start); expect(await screen.findByRole("heading", { level: 1, name: "HQ" })).toBeInTheDocument(); expect(api.call).toHaveBeenCalledWith("createGuest", {}); }); it("skips the start screen when a stored session is still valid", async () => { const api = fakeApi({ getPlayerState: () => samplePlayerState() }); renderApp(api); expect(await screen.findByRole("heading", { level: 1, name: "HQ" })).toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Start your company" })).not.toBeInTheDocument(); expect(api.call).not.toHaveBeenCalledWith("createGuest", expect.anything()); }); it("shows an error and stays on the start screen if creating the guest fails", async () => { const api = fakeApi({ getPlayerState: unauthorized, createGuest: () => { throw new ApiError(500, "internal-error", "boom"); }, }); renderApp(api); await userEvent.click(await screen.findByRole("button", { name: "Start your company" })); expect(await screen.findByRole("alert")).toHaveTextContent("Could not start a company"); expect(screen.getByRole("button", { name: "Start your company" })).toBeEnabled(); }); });