LCOV - code coverage report
Current view: top level - packages/account-core/src/core/billing/addCredits - addCreditsDialog.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 71.8 % 39 28
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   AddCreditsAmount,
       3              :   AddCreditsAmountCheck,
       4              :   AddCreditsBounds
       5              : } from './addCreditsAmount'
       6              : import { checkAddCreditsAmount } from './addCreditsAmount'
       7              : import type { AutoTopUpActivation } from './addCreditsAutoTopUp'
       8              : import type { AddCreditsRefusal } from './addCreditsRefusal'
       9              : 
      10              : /**
      11              :  * The dialog's own workflow. `opening` remembers whether a tab was reserved
      12              :  * in the click, because without one the session is still created and offered
      13              :  * as a plain link.
      14              :  */
      15              : export type AddCreditsState =
      16              :   | {
      17              :       readonly step: 'amount'
      18              :       readonly amount: AddCreditsAmount
      19              :       readonly refusal?: AddCreditsRefusal
      20              :     }
      21              :   | {
      22              :       readonly step: 'opening'
      23              :       readonly amount: AddCreditsAmount
      24              :       readonly amountCents: number
      25              :       readonly origin: 'amount' | 'checkout_opened'
      26              :       readonly tabReserved: boolean
      27              :     }
      28              :   | {
      29              :       readonly step: 'checkout_opened'
      30              :       readonly amount: AddCreditsAmount
      31              :       readonly amountCents: number
      32              :       readonly refusal?: AddCreditsRefusal
      33              :     }
      34              :   | {
      35              :       readonly step: 'checkout_blocked'
      36              :       readonly amount: AddCreditsAmount
      37              :       readonly amountCents: number
      38              :       /** `checkout_opened` while an earlier checkout from this dialog can still be paid. */
      39              :       readonly origin: 'amount' | 'checkout_opened'
      40              :       /** Absent when the checkout URL works once and is requested again from this step. */
      41              :       readonly checkoutUrl?: string
      42              :     }
      43              :   | { readonly step: 'auto_top_up_offer'; readonly setupUrl: string }
      44              :   | {
      45              :       readonly step: 'auto_top_up_on'
      46              :       readonly settings: AutoTopUpActivation
      47              :     }
      48              :   | { readonly step: 'auto_top_up_failed'; readonly setupUrl?: string }
      49              :   | { readonly step: 'landed' }
      50              : 
      51              : export type AutoTopUpActivationOutcome =
      52              :   | { readonly status: 'on'; readonly settings: AutoTopUpActivation }
      53              :   | { readonly status: 'failed' }
      54              : 
      55              : export type AddCreditsEvent =
      56              :   | { readonly type: 'preset_picked'; readonly cents: number }
      57              :   | { readonly type: 'amount_typed'; readonly input: string }
      58              :   | {
      59              :       readonly type: 'checkout_requested'
      60              :       readonly amountCents: number
      61              :       readonly tabReserved: boolean
      62              :     }
      63              :   | { readonly type: 'checkout_created'; readonly checkoutUrl: string }
      64              :   | { readonly type: 'tab_blocked'; readonly amountCents: number }
      65              :   | { readonly type: 'checkout_refused'; readonly refusal: AddCreditsRefusal }
      66              :   | { readonly type: 'checkout_abandoned' }
      67              :   | { readonly type: 'blocked_link_followed' }
      68              :   | {
      69              :       readonly type: 'payment_landed'
      70              :       /** Platform's Turn on auto top-up link, when the served facts offer it. */
      71              :       readonly autoTopUpSetupUrl?: string
      72              :       readonly activation?: AutoTopUpActivationOutcome
      73              :     }
      74              : 
      75            4 : export function startAddCredits(amount: AddCreditsAmount): AddCreditsState {
      76            4 :   return { step: 'amount', amount }
      77              : }
      78              : 
      79              : type EventOf<T extends AddCreditsEvent['type']> = Extract<
      80              :   AddCreditsEvent,
      81              :   { readonly type: T }
      82              : >
      83              : 
      84              : type Transition<T extends AddCreditsEvent['type']> = (
      85              :   state: AddCreditsState,
      86              :   event: EventOf<T>
      87              : ) => AddCreditsState
      88              : 
      89              : type Opening = Extract<AddCreditsState, { readonly step: 'opening' }>
      90              : 
      91            1 : function backToOrigin(
      92              :   state: Opening,
      93              :   refusal: AddCreditsRefusal | undefined
      94              : ): AddCreditsState {
      95            1 :   const withRefusal = refusal === undefined ? {} : { refusal }
      96            1 :   return state.origin === 'checkout_opened'
      97              :     ? {
      98              :         step: 'checkout_opened',
      99              :         amount: state.amount,
     100              :         amountCents: state.amountCents,
     101              :         ...withRefusal
     102              :       }
     103              :     : { step: 'amount', amount: state.amount, ...withRefusal }
     104              : }
     105              : 
     106         2356 : const LANDABLE_STEPS: ReadonlySet<AddCreditsState['step']> = new Set([
     107              :   'opening',
     108              :   'checkout_opened',
     109              :   'checkout_blocked'
     110              : ])
     111              : 
     112            0 : function landedState({
     113              :   activation,
     114              :   autoTopUpSetupUrl
     115              : }: EventOf<'payment_landed'>): AddCreditsState {
     116            0 :   if (activation?.status === 'on') {
     117            0 :     return { step: 'auto_top_up_on', settings: activation.settings }
     118              :   }
     119            0 :   if (activation?.status === 'failed') {
     120            0 :     return autoTopUpSetupUrl === undefined
     121              :       ? { step: 'auto_top_up_failed' }
     122              :       : { step: 'auto_top_up_failed', setupUrl: autoTopUpSetupUrl }
     123              :   }
     124            0 :   return autoTopUpSetupUrl === undefined
     125              :     ? { step: 'landed' }
     126              :     : { step: 'auto_top_up_offer', setupUrl: autoTopUpSetupUrl }
     127              : }
     128              : 
     129         2356 : const TRANSITIONS: { readonly [T in AddCreditsEvent['type']]: Transition<T> } =
     130              :   {
     131              :     preset_picked: (state, event) =>
     132              :       state.step === 'amount'
     133              :         ? { step: 'amount', amount: { kind: 'preset', cents: event.cents } }
     134              :         : state,
     135              :     amount_typed: (state, event) =>
     136              :       state.step === 'amount'
     137              :         ? { step: 'amount', amount: { kind: 'custom', input: event.input } }
     138              :         : state,
     139              :     checkout_requested: (state, event) =>
     140              :       state.step === 'amount' ||
     141              :       state.step === 'checkout_opened' ||
     142              :       state.step === 'checkout_blocked'
     143              :         ? {
     144              :             step: 'opening',
     145              :             amount: state.amount,
     146              :             amountCents: event.amountCents,
     147              :             origin:
     148              :               state.step === 'checkout_blocked' ? state.origin : state.step,
     149              :             tabReserved: event.tabReserved
     150              :           }
     151              :         : state,
     152              :     tab_blocked: (state, event) =>
     153              :       state.step === 'amount' ||
     154              :       state.step === 'checkout_opened' ||
     155              :       state.step === 'checkout_blocked'
     156              :         ? {
     157              :             step: 'checkout_blocked',
     158              :             amount: state.amount,
     159              :             amountCents: event.amountCents,
     160              :             origin:
     161              :               state.step === 'checkout_blocked' ? state.origin : state.step
     162              :           }
     163              :         : state,
     164              :     checkout_created: (state, event) => {
     165            1 :       if (state.step !== 'opening') return state
     166            1 :       const { amount, amountCents } = state
     167            1 :       return state.tabReserved
     168              :         ? { step: 'checkout_opened', amount, amountCents }
     169              :         : {
     170              :             step: 'checkout_blocked',
     171              :             amount,
     172              :             amountCents,
     173              :             origin: state.origin,
     174              :             checkoutUrl: event.checkoutUrl
     175              :           }
     176              :     },
     177              :     checkout_refused: (state, event) =>
     178              :       state.step === 'opening' ? backToOrigin(state, event.refusal) : state,
     179              :     checkout_abandoned: (state) =>
     180              :       state.step === 'opening' ? backToOrigin(state, undefined) : state,
     181              :     blocked_link_followed: (state) =>
     182              :       state.step === 'checkout_blocked'
     183              :         ? {
     184              :             step: 'checkout_opened',
     185              :             amount: state.amount,
     186              :             amountCents: state.amountCents
     187              :           }
     188              :         : state,
     189              :     payment_landed: (state, event) =>
     190              :       LANDABLE_STEPS.has(state.step) ? landedState(event) : state
     191              :   }
     192              : 
     193            5 : export function reduceAddCredits(
     194              :   state: AddCreditsState,
     195              :   event: AddCreditsEvent
     196              : ): AddCreditsState {
     197            5 :   switch (event.type) {
     198              :     case 'preset_picked':
     199            1 :       return TRANSITIONS.preset_picked(state, event)
     200              :     case 'amount_typed':
     201            0 :       return TRANSITIONS.amount_typed(state, event)
     202              :     case 'checkout_requested':
     203            2 :       return TRANSITIONS.checkout_requested(state, event)
     204              :     case 'checkout_created':
     205            1 :       return TRANSITIONS.checkout_created(state, event)
     206              :     case 'tab_blocked':
     207            0 :       return TRANSITIONS.tab_blocked(state, event)
     208              :     case 'checkout_refused':
     209            1 :       return TRANSITIONS.checkout_refused(state, event)
     210              :     case 'checkout_abandoned':
     211            0 :       return TRANSITIONS.checkout_abandoned(state, event)
     212              :     case 'blocked_link_followed':
     213            0 :       return TRANSITIONS.blocked_link_followed(state, event)
     214              :     case 'payment_landed':
     215            0 :       return TRANSITIONS.payment_landed(state, event)
     216              :   }
     217              : }
     218              : 
     219              : /** A top-up the server reports as still open when the dialog renders. */
     220              : export type AddCreditsPendingTopup =
     221              :   | { readonly kind: 'processing'; readonly amountCents?: number }
     222              :   | {
     223              :       readonly kind: 'needs_verification'
     224              :       readonly amountCents?: number
     225              :       readonly verifyUrl: string
     226              :     }
     227              :   | { readonly kind: 'not_verified'; readonly amountCents?: number }
     228              : 
     229              : /** The served facts the dialog renders as received. */
     230              : export interface AddCreditsFacts {
     231              :   readonly bounds: AddCreditsBounds
     232              :   /** Served `can_top_up`; false while the dialog is open shows a refusal and stops Continue. */
     233              :   readonly canTopUp: boolean
     234              :   readonly canTopUpRefusal?: AddCreditsRefusal
     235              :   readonly pendingTopup?: AddCreditsPendingTopup
     236              : }
     237              : 
     238              : export type AddCreditsNotice =
     239              :   | { readonly kind: 'refusal'; readonly refusal: AddCreditsRefusal }
     240              :   | AddCreditsPendingTopup
     241              : 
     242              : export interface AddCreditsAmountView {
     243              :   readonly step: 'amount' | 'opening'
     244              :   readonly amount: AddCreditsAmount
     245              :   readonly check: AddCreditsAmountCheck
     246              :   /** The amount the footer and the quote describe; undefined while none is valid. */
     247              :   readonly amountCents: number | undefined
     248              :   readonly notice: AddCreditsNotice | undefined
     249              :   readonly locked: boolean
     250              :   readonly canContinue: boolean
     251              :   readonly opening: boolean
     252              : }
     253              : 
     254         2356 : const BLOCKING_REFUSALS: ReadonlySet<AddCreditsRefusal['kind']> = new Set([
     255              :   'rate_limited',
     256              :   'purchases_paused',
     257              :   'not_allowed'
     258              : ])
     259              : 
     260              : /** A refusal that holds Continue and Reopen until the server lifts it. */
     261            7 : export function addCreditsRefusalBlocks(
     262              :   refusal: AddCreditsRefusal | undefined
     263              : ): boolean {
     264            7 :   return refusal !== undefined && BLOCKING_REFUSALS.has(refusal.kind)
     265              : }
     266              : 
     267              : /** What the amount step renders, from the dialog state and the served facts. */
     268            8 : export function viewAddCreditsAmount(
     269              :   state: Extract<AddCreditsState, { readonly step: 'amount' | 'opening' }>,
     270              :   facts: AddCreditsFacts
     271              : ): AddCreditsAmountView {
     272            8 :   const check = checkAddCreditsAmount(state.amount, facts.bounds)
     273            8 :   const pending = facts.pendingTopup
     274            8 :   const blockedByPending =
     275              :     pending !== undefined && pending.kind !== 'processing'
     276            8 :   const refusal = facts.canTopUp
     277              :     ? state.step === 'amount'
     278              :       ? state.refusal
     279              :       : undefined
     280              :     : (facts.canTopUpRefusal ?? { kind: 'not_allowed' })
     281            8 :   const notice: AddCreditsNotice | undefined = blockedByPending
     282              :     ? pending
     283              :     : refusal
     284              :       ? { kind: 'refusal', refusal }
     285              :       : pending
     286            8 :   const opening = state.step === 'opening'
     287            8 :   const locked = opening || blockedByPending
     288            8 :   return {
     289              :     step: state.step,
     290              :     amount: state.amount,
     291              :     check,
     292              :     amountCents: blockedByPending
     293              :       ? pending.amountCents
     294              :       : check.status === 'valid'
     295              :         ? check.cents
     296              :         : undefined,
     297              :     notice,
     298              :     locked,
     299              :     opening,
     300              :     canContinue:
     301              :       facts.canTopUp &&
     302              :       !locked &&
     303              :       check.status === 'valid' &&
     304              :       !addCreditsRefusalBlocks(refusal)
     305              :   }
     306              : }
        

Generated by: LCOV version 2.0-1