import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), apiPut: vi.fn() })); vi.mock("@/app/lib/clientApi", () => mocks); import { listAdminUsers, getAdminUser, saveAdminUser, sendAdminUserPasswordReset, setAdminUserPassword, USER_STATUS_FILTER_OPTIONS, } from "./adminUsers"; import { DEFAULT_MESSAGES } from "@/app/lib/i18n/defaultMessages"; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPost.mockReset(); mocks.apiPut.mockReset(); }); describe("listAdminUsers", () => { it("translates 1-based page to a 0-based query and normalizes the page shape", async () => { mocks.apiGet.mockResolvedValue({ content: [{ id: 1 }], totalElements: 1 }); const result = await listAdminUsers({ page: 2, pageSize: 20 }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/users?page=1&size=20"); expect(result).toEqual({ items: [{ id: 1 }], total: 1 }); }); it("sends q and enabled when given", async () => { mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 }); await listAdminUsers({ q: "jane", enabled: "false", page: 1, pageSize: 20 }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/users?q=jane&enabled=false&page=0&size=20"); }); }); describe("saveAdminUser", () => { it("POSTs when id is absent", async () => { mocks.apiPost.mockResolvedValue({ id: 1 }); await saveAdminUser({ name: "Jane", username: "jane.doe", email: "j@x.com", roles: [], enabled: true }); expect(mocks.apiPost).toHaveBeenCalledWith("/api/users", expect.objectContaining({ username: "jane.doe" })); }); it("PUTs to /api/users/{id} when id is present", async () => { mocks.apiPut.mockResolvedValue({ id: 9 }); await saveAdminUser({ id: 9, name: "Jane", username: "jane.doe", email: "j@x.com", roles: [], enabled: true }); expect(mocks.apiPut).toHaveBeenCalledWith("/api/users/9", expect.objectContaining({ id: 9 })); }); }); describe("sendAdminUserPasswordReset", () => { it("POSTs to /api/users/{id}/reset-password", async () => { mocks.apiPost.mockResolvedValue({ updated: 1 }); const result = await sendAdminUserPasswordReset(9); expect(mocks.apiPost).toHaveBeenCalledWith("/api/users/9/reset-password", undefined); expect(result).toEqual({ updated: 1 }); }); }); describe("setAdminUserPassword", () => { it("POSTs the password pair to /api/users/{id}/password", async () => { mocks.apiPost.mockResolvedValue(undefined); await setAdminUserPassword(9, "Good#Pass1", "Good#Pass1"); expect(mocks.apiPost).toHaveBeenCalledWith("/api/users/9/password", { password: "Good#Pass1", confirmPassword: "Good#Pass1" }); }); }); describe("normalizeAdminUserPage identity refs", () => { it("resolves bare role ids to the first full role object in the response and drops unresolvable ones", async () => { const { normalizeAdminUserPage } = await import("./adminUsers"); const { items } = normalizeAdminUserPage({ content: [ { id: 1, username: "a", roles: [{ id: 117, name: "ROLE_CV" }] }, { id: 2, username: "b", roles: [117] }, { id: 3, username: "c", roles: [999] }, ], totalElements: 3, }); expect(items[0].roles).toEqual([{ id: 117, name: "ROLE_CV" }]); expect(items[1].roles).toEqual([{ id: 117, name: "ROLE_CV" }]); expect(items[2].roles).toEqual([]); }); }); // Copy lock (spec "portal-copy-i18n", A7): the status filter labels are message codes, rendered with t(). describe("USER_STATUS_FILTER_OPTIONS", () => { it("labels the options with message codes", () => { expect(USER_STATUS_FILTER_OPTIONS).toEqual([ { value: "true", label: "Enabled" }, { value: "false", label: "Disabled" }, ]); for (const o of USER_STATUS_FILTER_OPTIONS) expect(Object.hasOwn(DEFAULT_MESSAGES, o.label)).toBe(true); }); });