LCOV - code coverage report
Current view: top level - src/platform/billing/composables - useAutoTopUpCardSection.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 80.8 % 26 21
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import {
       2              :   autoTopUpOffView,
       3              :   isAutoTopUpNotAvailable
       4              : } from '@comfyorg/account-core/billing'
       5              : import { autoTopUpCardView } from '@comfyorg/account-ui/billing'
       6              : import { reportError } from '@comfyorg/telemetry/reportError'
       7              : import { computedAsync } from '@vueuse/core'
       8              : import { computed, toValue, watch } from 'vue'
       9              : import type { MaybeRefOrGetter } from 'vue'
      10              : import { useI18n } from 'vue-i18n'
      11              : 
      12              : import { useAutoTopUpCardViewed } from '@/platform/billing/composables/useAutoTopUpCardViewed'
      13              : import { useAutoTopUpCardCopy } from '@/platform/billing/composables/useAutoTopUpCopy'
      14              : import { useBillingAutoTopUpCard } from '@/platform/billing/composables/useBillingAutoTopUpCard'
      15              : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
      16              : import { usePlatformBillingLink } from '@/platform/billing/composables/usePlatformHandoff'
      17              : import { platformAutoTopUpPath } from '@/platform/billing/platformAutoTopUpPath'
      18              : import { useBillingSdkStore } from '@/platform/billing/sdk/billingSdkStore'
      19              : 
      20              : /** The auto top-up card's served state, copy and Platform link while the card is shown. */
      21            5 : export function useAutoTopUpCardSection(shown: MaybeRefOrGetter<boolean>) {
      22            5 :   const { locale } = useI18n()
      23            5 :   const copy = useAutoTopUpCardCopy()
      24            5 :   const { autoTopUpSetup } = useBillingCapabilities()
      25            5 :   const { scope, state, isReady, ensureRead, retry } = useBillingAutoTopUpCard()
      26              : 
      27            5 :   watch(
      28              :     () => toValue(shown),
      29              :     (visible) => {
      30            5 :       if (visible) ensureRead()
      31              :     },
      32              :     { immediate: true }
      33              :   )
      34              : 
      35            5 :   useAutoTopUpCardViewed(() => {
      36            9 :     const current = state.value
      37            9 :     return toValue(shown) &&
      38              :       !isAutoTopUpNotAvailable(autoTopUpSetup.value) &&
      39              :       isReady.value &&
      40              :       current.kind === 'loaded'
      41              :       ? current.settings.status
      42              :       : undefined
      43              :   })
      44              : 
      45            5 :   const offSettings = computed(() =>
      46              :     toValue(shown) &&
      47              :     state.value.kind === 'loaded' &&
      48              :     state.value.settings.status === 'off'
      49              :       ? state.value.settings
      50              :       : null
      51              :   )
      52              : 
      53            5 :   const savedPaymentMethodsRead = computedAsync(async () => {
      54            9 :     const requestScope = scope.value
      55            9 :     if (
      56              :       !offSettings.value ||
      57              :       autoTopUpOffView({
      58              :         setup: autoTopUpSetup.value,
      59              :         savedPaymentMethodCount: null
      60              :       }) === 'off'
      61              :     ) {
      62            9 :       return null
      63              :     }
      64            0 :     const result = await useBillingSdkStore().readPaymentMethods()
      65            0 :     if (result.status === 'ok') {
      66            0 :       return { scope: requestScope, methods: result.value }
      67              :     }
      68            0 :     reportError(
      69              :       new Error(`Saved payment methods read failed: ${result.code}`),
      70              :       {
      71              :         errorType: 'auto_top_up_payment_methods_read_failed',
      72              :         surface: 'billing'
      73              :       }
      74              :     )
      75            0 :     return null
      76              :   }, null)
      77              : 
      78            5 :   const offView = computed(() => {
      79            4 :     const read = savedPaymentMethodsRead.value
      80            4 :     return autoTopUpOffView({
      81              :       setup: autoTopUpSetup.value,
      82              :       savedPaymentMethodCount:
      83              :         read?.scope === scope.value ? read.methods.length : null
      84              :     })
      85              :   })
      86              : 
      87            5 :   const action = computed(() =>
      88              :     state.value.kind === 'loaded'
      89              :       ? autoTopUpCardView(
      90              :           state.value.settings,
      91              :           offView.value,
      92              :           copy.value,
      93              :           locale.value
      94              :         ).action
      95              :       : 'turn_on'
      96              :   )
      97              : 
      98            5 :   const { href, open } = usePlatformBillingLink(
      99              :     () => platformAutoTopUpPath(action.value),
     100              :     () => ({ target: 'auto_top_up', action: action.value })
     101              :   )
     102              : 
     103            5 :   return {
     104              :     setup: autoTopUpSetup,
     105              :     state,
     106              :     offView,
     107              :     copy,
     108              :     retry,
     109              :     href,
     110              :     open
     111              :   }
     112              : }
        

Generated by: LCOV version 2.0-1