LCOV - code coverage report
Current view: top level - packages/account-ui/src/billing/tab - autoTopUpMemberView.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 7.4 % 27 2
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { formatPlanDate } from '@comfyorg/account-core/billing'
       2              : import type { CapabilitiesSnapshot } from '@comfyorg/account-core/billing'
       3              : 
       4              : import type { AutoTopUpStatusLabel } from './autoTopUpCardView'
       5              : 
       6              : /** The member's auto top-up view as served on the capabilities read. */
       7              : export type AutoTopUpMemberView = NonNullable<
       8              :   CapabilitiesSnapshot['autoTopUpMemberView']
       9              : >
      10              : 
      11              : /** Host-translated strings for the auto top-up member block; `body` is the line when no status is served. */
      12              : export interface AutoTopUpMemberBlockCopy {
      13              :   readonly title: string
      14              :   readonly body: string
      15              :   readonly status: Readonly<Record<AutoTopUpStatusLabel, string>>
      16              :   readonly on: (target: string, threshold: string) => string
      17              :   readonly limitReached: (date: string) => string
      18              :   readonly limitReachedNoDate: string
      19              :   readonly maximumReached: (date: string) => string
      20              :   readonly maximumReachedNoDate: string
      21              :   readonly failed: string
      22              :   readonly turnedOff: string
      23              :   readonly onHold: string
      24              : }
      25              : 
      26              : export interface AutoTopUpMemberBlockView {
      27              :   status: AutoTopUpStatusLabel
      28              :   line: string
      29              : }
      30              : 
      31              : type ServedStatus = AutoTopUpMemberView['status']
      32              : 
      33            5 : const STATUS: Record<ServedStatus, AutoTopUpStatusLabel> = {
      34              :   off: 'off',
      35              :   on: 'on',
      36              :   on_hold: 'onHold',
      37              :   paused_by_budget: 'limitReached',
      38              :   failed: 'failed'
      39              : }
      40              : 
      41            0 : function isServedStatus(status: string): status is ServedStatus {
      42            0 :   return Object.hasOwn(STATUS, status)
      43              : }
      44              : 
      45            0 : function statusOf(view: AutoTopUpMemberView): AutoTopUpStatusLabel | null {
      46            0 :   if (!isServedStatus(view.status)) return null
      47            0 :   if (
      48              :     view.status === 'failed' &&
      49              :     view.disabled_reason === 'consecutive_declines'
      50              :   ) {
      51            0 :     return 'turnedOff'
      52              :   }
      53            0 :   if (view.status === 'paused_by_budget' && view.monthly_budget_is_default) {
      54            0 :     return 'maximumReached'
      55              :   }
      56            0 :   return STATUS[view.status]
      57              : }
      58              : 
      59              : interface LineContext {
      60              :   view: AutoTopUpMemberView
      61              :   copy: AutoTopUpMemberBlockCopy
      62              :   locale: string
      63              : }
      64              : 
      65            0 : function budgetLine(
      66              :   { view, locale }: LineContext,
      67              :   dated: (date: string) => string,
      68              :   undated: string
      69              : ): string {
      70            0 :   const resetsAt = view.cycle_resets_at
      71            0 :   return resetsAt === null
      72              :     ? undated
      73              :     : dated(formatPlanDate(resetsAt, locale, 'monthly'))
      74              : }
      75              : 
      76            0 : function isWholeCredits(value: unknown): value is number {
      77            0 :   return Number.isSafeInteger(value)
      78              : }
      79              : 
      80            0 : function ruleLine({ view, copy, locale }: LineContext): string | null {
      81            0 :   const { target_credits: target, threshold_credits: threshold } = view
      82            0 :   if (!isWholeCredits(target) || !isWholeCredits(threshold)) return null
      83            0 :   const credits = new Intl.NumberFormat(locale, { maximumFractionDigits: 0 })
      84            0 :   return copy.on(credits.format(target), credits.format(threshold))
      85              : }
      86              : 
      87            5 : const LINES: Record<
      88              :   AutoTopUpStatusLabel,
      89              :   (context: LineContext) => string | null
      90              : > = {
      91              :   on: ruleLine,
      92              :   off: ({ copy }) => copy.body,
      93              :   limitReached: (context) =>
      94              :     budgetLine(
      95              :       context,
      96              :       context.copy.limitReached,
      97              :       context.copy.limitReachedNoDate
      98              :     ),
      99              :   maximumReached: (context) =>
     100              :     budgetLine(
     101              :       context,
     102              :       context.copy.maximumReached,
     103              :       context.copy.maximumReachedNoDate
     104              :     ),
     105              :   failed: ({ copy }) => copy.failed,
     106              :   turnedOff: ({ copy }) => copy.turnedOff,
     107              :   onHold: ({ copy }) => copy.onHold
     108              : }
     109              : 
     110              : /** The status and line for a served member view; null keeps the status-less block. */
     111            0 : export function autoTopUpMemberView(
     112              :   view: AutoTopUpMemberView | null | undefined,
     113              :   copy: AutoTopUpMemberBlockCopy,
     114              :   locale: string
     115              : ): AutoTopUpMemberBlockView | null {
     116            0 :   if (!view) return null
     117            0 :   const status = statusOf(view)
     118            0 :   if (status === null) return null
     119            0 :   const line = LINES[status]({ view, copy, locale })
     120            0 :   return line === null ? null : { status, line }
     121              : }
        

Generated by: LCOV version 2.0-1