Files
GEWU/web/src/shell/ProjectsSection.test.tsx
T

65 lines
4.3 KiB
TypeScript
Raw Normal View History

import { cleanup, render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, expect, it, vi } from "vitest";
import Admin from "./Admin";
afterEach(() => { cleanup(); vi.unstubAllGlobals(); });
it("creates a project from the admin form and makes it available for account and agent grants", async () => {
let created = false;
let conflict = true;
const project = {
schema_version: 1, project_id: "my-research", title: "My research", status: "active",
north_star: "Investigate useful techniques.", key_questions: ["What works?", "Under what conditions?"],
owners: ["user:admin"], autonomy_policy_id: "my-research-policy",
};
const fetchMock = vi.fn(async (path: string, init?: RequestInit) => {
if (path === "/v1/session" && init?.method === "GET") return { ok: false, status: 401, json: async () => ({}) };
let result: unknown = [];
if (path === "/v1/users/session") result = { csrf_token: "csrf-fixture" };
if (path === "/v1/me") result = { principal_id: "user:admin" };
if (path === "/v1/admin/projects") {
if (init?.method === "POST") {
if (conflict) return { ok: false, status: 409, json: async () => ({ detail: "Project ID is already registered" }) };
created = true; result = project;
} else result = created ? [project] : [];
}
return { ok: true, json: async () => result };
});
vi.stubGlobal("fetch", fetchMock);
const user = userEvent.setup();
render(<Admin />);
await user.type(await screen.findByLabelText("Username"), "admin");
await user.type(screen.getByLabelText("Password"), "password-fixture");
await user.click(screen.getByRole("button", { name: "Sign in" }));
const projects = await screen.findByRole("region", { name: "Projects" });
expect(within(projects).getByText("No projects yet. Create your first project below.")).toBeVisible();
await user.click(within(projects).getByRole("button", { name: "Create project" }));
const dialog = await screen.findByRole("dialog", { name: "Create a project" });
expect(within(dialog).getByLabelText(/Owner ID/)).toHaveValue("user:admin");
await user.type(within(dialog).getByLabelText("Project name"), "My research");
expect(within(dialog).getByLabelText(/Project ID/)).toHaveValue("my-research");
await user.type(within(dialog).getByLabelText("Project purpose"), "Investigate useful techniques.");
await user.type(within(dialog).getByLabelText("Key research questions"), "What works?\nUnder what conditions?");
await user.click(within(dialog).getByRole("button", { name: "Create project" }));
expect(await screen.findByRole("alert")).toHaveTextContent("Project ID is already registered");
expect(within(dialog).getByLabelText("Project name")).toHaveValue("My research");
conflict = false;
await user.click(within(dialog).getByRole("button", { name: "Create project" }));
await screen.findByText("Created My research. It is now available for user access and agent keys.");
expect(fetchMock).toHaveBeenCalledWith("/v1/admin/projects", expect.objectContaining({
method: "POST", headers: expect.objectContaining({ "X-GEWU-CSRF": "csrf-fixture" }),
body: JSON.stringify({
project: { project_id: "my-research", title: "My research", status: "active", owners: ["user:admin"], north_star: "Investigate useful techniques.", key_questions: ["What works?", "Under what conditions?"], autonomy_policy_id: "my-research-policy", default_includes: [] },
policy: { policy_id: "my-research-policy", policy_version: 1, project_id: "my-research", automatic_source_registration: true, automatic_node_types: ["finding", "problem", "technique", "case", "entity", "event", "concept", "datapoint", "synthesis"], require_supported_assessment: true },
}),
}));
await user.click(screen.getByRole("tab", { name: "Users" }));
await user.click(await screen.findByRole("button", { name: "Add user" }));
const usersDialog = await screen.findByRole("dialog", { name: "Add user" });
expect(within(usersDialog).getByLabelText("My research")).toBeVisible();
await user.click(screen.getByRole("tab", { name: "Credentials" }));
await user.click(await screen.findByRole("button", { name: "Add credential" }));
const credsDialog = await screen.findByRole("dialog", { name: "Add credential" });
expect(within(credsDialog).getByLabelText("My research")).toBeVisible();
});