LCOV - code coverage report
Current view: top level - packages/account-core/src/core/billing - credits.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 33.3 % 6 2
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : /**
       2              :  * The credits read: a workspace's balance, cached per scope.
       3              :  *
       4              :  * Balances are reported in micros and kept that way. Converting to a display
       5              :  * unit here would bake one currency's minor-unit assumption into the core and
       6              :  * lose precision before the host ever sees the number; formatting belongs to
       7              :  * the view layer, which has the locale.
       8              :  */
       9              : import { zBillingBalanceResponse } from '@comfyorg/ingest-types/zod'
      10              : import type { z } from 'zod'
      11              : 
      12              : import type { BillingResult, BillingTransport } from './billingContracts'
      13              : import type { BillingScope, BillingScopeSource } from './billingScope'
      14              : import { createScopedReader } from './scopedReader'
      15              : import { wireCents } from './wireCents'
      16              : 
      17         2356 : export const CREDITS_ROUTE = '/billing/balance'
      18              : 
      19         2356 : const BillingBalanceSchema = zBillingBalanceResponse.extend({
      20              :   suggested_topup_amount_cents: wireCents.nullish()
      21              : })
      22              : 
      23              : export type BillingBalance = z.infer<typeof BillingBalanceSchema>
      24              : 
      25              : export type CreditsScope = BillingScope
      26              : 
      27              : export interface CreditsSnapshot {
      28              :   readonly balance: BillingBalance
      29              :   readonly scope: CreditsScope
      30              :   /** ms since epoch at which this read settled. */
      31              :   readonly readAt: number
      32              : }
      33              : 
      34              : export interface CreditsReadOptions {
      35              :   readonly signal?: AbortSignal
      36              : }
      37              : 
      38              : export interface CreditsReader {
      39              :   /**
      40              :    * Always requests. A balance has no server-declared lifetime and moves on
      41              :    * every run, so there is no interval over which serving a cached one is
      42              :    * correct; the cache exists to survive a failure, not to skip a read.
      43              :    * Concurrent callers for one scope still share a single request.
      44              :    */
      45              :   read: (
      46              :     options?: CreditsReadOptions
      47              :   ) => Promise<BillingResult<CreditsSnapshot>>
      48              :   /** The last balance published for the current scope. */
      49              :   getSnapshot: () => CreditsSnapshot | undefined
      50              :   dispose: () => void
      51              : }
      52              : 
      53              : export interface CreditsReaderOptions {
      54              :   readonly transport: BillingTransport
      55              :   /** Where the core learns which user, workspace, and role it runs as. */
      56              :   readonly scopeSource: BillingScopeSource
      57              :   readonly now?: () => number
      58              : }
      59              : 
      60            0 : export function createCreditsReader(
      61              :   options: CreditsReaderOptions
      62              : ): CreditsReader {
      63            0 :   const { transport, scopeSource, now = Date.now } = options
      64              : 
      65            0 :   const reader = createScopedReader<
      66              :     BillingBalance,
      67              :     CreditsSnapshot,
      68              :     CreditsReadOptions
      69              :   >({
      70              :     transport,
      71              :     scopeSource,
      72              :     route: CREDITS_ROUTE,
      73              :     parse: (body) => BillingBalanceSchema.safeParse(body),
      74              :     project: ({ data }, scope) => ({
      75              :       status: 'ok',
      76              :       value: { balance: data, scope, readAt: now() }
      77              :     })
      78              :   })
      79              : 
      80            0 :   return {
      81              :     read: reader.read,
      82              :     getSnapshot: reader.getSnapshot,
      83              :     dispose: reader.dispose
      84              :   }
      85              : }
        

Generated by: LCOV version 2.0-1