Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/entitlements/src/plans.test.ts
T

177 lines
7.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
entitlementLimitTypes,
featureGateRegistry,
getFeatureGate,
getStandardEntitlementPlan,
getBillingProviderPricePlaceholder,
getBillingProviderProductPlaceholder,
listStandardEntitlementPlanIds,
standardEntitlementPlans,
type EntitlementPlanFeatureAssignment,
type FeatureKey,
} from "./index.js";
const expectedPlanIds = ["starter", "pro", "business"] as const;
const expectedFeatureKeys = [
"dashboard",
"crm-basic",
"leads",
"projects",
"support-tickets",
"knowledge-base-read",
"knowledge-base-tenant",
"files",
"users",
"tasks",
"hr",
"integrations",
"kwp_integration",
"telephony",
"chat",
"booking",
"forms",
"signature",
"billing",
"customer_portal",
"customer_portal_custom_domain",
"search",
"automation",
"api",
"email-marketing",
"imported-lists",
"crm-based-lists",
"onboarding",
"workflow-engine",
"mcp",
"advanced-builder",
"import-export",
"email-inbox",
"rate-limit-per-minute",
"rate-limit-per-second",
"priority-support",
] as const satisfies readonly FeatureKey[];
const seededFeatureRegistry = featureGateRegistry.filter((feature) => feature.key !== "ai-credits" && feature.key !== "private-generations");
const unsafeFixturePattern =
/(?:prod_[a-z0-9]|price_[a-z0-9]|sk_(?:live|test)|pk_(?:live|test)|api[_ -]?key|secret|password|token|tenant[_ -]?id|customer[_ -]?id|subscription[_ -]?id|acct_[a-z0-9]|cus_[a-z0-9]|sub_[a-z0-9])/i;
function limitTypeName(limitType: number): string | undefined {
return Object.entries(entitlementLimitTypes).find(([, value]) => value === limitType)?.[0];
}
function assignmentStrings(assignment: EntitlementPlanFeatureAssignment): readonly string[] {
return [assignment.featureKey, assignment.label, String(assignment.limit ?? ""), String(assignment.order), String(assignment.limitType)];
}
describe("standardEntitlementPlans", () => {
it("exports public standard plans in deterministic order", () => {
expect(listStandardEntitlementPlanIds()).toEqual(expectedPlanIds);
expect(standardEntitlementPlans.map((plan) => plan.metadata.sortOrder)).toEqual([10, 20, 30]);
expect(standardEntitlementPlans.map((plan) => plan.metadata.slug)).toEqual(expectedPlanIds);
expect(standardEntitlementPlans.every((plan) => plan.metadata.isPublic)).toBe(true);
});
it("uses unique plan IDs and immutable plan fixtures", () => {
const planIds = listStandardEntitlementPlanIds();
expect(new Set(planIds).size).toBe(planIds.length);
expect(Object.isFrozen(standardEntitlementPlans)).toBe(true);
for (const plan of standardEntitlementPlans) {
expect(Object.isFrozen(plan)).toBe(true);
expect(Object.isFrozen(plan.metadata)).toBe(true);
expect(Object.isFrozen(plan.features)).toBe(true);
}
});
it("covers every registered standard feature in every public plan", () => {
for (const plan of standardEntitlementPlans) {
expect(
plan.features.map((feature) => feature.featureKey),
`${plan.id} feature coverage`
).toEqual(expectedFeatureKeys);
expect(
plan.features.map((feature) => feature.featureKey),
`${plan.id} registry coverage`
).toEqual(seededFeatureRegistry.map((feature) => feature.key));
}
});
it("keeps plan feature assignments in registry order", () => {
const expectedOrders = seededFeatureRegistry.map((feature) => feature.defaultOrder);
for (const plan of standardEntitlementPlans) {
expect(
plan.features.map((feature) => feature.order),
`${plan.id} feature order`
).toEqual(expectedOrders);
}
});
it("references only known feature keys and supported limit types", () => {
for (const plan of standardEntitlementPlans) {
for (const assignment of plan.features) {
const featureGate = getFeatureGate(assignment.featureKey);
const registryLimitType = limitTypeName(assignment.limitType);
expect(featureGate, `${plan.id}.${assignment.featureKey} known feature`).toBeDefined();
expect(registryLimitType, `${plan.id}.${assignment.featureKey} limit type name`).toBeDefined();
expect(featureGate?.supportedLimitTypes, `${plan.id}.${assignment.featureKey} supported limit type`).toContain(
registryLimitType?.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`)
);
}
}
});
it("exposes lookup helpers without mutating fixture order", () => {
expect(getStandardEntitlementPlan("pro")?.metadata.publicName).toBe("Swarm");
expect(getStandardEntitlementPlan("pro")?.metadata.billingProviderProductId).toBe(getBillingProviderProductPlaceholder("pro"));
expect(getStandardEntitlementPlan("pro")?.metadata.billingProviderPriceId).toBe(getBillingProviderPricePlaceholder("pro"));
expect(getStandardEntitlementPlan("unknown-plan")).toBeUndefined();
expect(listStandardEntitlementPlanIds()).toEqual(expectedPlanIds);
});
it("keeps portal and custom-domain plan rights separate", () => {
const starter = getStandardEntitlementPlan("starter");
const pro = getStandardEntitlementPlan("pro");
const business = getStandardEntitlementPlan("business");
expect(starter?.features.find((feature) => feature.featureKey === "customer_portal")?.limitType).toBe(entitlementLimitTypes.notIncluded);
expect(pro?.features.find((feature) => feature.featureKey === "customer_portal")?.limitType).toBe(entitlementLimitTypes.notIncluded);
expect(business?.features.find((feature) => feature.featureKey === "customer_portal")?.limitType).toBe(entitlementLimitTypes.included);
for (const plan of [starter, pro, business]) {
expect(plan?.features.find((feature) => feature.featureKey === "customer_portal_custom_domain")?.limitType).toBe(entitlementLimitTypes.notIncluded);
}
});
it("uses deterministic safe Mollie placeholders and no secret-looking fixture strings", () => {
for (const plan of standardEntitlementPlans) {
expect(plan.metadata.billingProviderProductId, `${plan.id} product id placeholder`).toBe(getBillingProviderProductPlaceholder(plan.id));
expect(plan.metadata.billingProviderPriceId, `${plan.id} price id placeholder`).toBe(getBillingProviderPricePlaceholder(plan.id));
expect(plan.metadata.billingProviderProductId, `${plan.id} non-empty product placeholder`).not.toBe("");
expect(plan.metadata.billingProviderPriceId, `${plan.id} non-empty price placeholder`).not.toBe("");
expect(getStandardEntitlementPlan(plan.id)?.metadata.billingProviderProductId, `${plan.id} lookup product placeholder`).toBe(
plan.metadata.billingProviderProductId
);
expect(getStandardEntitlementPlan(plan.id)?.metadata.billingProviderPriceId, `${plan.id} lookup price placeholder`).toBe(
plan.metadata.billingProviderPriceId
);
const strings = [
plan.id,
plan.metadata.slug,
plan.metadata.publicName,
plan.metadata.description,
plan.metadata.billingProviderProductId,
plan.metadata.billingProviderPriceId,
...plan.features.flatMap(assignmentStrings),
];
for (const value of strings) {
expect(value, `${plan.id} safe fixture value`).not.toMatch(unsafeFixturePattern);
}
}
});
});