|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + events: [] as string[], |
| 8 | + getResolvedUserUsageData: vi.fn(), |
| 9 | + getCreditBalanceForEntity: vi.fn(), |
| 10 | + isOrgScopedSubscription: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@/lib/billing/core/usage', () => ({ |
| 14 | + getResolvedUserUsageData: mocks.getResolvedUserUsageData, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/lib/billing/credits/balance', () => ({ |
| 18 | + getCreditBalanceForEntity: mocks.getCreditBalanceForEntity, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/lib/billing/subscriptions/utils', () => ({ |
| 22 | + isOrgScopedSubscription: mocks.isOrgScopedSubscription, |
| 23 | +})) |
| 24 | + |
| 25 | +import { getAccountBillingSnapshot } from '@/lib/billing/core/account-billing-snapshot' |
| 26 | + |
| 27 | +const usage = { |
| 28 | + currentUsage: 18.5, |
| 29 | + limit: 40, |
| 30 | + percentUsed: 46.25, |
| 31 | + isWarning: false, |
| 32 | + isExceeded: false, |
| 33 | + billingPeriodStart: new Date('2026-08-01T00:00:00Z'), |
| 34 | + billingPeriodEnd: new Date('2026-09-01T00:00:00Z'), |
| 35 | + lastPeriodCost: 31, |
| 36 | +} |
| 37 | + |
| 38 | +describe('getAccountBillingSnapshot', () => { |
| 39 | + beforeEach(() => { |
| 40 | + vi.clearAllMocks() |
| 41 | + mocks.events.length = 0 |
| 42 | + }) |
| 43 | + |
| 44 | + it('reuses one resolved subscription for org scope, usage, limits, and credits', async () => { |
| 45 | + const subscription = { |
| 46 | + plan: 'team', |
| 47 | + referenceId: 'org-1', |
| 48 | + } |
| 49 | + mocks.getResolvedUserUsageData.mockImplementation(async () => { |
| 50 | + mocks.events.push('usage-and-subscription') |
| 51 | + return { usage, subscription, personalCreditBalance: 4 } |
| 52 | + }) |
| 53 | + mocks.isOrgScopedSubscription.mockReturnValue(true) |
| 54 | + mocks.getCreditBalanceForEntity.mockImplementation(async () => { |
| 55 | + mocks.events.push('credits') |
| 56 | + return 25 |
| 57 | + }) |
| 58 | + |
| 59 | + await expect(getAccountBillingSnapshot('user-1')).resolves.toEqual({ |
| 60 | + plan: 'team', |
| 61 | + billingScope: 'organization', |
| 62 | + organizationId: 'org-1', |
| 63 | + usage: { |
| 64 | + currentPeriodCost: 18.5, |
| 65 | + limit: 40, |
| 66 | + remaining: 21.5, |
| 67 | + percentUsed: 46.25, |
| 68 | + isExceeded: false, |
| 69 | + billingPeriodEnd: new Date('2026-09-01T00:00:00Z'), |
| 70 | + }, |
| 71 | + credits: { balance: 25, scope: 'organization' }, |
| 72 | + }) |
| 73 | + expect(mocks.getResolvedUserUsageData).toHaveBeenCalledOnce() |
| 74 | + expect(mocks.getCreditBalanceForEntity).toHaveBeenCalledWith( |
| 75 | + 'organization', |
| 76 | + 'org-1', |
| 77 | + expect.anything() |
| 78 | + ) |
| 79 | + expect(mocks.events).toEqual(['usage-and-subscription', 'credits']) |
| 80 | + }) |
| 81 | + |
| 82 | + it('preserves personal scope and clamps negative remaining usage to zero', async () => { |
| 83 | + mocks.getResolvedUserUsageData.mockResolvedValue({ |
| 84 | + usage: { ...usage, currentUsage: 45, isExceeded: true }, |
| 85 | + subscription: { plan: 'pro', referenceId: 'user-1' }, |
| 86 | + personalCreditBalance: 0, |
| 87 | + }) |
| 88 | + mocks.isOrgScopedSubscription.mockReturnValue(false) |
| 89 | + mocks.getCreditBalanceForEntity.mockResolvedValue(0) |
| 90 | + |
| 91 | + await expect(getAccountBillingSnapshot('user-1')).resolves.toMatchObject({ |
| 92 | + plan: 'pro', |
| 93 | + billingScope: 'user', |
| 94 | + organizationId: null, |
| 95 | + usage: { remaining: 0, isExceeded: true }, |
| 96 | + credits: { balance: 0, scope: 'user' }, |
| 97 | + }) |
| 98 | + expect(mocks.getCreditBalanceForEntity).not.toHaveBeenCalled() |
| 99 | + }) |
| 100 | +}) |
0 commit comments