// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; import type { LoaderFunctionArgs } from "react-router"; import { listAuditEvents } from "@hivegrid/db/audit-events-query.server"; import { getTranslations } from "~/locale/i18next.server"; import { runAsAdminIdentity } from "~/services/admin/admin-identity.server"; import { buildWhere, loader, parseFilters } from "./audit"; vi.mock("~/components/screens/audit/AuditListPage", () => ({ AuditListPage: () => null })); vi.mock("~/components/ui/errors/ServerError", () => ({ default: () => null })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() })); vi.mock("~/services/admin/admin-identity.server", () => ({ runAsAdminIdentity: vi.fn(async (_request, _permission, operation) => operation({ adminUserId: "admin_1" })), })); vi.mock("@hivegrid/db/audit-events-query.server", () => ({ AUDIT_VIEWER_DEFAULT_PAGE_SIZE: 25, listAuditEvents: vi.fn(), })); function callLoader(url: string) { return loader({ request: new Request(url), params: {}, context: {} } as LoaderFunctionArgs); } describe("admin audit route", () => { beforeEach(() => { vi.clearAllMocks(); process.env.APP_NAME = "HiveGrid"; vi.mocked(getTranslations).mockResolvedValue({ t: (key: string) => key } as never); vi.mocked(listAuditEvents).mockResolvedValue({ rows: [], total: 0, take: 25, skip: 0 } as never); }); it("parses the extended investigation filters", () => { const filters = parseFilters( new URL( "https://admin.example.test/audit?from=2026-07-01&tenantId=t1&actorType=admin&actorId=u1&action=admin&outcome=failure&targetType=Tenant&targetId=t1" ) ); expect(filters).toMatchObject({ from: "2026-07-01", tenantId: "t1", actorType: "admin", actorId: "u1", action: "admin", outcome: "failure", targetType: "Tenant", targetId: "t1", }); }); it("builds a Prisma where shape for tenant/user/action/entity/time filters", () => { const where = buildWhere({ from: "2026-07-01", to: "2026-07-02", tenantId: "t1", actorType: "admin", actorId: "u1", action: "tenant", outcome: "success", targetType: "Tenant", targetId: "t1", }); expect(where).toMatchObject({ tenantId: "t1", actorType: "admin", actorId: "u1", action: { contains: "tenant", mode: "insensitive" }, outcome: "success", targetType: "Tenant", targetId: "t1", }); expect((where.createdAt as { gte: Date; lt: Date }).lt.toISOString()).toBe("2026-07-03T00:00:00.000Z"); }); it("passes the extended filters to listAuditEvents and into the VM", async () => { const data = await callLoader("https://admin.example.test/audit?tenantId=t1&actorId=u1&targetId=target_1&page=2"); expect(runAsAdminIdentity).toHaveBeenCalledWith(expect.any(Request), "admin_identity.audit.read", expect.any(Function)); expect(listAuditEvents).toHaveBeenCalledWith( expect.objectContaining({ where: expect.objectContaining({ tenantId: "t1", actorId: "u1", targetId: "target_1" }), take: 25, skip: 25, }) ); expect(data.vm.status).toBe("ready"); if (data.vm.status !== "ready") return; expect(data.vm.data.filter).toMatchObject({ tenantId: "t1", actorId: "u1", targetId: "target_1" }); }); it("passes the resolved actor name into the view model", async () => { vi.mocked(listAuditEvents).mockResolvedValue({ rows: [ { id: "audit_1", createdAt: new Date("2026-07-27T08:00:00.000Z"), tenantId: null, actorType: "admin", actorId: "cms2q01t70000zz4ymxuk5hba", actorEmail: "admin@email.com", actorDisplayName: "Raffael Sheikh", action: "admin.login.fallback.success", targetType: null, targetId: null, outcome: "success", metadata: null, beforeDiff: null, afterDiff: null, summaryText: "cms2q01t70000zz4ymxuk5hba: admin.login.fallback.success", }, ], total: 1, take: 25, skip: 0, } as never); const data = await callLoader("https://admin.example.test/audit"); expect(data.vm.status).toBe("ready"); if (data.vm.status !== "ready") return; expect(data.vm.data.rows[0]).toMatchObject({ actorLabel: "Raffael Sheikh", actionLabelKey: "audit.action.adminFallbackLoginSuccess", summaryText: null, }); }); });