256 lines
7.6 KiB
TypeScript
256 lines
7.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
|
|
import { featureKeys, productBlueprintIds } from "@toolctrl/entitlements";
|
|
import type { EntitlementDecisionReason } from "@toolctrl/entitlements";
|
|
import { DefaultFeatures } from "~/application/dtos/shared/DefaultFeatures";
|
|
import type { SubscriptionFeatureDto } from "~/application/dtos/subscriptions/SubscriptionFeatureDto";
|
|
import { SubscriptionFeatureLimitType } from "~/application/enums/subscriptions/SubscriptionFeatureLimitType";
|
|
import { evaluateProductSubscriptionAccess, evaluateSubscriptionEntitlement } from "./subscriptionEntitlements.server";
|
|
|
|
const stableReasonCodes = [
|
|
"included",
|
|
"not-included",
|
|
"max-remaining",
|
|
"max-exhausted",
|
|
"monthly-remaining",
|
|
"monthly-exhausted",
|
|
"unlimited",
|
|
"trial-active",
|
|
"trial-expired",
|
|
"missing-subscription",
|
|
"unknown-feature",
|
|
] as const satisfies readonly EntitlementDecisionReason[];
|
|
|
|
const apiFeature = {
|
|
order: 0,
|
|
title: "API access",
|
|
name: DefaultFeatures.API,
|
|
type: SubscriptionFeatureLimitType.INCLUDED,
|
|
value: 0,
|
|
accumulate: false,
|
|
};
|
|
|
|
const prioritySupportFeature = {
|
|
order: 1,
|
|
title: "Priority support",
|
|
name: DefaultFeatures.PrioritySupport,
|
|
type: SubscriptionFeatureLimitType.NOT_INCLUDED,
|
|
value: 0,
|
|
accumulate: false,
|
|
};
|
|
|
|
function subscriptionWithFeatures(...features: SubscriptionFeatureDto[]) {
|
|
return {
|
|
products: [{ features }],
|
|
};
|
|
}
|
|
|
|
describe("evaluateSubscriptionEntitlement", () => {
|
|
it("uses semantic translation keys for trial entitlement copy", () => {
|
|
const source = fs.readFileSync(path.resolve(process.cwd(), "apps/web/app/utils/entitlements/.server/subscriptionEntitlements.server.ts"), "utf8");
|
|
|
|
expect(source).toContain("entitlements.trial.access");
|
|
expect(source).not.toContain("ui.autogenerated");
|
|
});
|
|
|
|
it("allows active trial access without subscription products", () => {
|
|
expect(
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: DefaultFeatures.API,
|
|
subscription: { products: [] },
|
|
trial: {
|
|
active: true,
|
|
period: "trial-day-7",
|
|
message: "Trial access",
|
|
},
|
|
})
|
|
).toEqual({
|
|
allowed: true,
|
|
reason: "trial-active",
|
|
featureKey: DefaultFeatures.API,
|
|
limitType: SubscriptionFeatureLimitType.INCLUDED,
|
|
period: undefined,
|
|
trialPeriod: "trial-day-7",
|
|
message: "Trial access",
|
|
});
|
|
});
|
|
|
|
it("denies expired trial access without a subscription", () => {
|
|
expect(
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: DefaultFeatures.API,
|
|
subscription: null,
|
|
trial: {
|
|
active: false,
|
|
period: "trial-ended",
|
|
},
|
|
})
|
|
).toMatchObject({
|
|
allowed: false,
|
|
reason: "trial-expired",
|
|
featureKey: DefaultFeatures.API,
|
|
limitType: undefined,
|
|
trialPeriod: "trial-ended",
|
|
});
|
|
});
|
|
|
|
it("denies access when neither subscription nor trial exists", () => {
|
|
expect(
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: DefaultFeatures.API,
|
|
subscription: { products: [] },
|
|
trial: null,
|
|
})
|
|
).toMatchObject({
|
|
allowed: false,
|
|
reason: "missing-subscription",
|
|
featureKey: DefaultFeatures.API,
|
|
limitType: undefined,
|
|
});
|
|
});
|
|
|
|
it("allows subscription product feature inclusion", () => {
|
|
expect(
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: DefaultFeatures.API,
|
|
subscription: subscriptionWithFeatures(apiFeature),
|
|
})
|
|
).toEqual({
|
|
allowed: true,
|
|
reason: "included",
|
|
featureKey: DefaultFeatures.API,
|
|
limitType: SubscriptionFeatureLimitType.INCLUDED,
|
|
period: undefined,
|
|
message: undefined,
|
|
});
|
|
});
|
|
|
|
it("denies product features that are explicitly not included", () => {
|
|
expect(
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: DefaultFeatures.PrioritySupport,
|
|
subscription: subscriptionWithFeatures(prioritySupportFeature),
|
|
message: "Upgrade required",
|
|
})
|
|
).toMatchObject({
|
|
allowed: false,
|
|
reason: "not-included",
|
|
featureKey: DefaultFeatures.PrioritySupport,
|
|
limitType: SubscriptionFeatureLimitType.NOT_INCLUDED,
|
|
message: "Upgrade required",
|
|
});
|
|
});
|
|
|
|
it("denies unknown features even with an active subscription and trial", () => {
|
|
expect(
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: "unknown-feature",
|
|
subscription: subscriptionWithFeatures(apiFeature),
|
|
trial: { active: true },
|
|
})
|
|
).toMatchObject({
|
|
allowed: false,
|
|
reason: "unknown-feature",
|
|
featureKey: "unknown-feature",
|
|
limitType: SubscriptionFeatureLimitType.INCLUDED,
|
|
});
|
|
});
|
|
|
|
it("keeps denied diagnostic serialization redacted", () => {
|
|
const decisions = [
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: DefaultFeatures.API,
|
|
subscription: { products: [] },
|
|
trial: null,
|
|
}),
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: DefaultFeatures.API,
|
|
subscription: null,
|
|
trial: { active: false, period: "trial-ended" },
|
|
}),
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: DefaultFeatures.PrioritySupport,
|
|
subscription: subscriptionWithFeatures(prioritySupportFeature),
|
|
}),
|
|
evaluateSubscriptionEntitlement({
|
|
featureKey: "unknown-feature",
|
|
subscription: subscriptionWithFeatures(apiFeature),
|
|
}),
|
|
];
|
|
const unsafeDiagnosticPattern = /tenant|customer|subscription id|user id|api key|secret|sk_[a-z0-9]/i;
|
|
|
|
for (const decision of decisions) {
|
|
expect(decision.allowed).toBe(false);
|
|
expect(JSON.stringify(decision)).not.toMatch(unsafeDiagnosticPattern);
|
|
}
|
|
});
|
|
|
|
it("keeps reason codes stable for app subscription consumers", () => {
|
|
expect(stableReasonCodes).toEqual([
|
|
"included",
|
|
"not-included",
|
|
"max-remaining",
|
|
"max-exhausted",
|
|
"monthly-remaining",
|
|
"monthly-exhausted",
|
|
"unlimited",
|
|
"trial-active",
|
|
"trial-expired",
|
|
"missing-subscription",
|
|
"unknown-feature",
|
|
]);
|
|
});
|
|
|
|
it("evaluates Product Core access through custom plan id, tenant override, and tenant role permissions", () => {
|
|
expect(
|
|
evaluateProductSubscriptionAccess({
|
|
blueprintId: productBlueprintIds.crmSaas,
|
|
featureKey: featureKeys.workflowEngine,
|
|
subscription: {
|
|
products: [
|
|
{
|
|
customEntitlementPlanId: "business",
|
|
features: [],
|
|
},
|
|
],
|
|
},
|
|
tenantRolePermissionNames: ["app.settings.roles.set"],
|
|
})
|
|
).toMatchObject({ allowed: true, reason: "monthly-remaining", limit: 10000 });
|
|
|
|
expect(
|
|
evaluateProductSubscriptionAccess({
|
|
blueprintId: productBlueprintIds.crmSaas,
|
|
featureKey: featureKeys.workflowEngine,
|
|
subscription: {
|
|
products: [
|
|
{
|
|
customEntitlementPlanId: "business",
|
|
features: [],
|
|
},
|
|
],
|
|
},
|
|
tenantOverride: "disabled",
|
|
tenantRolePermissionNames: ["app.settings.roles.set"],
|
|
})
|
|
).toMatchObject({ allowed: false, reason: "tenant-override-disabled" });
|
|
|
|
expect(
|
|
evaluateProductSubscriptionAccess({
|
|
blueprintId: productBlueprintIds.crmSaas,
|
|
featureKey: featureKeys.workflowEngine,
|
|
subscription: {
|
|
products: [
|
|
{
|
|
customEntitlementPlanId: "business",
|
|
features: [],
|
|
},
|
|
],
|
|
},
|
|
})
|
|
).toMatchObject({ allowed: false, reason: "tenant-role-missing", requiredTenantRolePermission: "app.settings.roles.set" });
|
|
});
|
|
});
|