64 lines
2.8 KiB
TypeScript
64 lines
2.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { ALL_SEARCH_INDEXES } from "./schemas/index.js";
|
|
import {
|
|
getIndexedEntitiesForPermissions,
|
|
getIndexedEntity,
|
|
listIndexedEntities,
|
|
validateIndexedEntities,
|
|
type IndexedEntityRegistry,
|
|
} from "./indexed-entities.js";
|
|
|
|
const validIndex = ALL_SEARCH_INDEXES[0]?.name ?? "rows";
|
|
|
|
const fixture: IndexedEntityRegistry = {
|
|
contact: { table: "Kontakt", index: validIndex, searchable: ["displayName", "email"], permission: "crm.read" },
|
|
deal: { table: "Verkaufsfall", index: validIndex, searchable: ["title"], permission: "sales.read" },
|
|
};
|
|
|
|
describe("indexedEntities registry", () => {
|
|
it("registers fachmodul entities as they are built (DoD #16) — files is the first", () => {
|
|
const keys = listIndexedEntities().map((entity) => entity.key);
|
|
expect(keys).toContain("file");
|
|
expect(getIndexedEntity("file")).toMatchObject({ key: "file", table: "File", index: "files", permission: "files.read_all" });
|
|
// CRM contacts (R-P6): one entry covers both private persons and organisations.
|
|
expect(keys).toContain("contact");
|
|
expect(getIndexedEntity("contact")).toMatchObject({
|
|
key: "contact",
|
|
table: "Kontakt",
|
|
index: "crm-contacts",
|
|
permission: "crm.read_all",
|
|
});
|
|
});
|
|
|
|
it("lists registered entities together with their key", () => {
|
|
expect(listIndexedEntities(fixture).map((entity) => entity.key)).toEqual(["contact", "deal"]);
|
|
expect(getIndexedEntity("contact", fixture)).toMatchObject({ key: "contact", table: "Kontakt", permission: "crm.read" });
|
|
expect(getIndexedEntity("missing", fixture)).toBeUndefined();
|
|
});
|
|
|
|
it("permission-gates entities for the caller's granted permissions (M057)", () => {
|
|
expect(getIndexedEntitiesForPermissions(["crm.read"], fixture).map((entity) => entity.key)).toEqual(["contact"]);
|
|
expect(getIndexedEntitiesForPermissions(new Set(["crm.read", "sales.read"]), fixture).map((entity) => entity.key)).toEqual([
|
|
"contact",
|
|
"deal",
|
|
]);
|
|
expect(getIndexedEntitiesForPermissions([], fixture)).toEqual([]);
|
|
});
|
|
|
|
it("flags entities that reference an unknown index or are missing fields/permission", () => {
|
|
expect(validateIndexedEntities(fixture)).toEqual([]);
|
|
|
|
const broken: IndexedEntityRegistry = {
|
|
ghost: { table: "Ghost", index: "does_not_exist", searchable: [], permission: "" },
|
|
};
|
|
const problems = validateIndexedEntities(broken);
|
|
expect(problems.some((problem) => problem.includes("unknown search index"))).toBe(true);
|
|
expect(problems.some((problem) => problem.includes("no searchable fields"))).toBe(true);
|
|
expect(problems.some((problem) => problem.includes("no permission"))).toBe(true);
|
|
});
|
|
|
|
it("keeps the live registry internally consistent", () => {
|
|
expect(validateIndexedEntities()).toEqual([]);
|
|
});
|
|
});
|