Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

3.3 KB
Raw Blame
// @vitest-environment jsdom
import type { ComponentType } from 'react'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { afterEach, expect, it, vi } from 'vitest'

import { Route } from './route'

const {
  probe,
  reconnect,
  saveConnection,
  serviceState,
  config,
  connectionState,
  healthState,
} = vi.hoisted(() => ({
  connectionState: {
    baseUrl: '',
    serviceSelection: 'auto',
    accountId: 'account-a',
    userId: 'alice',
    adminApiKey: '',
    apiKey: '',
  },
  probe: vi.fn(),
  reconnect: vi.fn(),
  saveConnection: vi.fn(),
  serviceState: {
    provider: 'opensource',
    ready: true,
    role: 'user',
    key: '',
  },
  config: { url: 'http://localhost:1933', selection: 'auto' },
  healthState: { value: null as null | Record<string, unknown> },
}))
vi.mock('#/lib/admin', () => ({ probeStudioConnection: probe }))
vi.mock('#/hooks/use-app-connection', () => ({
  useAppConnection: () => ({
    connection: {
      ...Object.assign(connectionState, {
        baseUrl: config.url,
        serviceSelection: config.selection,
        adminApiKey: serviceState.key,
      }),
    },
    serverMode: 'trusted',
    serverHealth: healthState.value,
    connectionRole: serviceState.role,
    isConnectionRoleLoading: false,
    identityScopeKey: 'settings-test',
    saveConnection,
    reconnect,
  }),
}))
vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    t: (key: string) => key,
    i18n: { resolvedLanguage: 'en' },
  }),
}))
vi.mock('#/hooks/use-studio-service', () => ({
  useStudioService: () => ({
    provider: serviceState.provider,
    ready: serviceState.ready,
    version: '',
    isChecking: false,
    health: { refetch: vi.fn() },
  }),
}))
vi.mock('./-components/connection-profiles', () => ({
  ConnectionProfilesList: () => <span data-testid="connection-profiles" />,
}))
vi.mock('./-components/server-configuration', () => ({
  ServerConfigurationEditor: () => <span data-testid="configuration-editor" />,
}))
afterEach(() => {
  cleanup()
  healthState.value = null
  saveConnection.mockClear()
  reconnect.mockClear()
})

it('unmounts the configuration editor and resets the active tab when switching to a hosted connection', async () => {
  Object.assign(serviceState, {
    provider: 'opensource',
    ready: true,
    role: 'root',
    key: 'root-key',
  })
  probe.mockResolvedValue({ admin: { state: 'ok' }, data: { state: 'ok' } })
  const client = new QueryClient()
  const Settings = Route.options.component as ComponentType & {
    preload?: () => Promise<unknown>
  }
  await Settings.preload?.()
  const content = () => (
    <QueryClientProvider client={client}>
      <Settings />
    </QueryClientProvider>
  )
  const view = render(content())
  fireEvent.click(screen.getByRole('tab', { name: 'tabs.configuration' }))
  expect(await screen.findByTestId('configuration-editor')).toBeTruthy()
  serviceState.provider = 'volcengine'
  view.rerender(content())
  expect(screen.queryByRole('tab', { name: 'tabs.configuration' })).toBeNull()
  expect(screen.queryByTestId('configuration-editor')).toBeNull()
  expect(
    screen
      .getByRole('tab', { name: 'tabs.connection' })
      .getAttribute('aria-selected'),
  ).toBe('true')
  client.clear()
  Object.assign(serviceState, { provider: 'opensource', role: 'user', key: '' })
})