92 lines
3.5 KiB
TypeScript
92 lines
3.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { hasTenantSubscriptionAccess, requiresTenantSubscriptionActivation, type TenantSubscriptionAccessState } from "./SubscriptionAccess";
|
|
|
|
const NOW = new Date("2026-08-03T12:00:00.000Z");
|
|
|
|
function baseProduct(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
cancelledAt: null,
|
|
currentPeriodStart: new Date("2026-08-01T00:00:00.000Z"),
|
|
endsAt: null,
|
|
subscriptionProduct: { kind: "base_plan" },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function subscription(overrides: Record<string, unknown> = {}): TenantSubscriptionAccessState {
|
|
return {
|
|
status: "active",
|
|
trialStartsAt: null,
|
|
trialEndsAt: null,
|
|
pastDueSince: null,
|
|
frozenAt: null,
|
|
cancelledAt: null,
|
|
products: [baseProduct()],
|
|
...overrides,
|
|
} as TenantSubscriptionAccessState;
|
|
}
|
|
|
|
describe("tenant subscription access", () => {
|
|
it("allows an active base-plan subscription in its effective period", () => {
|
|
expect(hasTenantSubscriptionAccess(subscription(), NOW)).toBe(true);
|
|
});
|
|
|
|
it("allows a bounded trial only while its real Prisma window is active", () => {
|
|
expect(
|
|
hasTenantSubscriptionAccess(
|
|
subscription({
|
|
status: "trial",
|
|
products: [],
|
|
trialStartsAt: new Date("2026-08-01T00:00:00.000Z"),
|
|
trialEndsAt: new Date("2026-08-10T00:00:00.000Z"),
|
|
}),
|
|
NOW
|
|
)
|
|
).toBe(true);
|
|
expect(hasTenantSubscriptionAccess(subscription({ status: "trial", products: [], trialEndsAt: NOW }), NOW)).toBe(false);
|
|
expect(hasTenantSubscriptionAccess(subscription({ status: "trial", products: [], trialEndsAt: null }), NOW)).toBe(false);
|
|
expect(
|
|
hasTenantSubscriptionAccess(
|
|
subscription({
|
|
status: "trial",
|
|
products: [],
|
|
trialStartsAt: new Date("2026-08-04T00:00:00.000Z"),
|
|
trialEndsAt: new Date("2026-08-10T00:00:00.000Z"),
|
|
}),
|
|
NOW
|
|
)
|
|
).toBe(false);
|
|
});
|
|
|
|
it.each(["past_due", "cancelled", "canceled", "frozen", "terminated"])("denies a %s subscription even with a base plan", (status) => {
|
|
expect(hasTenantSubscriptionAccess(subscription({ status }), NOW)).toBe(false);
|
|
});
|
|
|
|
it.each([
|
|
["past-due marker", { pastDueSince: NOW }],
|
|
["frozen marker", { frozenAt: NOW }],
|
|
["cancelled marker", { cancelledAt: NOW }],
|
|
])("denies an inconsistent active subscription carrying a %s", (_label, marker) => {
|
|
expect(hasTenantSubscriptionAccess(subscription(marker), NOW)).toBe(false);
|
|
});
|
|
|
|
it("denies add-on-only, cancelled, ended, and not-yet-effective products", () => {
|
|
expect(
|
|
hasTenantSubscriptionAccess(subscription({ products: [baseProduct({ subscriptionProduct: { kind: "recurring_add_on" } })] }), NOW)
|
|
).toBe(false);
|
|
expect(hasTenantSubscriptionAccess(subscription({ products: [baseProduct({ cancelledAt: NOW })] }), NOW)).toBe(false);
|
|
expect(hasTenantSubscriptionAccess(subscription({ products: [baseProduct({ endsAt: NOW })] }), NOW)).toBe(false);
|
|
expect(
|
|
hasTenantSubscriptionAccess(subscription({ products: [baseProduct({ currentPeriodStart: new Date("2026-08-04T00:00:00.000Z") })] }), NOW)
|
|
).toBe(false);
|
|
});
|
|
|
|
it("fails closed for malformed dates and drives the activation gate", () => {
|
|
const malformed = subscription({ products: [baseProduct({ endsAt: "not-a-date" })] });
|
|
expect(hasTenantSubscriptionAccess(malformed, NOW)).toBe(false);
|
|
expect(requiresTenantSubscriptionActivation(true, malformed)).toBe(true);
|
|
expect(requiresTenantSubscriptionActivation(false, malformed)).toBe(false);
|
|
});
|
|
});
|