import type { LangGeniusVersionResponse } from '@/models/common' const APP_CONTEXT_STATE_ATOM_KIND = Symbol('app-context-state-atom-kind') export type AppContextStateMockState = { userProfile?: { id?: string name?: string email?: string avatar?: string | null avatar_url?: string | null is_password_set?: boolean } | null currentWorkspace?: { id?: string name?: string } | null isCurrentWorkspaceManager?: boolean isCurrentWorkspaceOwner?: boolean isCurrentWorkspaceEditor?: boolean isCurrentWorkspaceDatasetOperator?: boolean isLoadingCurrentWorkspace?: boolean isLoadingWorkspacePermissionKeys?: boolean workspacePermissionKeys?: string[] langGeniusVersionInfo?: LangGeniusVersionResponse refreshUserProfile?: () => void refreshCurrentWorkspace?: () => void } type AppContextStateAtomKind = | 'userProfile' | 'userProfileId' | 'userProfileEmail' | 'currentWorkspace' | 'currentWorkspaceId' | 'workspaceRoleFlags' | 'isCurrentWorkspaceManager' | 'isCurrentWorkspaceOwner' | 'currentWorkspaceLoading' | 'workspacePermissionKeys' | 'workspacePermissionKeysLoading' | 'langGeniusVersionInfo' | 'langGeniusCurrentVersion' | 'refreshUserProfile' | 'refreshCurrentWorkspace' type AppContextStateMockAtom = { [APP_CONTEXT_STATE_ATOM_KIND]: AppContextStateAtomKind } type AppContextStateMockRegistry = { getState: () => AppContextStateMockState } const defaultUserProfile = { id: 'user-1', name: 'User', email: 'user@example.com', avatar: '', avatar_url: '', is_password_set: true, } const defaultCurrentWorkspace = { id: 'workspace-1', name: 'Workspace', } const defaultLangGeniusVersionInfo = { current_env: 'CLOUD', current_version: '', latest_version: '', version: '', release_date: '', release_notes: '', can_auto_update: false, } satisfies LangGeniusVersionResponse let appContextStateMockRegistry: AppContextStateMockRegistry | undefined const createMockAtom = ( kind: AppContextStateAtomKind, ): AppContextStateMockAtom => ({ [APP_CONTEXT_STATE_ATOM_KIND]: kind, }) const isAppContextStateMockAtom = (atom: unknown): atom is AppContextStateMockAtom => { return typeof atom === 'object' && atom !== null && APP_CONTEXT_STATE_ATOM_KIND in atom } const getUserProfile = (state: AppContextStateMockState) => ({ ...defaultUserProfile, ...state.userProfile, }) const getCurrentWorkspace = (state: AppContextStateMockState) => ({ ...defaultCurrentWorkspace, ...state.currentWorkspace, }) export const createAppContextStateAtomMock = async ( importOriginal: () => Promise, getState: () => AppContextStateMockState, ) => { const actual = await importOriginal() appContextStateMockRegistry = { getState, } return { ...actual, userProfileAtom: createMockAtom('userProfile'), userProfileIdAtom: createMockAtom('userProfileId'), userProfileEmailAtom: createMockAtom('userProfileEmail'), currentWorkspaceAtom: createMockAtom('currentWorkspace'), currentWorkspaceIdAtom: createMockAtom('currentWorkspaceId'), workspaceRoleFlagsAtom: createMockAtom('workspaceRoleFlags'), isCurrentWorkspaceManagerAtom: createMockAtom('isCurrentWorkspaceManager'), isCurrentWorkspaceOwnerAtom: createMockAtom('isCurrentWorkspaceOwner'), currentWorkspaceLoadingAtom: createMockAtom('currentWorkspaceLoading'), workspacePermissionKeysAtom: createMockAtom('workspacePermissionKeys'), workspacePermissionKeysLoadingAtom: createMockAtom('workspacePermissionKeysLoading'), langGeniusVersionInfoAtom: createMockAtom('langGeniusVersionInfo'), langGeniusCurrentVersionAtom: createMockAtom('langGeniusCurrentVersion'), refreshUserProfileAtom: createMockAtom('refreshUserProfile'), refreshCurrentWorkspaceAtom: createMockAtom('refreshCurrentWorkspace'), } } export const createAppContextStateJotaiMock = async ( importOriginal: () => Promise, ) => { const actual = await importOriginal() return { ...actual, useAtomValue: (atom: unknown) => { if (!isAppContextStateMockAtom(atom)) return actual.useAtomValue(atom as Parameters[0]) if (!appContextStateMockRegistry) throw new Error('App context state atom mock is not initialized') const state = appContextStateMockRegistry.getState() const userProfile = getUserProfile(state) const currentWorkspace = getCurrentWorkspace(state) if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'userProfile') return userProfile if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'userProfileId') return userProfile.id if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'userProfileEmail') return userProfile.email if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'currentWorkspace') return currentWorkspace if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'currentWorkspaceId') return currentWorkspace.id if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'workspaceRoleFlags') { return { isCurrentWorkspaceManager: state.isCurrentWorkspaceManager ?? false, isCurrentWorkspaceOwner: state.isCurrentWorkspaceOwner ?? false, isCurrentWorkspaceEditor: state.isCurrentWorkspaceEditor ?? false, isCurrentWorkspaceDatasetOperator: state.isCurrentWorkspaceDatasetOperator ?? false, } } if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'isCurrentWorkspaceManager') return state.isCurrentWorkspaceManager ?? false if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'isCurrentWorkspaceOwner') return state.isCurrentWorkspaceOwner ?? false if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'currentWorkspaceLoading') return state.isLoadingCurrentWorkspace ?? false if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'workspacePermissionKeys') return state.workspacePermissionKeys ?? [] if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'workspacePermissionKeysLoading') return state.isLoadingWorkspacePermissionKeys ?? false if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'langGeniusVersionInfo') return state.langGeniusVersionInfo ?? defaultLangGeniusVersionInfo if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'langGeniusCurrentVersion') return (state.langGeniusVersionInfo ?? defaultLangGeniusVersionInfo).current_version throw new Error(`Unsupported app context state atom: ${atom[APP_CONTEXT_STATE_ATOM_KIND]}`) }, useSetAtom: (atom: unknown) => { if (!isAppContextStateMockAtom(atom)) return actual.useSetAtom(atom as Parameters[0]) if (!appContextStateMockRegistry) throw new Error('App context state atom mock is not initialized') const state = appContextStateMockRegistry.getState() if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'refreshUserProfile') return state.refreshUserProfile ?? (() => {}) if (atom[APP_CONTEXT_STATE_ATOM_KIND] === 'refreshCurrentWorkspace') return state.refreshCurrentWorkspace ?? (() => {}) throw new Error(`Unsupported app context state write atom: ${atom[APP_CONTEXT_STATE_ATOM_KIND]}`) }, } }