import { render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { http, HttpResponse } from "msw"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { server } from "../test/msw"; import { FakeEventSource } from "../test/fakeEventSource"; import { AppShell } from "./AppShell"; function wrap() { const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }); return render(); } const LIST = [ { id: "s1", status: "open", question: "Attiva uno", summary: null, created_at: "2026-01-02T00:00:00Z", updated_at: null, author: null, name: null, group: "Aritmologia", archived: false }, { id: "s2", status: "finalized", question: "Archiviata due", summary: null, created_at: "2026-01-01T00:00:00Z", updated_at: null, author: null, name: null, group: null, archived: true }, ]; beforeEach(() => { FakeEventSource.instances = []; (globalThis as any).EventSource = FakeEventSource; server.use( http.get("http://localhost:8787/sessions", () => HttpResponse.json(LIST)), http.get("http://localhost:8787/sessions/:id/documents", () => HttpResponse.json([ { phase: "—", key: "question", title: "Domanda originale", format: "text", content: "Attiva uno" }, ])), http.post("http://localhost:8787/sessions/:id/archive", () => new HttpResponse(null, { status: 204 })), ); }); test("active list shows group header and hides archived sessions", async () => { wrap(); expect(await screen.findByText("Attiva uno")).toBeInTheDocument(); expect(screen.getByText("Aritmologia")).toBeInTheDocument(); // group header expect(screen.queryByText("Archiviata due")).not.toBeInTheDocument(); // archived hidden }); test("opening the panel shows the session documents", async () => { wrap(); await userEvent.click(await screen.findByText("Attiva uno")); expect(await screen.findByText("Domanda originale")).toBeInTheDocument(); }); test("Archive toggle reveals archived sessions", async () => { wrap(); await userEvent.click(await screen.findByRole("button", { name: /archive/i })); expect(await screen.findByText("Archiviata due")).toBeInTheDocument(); }); test("Resume from the panel activates the session and closes the panel", async () => { let resumed: string | null = null; server.use( http.post("http://localhost:8787/sessions/:id/resume", ({ params }) => { resumed = params.id as string; return new HttpResponse(null, { status: 204 }); }), ); wrap(); await userEvent.click(await screen.findByText("Attiva uno")); await screen.findByText("Domanda originale"); // panel open await userEvent.click(screen.getByRole("button", { name: /resume/i })); await waitFor(() => expect(resumed).toBe("s1")); await waitFor(() => expect(screen.queryByText("Domanda originale")).not.toBeInTheDocument()); // panel closed }); test("a failed resume keeps the panel open and does not activate the session", async () => { server.use(http.post("http://localhost:8787/sessions/:id/resume", () => new HttpResponse(null, { status: 409 }))); wrap(); await userEvent.click(await screen.findByText("Attiva uno")); await screen.findByText("Domanda originale"); // panel open await userEvent.click(screen.getByRole("button", { name: /resume/i })); // panel stays open (document still visible) after the failed resume await waitFor(() => expect(screen.getByText("Domanda originale")).toBeInTheDocument()); });