LCOV - code coverage report
Current view: top level - packages/account-ui/src/billing/addCredits - useAddCreditsAutoTopUp.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 70.0 % 20 14
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { computed, shallowRef, toValue } from 'vue'
       2              : import type { MaybeRefOrGetter } from 'vue'
       3              : 
       4              : import type {
       5              :   AddCreditsAutoTopUpOffer,
       6              :   AutoTopUpActivation,
       7              :   AutoTopUpFormInput
       8              : } from '@comfyorg/account-core/billing'
       9              : import {
      10              :   autoTopUpActivationFor,
      11              :   autoTopUpTargetFor,
      12              :   parseAutoTopUpForm,
      13              :   startAutoTopUpForm,
      14              :   validateAutoTopUp
      15              : } from '@comfyorg/account-core/billing'
      16              : 
      17              : import { centsToCredits } from '#/billing/tab/creditsCardCopy'
      18              : 
      19              : export type AddCreditsAutoTopUpRequest =
      20              :   | { readonly kind: 'not_offered' }
      21              :   | { readonly kind: 'unchecked' }
      22              :   | { readonly kind: 'invalid' }
      23              :   | { readonly kind: 'checked'; readonly activation: AutoTopUpActivation }
      24              : 
      25              : export type AutoTopUpEdit = {
      26              :   [K in keyof AutoTopUpFormInput]: [field: K, value: AutoTopUpFormInput[K]]
      27              : }[keyof AutoTopUpFormInput]
      28              : 
      29            4 : export function useAddCreditsAutoTopUp(
      30              :   offer: MaybeRefOrGetter<AddCreditsAutoTopUpOffer>,
      31              :   amountCents: MaybeRefOrGetter<number | undefined>
      32              : ) {
      33            4 :   const checked = shallowRef(false)
      34            4 :   const edits = shallowRef<Partial<AutoTopUpFormInput>>({})
      35              : 
      36            4 :   const creditsPicked = computed(() =>
      37              :     centsToCredits(toValue(amountCents) ?? 0)
      38              :   )
      39              : 
      40            4 :   const input = computed<AutoTopUpFormInput>(() => {
      41            0 :     const merged = {
      42              :       ...startAutoTopUpForm(creditsPicked.value),
      43              :       ...edits.value
      44              :     }
      45            0 :     return edits.value.target !== undefined
      46              :       ? merged
      47              :       : {
      48              :           ...merged,
      49              :           target: autoTopUpTargetFor(merged.threshold, creditsPicked.value)
      50              :         }
      51              :   })
      52              : 
      53            4 :   const values = computed(() => parseAutoTopUpForm(input.value))
      54              : 
      55            4 :   const rules = computed(() => {
      56            8 :     const current = toValue(offer)
      57            8 :     return current.kind === 'checkbox' ? current.rules : undefined
      58              :   })
      59              : 
      60            4 :   const errors = computed(() =>
      61              :     rules.value && checked.value
      62              :       ? validateAutoTopUp(values.value, rules.value)
      63              :       : {}
      64              :   )
      65              : 
      66            4 :   const request = computed<AddCreditsAutoTopUpRequest>(() => {
      67            4 :     if (!rules.value) return { kind: 'not_offered' }
      68            0 :     if (!checked.value) return { kind: 'unchecked' }
      69            0 :     const activation = autoTopUpActivationFor(values.value, rules.value)
      70            4 :     return activation === null
      71              :       ? { kind: 'invalid' }
      72              :       : { kind: 'checked', activation }
      73              :   })
      74              : 
      75            0 :   function edit(...[field, value]: AutoTopUpEdit) {
      76            0 :     edits.value = { ...edits.value, [field]: value }
      77              :   }
      78              : 
      79            4 :   return { checked, input, values, errors, request, edit }
      80              : }
        

Generated by: LCOV version 2.0-1