import { describe, expect, it } from "vitest"; import { entitlementLimitTypes, entitlementPlanIds, entitlementsPackageName, evaluateProductAccess, featureKeys, getDefaultProductBlueprint, getStandardEntitlementPlan, productBlueprintIds, } from "./index.js"; describe("entitlements package shell", () => { it("exports the package identity placeholder", () => { expect(entitlementsPackageName).toBe("@hivegrid/entitlements"); }); it("defines the CRM SaaS product core blueprint as the default showcase", () => { const blueprint = getDefaultProductBlueprint(); expect(blueprint).toMatchObject({ id: productBlueprintIds.crmSaas, version: "2026.07.20", defaultShowcase: true, }); expect(blueprint.features.map((feature) => feature.featureKey)).toEqual( expect.arrayContaining([featureKeys.crmBasic, featureKeys.chat, featureKeys.supportTickets, featureKeys.workflowEngine, featureKeys.mcp]) ); }); it("keeps Core, Swarm, and Colony on one HiveGrid core", () => { const starter = getStandardEntitlementPlan(entitlementPlanIds.starter)!; const team = getStandardEntitlementPlan(entitlementPlanIds.pro)!; const business = getStandardEntitlementPlan(entitlementPlanIds.business)!; const withoutPortal = (plan: typeof starter) => plan.features .filter((feature) => feature.featureKey !== featureKeys.customerPortal) .map(({ featureKey, limitType, limit }) => ({ featureKey, limitType, limit })); expect(starter.metadata.publicName).toBe("Core"); expect(team.metadata.publicName).toBe("Swarm"); expect(business.metadata.publicName).toBe("Colony"); expect(withoutPortal(starter)).toEqual(withoutPortal(team)); expect(withoutPortal(team)).toEqual(withoutPortal(business)); expect(starter.features).toEqual( expect.arrayContaining([ expect.objectContaining({ featureKey: featureKeys.api, limitType: entitlementLimitTypes.included }), expect.objectContaining({ featureKey: featureKeys.workflowEngine, limitType: entitlementLimitTypes.unlimited }), expect.objectContaining({ featureKey: featureKeys.kwpIntegration, limitType: entitlementLimitTypes.notIncluded }), ]) ); expect(starter.features.some((feature) => feature.featureKey === featureKeys.credits)).toBe(false); expect(starter.features.some((feature) => feature.featureKey === featureKeys.privateGenerations)).toBe(false); }); it("evaluates product access through blueprint, plan, tenant override, and tenant-owned role permission", () => { // Fundament D: workflow-engine is a core module, so a plan listing it as // not-included no longer blocks it — core access is granted with reason "included". expect( evaluateProductAccess({ blueprintId: productBlueprintIds.crmSaas, featureKey: featureKeys.workflowEngine, hasSubscription: true, limitType: entitlementLimitTypes.notIncluded, tenantRolePermissionNames: ["app.settings.roles.set"], }) ).toMatchObject({ allowed: true, reason: "included" }); expect( evaluateProductAccess({ blueprintId: productBlueprintIds.crmSaas, featureKey: featureKeys.workflowEngine, hasSubscription: true, limitType: entitlementLimitTypes.monthly, limit: 100, used: 10, tenantOverride: "disabled", tenantRolePermissionNames: ["app.settings.roles.set"], }) ).toMatchObject({ allowed: false, reason: "tenant-override-disabled" }); expect( evaluateProductAccess({ blueprintId: productBlueprintIds.crmSaas, featureKey: featureKeys.workflowEngine, hasSubscription: true, limitType: entitlementLimitTypes.monthly, limit: 100, used: 10, tenantRolePermissionNames: [], }) ).toMatchObject({ allowed: false, reason: "tenant-role-missing", requiredTenantRolePermission: "app.settings.roles.set" }); expect( evaluateProductAccess({ blueprintId: productBlueprintIds.crmSaas, featureKey: featureKeys.workflowEngine, hasSubscription: true, limitType: entitlementLimitTypes.monthly, limit: 100, used: 10, tenantRolePermissionNames: ["app.settings.roles.set"], }) ).toMatchObject({ allowed: true, reason: "monthly-remaining", remaining: 90 }); // A non-toggleable ADD-ON (ai-credits: not includedInCore) still rejects an "enabled" // override — a tenant cannot self-enable a paid add-on. Core modules are exempted (see // product-access.edge.test.ts), so this uses an add-on to keep exercising the path. expect( evaluateProductAccess({ blueprintId: productBlueprintIds.crmSaas, featureKey: featureKeys.credits, hasSubscription: true, limitType: entitlementLimitTypes.notIncluded, tenantOverride: "enabled", tenantRolePermissionNames: ["app.dashboard.view"], }) ).toMatchObject({ allowed: false, reason: "tenant-override-not-toggleable" }); }); });