import fs from "node:fs"; import path from "node:path"; import { describe, expect, it } from "vitest"; const routeRoot = path.resolve(process.cwd(), "apps/web/app/routes/admin/entities"); const helperPath = path.resolve(process.cwd(), "apps/web/app/utils/db/entities/entityRelationships.db.server.ts"); function readRoute(relativePath: string) { return fs.readFileSync(path.join(routeRoot, relativePath), "utf8"); } describe("admin entity relationship route scoping", () => { it("exposes scoped relationship DB helpers", () => { const source = fs.readFileSync(helperPath, "utf8"); expect(source).toContain("getScopedEntityRelationship"); expect(source).toContain("updateScopedEntityRelationship"); expect(source).toContain("deleteScopedEntityRelationship"); expect(source).toContain("OR: [{ parentId: scope.entityId }, { childId: scope.entityId }]"); }); it("scopes relationship detail mutations to the current entity", () => { const source = readRoute("$entity/relationships/$id.tsx"); expect(source).toContain("getScopedEntityRelationship"); expect(source).toContain("updateScopedEntityRelationship"); expect(source).toContain("deleteScopedEntityRelationship"); expect(source).toContain("entityId: entity.id"); expect(source).not.toMatch(/\bgetEntityRelationship\(/); expect(source).not.toMatch(/\bupdateEntityRelationship\(/); expect(source).not.toMatch(/\bdeleteEntityRelationship\(/); }); it("scopes relationship list order updates to the current entity", () => { const source = readRoute("$entity/relationships.tsx"); expect(source).toContain("allowedRelationshipIds"); expect(source).toContain("updateScopedEntityRelationship"); expect(source).toContain("entityId: entity.id"); expect(source).not.toMatch(/\bupdateEntityRelationship\(/); }); it("scopes relationship creation to admin entities and the current entity", () => { const source = readRoute("$entity/relationships/new.tsx"); expect(source).toContain("adminEntityIds"); expect(source).toContain("parentId !== entity.id && childId !== entity.id"); expect(source).toContain("getAllEntities({ tenantId: null })"); }); });