60 lines
2.5 KiB
TypeScript
60 lines
2.5 KiB
TypeScript
// 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(
|
|
<QueryClientProvider client={client}>
|
|
<NewSessionDialog onCreated={onCreated} />
|
|
</QueryClientProvider>,
|
|
);
|
|
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 includes browser-local migrated preferences and calls onCreated", async () => {
|
|
localStorage.clear();
|
|
let body: unknown = null;
|
|
server.use(
|
|
http.get("http://localhost:8787/settings", () => HttpResponse.json({
|
|
workspace: "default", provider: "zai", model: "glm-5.2", thinking: "low",
|
|
})),
|
|
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?",
|
|
workspaceId: "default", provider: "zai", model: "glm-5.2", thinking: "low",
|
|
}));
|
|
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);
|
|
});
|