mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 12:24:39 +00:00
feat(provider): refresh models per AI provider
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import type { RefreshedProviderModel } from '@shumai/dtos'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { RefreshProviderModelsDialog } from './RefreshProviderModelsDialog'
|
||||
|
||||
const { importModels } = vi.hoisted(() => ({ importModels: vi.fn() }))
|
||||
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
teams: {
|
||||
':teamId': {
|
||||
providers: { ':providerId': { models: { import: { $post: importModels } } } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
const models = Array.from({ length: 6 }, (_, index): RefreshedProviderModel => ({
|
||||
modelId: `model-${index + 1}`,
|
||||
name: `Model ${index + 1}`,
|
||||
selectionToken: `model-token-${index + 1}`,
|
||||
config: {
|
||||
api: 'openai-responses',
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 4096,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
},
|
||||
}))
|
||||
|
||||
function renderDialog() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
const onClose = vi.fn()
|
||||
const onImported = vi.fn()
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<RefreshProviderModelsDialog
|
||||
teamId="team-1"
|
||||
providerId="provider-1"
|
||||
providerName="OpenAI"
|
||||
models={models}
|
||||
onClose={onClose}
|
||||
onCloseAutoFocus={vi.fn()}
|
||||
onImported={onImported}
|
||||
/>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
return { onClose, onImported }
|
||||
}
|
||||
|
||||
describe('RefreshProviderModelsDialog', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
importModels.mockReset()
|
||||
})
|
||||
|
||||
it('opens a single-provider preview with selectable models and an empty selection', () => {
|
||||
renderDialog()
|
||||
expect(screen.getByRole('heading', { name: 'New models for OpenAI' })).toBeTruthy()
|
||||
expect(screen.getByText('Select the models you want to add.')).toBeTruthy()
|
||||
expect(screen.getAllByRole('checkbox')).toHaveLength(models.length)
|
||||
expect(screen.getByRole('button', { name: 'Add selected' }).hasAttribute('disabled')).toBe(true)
|
||||
})
|
||||
|
||||
it('filters models and selects or deselects the visible results', () => {
|
||||
renderDialog()
|
||||
const search = screen.getByRole('textbox')
|
||||
fireEvent.change(search, { target: { value: 'model-2' } })
|
||||
expect(screen.queryByText('Model 1')).toBeNull()
|
||||
expect(screen.getAllByRole('checkbox')).toHaveLength(1)
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Select visible' }))
|
||||
expect(screen.getByRole('checkbox').getAttribute('aria-checked')).toBe('true')
|
||||
expect(screen.getByRole('button', { name: 'Add selected (1)' })).toBeTruthy()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Deselect visible' }))
|
||||
expect(screen.getByRole('checkbox').getAttribute('aria-checked')).toBe('false')
|
||||
expect(screen.getByRole('button', { name: 'Add selected' }).hasAttribute('disabled')).toBe(true)
|
||||
})
|
||||
|
||||
it('imports only the selected models into the previewed provider', async () => {
|
||||
importModels.mockResolvedValueOnce(Response.json({ addedModels: 1 }))
|
||||
const { onClose, onImported } = renderDialog()
|
||||
fireEvent.click(screen.getAllByRole('checkbox')[1])
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Add selected (1)' }))
|
||||
|
||||
await waitFor(() => expect(onClose).toHaveBeenCalledOnce())
|
||||
expect(onImported).toHaveBeenCalledOnce()
|
||||
expect(importModels).toHaveBeenCalledWith({
|
||||
param: { teamId: 'team-1', providerId: 'provider-1' },
|
||||
json: { models: [models[1]] },
|
||||
})
|
||||
})
|
||||
|
||||
it('shows an actionable error when the preview becomes stale', async () => {
|
||||
importModels.mockResolvedValueOnce(new Response(null, { status: 409 }))
|
||||
const { onImported } = renderDialog()
|
||||
fireEvent.click(screen.getAllByRole('checkbox')[0])
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Add selected (1)' }))
|
||||
expect((await screen.findByRole('alert')).textContent).toContain('Refresh models again.')
|
||||
expect(onImported).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user