LCOV - code coverage report
Current view: top level - src/platform/billing/composables - useAddCreditsAutoTopUpOffer.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 59.1 % 22 13
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { computedAsync } from '@vueuse/core'
       2              : import { computed, watch } from 'vue'
       3              : 
       4              : import type {
       5              :   AddCreditsAutoTopUpOffer,
       6              :   AutoTopUpDefaultCard
       7              : } from '@comfyorg/account-core/billing'
       8              : import { addCreditsAutoTopUpOffer } from '@comfyorg/account-core/billing'
       9              : import { reportError } from '@comfyorg/telemetry/reportError'
      10              : 
      11              : import { useFeatureFlags } from '@/composables/useFeatureFlags'
      12              : import { useBillingAutoTopUpCard } from '@/platform/billing/composables/useBillingAutoTopUpCard'
      13              : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
      14              : import {
      15              :   openPlatformBilling,
      16              :   usePlatformHandoff
      17              : } from '@/platform/billing/composables/usePlatformHandoff'
      18              : import { useBillingSdkStore } from '@/platform/billing/sdk/billingSdkStore'
      19              : 
      20              : type DefaultCardRead =
      21              :   | { readonly kind: 'pending' }
      22              :   | { readonly kind: 'unknown' }
      23              :   | { readonly kind: 'loaded'; readonly card: AutoTopUpDefaultCard | null }
      24              : 
      25            0 : async function readDefaultCard(): Promise<DefaultCardRead> {
      26            0 :   const result = await useBillingSdkStore().readPaymentMethods()
      27            0 :   if (result.status !== 'ok') {
      28            0 :     reportError(
      29              :       new Error(`Saved payment methods read failed: ${result.code}`),
      30              :       {
      31              :         errorType: 'add_credits_payment_methods_read_failed',
      32              :         surface: 'billing'
      33              :       }
      34              :     )
      35            0 :     return { kind: 'unknown' }
      36              :   }
      37            0 :   const method = result.value.find((saved) => saved.is_default)
      38            0 :   if (!method) return { kind: 'loaded', card: null }
      39            0 :   const { brand, last4 } = method
      40            0 :   return brand && last4
      41              :     ? { kind: 'loaded', card: { brand, last4 } }
      42              :     : { kind: 'unknown' }
      43              : }
      44              : 
      45              : /** The Add credits dialog's auto top-up offer from served facts: the checkbox only with `billing_auto_top_up_at_checkout_enabled` and a settled saved-cards read, else just the already-on line. */
      46            4 : export function useAddCreditsAutoTopUpOffer() {
      47            4 :   const { flags } = useFeatureFlags()
      48            4 :   const { autoTopUpEnabled, canManageAutoTopUp, autoTopUpSetup } =
      49              :     useBillingCapabilities()
      50            4 :   const { state, ensureRead } = useBillingAutoTopUpCard()
      51            4 :   const { href, track } = usePlatformHandoff('/profile/billing', {
      52              :     target: 'auto_top_up',
      53              :     action: 'manage'
      54              :   })
      55              : 
      56            4 :   watch(
      57              :     autoTopUpEnabled,
      58              :     (enabled) => {
      59            4 :       if (enabled) ensureRead()
      60              :     },
      61              :     { immediate: true }
      62              :   )
      63              : 
      64            4 :   const defaultCard = computedAsync<DefaultCardRead>(
      65              :     () =>
      66              :       flags.billingAutoTopUpAtCheckoutEnabled && autoTopUpEnabled.value
      67              :         ? readDefaultCard()
      68              :         : { kind: 'pending' },
      69              :     { kind: 'pending' }
      70              :   )
      71              : 
      72              :   const offer = computed<AddCreditsAutoTopUpOffer>(() => {
      73            8 :     const read = state.value
      74            8 :     const card = defaultCard.value
      75            8 :     const served = addCreditsAutoTopUpOffer({
      76              :       autoTopUpEnabled: autoTopUpEnabled.value,
      77              :       canManageAutoTopUp: canManageAutoTopUp.value,
      78              :       setup: autoTopUpSetup.value,
      79              :       settings: read.kind === 'loaded' ? read.settings : null,
      80              :       defaultCard: card.kind === 'loaded' ? card.card : null
      81              :     })
      82            8 :     return served.kind === 'checkbox' &&
      83              :       (!flags.billingAutoTopUpAtCheckoutEnabled || card.kind !== 'loaded')
      84              :       ? { kind: 'hidden' }
      85              :       : served
      86              :   })
      87              : 
      88            4 :   return { offer, billingUrl: href, open: () => openPlatformBilling(track) }
      89              : }
        

Generated by: LCOV version 2.0-1