|
| 1 | +/** |
| 2 | + * Tests for getUserUsageLimit. |
| 3 | + * |
| 4 | + * Org-scoped members carry a null `currentUsageLimit` by design, so a user |
| 5 | + * whose subscription stops being org-scoped without a resync is left null. |
| 6 | + * The limit read must self-heal that state to the plan default instead of |
| 7 | + * failing closed and blocking every execution. |
| 8 | + * |
| 9 | + * @vitest-environment node |
| 10 | + */ |
| 11 | +import { dbChainMock, dbChainMockFns } from '@sim/testing' |
| 12 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | + |
| 14 | +vi.mock('@sim/db', () => dbChainMock) |
| 15 | + |
| 16 | +const { |
| 17 | + mockGetFreeTierLimit, |
| 18 | + mockGetPerUserMinimumLimit, |
| 19 | + mockHasPaidSubscriptionStatus, |
| 20 | + mockIsOrgScopedSubscription, |
| 21 | +} = vi.hoisted(() => ({ |
| 22 | + mockGetFreeTierLimit: vi.fn(), |
| 23 | + mockGetPerUserMinimumLimit: vi.fn(), |
| 24 | + mockHasPaidSubscriptionStatus: vi.fn(), |
| 25 | + mockIsOrgScopedSubscription: vi.fn(), |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/lib/billing/subscriptions/utils', () => ({ |
| 29 | + canEditUsageLimit: vi.fn(), |
| 30 | + getFreeTierLimit: mockGetFreeTierLimit, |
| 31 | + getPerUserMinimumLimit: mockGetPerUserMinimumLimit, |
| 32 | + getPlanPricing: vi.fn(() => ({ basePrice: 20 })), |
| 33 | + hasPaidSubscriptionStatus: mockHasPaidSubscriptionStatus, |
| 34 | + hasUsableSubscriptionAccess: vi.fn(), |
| 35 | + isOrgScopedSubscription: mockIsOrgScopedSubscription, |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock('@/lib/billing/core/plan', () => ({ |
| 39 | + getHighestPrioritySubscription: vi.fn(), |
| 40 | +})) |
| 41 | + |
| 42 | +vi.mock('@/lib/billing/core/access', () => ({ |
| 43 | + getEffectiveBillingStatus: vi.fn(), |
| 44 | +})) |
| 45 | + |
| 46 | +vi.mock('@/lib/billing/core/usage-log', () => ({ |
| 47 | + getBillingPeriodUsageCost: vi.fn(), |
| 48 | +})) |
| 49 | + |
| 50 | +vi.mock('@/lib/billing/credits/daily-refresh', () => ({ |
| 51 | + computeDailyRefreshConsumed: vi.fn(), |
| 52 | + getOrgMemberRefreshBounds: vi.fn(), |
| 53 | +})) |
| 54 | + |
| 55 | +vi.mock('@/components/emails', () => ({ |
| 56 | + getEmailSubject: vi.fn(), |
| 57 | + renderCreditsExhaustedEmail: vi.fn(), |
| 58 | + renderFreeTierUpgradeEmail: vi.fn(), |
| 59 | + renderUsageThresholdEmail: vi.fn(), |
| 60 | +})) |
| 61 | + |
| 62 | +vi.mock('@/lib/messaging/email/mailer', () => ({ |
| 63 | + sendEmail: vi.fn(), |
| 64 | +})) |
| 65 | + |
| 66 | +vi.mock('@/lib/messaging/email/unsubscribe', () => ({ |
| 67 | + getEmailPreferences: vi.fn(), |
| 68 | +})) |
| 69 | + |
| 70 | +import { getUserUsageLimit } from '@/lib/billing/core/usage' |
| 71 | + |
| 72 | +const PRO_SUBSCRIPTION = { |
| 73 | + id: 'sub-1', |
| 74 | + plan: 'pro', |
| 75 | + status: 'active', |
| 76 | + referenceId: 'user-1', |
| 77 | + seats: 1, |
| 78 | + periodStart: null, |
| 79 | + periodEnd: null, |
| 80 | +} as never |
| 81 | + |
| 82 | +describe('getUserUsageLimit', () => { |
| 83 | + beforeEach(() => { |
| 84 | + vi.clearAllMocks() |
| 85 | + mockIsOrgScopedSubscription.mockReturnValue(false) |
| 86 | + }) |
| 87 | + |
| 88 | + it('returns the stored limit when set', async () => { |
| 89 | + dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: '25' }]) |
| 90 | + |
| 91 | + const limit = await getUserUsageLimit('user-1', null) |
| 92 | + |
| 93 | + expect(limit).toBe(25) |
| 94 | + expect(dbChainMockFns.update).not.toHaveBeenCalled() |
| 95 | + }) |
| 96 | + |
| 97 | + it('throws when no userStats row exists', async () => { |
| 98 | + dbChainMockFns.limit.mockResolvedValueOnce([]) |
| 99 | + |
| 100 | + await expect(getUserUsageLimit('user-1', null)).rejects.toThrow('No user stats record found') |
| 101 | + }) |
| 102 | + |
| 103 | + it('heals a null limit to the free-tier default for free users', async () => { |
| 104 | + dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: null }]) |
| 105 | + mockGetFreeTierLimit.mockReturnValue(10) |
| 106 | + |
| 107 | + const limit = await getUserUsageLimit('user-1', null) |
| 108 | + |
| 109 | + expect(limit).toBe(10) |
| 110 | + expect(dbChainMockFns.update).toHaveBeenCalledTimes(1) |
| 111 | + expect(dbChainMockFns.set).toHaveBeenCalledWith({ |
| 112 | + currentUsageLimit: '10', |
| 113 | + usageLimitUpdatedAt: expect.any(Date), |
| 114 | + }) |
| 115 | + const [condition] = dbChainMockFns.where.mock.calls.at(-1) ?? [] |
| 116 | + expect(condition).toMatchObject({ |
| 117 | + type: 'and', |
| 118 | + conditions: [{ type: 'eq', right: 'user-1' }, { type: 'isNull' }], |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + it('heals a null limit to the plan minimum for paid personal subscriptions', async () => { |
| 123 | + dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: null }]) |
| 124 | + mockHasPaidSubscriptionStatus.mockReturnValue(true) |
| 125 | + mockGetPerUserMinimumLimit.mockReturnValue(40) |
| 126 | + |
| 127 | + const limit = await getUserUsageLimit('user-1', PRO_SUBSCRIPTION) |
| 128 | + |
| 129 | + expect(limit).toBe(40) |
| 130 | + expect(mockGetPerUserMinimumLimit).toHaveBeenCalledWith(PRO_SUBSCRIPTION) |
| 131 | + expect(dbChainMockFns.set).toHaveBeenCalledWith({ |
| 132 | + currentUsageLimit: '40', |
| 133 | + usageLimitUpdatedAt: expect.any(Date), |
| 134 | + }) |
| 135 | + }) |
| 136 | +}) |
0 commit comments