import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { resolvePriceId, priceIdToTier } from '@/lib/billing/stripe-prices'; vi.mock('@/lib/prisma', () => ({ default: { systemConfig: { findMany: vi.fn().mockResolvedValue([]), }, }, })); describe('billing-price-map', () => { const originalEnv = process.env; beforeEach(() => { process.env = { ...originalEnv, STRIPE_PRICE_PRO_MONTHLY: 'price_pro_monthly_test', STRIPE_PRICE_PRO_ANNUAL: 'price_pro_annual_test', STRIPE_PRICE_BUSINESS_MONTHLY: 'price_business_monthly_test', STRIPE_PRICE_BUSINESS_ANNUAL: 'price_business_annual_test', }; }); afterEach(() => { process.env = originalEnv; }); describe('resolvePriceId', () => { it('returns PRO monthly price ID', async () => { expect(await resolvePriceId('PRO', 'month')).toBe('price_pro_monthly_test'); }); it('returns PRO annual price ID', async () => { expect(await resolvePriceId('PRO', 'year')).toBe('price_pro_annual_test'); }); it('returns BUSINESS monthly price ID', async () => { expect(await resolvePriceId('BUSINESS', 'month')).toBe('price_business_monthly_test'); }); it('returns BUSINESS annual price ID', async () => { expect(await resolvePriceId('BUSINESS', 'year')).toBe('price_business_annual_test'); }); it('throws if env variable is not set', async () => { delete process.env.STRIPE_PRICE_PRO_MONTHLY; await expect(resolvePriceId('PRO', 'month')).rejects.toThrow(); }); }); describe('priceIdToTier', () => { it('resolves PRO monthly price ID to PRO tier', async () => { expect(await priceIdToTier('price_pro_monthly_test')).toBe('PRO'); }); it('resolves PRO annual price ID to PRO tier', async () => { expect(await priceIdToTier('price_pro_annual_test')).toBe('PRO'); }); it('resolves BUSINESS monthly price ID to BUSINESS tier', async () => { expect(await priceIdToTier('price_business_monthly_test')).toBe('BUSINESS'); }); it('resolves BUSINESS annual price ID to BUSINESS tier', async () => { expect(await priceIdToTier('price_business_annual_test')).toBe('BUSINESS'); }); it('returns null for unknown price ID', async () => { expect(await priceIdToTier('price_unknown_xyz')).toBeNull(); }); it('returns null when env variables are not set', async () => { delete process.env.STRIPE_PRICE_PRO_MONTHLY; delete process.env.STRIPE_PRICE_PRO_ANNUAL; delete process.env.STRIPE_PRICE_BUSINESS_MONTHLY; delete process.env.STRIPE_PRICE_BUSINESS_ANNUAL; expect(await priceIdToTier('price_pro_monthly_test')).toBeNull(); }); }); });