@@ -7,7 +7,10 @@ import type { ComfyWorkflow } from '@/platform/workflow/management/stores/workfl
77type ModifiedWorkflow = Pick < ComfyWorkflow , 'path' | 'isModified' >
88
99const mockAuthStore = vi . hoisted ( ( ) => ( {
10- logout : vi . fn ( ) . mockResolvedValue ( undefined )
10+ logout : vi . fn ( ) . mockResolvedValue ( undefined ) ,
11+ initiateCreditPurchase : vi
12+ . fn ( )
13+ . mockResolvedValue ( { checkout_url : 'https://checkout.example.com' } )
1114} ) )
1215
1316const mockToastStore = vi . hoisted ( ( ) => ( {
@@ -35,8 +38,12 @@ vi.mock('@/platform/distribution/types', () => ({
3538 isCloud : false
3639} ) )
3740
41+ const mockStartTopupTracking = vi . hoisted ( ( ) => vi . fn ( ) )
42+
3843vi . mock ( '@/platform/telemetry' , ( ) => ( {
39- useTelemetry : vi . fn ( ( ) => undefined )
44+ useTelemetry : vi . fn ( ( ) => ( {
45+ startTopupTracking : mockStartTopupTracking
46+ } ) )
4047} ) )
4148
4249vi . mock ( '@/platform/updates/common/toastStore' , ( ) => ( {
@@ -59,9 +66,13 @@ vi.mock('@/stores/authStore', () => ({
5966 useAuthStore : vi . fn ( ( ) => mockAuthStore )
6067} ) )
6168
69+ const mockCanAccessSubscriptionFeatures = vi . hoisted ( ( ) => ( {
70+ value : false
71+ } ) )
72+
6273vi . mock ( '@/composables/billing/useBillingContext' , ( ) => ( {
6374 useBillingContext : vi . fn ( ( ) => ( {
64- isActiveSubscription : { value : false } ,
75+ canAccessSubscriptionFeatures : mockCanAccessSubscriptionFeatures ,
6576 isFreeTier : { value : true } ,
6677 type : { value : 'free' }
6778 } ) )
@@ -193,3 +204,40 @@ describe('useAuthActions.logout', () => {
193204 )
194205 } )
195206} )
207+
208+ describe ( 'useAuthActions.purchaseCredits' , ( ) => {
209+ beforeEach ( ( ) => {
210+ setActivePinia ( createPinia ( ) )
211+ vi . clearAllMocks ( )
212+ mockCanAccessSubscriptionFeatures . value = false
213+ } )
214+
215+ it ( 'returns early when canAccessSubscriptionFeatures is false' , async ( ) => {
216+ mockCanAccessSubscriptionFeatures . value = false
217+
218+ const { purchaseCredits } = useAuthActions ( )
219+ await purchaseCredits ( 10 )
220+
221+ expect ( mockAuthStore . initiateCreditPurchase ) . not . toHaveBeenCalled ( )
222+ } )
223+
224+ it ( 'initiates credit purchase when canAccessSubscriptionFeatures is true' , async ( ) => {
225+ mockCanAccessSubscriptionFeatures . value = true
226+ const mockOpen = vi . spyOn ( window , 'open' ) . mockImplementation ( ( ) => null )
227+
228+ const { purchaseCredits } = useAuthActions ( )
229+ await purchaseCredits ( 10 )
230+
231+ expect ( mockAuthStore . initiateCreditPurchase ) . toHaveBeenCalledWith ( {
232+ amount_micros : 10_000_000 ,
233+ currency : 'usd'
234+ } )
235+ expect ( mockStartTopupTracking ) . toHaveBeenCalled ( )
236+ expect ( mockOpen ) . toHaveBeenCalledWith (
237+ 'https://checkout.example.com' ,
238+ '_blank'
239+ )
240+
241+ mockOpen . mockRestore ( )
242+ } )
243+ } )
0 commit comments