/** * Pins the `maxGroupDepth` repository guard against real SurrealDB. */ import { SurrealAdapter } from "@suaveplan/surrealdb"; import { afterAll, beforeAll, describe, expect, it, } from "@suaveplan/testing/runner"; import { SurrealDBFixture } from "@suaveplan/testing-integration-surrealdb"; import { applyContactsSchema, CONTACTS_API_CODES, SurrealGroupRepository, } from "../../src/index.js"; let fixture: SurrealDBFixture | null = null; let client: SurrealAdapter | null = null; beforeAll(async () => { fixture = new SurrealDBFixture(); const cfg = await fixture.start(); client = new SurrealAdapter({ url: cfg.url, namespace: cfg.namespace, database: cfg.database, username: cfg.username, password: cfg.password, }); await client.connect(); await applyContactsSchema({ db: client }); }, 180_000); afterAll(async () => { if (client !== null) { await client.disconnect(); client = null; } if (fixture !== null) { await fixture.stop(); fixture = null; } }, 60_000); describe("api-plugin-contacts group-depth integration", () => { it("createGroup raises GROUP_DEPTH_EXCEEDED when enforceGroupDepth is on", { timeout: 60_000, }, async () => { if (client === null) throw new Error("client not ready"); const repo = new SurrealGroupRepository(client, { maxGroupDepth: 2, enforceGroupDepth: true, }); const a = await repo.createGroup({ tenantId: "t-depth", name: "A" }); const b = await repo.createGroup({ tenantId: "t-depth", name: "B", parentId: a.id, }); // b.depth === 1; another child would be depth 2 == maxGroupDepth (>=). await expect( repo.createGroup({ tenantId: "t-depth", name: "C", parentId: b.id, }) ).rejects.toMatchObject({ code: CONTACTS_API_CODES.GROUP_DEPTH_EXCEEDED, }); await repo.delete(b.id); await repo.delete(a.id); }); it("createGroup permits any depth when enforceGroupDepth is false", { timeout: 60_000, }, async () => { if (client === null) throw new Error("client not ready"); const repo = new SurrealGroupRepository(client, { maxGroupDepth: 1, enforceGroupDepth: false, }); const a = await repo.createGroup({ tenantId: "t-depth-off", name: "A", }); const b = await repo.createGroup({ tenantId: "t-depth-off", name: "B", parentId: a.id, }); const c = await repo.createGroup({ tenantId: "t-depth-off", name: "C", parentId: b.id, }); expect(c.depth).toBe(2); await repo.delete(c.id); await repo.delete(b.id); await repo.delete(a.id); }); });