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

            Line data    Source code
       1              : import { topupCheckoutBannerKey } from '@comfyorg/account-core/billing'
       2              : import type {
       3              :   PlanCheckoutBanner,
       4              :   TopupCheckoutBanner
       5              : } from '@comfyorg/account-core/billing'
       6              : 
       7              : export type BillingBannerAction =
       8              :   | 'addCredits'
       9              :   | 'updatePayment'
      10              :   | 'verifyPayment'
      11              :   | 'contactSupport'
      12              :   | 'resumePlan'
      13              :   | 'choosePlan'
      14              :   | 'contactSales'
      15              : 
      16              : export interface BillingBannerText {
      17              :   readonly title: string
      18              :   readonly body: string
      19              : }
      20              : 
      21              : /** One Billing tab banner as the host resolved it: text, offered action, dismiss key. */
      22              : export type BillingBannerView = BillingBannerText &
      23              :   (
      24              :     | {
      25              :         readonly kind: 'paused'
      26              :         readonly muted: boolean
      27              :         readonly action: 'updatePayment' | null
      28              :       }
      29              :     | {
      30              :         readonly kind: 'planCheckout'
      31              :         readonly muted: boolean
      32              :         readonly action: 'verifyPayment' | null
      33              :         readonly href: string | null
      34              :         readonly dismissKey: string | null
      35              :       }
      36              :     | {
      37              :         readonly kind: 'topupCheckout'
      38              :         readonly action: 'addCredits' | null
      39              :         readonly dismissKey: string
      40              :       }
      41              :     | {
      42              :         readonly kind: 'parked'
      43              :         readonly action: 'contactSupport'
      44              :         readonly href: string
      45              :       }
      46              :     | {
      47              :         readonly kind: 'planStatus'
      48              :         readonly action: 'resumePlan' | 'choosePlan' | 'contactSales' | null
      49              :         readonly href: string | null
      50              :       }
      51              :     | {
      52              :         readonly kind: 'autoTopUp'
      53              :         readonly action: 'updatePayment' | null
      54              :         readonly href: string | null
      55              :       }
      56              :     | {
      57              :         readonly kind: 'credit'
      58              :         readonly action: 'addCredits' | 'updatePayment' | null
      59              :         readonly dismissKey: string
      60              :       }
      61              :   )
      62              : 
      63              : export type BillingBannerKind = BillingBannerView['kind']
      64              : 
      65              : /** Host-translated strings for the Billing tab banners. */
      66              : export interface BillingBannerCopy {
      67              :   readonly dismiss: string
      68              :   readonly actions: Readonly<Record<BillingBannerAction, string>>
      69              :   readonly planCheckout: Readonly<
      70              :     Record<PlanCheckoutBanner['kind'], BillingBannerText>
      71              :   >
      72              :   readonly topupCheckout: {
      73              :     readonly failedTitle: (paymentAmountCents: number | null) => string
      74              :     readonly failedBody: string
      75              :     readonly creditsApplying: BillingBannerText
      76              :     readonly processing: BillingBannerText
      77              :   }
      78              : }
      79              : 
      80              : interface BillingBannerLook {
      81              :   muted: boolean
      82              :   roomy: boolean
      83              :   windowBreakpoint: boolean
      84              :   action: BillingBannerAction | null
      85              :   href: string | null
      86              :   dismissKey: string | null
      87              : }
      88              : 
      89              : /** The banner view for a served plan checkout state. */
      90            0 : export function planCheckoutBannerView(
      91              :   banner: PlanCheckoutBanner,
      92              :   copy: BillingBannerCopy
      93              : ): BillingBannerView {
      94            0 :   const text = copy.planCheckout[banner.kind]
      95            0 :   if (banner.kind === 'verifyPayment') {
      96            0 :     return {
      97              :       kind: 'planCheckout',
      98              :       ...text,
      99              :       muted: false,
     100              :       action: banner.actionUrl ? 'verifyPayment' : null,
     101              :       href: banner.actionUrl,
     102              :       dismissKey: null
     103              :     }
     104              :   }
     105            0 :   return {
     106              :     kind: 'planCheckout',
     107              :     ...text,
     108              :     muted: true,
     109              :     action: null,
     110              :     href: null,
     111              :     dismissKey: `${banner.opId}:${banner.kind}`
     112              :   }
     113              : }
     114              : 
     115              : /** The banner view for a served top-up checkout; Add credits only when the caller can top up. */
     116            0 : export function topupCheckoutBannerView(
     117              :   banner: TopupCheckoutBanner,
     118              :   copy: BillingBannerCopy,
     119              :   canTopUp: boolean
     120              : ): BillingBannerView {
     121            0 :   const dismissKey = topupCheckoutBannerKey(banner)
     122            0 :   switch (banner.kind) {
     123              :     case 'failed':
     124            0 :       return {
     125              :         kind: 'topupCheckout',
     126              :         title: copy.topupCheckout.failedTitle(banner.paymentAmountCents),
     127              :         body: copy.topupCheckout.failedBody,
     128              :         action: canTopUp ? 'addCredits' : null,
     129              :         dismissKey
     130              :       }
     131              :     case 'creditsApplying':
     132              :     case 'processing':
     133            0 :       return {
     134              :         kind: 'topupCheckout',
     135              :         ...copy.topupCheckout[banner.kind],
     136              :         action: null,
     137              :         dismissKey
     138              :       }
     139              :   }
     140              : }
     141              : 
     142              : /** How a banner view renders: tone, shell size, action and dismissal. */
     143            0 : export function billingBannerLook(view: BillingBannerView): BillingBannerLook {
     144            0 :   switch (view.kind) {
     145              :     case 'paused':
     146            0 :       return { ...look(view.muted, false, view.action), windowBreakpoint: true }
     147              :     case 'planCheckout':
     148            0 :       return {
     149              :         ...look(view.muted, false, view.action, view.href, view.dismissKey),
     150              :         windowBreakpoint: true
     151              :       }
     152              :     case 'topupCheckout':
     153            0 :       return look(true, true, view.action, null, view.dismissKey)
     154              :     case 'parked':
     155            0 :       return {
     156              :         ...look(false, true, view.action, view.href),
     157              :         windowBreakpoint: true
     158              :       }
     159              :     case 'planStatus':
     160            0 :       return look(true, true, view.action, view.href)
     161              :     case 'autoTopUp':
     162            0 :       return look(false, false, view.action, view.href)
     163              :     case 'credit':
     164            0 :       return look(false, false, view.action, null, view.dismissKey)
     165              :   }
     166              : }
     167              : 
     168            0 : function look(
     169              :   muted: boolean,
     170              :   roomy: boolean,
     171              :   action: BillingBannerAction | null,
     172              :   href: string | null = null,
     173              :   dismissKey: string | null = null
     174              : ): BillingBannerLook {
     175            0 :   return { muted, roomy, windowBreakpoint: false, action, href, dismissKey }
     176              : }
     177              : 
     178              : const STACK_ORDER: readonly BillingBannerKind[] = [
     179              :   'paused',
     180              :   'planCheckout',
     181              :   'topupCheckout',
     182              :   'parked',
     183              :   'planStatus',
     184              :   'autoTopUp',
     185              :   'credit'
     186              : ]
     187              : 
     188            5 : const CREDIT_SUPPRESSORS: ReadonlySet<BillingBannerKind> = new Set([
     189              :   'paused',
     190              :   'planCheckout',
     191              :   'topupCheckout'
     192              : ])
     193              : 
     194              : const CREDIT_KINDS: ReadonlySet<BillingBannerKind> = new Set([
     195              :   'autoTopUp',
     196              :   'credit'
     197              : ])
     198              : 
     199              : /** Whether a shown paused or checkout banner hides the credit banners. */
     200            5 : export function suppressesCreditBanners(
     201              :   kinds: readonly BillingBannerKind[]
     202              : ): boolean {
     203            5 :   return kinds.some((kind) => CREDIT_SUPPRESSORS.has(kind))
     204              : }
     205              : 
     206              : /** The Billing tab banner stack: fixed order, credit banners hidden behind a paused or checkout banner. */
     207              : export function selectBillingBanners(
     208              :   views: readonly BillingBannerView[]
     209              : ): BillingBannerView[] {
     210              :   const suppressCredit = suppressesCreditBanners(views.map(({ kind }) => kind))
     211              :   return views
     212              :     .filter(({ kind }) => !(suppressCredit && CREDIT_KINDS.has(kind)))
     213              :     .toSorted(
     214              :       (a, b) => STACK_ORDER.indexOf(a.kind) - STACK_ORDER.indexOf(b.kind)
     215              :     )
     216              : }
        

Generated by: LCOV version 2.0-1