LCOV - code coverage report
Current view: top level - src/platform/billing/utils - pendingSubscriptionCheckout.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 8.9 % 45 4
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { TierKey } from '@/platform/billing/subscription/constants/tierPricing'
       2              : import type { BillingCycle } from '@/platform/billing/subscription/utils/subscriptionTierRank'
       3              : import type { BillingOpStatusResponse } from '@comfyorg/billing-core'
       4              : 
       5         2356 : const STORAGE_KEY = 'comfy:pending-subscription-checkout'
       6         2356 : const MAX_AGE_MS = 24 * 60 * 60_000
       7              : 
       8              : type CheckoutTierKey = Exclude<TierKey, 'free' | 'founder'>
       9              : 
      10              : type PendingSubscriptionSelection =
      11              :   | {
      12              :       planMode: 'personal'
      13              :       tierKey: CheckoutTierKey
      14              :       billingCycle: BillingCycle
      15              :     }
      16              :   | {
      17              :       planMode: 'team'
      18              :       teamCreditStopId: string
      19              :       billingCycle: BillingCycle
      20              :     }
      21              : 
      22              : export interface PendingSubscriptionCheckout {
      23              :   operationId: string
      24              :   workspaceId: string
      25              :   ownerUid: string
      26              :   selection: PendingSubscriptionSelection
      27              :   attemptedAt: number
      28              : }
      29              : 
      30         2356 : const CHECKOUT_TIERS: readonly CheckoutTierKey[] = [
      31              :   'standard',
      32              :   'creator',
      33              :   'pro'
      34              : ]
      35              : 
      36            0 : function isNonEmptyString(value: unknown): value is string {
      37            0 :   return typeof value === 'string' && value.length > 0
      38              : }
      39              : 
      40            0 : function isBillingCycle(value: unknown): value is BillingCycle {
      41            0 :   return value === 'monthly' || value === 'yearly'
      42              : }
      43              : 
      44            0 : function isSelection(value: unknown): value is PendingSubscriptionSelection {
      45            0 :   if (!value || typeof value !== 'object') return false
      46            0 :   if (!('planMode' in value) || !('billingCycle' in value)) return false
      47            0 :   if (!isBillingCycle(value.billingCycle)) return false
      48              : 
      49            0 :   if (value.planMode === 'personal') {
      50            0 :     return (
      51              :       'tierKey' in value &&
      52              :       CHECKOUT_TIERS.some((tierKey) => tierKey === value.tierKey)
      53              :     )
      54              :   }
      55              : 
      56            0 :   return (
      57              :     value.planMode === 'team' &&
      58              :     'teamCreditStopId' in value &&
      59              :     isNonEmptyString(value.teamCreditStopId)
      60              :   )
      61              : }
      62              : 
      63            0 : function isPendingCheckout(
      64              :   value: unknown,
      65              :   now: number
      66              : ): value is PendingSubscriptionCheckout {
      67            0 :   if (!value || typeof value !== 'object') return false
      68            0 :   if (
      69              :     !('operationId' in value) ||
      70              :     !isNonEmptyString(value.operationId) ||
      71              :     !('workspaceId' in value) ||
      72              :     !isNonEmptyString(value.workspaceId) ||
      73              :     !('ownerUid' in value) ||
      74              :     !isNonEmptyString(value.ownerUid) ||
      75              :     !('attemptedAt' in value) ||
      76              :     typeof value.attemptedAt !== 'number' ||
      77              :     !Number.isFinite(value.attemptedAt) ||
      78              :     value.attemptedAt > now ||
      79              :     now - value.attemptedAt > MAX_AGE_MS ||
      80              :     !('selection' in value)
      81              :   ) {
      82            0 :     return false
      83              :   }
      84            0 :   return isSelection(value.selection)
      85              : }
      86              : 
      87            0 : export function savePendingSubscriptionCheckout(
      88              :   checkout: PendingSubscriptionCheckout
      89              : ): void {
      90            0 :   try {
      91            0 :     sessionStorage.setItem(STORAGE_KEY, JSON.stringify(checkout))
      92              :   } catch {
      93            0 :     return
      94              :   }
      95              : }
      96              : 
      97            0 : export function getPendingSubscriptionCheckout(
      98              :   now = Date.now()
      99              : ): PendingSubscriptionCheckout | null {
     100            0 :   try {
     101            0 :     const stored = sessionStorage.getItem(STORAGE_KEY)
     102            0 :     if (!stored) return null
     103            0 :     const checkout: unknown = JSON.parse(stored)
     104            0 :     if (isPendingCheckout(checkout, now)) return checkout
     105            0 :     sessionStorage.removeItem(STORAGE_KEY)
     106              :   } catch {
     107            0 :     try {
     108            0 :       sessionStorage.removeItem(STORAGE_KEY)
     109              :     } catch {
     110            0 :       return null
     111              :     }
     112              :   }
     113            0 :   return null
     114              : }
     115              : 
     116            0 : export function clearPendingSubscriptionCheckout(operationId?: string): void {
     117            0 :   try {
     118            0 :     if (operationId) {
     119            0 :       const checkout = getPendingSubscriptionCheckout()
     120            0 :       if (checkout?.operationId !== operationId) return
     121              :     }
     122            0 :     sessionStorage.removeItem(STORAGE_KEY)
     123              :   } catch {
     124            0 :     return
     125              :   }
     126              : }
     127              : 
     128              : export type PollableOperationStatus =
     129              :   | BillingOpStatusResponse['status']
     130              :   | 'timeout'
     131              : 
     132              : // A client-side `timeout` is this tab giving up, not the server finishing: an
     133              : // operation awaiting bank authentication stays pending for hours. Dropping the
     134              : // pointer there is the defect. `reconciliation_needed` does belong here — the
     135              : // contract calls it terminal for polling, and the store stops and resolves on
     136              : // it, so retaining the pointer would re-poll a settled operation every reload.
     137         2356 : const CLEARS_PENDING_POINTER: Record<PollableOperationStatus, boolean> = {
     138              :   pending: false,
     139              :   timeout: false,
     140              :   succeeded: true,
     141              :   failed: true,
     142              :   reconciliation_needed: true
     143              : }
     144              : 
     145            0 : export function clearPendingSubscriptionCheckoutIfTerminal(
     146              :   operationId: string,
     147              :   status: PollableOperationStatus
     148              : ): void {
     149            0 :   if (!CLEARS_PENDING_POINTER[status]) return
     150            0 :   clearPendingSubscriptionCheckout(operationId)
     151              : }
        

Generated by: LCOV version 2.0-1