import { beforeEach, describe, expect, it, vi } from "vitest"; import { CatalogError } from "@hivegrid/foundation"; const mocks = vi.hoisted(() => { const tenantProductConfiguration = { findUnique: vi.fn(), upsert: vi.fn(), }; return { tenantProductConfiguration, }; }); vi.mock("~/utils/db.server", () => ({ db: { tenantProductConfiguration: mocks.tenantProductConfiguration, }, })); import { loadTenantRegistrationBillingPreference, persistTenantRegistrationBillingPreferenceWithTx, tenantRegistrationBillingPreferenceMatchesWithTx, } from "./registration-billing-preference.server"; const preference = { tenantId: "tenant-1", selectedSubscriptionPriceId: "price_swarm_monthly", selectedOfficeSeatQuantity: 5, selectedFieldSeatQuantity: 15, }; describe("tenant registration billing preference", () => { beforeEach(() => { vi.clearAllMocks(); }); it("loads only a complete bounded preference", async () => { mocks.tenantProductConfiguration.findUnique.mockResolvedValueOnce({ signupSubscriptionPriceId: "price_swarm_monthly", signupOfficeSeatQuantity: 5, signupFieldSeatQuantity: 15, }); await expect(loadTenantRegistrationBillingPreference("tenant-1")).resolves.toEqual({ selectedSubscriptionPriceId: "price_swarm_monthly", selectedOfficeSeatQuantity: 5, selectedFieldSeatQuantity: 15, }); expect(mocks.tenantProductConfiguration.findUnique).toHaveBeenCalledWith({ where: { tenantId: "tenant-1" }, select: { signupSubscriptionPriceId: true, signupOfficeSeatQuantity: true, signupFieldSeatQuantity: true, }, }); }); it.each([ null, { signupSubscriptionPriceId: null, signupOfficeSeatQuantity: null, signupFieldSeatQuantity: null }, { signupSubscriptionPriceId: "price", signupOfficeSeatQuantity: 0, signupFieldSeatQuantity: 1 }, { signupSubscriptionPriceId: "price", signupOfficeSeatQuantity: 1, signupFieldSeatQuantity: null }, { signupSubscriptionPriceId: "p".repeat(192), signupOfficeSeatQuantity: 1, signupFieldSeatQuantity: 0 }, ])("ignores an absent, partial, or invalid stored preference", async (stored) => { mocks.tenantProductConfiguration.findUnique.mockResolvedValueOnce(stored); await expect(loadTenantRegistrationBillingPreference("tenant-1")).resolves.toBeNull(); }); it("upserts the validated preference through the caller's transaction", async () => { const tx = { tenantProductConfiguration: mocks.tenantProductConfiguration }; await persistTenantRegistrationBillingPreferenceWithTx(tx as never, preference); expect(mocks.tenantProductConfiguration.upsert).toHaveBeenCalledTimes(1); expect(mocks.tenantProductConfiguration.upsert).toHaveBeenCalledWith({ where: { tenantId: "tenant-1" }, create: { tenantId: "tenant-1", signupSubscriptionPriceId: "price_swarm_monthly", signupOfficeSeatQuantity: 5, signupFieldSeatQuantity: 15, }, update: { signupSubscriptionPriceId: "price_swarm_monthly", signupOfficeSeatQuantity: 5, signupFieldSeatQuantity: 15, }, }); }); it("matches only the exact complete preference during a signup replay", async () => { const tx = { tenantProductConfiguration: mocks.tenantProductConfiguration }; mocks.tenantProductConfiguration.findUnique.mockResolvedValue({ signupSubscriptionPriceId: "price_swarm_monthly", signupOfficeSeatQuantity: 5, signupFieldSeatQuantity: 15, }); await expect(tenantRegistrationBillingPreferenceMatchesWithTx(tx as never, preference)).resolves.toBe(true); await expect( tenantRegistrationBillingPreferenceMatchesWithTx(tx as never, { ...preference, selectedFieldSeatQuantity: 16 }) ).resolves.toBe(false); }); it("fails closed before writing malformed values", async () => { const tx = { tenantProductConfiguration: mocks.tenantProductConfiguration }; await expect( persistTenantRegistrationBillingPreferenceWithTx(tx as never, { ...preference, selectedOfficeSeatQuantity: 0 }) ).rejects.toBeInstanceOf(CatalogError); expect(mocks.tenantProductConfiguration.upsert).not.toHaveBeenCalled(); }); });