import type { GetAccountProfileResponse } from '@dify/contracts/api/console/account/types.gen' import type { QueryClient } from '@tanstack/react-query' import type { RenderOptions } from '@testing-library/react' import type { ReactElement } from 'react' import type { UserProfileWithMeta } from '@/features/account-profile/client' import { render } from '@testing-library/react' import { createQueryClientWrapper } from '@/test/console/query-client' import { createTestQueryClient } from '@/test/query-client' const accountProfileQueryKey = [ ['console', 'account', 'profile', 'get'], { type: 'query' }, ] as const const createAccountProfileFixture = ( overrides: Partial = {}, ): GetAccountProfileResponse => ({ id: 'user-1', name: 'Test User', email: 'test@dify.ai', avatar: '', avatar_url: null, is_password_set: false, timezone: 'Asia/Shanghai', ...overrides, }) const createMockUserProfileResponse = ( profile: Partial = {}, ): UserProfileWithMeta => ({ profile: createAccountProfileFixture(profile), meta: { currentVersion: null, currentEnv: null, }, }) export const seedAccountProfileQuery = ( queryClient: QueryClient, profile: Partial = {}, ) => { const data = createMockUserProfileResponse(profile) queryClient.setQueryData(accountProfileQueryKey, data) return data } export const ensureAccountProfileQuery = ( queryClient: QueryClient, profile: Partial = {}, ) => { const existingProfile = queryClient.getQueryData(accountProfileQueryKey) if (existingProfile === undefined) return seedAccountProfileQuery(queryClient, profile) return existingProfile } export const createAccountProfileQueryClient = ( profile: Partial = {}, ) => { const queryClient = createTestQueryClient() seedAccountProfileQuery(queryClient, profile) return queryClient } export const createAccountProfileQueryWrapper = ( profile: Partial = {}, ) => { const queryClient = createAccountProfileQueryClient(profile) return createQueryClientWrapper(queryClient) } export const renderWithAccountProfile = ( ui: ReactElement, options: Omit & { accountProfile?: Partial } = {}, ) => { const { accountProfile, ...renderOptions } = options return render(ui, { ...renderOptions, wrapper: createAccountProfileQueryWrapper(accountProfile), }) }