Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/billing/SelfServiceSubscriptionPage.selection.test.ts
T

265 lines
8.6 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from "vitest";
import { buildSelfServiceSubscriptionPageVM } from "@hivegrid/view-models";
import {
applyRecommendedSubscriptionPlan,
initialActivationSubscriptionSelection,
initialSelfServiceSubscriptionSelection,
type SelfServiceSubscriptionPlanAdvice,
} from "./SelfServiceSubscriptionPage";
import { clearPersistedCheckoutRequest, stableCheckoutRequestKey, type SelfServiceSubscriptionSelection } from "./checkout-request";
const plans = [
{
id: "product_starter",
code: "starter",
titleKey: "Core",
descriptionKey: "Core plan",
badge: null,
quantityMode: "flat" as const,
features: [],
prices: [{ id: "price_starter", billingPeriod: 3, currency: "EUR", unitAmountCents: 19_900, trialDays: 0 }],
},
{
id: "product_pro",
code: "pro",
titleKey: "Swarm",
descriptionKey: "Swarm plan",
badge: null,
quantityMode: "flat" as const,
features: [],
prices: [{ id: "price_pro", billingPeriod: 3, currency: "EUR", unitAmountCents: 49_900, trialDays: 0 }],
},
] as const;
const effectiveAtByChangeKind = {
new: "immediate",
upgrade: "immediate",
downgrade: "period_end",
billing_period_change: "period_end",
seat_increase: "immediate",
seat_decrease: "period_end",
configuration_increase: "immediate",
configuration_decrease: "period_end",
configuration_change: "period_end",
unchanged: "immediate",
} as const;
describe("registration billing preference", () => {
it("preselects the persisted registration price for a tenant without a subscription", () => {
const vm = buildSelfServiceSubscriptionPageVM({
overview: { status: "empty" },
catalog: { plans },
managementPolicy: { planOrder: ["starter", "pro"], effectiveAtByChangeKind },
minimumSeatQuantity: 1,
minimumSeatQuantities: { office: 1, field: 0 },
canManage: true,
billingHref: "/acme/settings/billing",
preferredSelection: {
planId: "product_pro",
priceId: "price_pro",
officeSeatQuantity: 5,
fieldSeatQuantity: 15,
},
});
expect(initialSelfServiceSubscriptionSelection(vm)).toMatchObject({
productId: "product_pro",
priceId: "price_pro",
officeSeatQuantity: 5,
fieldSeatQuantity: 15,
});
});
it("keeps an active subscription selection ahead of a stale registration preference", () => {
const vm = buildSelfServiceSubscriptionPageVM({
overview: { status: "empty" },
catalog: { plans },
managementPolicy: { planOrder: ["starter", "pro"], effectiveAtByChangeKind },
minimumSeatQuantity: 1,
canManage: true,
billingHref: "/acme/settings/billing",
currentSelection: {
planId: "product_starter",
planCode: "starter",
priceId: "price_starter",
seatQuantity: 1,
},
preferredSelection: { planId: "product_pro", priceId: "price_pro" },
});
expect(initialSelfServiceSubscriptionSelection(vm)).toMatchObject({
productId: "product_starter",
priceId: "price_starter",
});
});
});
const coreSelection: SelfServiceSubscriptionSelection = {
productId: "product_starter",
priceId: "price_starter",
billingPeriod: 3,
currency: "EUR",
officeSeatQuantity: 5,
fieldSeatQuantity: 15,
storageAddOnCodes: ["storage_100"],
};
const swarmAdvice: SelfServiceSubscriptionPlanAdvice = {
plans: [
{
planId: "product_starter",
priceId: "price_starter",
amountCents: 62_600,
currency: "EUR",
billingPeriod: 3,
recommended: false,
},
{
planId: "product_pro",
priceId: "price_pro",
amountCents: 49_900,
currency: "EUR",
billingPeriod: 3,
recommended: true,
},
],
selectedPriceId: "price_pro",
recommendedPriceId: "price_pro",
officeSeatQuantity: 5,
fieldSeatQuantity: 15,
};
describe("activation plan advice", () => {
it("starts with the server recommendation when no plan was selected explicitly", () => {
const vm = buildSelfServiceSubscriptionPageVM({
overview: { status: "empty" },
catalog: { plans },
managementPolicy: { planOrder: ["starter", "pro"], effectiveAtByChangeKind },
minimumSeatQuantity: 20,
minimumSeatQuantities: { office: 5, field: 15 },
canManage: true,
billingHref: "/acme/settings/billing",
});
expect(initialActivationSubscriptionSelection(vm, swarmAdvice)).toMatchObject({
productId: "product_pro",
priceId: "price_pro",
});
});
it("does not replace an explicitly persisted plan during activation initialization", () => {
const vm = buildSelfServiceSubscriptionPageVM({
overview: { status: "empty" },
catalog: { plans },
managementPolicy: { planOrder: ["starter", "pro"], effectiveAtByChangeKind },
minimumSeatQuantity: 1,
minimumSeatQuantities: { office: 1, field: 0 },
canManage: true,
billingHref: "/acme/settings/billing",
preferredSelection: {
planId: "product_starter",
priceId: "price_starter",
officeSeatQuantity: 5,
fieldSeatQuantity: 15,
},
});
expect(initialActivationSubscriptionSelection(vm, swarmAdvice)).toMatchObject({
productId: "product_starter",
priceId: "price_starter",
});
});
it("applies the server-recommended plan while retaining seats and add-ons", () => {
expect(applyRecommendedSubscriptionPlan(coreSelection, swarmAdvice, false)).toEqual({
...coreSelection,
productId: "product_pro",
priceId: "price_pro",
});
});
it("keeps a deliberately selected different plan when fresh advice arrives", () => {
expect(applyRecommendedSubscriptionPlan(coreSelection, swarmAdvice, true)).toBe(coreSelection);
});
});
describe("checkout request recovery", () => {
function memoryStorage() {
const values = new Map<string, string>();
return {
getItem: (key: string) => values.get(key) ?? null,
setItem: (key: string, value: string) => void values.set(key, value),
removeItem: (key: string) => void values.delete(key),
};
}
it("reuses the same request after a reload for the same tenant selection", () => {
const storage = memoryStorage();
const options = { storage, namespace: "/acme/settings/billing", now: 1_000, createKey: () => "00000000-0000-4000-8000-000000000001" };
const first = stableCheckoutRequestKey(null, coreSelection, "creditcard", options);
const afterReload = stableCheckoutRequestKey(null, coreSelection, "creditcard", {
...options,
now: 2_000,
createKey: () => "00000000-0000-4000-8000-000000000002",
});
expect(afterReload).toEqual(first);
});
it("creates a fresh request for a changed selection, tenant, or expired attempt", () => {
const storage = memoryStorage();
const first = stableCheckoutRequestKey(null, coreSelection, "creditcard", {
storage,
namespace: "/acme/settings/billing",
now: 1_000,
createKey: () => "00000000-0000-4000-8000-000000000001",
});
const changed = stableCheckoutRequestKey(first, { ...coreSelection, fieldSeatQuantity: 16 }, "creditcard", {
storage,
namespace: "/acme/settings/billing",
now: 2_000,
createKey: () => "00000000-0000-4000-8000-000000000002",
});
const otherTenant = stableCheckoutRequestKey(null, coreSelection, "creditcard", {
storage,
namespace: "/other/settings/billing",
now: 2_000,
createKey: () => "00000000-0000-4000-8000-000000000003",
});
const expired = stableCheckoutRequestKey(null, coreSelection, "creditcard", {
storage,
namespace: "/acme/settings/billing",
now: 30 * 60 * 1000 + 3_000,
createKey: () => "00000000-0000-4000-8000-000000000004",
});
expect(changed.key).toBe("00000000-0000-4000-8000-000000000002");
expect(otherTenant.key).toBe("00000000-0000-4000-8000-000000000003");
expect(expired.key).toBe("00000000-0000-4000-8000-000000000004");
});
it("clears a terminal checkout request so the identical selection can retry immediately", () => {
const storage = memoryStorage();
const namespace = "/acme/settings/billing";
const first = stableCheckoutRequestKey(null, coreSelection, "creditcard", {
storage,
namespace,
now: 1_000,
createKey: () => "00000000-0000-4000-8000-000000000001",
});
clearPersistedCheckoutRequest(namespace, storage);
const retry = stableCheckoutRequestKey(null, coreSelection, "creditcard", {
storage,
namespace,
now: 2_000,
createKey: () => "00000000-0000-4000-8000-000000000002",
});
expect(first.key).not.toBe(retry.key);
expect(retry.key).toBe("00000000-0000-4000-8000-000000000002");
});
});