1import { describe, it, expect } from "vitest";
2import { auditOrgScopeWhere, auditFilterWhere, auditScopedWhere } from "./audit-scope.js";
3
4describe("audit-scope (the shared org-scope where-builder)", () => {
5 it("org scope = workspace rows OR (org-level rows: metadata.orgId OR organization resourceType+resourceId)", () => {
6 const w = auditOrgScopeWhere("org1", ["ws1", "ws2"]);
7 expect(w).toEqual({
8 OR: [
9 { workspaceId: { in: ["ws1", "ws2"] } },
10 {
11 workspaceId: null,
12 OR: [
13
14 { metadata: { path: ["orgId"], equals: "org1" } },
15 { resourceType: { in: ["organization", "org"] }, resourceId: "org1" }
16 ]
17 }
18 ]
19 });
20 });
21
22 it("filter builder: empty filters → empty clause (no crash on AND:[.., {}])", () => {
23 expect(auditFilterWhere({})).toEqual({});
24 });
25
26 it("filter builder: each filter maps to its clause", () => {
27 const from = new Date("2026-01-01T00:00:00Z");
28 const to = new Date("2026-01-31T00:00:00Z");
29 expect(
30 auditFilterWhere({
31 actorId: "u1",
32 actorType: "USER",
33 action: "page.created",
34 resourceType: "page",
35 workspaceId: "ws1",
36 from,
37 to
38 })
39 ).toEqual({
40 actorId: "u1",
41 actorType: "USER",
42 action: "page.created",
43 resourceType: "page",
44 workspaceId: "ws1",
45 createdAt: { gte: from, lte: to }
46 });
47 });
48
49 it("actionPrefix → startsWith (LIKE 'prefix%')", () => {
50 expect(auditFilterWhere({ actionPrefix: "page." })).toEqual({ action: { startsWith: "page." } });
51 });
52
53 it("single-sided date windows (only from / only to)", () => {
54 const from = new Date("2026-01-01T00:00:00Z");
55 expect(auditFilterWhere({ from })).toEqual({ createdAt: { gte: from } });
56 const to = new Date("2026-01-31T00:00:00Z");
57 expect(auditFilterWhere({ to })).toEqual({ createdAt: { lte: to } });
58 });
59
60 it("scoped = AND[org scope, filters]", () => {
61 const w = auditScopedWhere("org1", ["ws1"], { action: "user.login" });
62 expect(w).toEqual({
63 AND: [
64 auditOrgScopeWhere("org1", ["ws1"]),
65 { action: "user.login" }
66 ]
67 });
68 });
69});
70