LCOV - code coverage report
Current view: top level - src/platform/billing/components/billingTab - usePlanLifecycleCommands.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 12.5 % 48 6
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { delay } from 'es-toolkit'
       2              : 
       3              : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
       4              : import { reportError } from '@comfyorg/telemetry/reportError'
       5              : import { billingApi } from '@/platform/billing/api/billingApi'
       6              : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
       7              : import type { PlanCommandOutcome } from '@comfyorg/account-core/billing'
       8              : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
       9              : 
      10            0 : function isCodedServerRefusal(err: unknown): err is WorkspaceApiError {
      11            0 :   return (
      12              :     err instanceof WorkspaceApiError &&
      13              :     err.code !== undefined &&
      14              :     err.status !== undefined &&
      15              :     err.status >= 400 &&
      16              :     err.status < 500
      17              :   )
      18              : }
      19              : 
      20            5 : const REVERT_POLL_INTERVAL_MS = 1500
      21            5 : const REVERT_POLL_ATTEMPTS = 20
      22              : 
      23              : type RevertSettlement = { settled: true } | { settled: false; cause: unknown }
      24              : 
      25            0 : async function readRevertOperation(
      26              :   opId: string
      27              : ): Promise<RevertSettlement | null> {
      28            0 :   try {
      29            0 :     const operation = await billingApi.getBillingOpStatus(opId)
      30            0 :     if (operation.status === 'succeeded') return { settled: true }
      31            0 :     if (operation.status === 'pending') return null
      32            0 :     return {
      33              :       settled: false,
      34              :       cause:
      35              :         operation.error_message ??
      36              :         `Scheduled change revert ended as ${operation.status}`
      37              :     }
      38              :   } catch (cause) {
      39            0 :     return { settled: false, cause }
      40              :   }
      41              : }
      42              : 
      43            0 : async function waitForRevertOperation(opId: string): Promise<RevertSettlement> {
      44            0 :   for (let attempt = 0; attempt < REVERT_POLL_ATTEMPTS; attempt++) {
      45            0 :     await delay(REVERT_POLL_INTERVAL_MS)
      46            0 :     const settlement = await readRevertOperation(opId)
      47            0 :     if (settlement) return settlement
      48              :   }
      49            0 :   return { settled: false, cause: 'Scheduled change revert did not settle' }
      50              : }
      51              : 
      52            0 : async function requestRevert(): Promise<RevertSettlement> {
      53            0 :   const response = await billingApi.revertScheduledChange()
      54            0 :   return response.status === 'pending'
      55              :     ? waitForRevertOperation(response.billing_op_id)
      56              :     : { settled: true }
      57              : }
      58              : 
      59            0 : function refusalOf(err: unknown, errorType: string): PlanCommandOutcome {
      60            0 :   if (isCodedServerRefusal(err)) {
      61            0 :     return { ok: false, serverMessage: err.message }
      62              :   }
      63            0 :   reportError(err, { surface: 'billing', errorType })
      64            0 :   return { ok: false, serverMessage: null }
      65              : }
      66              : 
      67              : /** Revert a scheduled plan change or resume an ending plan, returning the server's refusal as data. */
      68            5 : export function usePlanLifecycleCommands() {
      69            5 :   const billing = useBillingContext()
      70            5 :   const capabilities = useBillingCapabilities()
      71              : 
      72            0 :   async function resync() {
      73            0 :     await Promise.allSettled([billing.fetchStatus(), capabilities.refresh()])
      74              :   }
      75              : 
      76            0 :   async function revertScheduledChange(): Promise<PlanCommandOutcome> {
      77            0 :     const errorType = 'failure_reverting_scheduled_plan_change'
      78            0 :     try {
      79            0 :       const settlement = await requestRevert()
      80            0 :       if (!settlement.settled) {
      81            0 :         reportError(settlement.cause, { surface: 'billing', errorType })
      82            0 :         await resync()
      83            0 :         return { ok: false, serverMessage: null }
      84              :       }
      85              :     } catch (err) {
      86            0 :       return refusalOf(err, errorType)
      87              :     }
      88            0 :     await resync()
      89            0 :     return { ok: true }
      90              :   }
      91              : 
      92            0 :   async function resumePlan(): Promise<PlanCommandOutcome> {
      93            0 :     try {
      94            0 :       await billing.resubscribe({ source: 'settings_billing_panel' })
      95              :     } catch (err) {
      96            0 :       return refusalOf(err, 'failure_resuming_subscription')
      97              :     }
      98            0 :     await resync()
      99            0 :     return { ok: true }
     100              :   }
     101              : 
     102            5 :   return { revertScheduledChange, resumePlan }
     103              : }
        

Generated by: LCOV version 2.0-1