import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ loadCatalog: vi.fn(), })); vi.mock("./self-service-subscription.server", () => ({ loadSelfServiceSubscriptionCatalog: (...args: unknown[]) => mocks.loadCatalog(...args), })); import { loadRegistrationPlanSelection, resolveRegistrationSubscriptionPrice } from "./registration-plan-selection.server"; const plans = [ { id: "product_starter", code: "starter", titleKey: "Core", descriptionKey: "Core plan", prices: [ { id: "price_starter_annual", billingPeriod: 4, currency: "EUR", unitAmountCents: 17_900, trialDays: 0 }, { id: "price_starter", billingPeriod: 3, currency: "EUR", unitAmountCents: 19_900, trialDays: 0 }, ], }, { id: "product_pro", code: "pro", titleKey: "Swarm", descriptionKey: "Swarm plan", prices: [{ id: "price_pro", billingPeriod: 3, currency: "EUR", unitAmountCents: 49_900, trialDays: 0 }], }, ]; describe("registration plan selection", () => { beforeEach(() => { vi.clearAllMocks(); mocks.loadCatalog.mockResolvedValue({ plans }); }); it("offers one canonical monthly registration choice per base plan", async () => { await expect(loadRegistrationPlanSelection("pro")).resolves.toMatchObject({ selectedPriceId: "price_pro", recommendedPriceId: "price_starter", officeSeatQuantity: 1, fieldSeatQuantity: 0, plans: [ expect.objectContaining({ planCode: "starter", priceId: "price_starter", amountCents: 19_900, recommended: true }), expect.objectContaining({ planCode: "pro", priceId: "price_pro", amountCents: 49_900, recommended: false }), ], }); }); it("recommends and prices the cheapest complete configuration for the submitted team", async () => { await expect(loadRegistrationPlanSelection(null, { officeSeatQuantity: 5, fieldSeatQuantity: 15 })).resolves.toMatchObject({ selectedPriceId: "price_pro", recommendedPriceId: "price_pro", officeSeatQuantity: 5, fieldSeatQuantity: 15, plans: [ expect.objectContaining({ planCode: "starter", amountCents: 62_600, extraOfficeSeatQuantity: 3, extraFieldSeatQuantity: 10 }), expect.objectContaining({ planCode: "pro", amountCents: 49_900, recommended: true }), ], }); }); it("accepts only an exact server-catalog price id", async () => { await expect(resolveRegistrationSubscriptionPrice("price_pro")).resolves.toMatchObject({ planCode: "pro", priceId: "price_pro", }); await expect(resolveRegistrationSubscriptionPrice("forged_price")).resolves.toBeNull(); await expect(resolveRegistrationSubscriptionPrice(null)).resolves.toBeNull(); }); it("fails closed when the canonical catalog is unavailable", async () => { mocks.loadCatalog.mockResolvedValue({ plans: [] }); await expect(loadRegistrationPlanSelection()).rejects.toThrow("No canonical registration plan is available"); }); });