124 lines
3.3 KiB
TypeScript
124 lines
3.3 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { entitlementLimitTypes, evaluateEntitlement } from "./index.js";
|
|
|
|
describe("evaluateEntitlement boundary behaviour", () => {
|
|
it("allows an active trial even when the subscription flag is explicitly false", () => {
|
|
const decision = evaluateEntitlement({
|
|
featureKey: "api",
|
|
hasSubscription: false,
|
|
trialActive: true,
|
|
limitType: entitlementLimitTypes.included,
|
|
});
|
|
|
|
expect(decision.allowed).toBe(true);
|
|
expect(decision.reason).toBe("trial-active");
|
|
});
|
|
|
|
it("lets an active trial bypass an otherwise exhausted usage quota", () => {
|
|
const decision = evaluateEntitlement({
|
|
featureKey: "users",
|
|
trialActive: true,
|
|
limitType: entitlementLimitTypes.max,
|
|
limit: 1,
|
|
used: 99,
|
|
});
|
|
|
|
// The trial branch short-circuits before any limit math, so no usage fields leak through.
|
|
expect(decision.allowed).toBe(true);
|
|
expect(decision.reason).toBe("trial-active");
|
|
expect(decision.remaining).toBeUndefined();
|
|
expect(decision.limit).toBeUndefined();
|
|
expect(decision.used).toBeUndefined();
|
|
});
|
|
|
|
it("treats a missing usage counter as zero on the allowed path for max limits", () => {
|
|
const decision = evaluateEntitlement({
|
|
featureKey: "users",
|
|
hasSubscription: true,
|
|
limitType: entitlementLimitTypes.max,
|
|
limit: 5,
|
|
});
|
|
|
|
expect(decision).toMatchObject({
|
|
allowed: true,
|
|
reason: "max-remaining",
|
|
limit: 5,
|
|
used: 0,
|
|
remaining: 5,
|
|
});
|
|
});
|
|
|
|
it("treats a missing usage counter as zero on the allowed path for monthly limits", () => {
|
|
const decision = evaluateEntitlement({
|
|
featureKey: "ai-credits",
|
|
hasSubscription: true,
|
|
limitType: entitlementLimitTypes.monthly,
|
|
limit: 250,
|
|
period: "2026-06",
|
|
});
|
|
|
|
expect(decision).toMatchObject({
|
|
allowed: true,
|
|
reason: "monthly-remaining",
|
|
limit: 250,
|
|
used: 0,
|
|
remaining: 250,
|
|
period: "2026-06",
|
|
});
|
|
});
|
|
|
|
it("normalizes an explicit null usage counter to zero", () => {
|
|
const decision = evaluateEntitlement({
|
|
featureKey: "users",
|
|
hasSubscription: true,
|
|
limitType: entitlementLimitTypes.max,
|
|
limit: 4,
|
|
used: null,
|
|
});
|
|
|
|
expect(decision).toMatchObject({
|
|
allowed: true,
|
|
reason: "max-remaining",
|
|
limit: 4,
|
|
used: 0,
|
|
remaining: 4,
|
|
});
|
|
});
|
|
|
|
it("denies a zero-limit usage quota as exhausted at the boundary", () => {
|
|
const decision = evaluateEntitlement({
|
|
featureKey: "ai-credits",
|
|
hasSubscription: true,
|
|
limitType: entitlementLimitTypes.monthly,
|
|
limit: 0,
|
|
used: 0,
|
|
period: "2026-06",
|
|
});
|
|
|
|
expect(decision).toMatchObject({
|
|
allowed: false,
|
|
reason: "monthly-exhausted",
|
|
limit: 0,
|
|
used: 0,
|
|
remaining: 0,
|
|
});
|
|
});
|
|
|
|
it("reports a missing subscription when the flag is explicitly false and no trial is set", () => {
|
|
const decision = evaluateEntitlement({
|
|
featureKey: "api",
|
|
hasSubscription: false,
|
|
limitType: entitlementLimitTypes.included,
|
|
});
|
|
|
|
expect(decision).toMatchObject({
|
|
allowed: false,
|
|
reason: "missing-subscription",
|
|
featureKey: "api",
|
|
});
|
|
// An undefined trial flag must not be mistaken for an expired trial.
|
|
expect(decision.reason).not.toBe("trial-expired");
|
|
});
|
|
});
|