import { describe, expect, it } from "vitest"; import { entitlementLimitTypes, evaluateEntitlement, type EntitlementDecisionReason } from "./index.js"; 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[]; describe("evaluateEntitlement", () => { it("allows included features", () => { expect( evaluateEntitlement({ featureKey: "api", hasSubscription: true, limitType: entitlementLimitTypes.included, }), ).toEqual({ allowed: true, reason: "included", featureKey: "api", limitType: 1, period: undefined, message: undefined, }); }); it("denies not-included features", () => { expect( evaluateEntitlement({ featureKey: "priority-support", hasSubscription: true, limitType: entitlementLimitTypes.notIncluded, message: "Upgrade required", }), ).toMatchObject({ allowed: false, reason: "not-included", featureKey: "priority-support", limitType: 0, message: "Upgrade required", }); }); it("allows max-limited features with remaining usage", () => { expect( evaluateEntitlement({ featureKey: "users", hasSubscription: true, limitType: entitlementLimitTypes.max, limit: 5, used: 3, }), ).toMatchObject({ allowed: true, reason: "max-remaining", featureKey: "users", limitType: 3, limit: 5, used: 3, remaining: 2, }); }); it("denies max-limited features with exhausted usage", () => { expect( evaluateEntitlement({ featureKey: "users", hasSubscription: true, limitType: entitlementLimitTypes.max, limit: 5, used: 5, }), ).toMatchObject({ allowed: false, reason: "max-exhausted", limit: 5, used: 5, remaining: 0, }); }); it("allows monthly-limited features with remaining usage and period diagnostics", () => { expect( evaluateEntitlement({ featureKey: "ai-credits", hasSubscription: true, limitType: entitlementLimitTypes.monthly, limit: 100, used: 25, period: "2026-05", }), ).toMatchObject({ allowed: true, reason: "monthly-remaining", featureKey: "ai-credits", limitType: 2, limit: 100, used: 25, remaining: 75, period: "2026-05", }); }); it("denies monthly-limited features with exhausted usage", () => { expect( evaluateEntitlement({ featureKey: "ai-credits", hasSubscription: true, limitType: entitlementLimitTypes.monthly, limit: 100, used: 125, period: "2026-05", }), ).toMatchObject({ allowed: false, reason: "monthly-exhausted", limit: 100, used: 125, remaining: 0, period: "2026-05", }); }); it("allows unlimited features", () => { expect( evaluateEntitlement({ featureKey: "private-generations", hasSubscription: true, limitType: entitlementLimitTypes.unlimited, }), ).toMatchObject({ allowed: true, reason: "unlimited", featureKey: "private-generations", limitType: 4, }); }); it("denies known features when subscription state is missing", () => { expect( evaluateEntitlement({ featureKey: "api", limitType: entitlementLimitTypes.included, }), ).toMatchObject({ allowed: false, reason: "missing-subscription", featureKey: "api", limitType: 1, }); }); it("allows known features during an active trial without a subscription", () => { expect( evaluateEntitlement({ featureKey: "api", trialActive: true, trialPeriod: "trial-day-7", trialMessage: "Trial access", limitType: entitlementLimitTypes.included, }), ).toEqual({ allowed: true, reason: "trial-active", featureKey: "api", limitType: 1, period: undefined, trialPeriod: "trial-day-7", message: "Trial access", }); }); it("denies known features when the trial is expired and no subscription exists", () => { expect( evaluateEntitlement({ featureKey: "api", trialActive: false, trialPeriod: "trial-ended", limitType: entitlementLimitTypes.included, }), ).toMatchObject({ allowed: false, reason: "trial-expired", featureKey: "api", limitType: 1, trialPeriod: "trial-ended", }); }); it("denies unknown features before evaluating trial state", () => { expect( evaluateEntitlement({ featureKey: "unknown-feature", trialActive: true, limitType: entitlementLimitTypes.unlimited, }), ).toMatchObject({ allowed: false, reason: "unknown-feature", featureKey: "unknown-feature", limitType: 4, }); }); it("denies unknown features before evaluating subscription state", () => { expect( evaluateEntitlement({ featureKey: "unknown-feature", hasSubscription: true, limitType: entitlementLimitTypes.unlimited, }), ).toMatchObject({ allowed: false, reason: "unknown-feature", featureKey: "unknown-feature", limitType: 4, }); }); it("keeps denied diagnostics free of obvious identifiers and secrets", () => { const decisions = [ evaluateEntitlement({ featureKey: "api", limitType: entitlementLimitTypes.included }), evaluateEntitlement({ featureKey: "api", trialActive: false, trialPeriod: "trial-ended" }), evaluateEntitlement({ featureKey: "ai-credits", hasSubscription: true, limitType: entitlementLimitTypes.monthly, limit: 1, used: 1 }), evaluateEntitlement({ featureKey: "unknown-feature", hasSubscription: true, trialActive: true }), ]; const unsafeDiagnosticPattern = /tenant|customer|subscription id|user id|api key|secret|sk_[a-z0-9]/i; for (const decision of decisions) { expect(JSON.stringify(decision)).not.toMatch(unsafeDiagnosticPattern); } }); it("keeps reason codes stable for package-boundary consumers", () => { expect(stableReasonCodes).toEqual([ "included", "not-included", "max-remaining", "max-exhausted", "monthly-remaining", "monthly-exhausted", "unlimited", "trial-active", "trial-expired", "missing-subscription", "unknown-feature", ]); }); });