Files
shumai/packages/webui/components/settings/AgentsSettings.test.tsx
T

89 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-12-04 19:15:58 +08:00
// @vitest-environment happy-dom
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { AgentsSettings } from './AgentsSettings'
const getAgents = vi.fn()
vi.mock('@/ui/api/client', () => ({
client: {
api: {
teams: { ':teamId': { agents: { $get: () => getAgents() } } },
},
},
}))
vi.mock('@/ui/hooks/use-permissions', () => ({
usePermissions: () => ({ canAdmin: true }),
}))
vi.mock('./AgentFormDialog', () => ({
AgentFormDialog: ({ title }: { title: string }) => <div role="dialog">{title}</div>,
}))
function renderSettings(initiallyConfigured: boolean) {
const queryClient = new QueryClient()
const onOpenIntegrations = vi.fn()
const view = (configured: boolean) => (
<QueryClientProvider client={queryClient}>
<AgentsSettings
teamId="team"
localEmbeddingProviderConfigured={configured}
onOpenIntegrations={onOpenIntegrations}
/>
</QueryClientProvider>
)
const { rerender } = render(view(initiallyConfigured))
return {
onOpenIntegrations,
updateConfigured: (configured: boolean) => rerender(view(configured)),
}
}
describe('Transcript Agent setup', () => {
afterEach(() => {
cleanup()
vi.resetAllMocks()
})
it('blocks creation and offers to open Integrations when setup is missing', async () => {
getAgents.mockResolvedValue(Response.json([]))
const { onOpenIntegrations, updateConfigured } = renderSettings(false)
await screen.findByText('Set up a local embedding server to use the Transcript Agent.')
const create = screen.getByRole<HTMLButtonElement>('button', {
name: 'Create Transcript Agent',
})
expect(create.disabled).toBe(true)
fireEvent.click(screen.getByRole('button', { name: 'Open Integrations' }))
expect(onOpenIntegrations).toHaveBeenCalledOnce()
updateConfigured(true)
expect(create.disabled).toBe(false)
fireEvent.click(create)
expect(screen.getByRole('dialog')).toBeTruthy()
})
it('keeps an existing Transcript Agent visible with its controls disabled', async () => {
getAgents.mockResolvedValue(
Response.json([
{ id: 'transcriber', name: 'Video Transcriber', type: 'transcription', enabled: true },
]),
)
const { updateConfigured } = renderSettings(false)
const transcriber = await screen.findByText('Video Transcriber')
const enabled = screen.getByRole<HTMLButtonElement>('switch')
expect(enabled.disabled).toBe(true)
fireEvent.click(transcriber)
expect(screen.queryByRole('dialog')).toBeNull()
updateConfigured(true)
expect(enabled.disabled).toBe(false)
fireEvent.click(transcriber)
expect(screen.getByRole('dialog')).toBeTruthy()
})
})