LCOV - code coverage report
Current view: top level - src/stores - userStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 55.9 % 34 19
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { computed, ref, watchEffect } from 'vue'
       3              : 
       4              : import type { UserConfigResponse } from '@comfyorg/api-client/types'
       5              : import { api } from '@/scripts/api'
       6              : 
       7              : export interface User {
       8              :   userId: string
       9              :   username: string
      10              : }
      11              : 
      12         2356 : export const useUserStore = defineStore('user', () => {
      13              :   /**
      14              :    * The user config. null if not loaded.
      15              :    */
      16         2355 :   const userConfig = ref<UserConfigResponse | null>(null)
      17              :   /**
      18              :    * The current user id. null if not logged in or in single user mode.
      19              :    */
      20         2355 :   const currentUserId = ref<string | null>(null)
      21         2355 :   const isMultiUserServer = computed(
      22              :     () => userConfig.value && 'users' in userConfig.value
      23              :   )
      24         2355 :   const needsLogin = computed(
      25              :     () => !currentUserId.value && isMultiUserServer.value
      26              :   )
      27         2355 :   const users = computed<User[]>(() =>
      28              :     Object.entries(userConfig.value?.users ?? {}).map(([userId, username]) => ({
      29              :       userId,
      30              :       username
      31              :     }))
      32              :   )
      33         2355 :   const currentUser = computed<User | null>(
      34              :     () =>
      35              :       users.value.find((user) => user.userId === currentUserId.value) ?? null
      36              :   )
      37         2355 :   const initialized = computed(() => userConfig.value !== null)
      38              : 
      39         2355 :   let initializePromise: Promise<void> | null = null
      40              : 
      41              :   /**
      42              :    * Initialize the user store.
      43              :    */
      44         4710 :   async function initialize() {
      45         4710 :     initializePromise ??= (async () => {
      46         2355 :       try {
      47         2355 :         userConfig.value = await api.getUserConfig()
      48         2355 :         currentUserId.value = localStorage['Comfy.userId']
      49              :       } catch (err) {
      50            0 :         initializePromise = null
      51            0 :         throw err
      52              :       }
      53              :     })()
      54         4710 :     return initializePromise
      55              :   }
      56              : 
      57              :   /**
      58              :    * Create a new user.
      59              :    *
      60              :    * @param username - The username.
      61              :    * @returns The new user.
      62              :    */
      63            0 :   async function createUser(username: string): Promise<User> {
      64            0 :     const resp = await api.createUser(username)
      65            0 :     const data = await resp.json()
      66            0 :     if (resp.status >= 300) {
      67            0 :       throw new Error(
      68              :         data.error ??
      69              :           'Error creating user: ' + resp.status + ' ' + resp.statusText
      70              :       )
      71              :     }
      72            0 :     return {
      73              :       userId: data,
      74              :       username
      75              :     }
      76              :   }
      77              : 
      78              :   /**
      79              :    * Login the current user.
      80              :    *
      81              :    * @param user - The user.
      82              :    */
      83            0 :   async function login({
      84              :     userId,
      85              :     username
      86              :   }: {
      87              :     userId: string
      88              :     username: string
      89              :   }) {
      90            0 :     currentUserId.value = userId
      91            0 :     localStorage['Comfy.userId'] = userId
      92            0 :     localStorage['Comfy.userName'] = username
      93              :   }
      94              : 
      95         2355 :   watchEffect(() => {
      96         4710 :     if (isMultiUserServer.value && currentUserId.value) {
      97         2355 :       api.user = currentUserId.value
      98              :     }
      99              :   })
     100              : 
     101              :   /**
     102              :    * Logout the current user.
     103              :    */
     104            0 :   async function logout() {
     105            0 :     delete localStorage['Comfy.userId']
     106            0 :     delete localStorage['Comfy.userName']
     107              :   }
     108              : 
     109         2355 :   return {
     110              :     users,
     111              :     currentUser,
     112              :     isMultiUserServer,
     113              :     needsLogin,
     114              :     initialized,
     115              :     initialize,
     116              :     createUser,
     117              :     login,
     118              :     logout
     119              :   }
     120              : })
        

Generated by: LCOV version 2.0-1