mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-06 08:03:46 +00:00
fix(web): restore CC Switch model dropdown
This commit is contained in:
2 files changed
+141
-13
No files matched your search
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
Copyright (C) 2023-2026 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { act, render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { CCSwitchDialog } from '../cc-switch-dialog'
|
||||
|
||||
let queryClient: QueryClient
|
||||
|
||||
beforeEach(() => {
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false, gcTime: Infinity } },
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
queryClient.clear()
|
||||
})
|
||||
|
||||
function renderDialog(models = ['gpt-5.4', 'claude-sonnet-4-6']) {
|
||||
queryClient.setQueryData(['user-models-ccswitch'], {
|
||||
success: true,
|
||||
data: models,
|
||||
})
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<CCSwitchDialog open onOpenChange={vi.fn()} tokenKey='test-only' />
|
||||
</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
describe('CC Switch model selection', () => {
|
||||
it.each(['Claude', 'Codex', 'Gemini'])(
|
||||
'opens %s models outside the clipping dialog and keeps the dialog open after selection',
|
||||
async (app) => {
|
||||
renderDialog()
|
||||
const user = userEvent.setup()
|
||||
await user.click(screen.getByRole('radio', { name: app }))
|
||||
const input = screen.getByRole('combobox', { name: 'Primary Model' })
|
||||
|
||||
await user.click(input)
|
||||
|
||||
expect(input).toHaveAttribute('aria-expanded', 'true')
|
||||
const list = await screen.findByRole('listbox')
|
||||
const dialog = screen.getByRole('dialog', { name: 'Import to CC Switch' })
|
||||
// The dialog is translated and clips overflow. Its popup must escape
|
||||
// that containing block to remain aligned and fully visible.
|
||||
expect(dialog).not.toContainElement(list)
|
||||
await user.click(screen.getByRole('option', { name: 'gpt-5.4' }))
|
||||
await waitFor(() => expect(input).toHaveValue('gpt-5.4'))
|
||||
expect(input).toHaveAttribute('aria-expanded', 'false')
|
||||
expect(dialog).toBeVisible()
|
||||
}
|
||||
)
|
||||
|
||||
it('filters model names and supports keyboard selection and Escape without closing the dialog', async () => {
|
||||
renderDialog()
|
||||
const user = userEvent.setup()
|
||||
await user.click(screen.getByRole('radio', { name: 'Codex' }))
|
||||
const input = screen.getByRole('combobox', { name: 'Primary Model' })
|
||||
await user.click(input)
|
||||
await user.type(input, 'sonnet')
|
||||
|
||||
expect(
|
||||
screen.queryByRole('option', { name: 'gpt-5.4' })
|
||||
).not.toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByRole('option', { name: 'claude-sonnet-4-6' })
|
||||
).toBeVisible()
|
||||
await user.keyboard('{ArrowDown}{Enter}')
|
||||
await waitFor(() => expect(input).toHaveValue('claude-sonnet-4-6'))
|
||||
await user.click(input)
|
||||
await user.keyboard('{Escape}')
|
||||
|
||||
expect(input).toHaveValue('claude-sonnet-4-6')
|
||||
expect(input).toHaveAttribute('aria-expanded', 'false')
|
||||
expect(screen.getByRole('dialog')).toBeVisible()
|
||||
})
|
||||
|
||||
it('shows an empty result when no models are available and updates an open dropdown when models arrive', async () => {
|
||||
renderDialog([])
|
||||
const user = userEvent.setup()
|
||||
await user.click(screen.getByRole('radio', { name: 'Codex' }))
|
||||
const input = screen.getByRole('combobox', { name: 'Primary Model' })
|
||||
await user.click(screen.getByRole('button', { name: 'Primary Model' }))
|
||||
|
||||
expect(await screen.findByText('No models found')).toBeVisible()
|
||||
await act(async () => {
|
||||
queryClient.setQueryData(['user-models-ccswitch'], {
|
||||
success: true,
|
||||
data: ['gpt-5.4'],
|
||||
})
|
||||
})
|
||||
await user.click(await screen.findByRole('option', { name: 'gpt-5.4' }))
|
||||
await waitFor(() => expect(input).toHaveValue('gpt-5.4'))
|
||||
})
|
||||
|
||||
it('lets users edit the provider name without opening a model dropdown', async () => {
|
||||
renderDialog()
|
||||
const user = userEvent.setup()
|
||||
const input = screen.getByRole('textbox', { name: 'Name' })
|
||||
|
||||
await user.clear(input)
|
||||
await user.type(input, 'Development')
|
||||
|
||||
expect(input).toHaveValue('Development')
|
||||
expect(screen.queryByRole('listbox')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
@@ -23,7 +23,8 @@ import { toast } from 'sonner'
|
||||
|
||||
import { Dialog } from '@/components/dialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { ComboboxInput } from '@/components/ui/combobox-input'
|
||||
import { Combobox } from '@/components/ui/combobox'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
|
||||
import { getUserModels } from '@/lib/api'
|
||||
@@ -153,9 +154,7 @@ export function CCSwitchDialog(props: Props) {
|
||||
title={t('Import to CC Switch')}
|
||||
contentClassName='sm:max-w-md'
|
||||
contentHeight='auto'
|
||||
bodyClassName={
|
||||
currentConfig.modelFields.length === 1 ? 'space-y-4 pb-52' : 'space-y-4'
|
||||
}
|
||||
bodyClassName='space-y-4'
|
||||
footer={
|
||||
<>
|
||||
<Button variant='outline' onClick={() => props.onOpenChange(false)}>
|
||||
@@ -190,25 +189,27 @@ export function CCSwitchDialog(props: Props) {
|
||||
</div>
|
||||
|
||||
<div className='space-y-2'>
|
||||
<Label>{t('Name')}</Label>
|
||||
<ComboboxInput
|
||||
options={[]}
|
||||
<Label htmlFor='cc-switch-name'>{t('Name')}</Label>
|
||||
<Input
|
||||
id='cc-switch-name'
|
||||
value={name}
|
||||
onValueChange={setName}
|
||||
onChange={(event) => setName(event.target.value)}
|
||||
placeholder={currentConfig.defaultName}
|
||||
emptyText=''
|
||||
allowCustomValue
|
||||
/>
|
||||
</div>
|
||||
|
||||
{currentConfig.modelFields.map((field) => (
|
||||
<div key={field.key} className='space-y-2'>
|
||||
<Label required={field.required}>{t(field.labelKey)}</Label>
|
||||
<ComboboxInput
|
||||
<Label htmlFor={`cc-switch-${field.key}`} required={field.required}>
|
||||
{t(field.labelKey)}
|
||||
</Label>
|
||||
<Combobox
|
||||
id={`cc-switch-${field.key}`}
|
||||
aria-label={t(field.labelKey)}
|
||||
options={modelOptions}
|
||||
value={models[field.key] || ''}
|
||||
onValueChange={(v) =>
|
||||
setModels((prev) => ({ ...prev, [field.key]: v }))
|
||||
setModels((prev) => ({ ...prev, [field.key]: v ?? '' }))
|
||||
}
|
||||
placeholder={t('Select or enter model name')}
|
||||
emptyText={t('No models found')}
|
||||
|
||||
Reference in new issue
Block a user