From 0a06ccae752d97785eaa13c38dee0e12e0bcd14f Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 14 May 2026 14:43:31 -0700 Subject: [PATCH] test: add purchaseCredits tests using canAccessSubscriptionFeatures - Update mock from isActiveSubscription to canAccessSubscriptionFeatures - Add tests for purchaseCredits early return and successful flow Co-Authored-By: Claude Opus 4.5 --- src/composables/auth/useAuthActions.test.ts | 54 +++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/composables/auth/useAuthActions.test.ts b/src/composables/auth/useAuthActions.test.ts index 4073434e5d..d6ba7e2a35 100644 --- a/src/composables/auth/useAuthActions.test.ts +++ b/src/composables/auth/useAuthActions.test.ts @@ -7,7 +7,10 @@ import type { ComfyWorkflow } from '@/platform/workflow/management/stores/workfl type ModifiedWorkflow = Pick const mockAuthStore = vi.hoisted(() => ({ - logout: vi.fn().mockResolvedValue(undefined) + logout: vi.fn().mockResolvedValue(undefined), + initiateCreditPurchase: vi + .fn() + .mockResolvedValue({ checkout_url: 'https://checkout.example.com' }) })) const mockToastStore = vi.hoisted(() => ({ @@ -35,8 +38,12 @@ vi.mock('@/platform/distribution/types', () => ({ isCloud: false })) +const mockStartTopupTracking = vi.hoisted(() => vi.fn()) + vi.mock('@/platform/telemetry', () => ({ - useTelemetry: vi.fn(() => undefined) + useTelemetry: vi.fn(() => ({ + startTopupTracking: mockStartTopupTracking + })) })) vi.mock('@/platform/updates/common/toastStore', () => ({ @@ -59,9 +66,13 @@ vi.mock('@/stores/authStore', () => ({ useAuthStore: vi.fn(() => mockAuthStore) })) +const mockCanAccessSubscriptionFeatures = vi.hoisted(() => ({ + value: false +})) + vi.mock('@/composables/billing/useBillingContext', () => ({ useBillingContext: vi.fn(() => ({ - isActiveSubscription: { value: false }, + canAccessSubscriptionFeatures: mockCanAccessSubscriptionFeatures, isFreeTier: { value: true }, type: { value: 'free' } })) @@ -193,3 +204,40 @@ describe('useAuthActions.logout', () => { ) }) }) + +describe('useAuthActions.purchaseCredits', () => { + beforeEach(() => { + setActivePinia(createPinia()) + vi.clearAllMocks() + mockCanAccessSubscriptionFeatures.value = false + }) + + it('returns early when canAccessSubscriptionFeatures is false', async () => { + mockCanAccessSubscriptionFeatures.value = false + + const { purchaseCredits } = useAuthActions() + await purchaseCredits(10) + + expect(mockAuthStore.initiateCreditPurchase).not.toHaveBeenCalled() + }) + + it('initiates credit purchase when canAccessSubscriptionFeatures is true', async () => { + mockCanAccessSubscriptionFeatures.value = true + const mockOpen = vi.spyOn(window, 'open').mockImplementation(() => null) + + const { purchaseCredits } = useAuthActions() + await purchaseCredits(10) + + expect(mockAuthStore.initiateCreditPurchase).toHaveBeenCalledWith({ + amount_micros: 10_000_000, + currency: 'usd' + }) + expect(mockStartTopupTracking).toHaveBeenCalled() + expect(mockOpen).toHaveBeenCalledWith( + 'https://checkout.example.com', + '_blank' + ) + + mockOpen.mockRestore() + }) +})