import { act, 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"; import { useSessionStore } from "../store/sessionStore"; 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 accordion expands to reveal archived sessions", async () => { wrap(); // archived hidden until the Archive accordion is expanded expect(await screen.findByText("Attiva uno")).toBeInTheDocument(); expect(screen.queryByText("Archiviata due")).not.toBeInTheDocument(); await userEvent.click(screen.getByRole("button", { name: /archive/i })); expect(await screen.findByText("Archiviata due")).toBeInTheDocument(); // Active sessions remain visible (it is a separate accordion, not a swap) expect(screen.getByText("Attiva uno")).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()); }); test("WIP icon toggles the Model activity panel and shows the streamed text", async () => { wrap(); // Activate a session by opening + resuming is heavy; instead drive the store directly. // The WIP icon only renders with an active session, so simulate one via Resume. server.use(http.post("http://localhost:8787/sessions/:id/resume", () => new HttpResponse(null, { status: 204 }))); await userEvent.click(await screen.findByText("Attiva uno")); // opens docs panel await userEvent.click(await screen.findByRole("button", { name: /resume/i })); // active session act(() => { useSessionStore.getState().applyEvent({ type: "text_delta", text: "Looking at dim_patient." }); }); // Model activity hidden until the WIP icon is clicked expect(screen.queryByText(/Looking at dim_patient/)).not.toBeInTheDocument(); await userEvent.click(screen.getByRole("button", { name: /model activity/i })); expect(await screen.findByText(/Looking at dim_patient/)).toBeInTheDocument(); }); test("renaming a group reassigns its members via setSessionGroup", async () => { const groupSets: Array<{ id: string; group: string }> = []; server.use( http.post("http://localhost:8787/sessions/:id/group", async ({ params, request }) => { const body = (await request.json()) as { group: string }; groupSets.push({ id: params.id as string, group: body.group }); return new HttpResponse(null, { status: 204 }); }), ); wrap(); await screen.findByText("Aritmologia"); await userEvent.click(screen.getByRole("button", { name: /rename group aritmologia/i })); const input = await screen.findByLabelText(/name/i, { selector: "input" }); await userEvent.clear(input); await userEvent.type(input, "Cardiologia"); await userEvent.click(screen.getByRole("button", { name: /save/i })); await waitFor(() => expect(groupSets).toEqual([{ id: "s1", group: "Cardiologia" }])); });