116 lines
4.6 KiB
TypeScript
116 lines
4.6 KiB
TypeScript
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.05.15",
|
|
defaultShowcase: true,
|
|
});
|
|
expect(blueprint.features.map((feature) => feature.featureKey)).toEqual(
|
|
expect.arrayContaining([featureKeys.crmBasic, featureKeys.supportTickets, featureKeys.workflowEngine, featureKeys.mcp])
|
|
);
|
|
});
|
|
|
|
it("keeps Starter, Pro, and Business realistically strict", () => {
|
|
expect(getStandardEntitlementPlan(entitlementPlanIds.starter)?.features).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({ featureKey: featureKeys.crmBasic, limitType: entitlementLimitTypes.included }),
|
|
expect.objectContaining({ featureKey: featureKeys.api, limitType: entitlementLimitTypes.notIncluded }),
|
|
expect.objectContaining({ featureKey: featureKeys.workflowEngine, limitType: entitlementLimitTypes.notIncluded }),
|
|
])
|
|
);
|
|
expect(getStandardEntitlementPlan(entitlementPlanIds.pro)?.features).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({ featureKey: featureKeys.emailMarketing, limitType: entitlementLimitTypes.monthly }),
|
|
expect.objectContaining({ featureKey: featureKeys.api, limitType: entitlementLimitTypes.included }),
|
|
expect.objectContaining({ featureKey: featureKeys.workflowEngine, limitType: entitlementLimitTypes.notIncluded }),
|
|
])
|
|
);
|
|
expect(getStandardEntitlementPlan(entitlementPlanIds.business)?.features).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({ featureKey: featureKeys.workflowEngine, limitType: entitlementLimitTypes.monthly }),
|
|
expect.objectContaining({ featureKey: featureKeys.mcp, limitType: entitlementLimitTypes.included }),
|
|
expect.objectContaining({ featureKey: featureKeys.advancedBuilder, limitType: entitlementLimitTypes.included }),
|
|
])
|
|
);
|
|
});
|
|
|
|
it("evaluates product access through blueprint, plan, tenant override, and tenant-owned role permission", () => {
|
|
expect(
|
|
evaluateProductAccess({
|
|
blueprintId: productBlueprintIds.crmSaas,
|
|
featureKey: featureKeys.workflowEngine,
|
|
hasSubscription: true,
|
|
limitType: entitlementLimitTypes.notIncluded,
|
|
tenantRolePermissionNames: ["app.settings.roles.set"],
|
|
})
|
|
).toMatchObject({ allowed: false, reason: "not-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 });
|
|
|
|
expect(
|
|
evaluateProductAccess({
|
|
blueprintId: productBlueprintIds.crmSaas,
|
|
featureKey: featureKeys.crmBasic,
|
|
hasSubscription: true,
|
|
limitType: entitlementLimitTypes.notIncluded,
|
|
tenantOverride: "enabled",
|
|
tenantRolePermissionNames: ["app.dashboard.view"],
|
|
})
|
|
).toMatchObject({ allowed: false, reason: "tenant-override-not-toggleable" });
|
|
});
|
|
});
|