feat: 供应商列表 Base URL、API Key 和模型列表统一模型 ID 增加一键复制按钮
This commit is contained in:
@@ -1,9 +1,24 @@
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { ProviderTable } from '@/pages/Providers/ProviderTable'
|
||||
import type { Provider } from '@/types'
|
||||
|
||||
const { mockMessagePluginSuccess } = vi.hoisted(() => ({
|
||||
mockMessagePluginSuccess: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('tdesign-react', async () => {
|
||||
const actual = await vi.importActual('tdesign-react')
|
||||
return {
|
||||
...actual,
|
||||
MessagePlugin: {
|
||||
success: mockMessagePluginSuccess,
|
||||
error: vi.fn(),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const mockModelsData = [
|
||||
{ id: 'model-1', providerId: 'openai', modelName: 'gpt-4o', enabled: true, unifiedId: 'openai/gpt-4o' },
|
||||
{
|
||||
@@ -54,6 +69,9 @@ const defaultProps = {
|
||||
}
|
||||
|
||||
describe('ProviderTable', () => {
|
||||
beforeEach(() => {
|
||||
mockMessagePluginSuccess.mockClear()
|
||||
})
|
||||
it('renders provider list with name, baseUrl, apiKey, and status tags', () => {
|
||||
render(<ProviderTable {...defaultProps} />)
|
||||
|
||||
@@ -203,4 +221,66 @@ describe('ProviderTable', () => {
|
||||
const protocolCell = container.querySelector('[data-colkey="protocol"]')
|
||||
expect(protocolCell).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders Base URL with copy button and copies on click', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { container } = render(<ProviderTable {...defaultProps} />)
|
||||
|
||||
const baseUrlCells = container.querySelectorAll('td')
|
||||
const baseUrlCellWithContent = Array.from(baseUrlCells).find((td) =>
|
||||
td.textContent?.includes('https://api.openai.com/v1')
|
||||
)
|
||||
expect(baseUrlCellWithContent).toBeTruthy()
|
||||
|
||||
const buttons = baseUrlCellWithContent!.querySelectorAll('button')
|
||||
expect(buttons.length).toBeGreaterThan(0)
|
||||
|
||||
await user.click(buttons[0]!)
|
||||
expect(mockMessagePluginSuccess).toHaveBeenCalledWith('已复制 Base URL')
|
||||
})
|
||||
|
||||
it('renders API Key with copy button and copies on click', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { container } = render(<ProviderTable {...defaultProps} />)
|
||||
|
||||
const allCells = container.querySelectorAll('td')
|
||||
const apiKeyCell = Array.from(allCells).find((td) => td.textContent?.includes('sk-abcdefgh12345678'))
|
||||
expect(apiKeyCell).toBeTruthy()
|
||||
|
||||
const buttons = apiKeyCell!.querySelectorAll('button')
|
||||
expect(buttons.length).toBeGreaterThan(0)
|
||||
|
||||
await user.click(buttons[0]!)
|
||||
expect(mockMessagePluginSuccess).toHaveBeenCalledWith('已复制 API Key')
|
||||
})
|
||||
|
||||
it('does not render copy button when Base URL is empty', () => {
|
||||
const emptyUrlProvider: Provider[] = [
|
||||
{
|
||||
...mockProviders[0],
|
||||
id: 'empty-url',
|
||||
baseUrl: '',
|
||||
},
|
||||
]
|
||||
const { container } = render(<ProviderTable {...defaultProps} providers={emptyUrlProvider} />)
|
||||
|
||||
const allCells = container.querySelectorAll('td')
|
||||
const baseUrlCells = Array.from(allCells).filter((td) => td.textContent === '')
|
||||
expect(baseUrlCells.length).toBeGreaterThanOrEqual(0)
|
||||
})
|
||||
|
||||
it('does not render copy button when API Key is empty', () => {
|
||||
const emptyKeyProvider: Provider[] = [
|
||||
{
|
||||
...mockProviders[0],
|
||||
id: 'empty-key',
|
||||
apiKey: '',
|
||||
},
|
||||
]
|
||||
const { container } = render(<ProviderTable {...defaultProps} providers={emptyKeyProvider} />)
|
||||
|
||||
const allCells = container.querySelectorAll('td')
|
||||
const apiKeyCells = Array.from(allCells).filter((td) => td.textContent === '')
|
||||
expect(apiKeyCells.length).toBeGreaterThanOrEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user