-
Notifications
You must be signed in to change notification settings - Fork 3.7k
improvement(billing): self-heal null usage limits and debounce api-key last-used writes #5000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+254
−8
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
077ee24
improvement(billing): self-heal null usage limits and debounce api-ke…
waleedlatif1 eb7bf1d
fix(billing): make usage-limit self-heal best-effort and respect conc…
waleedlatif1 1814df5
improvement(api-key): widen last-used staleness window to 10 minutes
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /** | ||
| * Tests for getUserUsageLimit. | ||
| * | ||
| * Org-scoped members carry a null `currentUsageLimit` by design, so a user | ||
| * whose subscription stops being org-scoped without a resync is left null. | ||
| * The limit read must self-heal that state to the plan default instead of | ||
| * failing closed and blocking every execution. | ||
| * | ||
| * @vitest-environment node | ||
| */ | ||
| import { dbChainMock, dbChainMockFns } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('@sim/db', () => dbChainMock) | ||
|
|
||
| const { | ||
| mockGetFreeTierLimit, | ||
| mockGetPerUserMinimumLimit, | ||
| mockHasPaidSubscriptionStatus, | ||
| mockIsOrgScopedSubscription, | ||
| } = vi.hoisted(() => ({ | ||
| mockGetFreeTierLimit: vi.fn(), | ||
| mockGetPerUserMinimumLimit: vi.fn(), | ||
| mockHasPaidSubscriptionStatus: vi.fn(), | ||
| mockIsOrgScopedSubscription: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/subscriptions/utils', () => ({ | ||
| canEditUsageLimit: vi.fn(), | ||
| getFreeTierLimit: mockGetFreeTierLimit, | ||
| getPerUserMinimumLimit: mockGetPerUserMinimumLimit, | ||
| getPlanPricing: vi.fn(() => ({ basePrice: 20 })), | ||
| hasPaidSubscriptionStatus: mockHasPaidSubscriptionStatus, | ||
| hasUsableSubscriptionAccess: vi.fn(), | ||
| isOrgScopedSubscription: mockIsOrgScopedSubscription, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/core/plan', () => ({ | ||
| getHighestPrioritySubscription: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/core/access', () => ({ | ||
| getEffectiveBillingStatus: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/core/usage-log', () => ({ | ||
| getBillingPeriodUsageCost: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/credits/daily-refresh', () => ({ | ||
| computeDailyRefreshConsumed: vi.fn(), | ||
| getOrgMemberRefreshBounds: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/components/emails', () => ({ | ||
| getEmailSubject: vi.fn(), | ||
| renderCreditsExhaustedEmail: vi.fn(), | ||
| renderFreeTierUpgradeEmail: vi.fn(), | ||
| renderUsageThresholdEmail: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/messaging/email/mailer', () => ({ | ||
| sendEmail: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/messaging/email/unsubscribe', () => ({ | ||
| getEmailPreferences: vi.fn(), | ||
| })) | ||
|
|
||
| import { getUserUsageLimit } from '@/lib/billing/core/usage' | ||
|
|
||
| const PRO_SUBSCRIPTION = { | ||
| id: 'sub-1', | ||
| plan: 'pro', | ||
| status: 'active', | ||
| referenceId: 'user-1', | ||
| seats: 1, | ||
| periodStart: null, | ||
| periodEnd: null, | ||
| } as never | ||
|
|
||
| describe('getUserUsageLimit', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockIsOrgScopedSubscription.mockReturnValue(false) | ||
| }) | ||
|
|
||
| it('returns the stored limit when set', async () => { | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: '25' }]) | ||
|
|
||
| const limit = await getUserUsageLimit('user-1', null) | ||
|
|
||
| expect(limit).toBe(25) | ||
| expect(dbChainMockFns.update).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('throws when no userStats row exists', async () => { | ||
| dbChainMockFns.limit.mockResolvedValueOnce([]) | ||
|
|
||
| await expect(getUserUsageLimit('user-1', null)).rejects.toThrow('No user stats record found') | ||
| }) | ||
|
|
||
| it('heals a null limit to the free-tier default for free users', async () => { | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: null }]) | ||
| dbChainMockFns.returning.mockResolvedValueOnce([{ currentUsageLimit: '10' }]) | ||
| mockGetFreeTierLimit.mockReturnValue(10) | ||
|
|
||
| const limit = await getUserUsageLimit('user-1', null) | ||
|
|
||
| expect(limit).toBe(10) | ||
| expect(dbChainMockFns.update).toHaveBeenCalledTimes(1) | ||
| expect(dbChainMockFns.set).toHaveBeenCalledWith({ | ||
| currentUsageLimit: '10', | ||
| usageLimitUpdatedAt: expect.any(Date), | ||
| }) | ||
| const [condition] = dbChainMockFns.where.mock.calls.at(-1) ?? [] | ||
| expect(condition).toMatchObject({ | ||
| type: 'and', | ||
| conditions: [{ type: 'eq', right: 'user-1' }, { type: 'isNull' }], | ||
| }) | ||
| }) | ||
|
|
||
| it('heals a null limit to the plan minimum for paid personal subscriptions', async () => { | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: null }]) | ||
| dbChainMockFns.returning.mockResolvedValueOnce([{ currentUsageLimit: '40' }]) | ||
| mockHasPaidSubscriptionStatus.mockReturnValue(true) | ||
| mockGetPerUserMinimumLimit.mockReturnValue(40) | ||
|
|
||
| const limit = await getUserUsageLimit('user-1', PRO_SUBSCRIPTION) | ||
|
|
||
| expect(limit).toBe(40) | ||
| expect(mockGetPerUserMinimumLimit).toHaveBeenCalledWith(PRO_SUBSCRIPTION) | ||
| expect(dbChainMockFns.set).toHaveBeenCalledWith({ | ||
| currentUsageLimit: '40', | ||
| usageLimitUpdatedAt: expect.any(Date), | ||
| }) | ||
| }) | ||
|
|
||
| it('returns a concurrently written limit when the guarded heal matches no rows', async () => { | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: null }]) | ||
| dbChainMockFns.returning.mockResolvedValueOnce([]) | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: '30' }]) | ||
| mockGetFreeTierLimit.mockReturnValue(10) | ||
|
|
||
| const limit = await getUserUsageLimit('user-1', null) | ||
|
|
||
| expect(limit).toBe(30) | ||
| }) | ||
|
|
||
| it('still returns the fallback when the heal write fails', async () => { | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ currentUsageLimit: null }]) | ||
| dbChainMockFns.returning.mockRejectedValueOnce(new Error('connection lost')) | ||
| mockGetFreeTierLimit.mockReturnValue(10) | ||
|
|
||
| await expect(getUserUsageLimit('user-1', null)).resolves.toBe(10) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.