import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn() })); vi.mock("@/app/lib/clientApi", () => mocks); import { getAuditEvent, searchAuditLog } from "./auditLog"; beforeEach(() => mocks.apiGet.mockReset()); describe("auditLog service", () => { it("searchAuditLog sends only non-empty filters with a 0-based page", async () => { mocks.apiGet.mockResolvedValue({ content: [{ id: "e1" }], totalElements: 9 }); const out = await searchAuditLog({ actor: "admin", module: "", action: "USER_UPDATE", outcome: "FAILURE", from: "2026-11-01", to: "", page: 2, pageSize: 10 }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/audit-log?actor=admin&action=USER_UPDATE&outcome=FAILURE&from=2026-11-01&page=1&size=10"); expect(out).toEqual({ items: [{ id: "e1" }], total: 9 }); }); it("searchAuditLog tolerates an empty body", async () => { mocks.apiGet.mockResolvedValue(null); expect(await searchAuditLog({ page: 1, pageSize: 10 })).toEqual({ items: [], total: 0 }); }); it("getAuditEvent GETs one event by encoded id", async () => { mocks.apiGet.mockResolvedValue({ id: "a/b" }); await getAuditEvent("a/b"); expect(mocks.apiGet).toHaveBeenCalledWith("/api/audit-log/a%2Fb"); }); });