Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.9 KB
Raw Blame
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 { consoleQueryKey } from '@/test/console/operation-hooks'
import { createQueryClientWrapper } from '@/test/console/query-client'
import { createTestQueryClient } from '@/test/query-client'

type AccountProfileMeta = UserProfileWithMeta['meta']

const accountProfileQueryKey = consoleQueryKey('account.profile.get')

const createAccountProfileFixture = (
  overrides: Partial<GetAccountProfileResponse> = {},
): GetAccountProfileResponse => ({
  id: 'user-1',
  name: 'Test User',
  email: '[email protected]',
  avatar: '',
  avatar_url: null,
  is_password_set: false,
  timezone: 'Asia/Shanghai',
  ...overrides,
})

const createMockUserProfileResponse = (
  profile: Partial<GetAccountProfileResponse> = {},
  meta: Partial<AccountProfileMeta> = {},
): UserProfileWithMeta => ({
  profile: createAccountProfileFixture(profile),
  meta: {
    currentVersion: null,
    currentEnv: null,
    ...meta,
  },
})

export const seedAccountProfileQuery = (
  queryClient: QueryClient,
  profile: Partial<GetAccountProfileResponse> = {},
  meta: Partial<AccountProfileMeta> = {},
) => {
  const data = createMockUserProfileResponse(profile, meta)
  queryClient.setQueryData(accountProfileQueryKey, data)
  return data
}

export const ensureAccountProfileQuery = (
  queryClient: QueryClient,
  profile: Partial<GetAccountProfileResponse> = {},
  meta: Partial<AccountProfileMeta> = {},
) => {
  const existingProfile = queryClient.getQueryData<UserProfileWithMeta>(accountProfileQueryKey)
  if (existingProfile === undefined) return seedAccountProfileQuery(queryClient, profile, meta)

  return existingProfile
}

export const createAccountProfileQueryClient = (
  profile: Partial<GetAccountProfileResponse> = {},
  meta: Partial<AccountProfileMeta> = {},
) => {
  const queryClient = createTestQueryClient()

  seedAccountProfileQuery(queryClient, profile, meta)

  return queryClient
}

export const createAccountProfileQueryWrapper = (
  profile: Partial<GetAccountProfileResponse> = {},
  meta: Partial<AccountProfileMeta> = {},
) => {
  const queryClient = createAccountProfileQueryClient(profile, meta)
  return createQueryClientWrapper(queryClient)
}

export const renderWithAccountProfile = (
  ui: ReactElement,
  options: Omit<RenderOptions, 'wrapper'> & {
    accountProfile?: Partial<GetAccountProfileResponse>
    accountProfileMeta?: Partial<AccountProfileMeta>
  } = {},
) => {
  const { accountProfile, accountProfileMeta, ...renderOptions } = options
  return render(ui, {
    ...renderOptions,
    wrapper: createAccountProfileQueryWrapper(accountProfile, accountProfileMeta),
  })
}