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

            Line data    Source code
       1              : import { matchesServerCode } from '#/core/billing/billingContracts'
       2              : import type { CapabilityDenialReason } from '#/core/billing/capabilityDenials'
       3              : import type { HostedTopupCheckoutFailure } from '#/core/billing/topup'
       4              : 
       5              : /** Why the server would not open a checkout; each kind has its own notice copy. */
       6              : export type AddCreditsRefusal =
       7              :   | { readonly kind: 'rate_limited'; readonly retryAfterSeconds?: number }
       8              :   | { readonly kind: 'purchases_paused' }
       9              :   | { readonly kind: 'not_allowed' }
      10              :   | { readonly kind: 'checkout_failed'; readonly serverMessage?: string }
      11              : 
      12              : export type AddCreditsWait =
      13              :   | { readonly unit: 'later' }
      14              :   | { readonly unit: 'minute' | 'hour' | 'day'; readonly count: number }
      15              : 
      16         2356 : const RATE_LIMITED = 'TOPUP_RATE_LIMITED'
      17         2356 : const PURCHASES_PAUSED = 'TOPUP_PURCHASES_PAUSED'
      18         2356 : const PURCHASES_PAUSED_DENIAL: CapabilityDenialReason = 'topup_purchases_paused'
      19              : 
      20              : /** Maps a failed checkout to its notice; codes the dialog does not know read as "couldn't open checkout". */
      21            0 : export function addCreditsRefusalFor(
      22              :   failure: HostedTopupCheckoutFailure
      23              : ): AddCreditsRefusal {
      24            0 :   if ('serverCode' in failure && matchesServerCode(failure, RATE_LIMITED)) {
      25            0 :     return failure.retryAfterSeconds === undefined
      26              :       ? { kind: 'rate_limited' }
      27              :       : { kind: 'rate_limited', retryAfterSeconds: failure.retryAfterSeconds }
      28              :   }
      29            0 :   if ('serverCode' in failure && matchesServerCode(failure, PURCHASES_PAUSED)) {
      30            0 :     return { kind: 'purchases_paused' }
      31              :   }
      32            0 :   if (failure.code === 'ACCESS_DENIED') {
      33            0 :     return addCreditsRefusalForDenial(
      34              :       'denial' in failure ? failure.denial : undefined
      35              :     )
      36              :   }
      37            0 :   return { kind: 'checkout_failed' }
      38              : }
      39              : 
      40              : /** The served `can_top_up` refusal the dialog shows while it is open. */
      41            8 : export function addCreditsRefusalForDenial(
      42              :   denial: CapabilityDenialReason | undefined
      43              : ): AddCreditsRefusal {
      44            8 :   return denial === PURCHASES_PAUSED_DENIAL
      45              :     ? { kind: 'purchases_paused' }
      46              :     : { kind: 'not_allowed' }
      47              : }
      48              : 
      49         2356 : const MINUTE = 60
      50         2356 : const HOUR = 60 * MINUTE
      51         2356 : const DAY = 24 * HOUR
      52              : 
      53              : /** `Retry-After` seconds rounded up to the coarsest whole unit that still reads naturally. */
      54            0 : export function addCreditsWait(seconds: number | undefined): AddCreditsWait {
      55            0 :   if (seconds === undefined || seconds <= 0) return { unit: 'later' }
      56            0 :   if (seconds < HOUR) {
      57            0 :     return { unit: 'minute', count: Math.max(1, Math.ceil(seconds / MINUTE)) }
      58              :   }
      59            0 :   if (seconds < DAY) return { unit: 'hour', count: Math.ceil(seconds / HOUR) }
      60            0 :   return { unit: 'day', count: Math.ceil(seconds / DAY) }
      61              : }
        

Generated by: LCOV version 2.0-1