import { describe, it, expect } from "vitest"; import { auditOrgScopeWhere, auditFilterWhere, auditScopedWhere } from "./audit-scope.js"; describe("audit-scope (the shared org-scope where-builder)", () => { it("org scope = workspace rows OR (org-level rows: metadata.orgId OR organization resourceType+resourceId)", () => { const w = auditOrgScopeWhere("org1", ["ws1", "ws2"]); expect(w).toEqual({ OR: [ { workspaceId: { in: ["ws1", "ws2"] } }, { workspaceId: null, OR: [ // Prisma JsonFilter: `{ path, equals }` (a bare `{ orgId }` is rejected at runtime). { metadata: { path: ["orgId"], equals: "org1" } }, { resourceType: { in: ["organization", "org"] }, resourceId: "org1" } ] } ] }); }); it("filter builder: empty filters → empty clause (no crash on AND:[.., {}])", () => { expect(auditFilterWhere({})).toEqual({}); }); it("filter builder: each filter maps to its clause", () => { const from = new Date("2026-01-01T00:00:00Z"); const to = new Date("2026-01-31T00:00:00Z"); expect( auditFilterWhere({ actorId: "u1", actorType: "USER", action: "page.created", resourceType: "page", workspaceId: "ws1", from, to }) ).toEqual({ actorId: "u1", actorType: "USER", action: "page.created", resourceType: "page", workspaceId: "ws1", createdAt: { gte: from, lte: to } }); }); it("actionPrefix → startsWith (LIKE 'prefix%')", () => { expect(auditFilterWhere({ actionPrefix: "page." })).toEqual({ action: { startsWith: "page." } }); }); it("single-sided date windows (only from / only to)", () => { const from = new Date("2026-01-01T00:00:00Z"); expect(auditFilterWhere({ from })).toEqual({ createdAt: { gte: from } }); const to = new Date("2026-01-31T00:00:00Z"); expect(auditFilterWhere({ to })).toEqual({ createdAt: { lte: to } }); }); it("scoped = AND[org scope, filters]", () => { const w = auditScopedWhere("org1", ["ws1"], { action: "user.login" }); expect(w).toEqual({ AND: [ auditOrgScopeWhere("org1", ["ws1"]), { action: "user.login" } ] }); }); });