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

            Line data    Source code
       1              : /**
       2              :  * The billing events read: one page of the workspace's history feed, the
       3              :  * charges, credits and adjustments behind the usage log.
       4              :  *
       5              :  * It is the only billing read whose request varies per call, so the page it
       6              :  * asks for is part of what identifies the read: two callers on different
       7              :  * pages each get their own answer rather than sharing the first one's. The
       8              :  * snapshot reports the page the server says it served, not the one that was
       9              :  * asked for, because the endpoint clamps a page past the end.
      10              :  */
      11              : import { zGetBillingEventsResponse } from '@comfyorg/ingest-types/zod'
      12              : import type { z } from 'zod'
      13              : 
      14              : import type { BillingResult, BillingTransport } from './billingContracts'
      15              : import type { BillingScope, BillingScopeSource } from './billingScope'
      16              : import { createScopedReader } from './scopedReader'
      17              : 
      18         2356 : export const BILLING_EVENTS_ROUTE = '/billing/events'
      19              : 
      20              : export type BillingEventsData = z.infer<typeof zGetBillingEventsResponse>
      21              : export type BillingEvent = BillingEventsData['events'][number]
      22              : 
      23              : export type BillingEventsScope = BillingScope
      24              : 
      25              : export interface BillingEventsSnapshot {
      26              :   readonly events: readonly BillingEvent[]
      27              :   /** 1-indexed, as the endpoint counts. */
      28              :   readonly page: number
      29              :   readonly limit: number
      30              :   readonly total: number
      31              :   readonly totalPages: number
      32              :   readonly scope: BillingEventsScope
      33              :   readonly readAt: number
      34              : }
      35              : 
      36              : export interface BillingEventsReadOptions {
      37              :   readonly signal?: AbortSignal
      38              :   /** 1-indexed; the endpoint's own default applies when omitted. */
      39              :   readonly page?: number
      40              :   readonly limit?: number
      41              : }
      42              : 
      43              : export interface BillingEventsReader {
      44              :   read: (
      45              :     options?: BillingEventsReadOptions
      46              :   ) => Promise<BillingResult<BillingEventsSnapshot>>
      47              :   /** The last page read for the current scope; it carries which page it is. */
      48              :   getSnapshot: () => BillingEventsSnapshot | undefined
      49              :   dispose: () => void
      50              : }
      51              : 
      52              : export interface BillingEventsReaderOptions {
      53              :   readonly transport: BillingTransport
      54              :   /** Where the core learns which user, workspace, and role it runs as. */
      55              :   readonly scopeSource: BillingScopeSource
      56              :   readonly now?: () => number
      57              : }
      58              : 
      59              : /** Stable key order, so the same page asked for twice is the same request. */
      60            0 : function billingEventsRoute(
      61              :   options: BillingEventsReadOptions | undefined
      62              : ): string {
      63            0 :   const query = new URLSearchParams()
      64            0 :   if (options?.page !== undefined) query.set('page', String(options.page))
      65            0 :   if (options?.limit !== undefined) query.set('limit', String(options.limit))
      66            0 :   const search = query.toString()
      67            0 :   return search === ''
      68              :     ? BILLING_EVENTS_ROUTE
      69              :     : `${BILLING_EVENTS_ROUTE}?${search}`
      70              : }
      71              : 
      72            0 : export function createBillingEventsReader(
      73              :   options: BillingEventsReaderOptions
      74              : ): BillingEventsReader {
      75            0 :   const { transport, scopeSource, now = Date.now } = options
      76              : 
      77            0 :   const reader = createScopedReader<
      78              :     BillingEventsData,
      79              :     BillingEventsSnapshot,
      80              :     BillingEventsReadOptions
      81              :   >({
      82              :     transport,
      83              :     scopeSource,
      84              :     route: billingEventsRoute,
      85              :     parse: (body) => zGetBillingEventsResponse.safeParse(body),
      86              :     project: ({ data }, scope) => ({
      87              :       status: 'ok',
      88              :       value: { ...data, scope, readAt: now() }
      89              :     })
      90              :   })
      91              : 
      92            0 :   return {
      93              :     read: reader.read,
      94              :     getSnapshot: reader.getSnapshot,
      95              :     dispose: reader.dispose
      96              :   }
      97              : }
        

Generated by: LCOV version 2.0-1