LCOV - code coverage report
Current view: top level - src/platform/billing/composables - useTopupCheckoutBanner.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 32.8 % 61 20
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { HostedTopupCheckout } from '@comfyorg/ingest-types'
       2              : import {
       3              :   useAsyncState,
       4              :   useDocumentVisibility,
       5              :   useIntervalFn
       6              : } from '@vueuse/core'
       7              : import { computed, shallowRef, toValue, watch } from 'vue'
       8              : import type { MaybeRefOrGetter } from 'vue'
       9              : 
      10              : import { reportError } from '@comfyorg/telemetry/reportError'
      11              : import { billingApi } from '@/platform/billing/api/billingApi'
      12              : import {
      13              :   selectTopupCheckoutBanner,
      14              :   topupCheckoutBannerKey
      15              : } from '@comfyorg/account-core/billing'
      16              : import type { TopupCheckoutSnapshot } from '@comfyorg/account-core/billing'
      17              : import { useWorkspaceIdentity } from '@/platform/workspace/ports/workspaceIdentity'
      18              : 
      19            5 : const CREDITS_APPLYING_POLL_MS = 5_000
      20              : 
      21            5 : const NO_CHECKOUTS: TopupCheckoutSnapshot = {
      22              :   newestSessionId: null,
      23              :   checkouts: []
      24              : }
      25              : 
      26            5 : export function useTopupCheckoutBanner(enabled: MaybeRefOrGetter<boolean>) {
      27            5 :   const workspaceStore = useWorkspaceIdentity()
      28            5 :   const workspaceScope = () =>
      29              :     `${workspaceStore.workspaceTransitionGeneration}:${workspaceStore.activeWorkspaceId}`
      30            5 :   const reportedSessions = new Set<string>()
      31            5 :   let listFailureOpen = false
      32            5 :   let refreshQueued = false
      33              : 
      34            0 :   function reportCheckoutReadFailure(error: unknown, key: string) {
      35            0 :     if (key === 'list') {
      36            0 :       if (listFailureOpen) return
      37            0 :       listFailureOpen = true
      38            0 :     } else if (reportedSessions.has(key)) {
      39            0 :       return
      40              :     } else {
      41            0 :       reportedSessions.add(key)
      42              :     }
      43            0 :     reportError(error, {
      44              :       errorType: 'topup_checkout_read_failed',
      45              :       surface: 'billing'
      46              :     })
      47              :   }
      48              : 
      49            0 :   async function readTopupCheckouts(): Promise<TopupCheckoutSnapshot> {
      50            0 :     const launchedScope = workspaceScope()
      51            0 :     let listed: Awaited<ReturnType<typeof billingApi.listTopupCheckouts>>
      52            0 :     try {
      53            0 :       listed = await billingApi.listTopupCheckouts()
      54            0 :       listFailureOpen = false
      55              :     } catch (error) {
      56            0 :       reportCheckoutReadFailure(error, 'list')
      57            0 :       throw error
      58              :     }
      59            0 :     const { checkouts } = listed
      60            0 :     const reads = await Promise.allSettled(
      61              :       checkouts.map(({ session_id }) => billingApi.getTopupCheckout(session_id))
      62              :     )
      63            0 :     if (workspaceScope() !== launchedScope) return read.state.value
      64            0 :     const lastKnown = new Map(
      65              :       read.state.value.checkouts.map((known) => [known.session_id, known])
      66              :     )
      67            0 :     return {
      68              :       newestSessionId: checkouts[0]?.session_id ?? null,
      69              :       checkouts: reads.flatMap((result, index): HostedTopupCheckout[] => {
      70            0 :         if (result.status === 'fulfilled') return [result.value]
      71            0 :         const sessionId = checkouts[index]?.session_id
      72            0 :         if (!sessionId) return []
      73            0 :         reportCheckoutReadFailure(result.reason, sessionId)
      74            0 :         const known = lastKnown.get(sessionId)
      75            0 :         return known ? [known] : []
      76              :       })
      77              :     }
      78              :   }
      79              : 
      80            5 :   const read = useAsyncState(readTopupCheckouts, NO_CHECKOUTS, {
      81              :     immediate: false,
      82              :     resetOnExecute: false
      83              :   })
      84            5 :   const dismissed = shallowRef<ReadonlySet<string>>(new Set())
      85              : 
      86            5 :   const banner = computed(() =>
      87              :     toValue(enabled)
      88              :       ? selectTopupCheckoutBanner(read.state.value, dismissed.value)
      89              :       : null
      90              :   )
      91              : 
      92            0 :   function dismiss() {
      93            0 :     if (!banner.value) return
      94            0 :     dismissed.value = new Set([
      95              :       ...dismissed.value,
      96              :       topupCheckoutBannerKey(banner.value)
      97              :     ])
      98              :   }
      99              : 
     100            5 :   function executeRead() {
     101            5 :     if (!toValue(enabled)) return
     102            0 :     if (read.isLoading.value) {
     103            0 :       refreshQueued = true
     104            0 :       return
     105              :     }
     106            0 :     void read.execute()
     107              :   }
     108              : 
     109            5 :   watch(read.isLoading, (isLoading) => {
     110            0 :     if (isLoading || !refreshQueued) return
     111            0 :     refreshQueued = false
     112            0 :     executeRead()
     113              :   })
     114              : 
     115            5 :   const poll = useIntervalFn(executeRead, CREDITS_APPLYING_POLL_MS, {
     116              :     immediate: false
     117              :   })
     118            5 :   watch(
     119              :     () =>
     120              :       toValue(enabled) &&
     121              :       read.state.value.checkouts.some(
     122              :         ({ state }) => state === 'credits_applying'
     123              :       ),
     124              :     (applying) => (applying ? poll.resume() : poll.pause())
     125              :   )
     126              : 
     127            5 :   watch(workspaceScope, () => {
     128            0 :     read.state.value = NO_CHECKOUTS
     129            0 :     dismissed.value = new Set()
     130            0 :     executeRead()
     131              :   })
     132            5 :   watch(() => toValue(enabled), executeRead, { immediate: true })
     133            5 :   watch(useDocumentVisibility(), (visibility) => {
     134            0 :     if (visibility === 'visible') executeRead()
     135              :   })
     136              : 
     137            5 :   return { banner, dismiss }
     138              : }
        

Generated by: LCOV version 2.0-1