// frontend/src/shell/NewSessionDialog.test.tsx import { render, screen, waitFor } 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 { NewSessionDialog } from "./NewSessionDialog"; function renderDialog() { const onCreated = vi.fn(); const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }); render( , ); return { onCreated }; } test("the form has only a question field (no workspace/model/provider/thinking)", async () => { renderDialog(); await userEvent.click(screen.getByRole("button", { name: /new/i })); expect(await screen.findByLabelText(/question/i)).toBeInTheDocument(); expect(screen.queryByLabelText(/workspace/i)).not.toBeInTheDocument(); expect(screen.queryByLabelText(/model/i)).not.toBeInTheDocument(); expect(screen.queryByLabelText(/provider/i)).not.toBeInTheDocument(); expect(screen.queryByLabelText(/thinking/i)).not.toBeInTheDocument(); }); test("submitting posts only { question } and calls onCreated", async () => { let body: unknown = null; server.use( http.post("http://localhost:8787/sessions", async ({ request }) => { body = await request.json(); return HttpResponse.json({ id: "s1" }); }), ); const { onCreated } = renderDialog(); await userEvent.click(screen.getByRole("button", { name: /new/i })); await userEvent.type(await screen.findByLabelText(/question/i), "Quante vendite nel 2025?"); await userEvent.click(screen.getByRole("button", { name: /^create$/i })); await waitFor(() => expect(body).toEqual({ question: "Quante vendite nel 2025?" })); await waitFor(() => expect(onCreated).toHaveBeenCalledWith("s1")); }); test("empty question shows a validation error and does not submit", async () => { renderDialog(); await userEvent.click(screen.getByRole("button", { name: /new/i })); await userEvent.click(screen.getByRole("button", { name: /^create$/i })); expect(await screen.findByRole("alert")).toHaveTextContent(/empty/i); });