|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockAtomicallyClaim, mockRelease, mockIdempotencyService } = vi.hoisted(() => ({ |
| 7 | + mockAtomicallyClaim: vi.fn(), |
| 8 | + mockRelease: vi.fn(), |
| 9 | + mockIdempotencyService: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/lib/core/idempotency/service', () => ({ |
| 13 | + IdempotencyService: class MockIdempotencyService { |
| 14 | + constructor(options: unknown) { |
| 15 | + mockIdempotencyService(options) |
| 16 | + } |
| 17 | + |
| 18 | + atomicallyClaim(...args: unknown[]) { |
| 19 | + return mockAtomicallyClaim(...args) |
| 20 | + } |
| 21 | + |
| 22 | + release(...args: unknown[]) { |
| 23 | + return mockRelease(...args) |
| 24 | + } |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +import { |
| 29 | + claimCheckoutAdmission, |
| 30 | + releaseCheckoutAdmission, |
| 31 | + resolveCheckoutReferenceId, |
| 32 | +} from '@/lib/billing/checkout-admission' |
| 33 | + |
| 34 | +describe('checkout admission', () => { |
| 35 | + beforeEach(() => { |
| 36 | + mockAtomicallyClaim.mockReset() |
| 37 | + mockRelease.mockReset() |
| 38 | + mockRelease.mockResolvedValue(undefined) |
| 39 | + }) |
| 40 | + |
| 41 | + it('uses durable, short-lived database claims', () => { |
| 42 | + expect(mockIdempotencyService).toHaveBeenCalledWith({ |
| 43 | + namespace: 'billing-checkout-admission', |
| 44 | + ttlSeconds: 120, |
| 45 | + inProgressTtlSeconds: 120, |
| 46 | + retryFailures: true, |
| 47 | + storeResultBody: false, |
| 48 | + forceStorage: 'database', |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('resolves explicit, organization, and personal references like Better Auth', () => { |
| 53 | + expect( |
| 54 | + resolveCheckoutReferenceId({ referenceId: 'org-explicit' }, 'user-1', 'org-active') |
| 55 | + ).toBe('org-explicit') |
| 56 | + expect( |
| 57 | + resolveCheckoutReferenceId({ customerType: 'organization' }, 'user-1', 'org-active') |
| 58 | + ).toBe('org-active') |
| 59 | + expect(resolveCheckoutReferenceId({}, 'user-1', 'org-active')).toBe('user-1') |
| 60 | + }) |
| 61 | + |
| 62 | + it('admits only one overlapping checkout for a billing reference', async () => { |
| 63 | + mockAtomicallyClaim |
| 64 | + .mockResolvedValueOnce({ |
| 65 | + claimed: true, |
| 66 | + normalizedKey: 'billing-checkout-admission:stripe:org-1', |
| 67 | + storageMethod: 'database', |
| 68 | + claimToken: 'claim-1', |
| 69 | + }) |
| 70 | + .mockResolvedValueOnce({ |
| 71 | + claimed: false, |
| 72 | + normalizedKey: 'billing-checkout-admission:stripe:org-1', |
| 73 | + storageMethod: 'database', |
| 74 | + existingResult: { status: 'in-progress' }, |
| 75 | + }) |
| 76 | + |
| 77 | + const firstClaim = await claimCheckoutAdmission('org-1') |
| 78 | + await expect(claimCheckoutAdmission('org-1')).rejects.toThrow( |
| 79 | + /checkout is already being started/ |
| 80 | + ) |
| 81 | + expect(firstClaim.claimToken).toBe('claim-1') |
| 82 | + }) |
| 83 | + |
| 84 | + it('releases only the claim owned by this request', async () => { |
| 85 | + await releaseCheckoutAdmission({ |
| 86 | + normalizedKey: 'billing-checkout-admission:stripe:org-1', |
| 87 | + storageMethod: 'database', |
| 88 | + claimToken: 'claim-1', |
| 89 | + }) |
| 90 | + |
| 91 | + expect(mockRelease).toHaveBeenCalledWith( |
| 92 | + 'billing-checkout-admission:stripe:org-1', |
| 93 | + 'database', |
| 94 | + 'claim-1' |
| 95 | + ) |
| 96 | + }) |
| 97 | + |
| 98 | + it('does not replace a successful checkout response when release fails', async () => { |
| 99 | + mockRelease.mockRejectedValueOnce(new Error('database unavailable')) |
| 100 | + |
| 101 | + await expect( |
| 102 | + releaseCheckoutAdmission({ |
| 103 | + normalizedKey: 'billing-checkout-admission:stripe:org-1', |
| 104 | + storageMethod: 'database', |
| 105 | + claimToken: 'claim-1', |
| 106 | + }) |
| 107 | + ).resolves.toBeUndefined() |
| 108 | + }) |
| 109 | +}) |
0 commit comments