LCOV - code coverage report
Current view: top level - src/platform/billing/sdk - webSessionBillingSession.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 8.3 % 24 2
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   BillingScope,
       3              :   BillingScopeSource,
       4              :   BillingSession
       5              : } from '@comfyorg/account-core/billing'
       6              : import type {
       7              :   SessionErrorCode,
       8              :   SessionRequestOptions,
       9              :   SessionResult,
      10              :   SessionSnapshot
      11              : } from '@comfyorg/account-core/session'
      12              : import type { SessionTokenResult } from '@comfyorg/account-core/sessionTokenMint'
      13              : import type { WebSessionErrorCode } from '@comfyorg/account-core/webSession'
      14              : import { watch } from 'vue'
      15              : 
      16              : import { useCloudWebSessionStore } from '@/platform/auth/session/cloudWebSessionStore'
      17              : import type {
      18              :   WebSessionRequestScope,
      19              :   WebSessionRequests
      20              : } from '@comfyorg/api-client/webSessionFetch'
      21              : import { useWorkspaceSession } from '@/platform/workspace/ports/workspaceSession'
      22              : 
      23              : export type WebSessionBillingSession = BillingSession & BillingScopeSource
      24              : 
      25         2356 : const SESSION_CODE: Readonly<
      26              :   Record<Exclude<WebSessionErrorCode, 'IDENTITY_CHANGED'>, SessionErrorCode>
      27              : > = {
      28              :   NO_SESSION: 'NOT_AUTHENTICATED',
      29              :   SESSION_EXPIRED: 'NOT_AUTHENTICATED',
      30              :   SESSION_REVOKED: 'NOT_AUTHENTICATED',
      31              :   WORKSPACE_ACCESS_DENIED: 'ACCESS_DENIED',
      32              :   CSRF_STALE: 'TOKEN_EXCHANGE_FAILED',
      33              :   SESSION_REQUEST_REFUSED: 'TOKEN_EXCHANGE_FAILED',
      34              :   SSO_REQUIRED: 'SSO_REQUIRED',
      35              :   SESSION_UNAVAILABLE: 'TOKEN_EXCHANGE_FAILED'
      36              : }
      37              : 
      38         2356 : const SIGNED_OUT: SessionSnapshot = {
      39              :   phase: 'signed-out',
      40              :   user: null,
      41              :   session: undefined
      42              : }
      43              : 
      44            0 : function sessionResult(result: SessionTokenResult): SessionResult | undefined {
      45            0 :   if (result.status === 'ok')
      46            0 :     return { status: 'ok', session: result.credential }
      47            0 :   if (result.code === 'IDENTITY_CHANGED') return undefined
      48            0 :   return {
      49              :     status: 'error',
      50              :     code: SESSION_CODE[result.code],
      51              :     ...(result.httpStatus === undefined
      52              :       ? {}
      53              :       : { httpStatus: result.httpStatus })
      54              :   }
      55              : }
      56              : 
      57              : /**
      58              :  * The billing SDK's session on a tab signed in on the web session: tokens
      59              :  * come from the session mint, and the scope from the session user and the
      60              :  * selected workspace, which are known before anything is minted.
      61              :  */
      62            0 : export function createWebSessionBillingSession(
      63              :   requests: WebSessionRequests
      64              : ): WebSessionBillingSession {
      65            0 :   const webSession = useCloudWebSessionStore()
      66            0 :   const workspaceAuth = useWorkspaceSession()
      67              : 
      68            0 :   function getScope(): BillingScope | undefined {
      69            0 :     const user = webSession.signedInUser
      70            0 :     const workspace = workspaceAuth.currentWorkspace
      71            0 :     if (!user || !workspace) return undefined
      72            0 :     return { userId: user.id, workspaceId: workspace.id, role: workspace.role }
      73              :   }
      74              : 
      75            0 :   function getSnapshot(): SessionSnapshot {
      76            0 :     const scope = getScope()
      77            0 :     if (!scope) return SIGNED_OUT
      78            0 :     return {
      79              :       phase: 'minting',
      80              :       user: {
      81              :         uid: scope.userId,
      82              :         getIdToken: () =>
      83              :           Promise.reject(new Error('A web session has no Firebase ID token'))
      84              :       },
      85              :       session: undefined
      86              :     }
      87              :   }
      88              : 
      89            0 :   const mintWith =
      90              :     (mint: (scope: WebSessionRequestScope) => Promise<SessionTokenResult>) =>
      91              :     async (
      92              :       _user: unknown,
      93              :       options?: SessionRequestOptions
      94              :     ): Promise<SessionResult | undefined> => {
      95            0 :       const scope = await requests.scope()
      96            0 :       if (!scope) return undefined
      97            0 :       return sessionResult(
      98              :         await mint({ ...scope, workspaceId: options?.workspaceId })
      99              :       )
     100              :     }
     101              : 
     102            0 :   return {
     103              :     getScope,
     104              :     getSnapshot,
     105              :     subscribe: (listener) =>
     106              :       watch(getScope, () => listener(getSnapshot()), {
     107              :         flush: 'sync'
     108              :       }),
     109              :     ensureFresh: mintWith(requests.workspaceToken),
     110              :     remint: mintWith(requests.remintWorkspaceToken)
     111              :   }
     112              : }
        

Generated by: LCOV version 2.0-1