176 lines
7.8 KiB
TypeScript
176 lines
7.8 KiB
TypeScript
import { render, screen, waitFor } from '@testing-library/react'
|
||
import userEvent from '@testing-library/user-event'
|
||
import SuperAdminsTab from './SuperAdminsTab'
|
||
|
||
jest.mock('@/components/admin/NotificationCenterPanel', () => ({
|
||
__esModule: true,
|
||
default: () => <div>统一通知中心管理工作面</div>,
|
||
}))
|
||
|
||
describe('SuperAdminsTab workbench navigation', () => {
|
||
beforeEach(() => {
|
||
global.fetch = jest.fn((input: RequestInfo | URL) => {
|
||
const url = String(input)
|
||
if (url === '/api/admin/super-admins') {
|
||
return Promise.resolve({
|
||
ok: true,
|
||
json: async () => ({ superAdmins: [], count: 0, canAdd: true }),
|
||
} as Response)
|
||
}
|
||
if (url.startsWith('/api/admin/auto-listing-targets')) {
|
||
return Promise.resolve({
|
||
ok: true,
|
||
json: async () => ({ data: { targets: [], recentDeliveries: [] } }),
|
||
} as Response)
|
||
}
|
||
return Promise.resolve({
|
||
ok: true,
|
||
json: async () => ({ data: [] }),
|
||
} as Response)
|
||
}) as jest.Mock
|
||
})
|
||
|
||
test('默认进入统一通知中心且三类能力互不混排', async () => {
|
||
const user = userEvent.setup()
|
||
render(<SuperAdminsTab showToast={jest.fn()} />)
|
||
|
||
expect(await screen.findByText('统一通知中心管理工作面')).toBeInTheDocument()
|
||
expect(screen.queryByText('素材平台 API Key')).not.toBeInTheDocument()
|
||
expect(screen.queryByText('自动上架回调配置')).not.toBeInTheDocument()
|
||
|
||
await user.click(screen.getByRole('button', { name: /权限与凭证/ }))
|
||
expect(screen.getByText('素材平台 API Key')).toBeInTheDocument()
|
||
expect(screen.queryByText('统一通知中心管理工作面')).not.toBeInTheDocument()
|
||
|
||
await user.click(screen.getByRole('button', { name: /素材外发/ }))
|
||
expect(screen.getByText('自动上架回调配置')).toBeInTheDocument()
|
||
expect(screen.queryByText('素材平台 API Key')).not.toBeInTheDocument()
|
||
|
||
await waitFor(() => expect(global.fetch).toHaveBeenCalledWith('/api/admin/super-admins'))
|
||
})
|
||
|
||
test('新增回调默认订阅小壹交付事件并保持停用', async () => {
|
||
const user = userEvent.setup()
|
||
render(<SuperAdminsTab showToast={jest.fn()} />)
|
||
await user.click(await screen.findByRole('button', { name: /素材外发/ }))
|
||
const choice = screen.getByRole('checkbox', { name: /小壹自动上架素材入库/ })
|
||
expect(choice).toBeChecked()
|
||
expect(screen.getByRole('checkbox', { name: '创建后立即启用' })).not.toBeChecked()
|
||
await user.click(choice)
|
||
expect(choice).not.toBeChecked()
|
||
})
|
||
|
||
test('可测试已停用的目标,显示探针状态和历史投递错误', async () => {
|
||
const user = userEvent.setup()
|
||
const fetchMock = jest.mocked(global.fetch)
|
||
const fallback = fetchMock.getMockImplementation()!
|
||
fetchMock.mockImplementation((input, init) => {
|
||
const url = String(input)
|
||
if (url === '/api/admin/auto-listing-targets/test') {
|
||
return Promise.resolve({ ok: true, json: async () => ({ success: true, statusCode: 204 }) } as Response)
|
||
}
|
||
if (url.startsWith('/api/admin/auto-listing-targets?')) {
|
||
return Promise.resolve({ ok: true, json: async () => ({
|
||
data: {
|
||
targets: [{
|
||
id: 'target-1', name: '上架系统', callbackUrl: 'https://listing.example.com/callback',
|
||
isActive: false, authType: 'header', authConfig: { headerName: 'X-Signature' },
|
||
authMasked: '自定义请求头 · 已配置', eventTypes: ['xiaoyi.auto-listing.material.ready'],
|
||
updatedAt: '2026-12-04T08:00:00.000Z',
|
||
}],
|
||
recentDeliveries: [{
|
||
id: 'delivery-1', targetName: '上架系统', materialId: 'material-1',
|
||
status: 'retrying', attemptCount: 1, maxAttempts: 5,
|
||
lastError: '接收方返回 HTTP 503', lastStatusCode: 503,
|
||
}],
|
||
},
|
||
}) } as Response)
|
||
}
|
||
return fallback(input, init)
|
||
})
|
||
|
||
render(<SuperAdminsTab showToast={jest.fn()} />)
|
||
await user.click(await screen.findByRole('button', { name: /素材外发/ }))
|
||
expect(await screen.findByText('接收方返回 HTTP 503')).toBeInTheDocument()
|
||
expect(screen.getByText('已停用')).toBeInTheDocument()
|
||
await user.click(screen.getByRole('button', { name: /测试连接.*上架系统/ }))
|
||
expect(await screen.findByText('连接成功(HTTP 204)')).toBeInTheDocument()
|
||
expect(fetchMock).toHaveBeenCalledWith('/api/admin/auto-listing-targets/test', expect.objectContaining({
|
||
method: 'POST', body: JSON.stringify({ id: 'target-1' }),
|
||
}))
|
||
|
||
await user.click(screen.getByRole('button', { name: '编辑上架系统' }))
|
||
expect(screen.getByLabelText('请求头值')).toHaveAttribute('type', 'password')
|
||
expect(screen.getByLabelText('请求头值')).toHaveValue('')
|
||
})
|
||
|
||
test('可以创建只能拉取小壹素材并回传状态的专用 Key', async () => {
|
||
const user = userEvent.setup()
|
||
const fetchMock = global.fetch as jest.Mock
|
||
const fallback = fetchMock.getMockImplementation()!
|
||
fetchMock.mockImplementation((input: RequestInfo | URL, init?: RequestInit) =>
|
||
init?.method === 'POST'
|
||
? Promise.resolve({ ok: true, json: async () => ({ data: { key: 'isolated-test-key' } }) } as Response)
|
||
: fallback(input, init),
|
||
)
|
||
render(<SuperAdminsTab showToast={jest.fn()} />)
|
||
await user.click(await screen.findByRole('button', { name: /权限与凭证/ }))
|
||
await user.click(screen.getByRole('button', { name: '创建素材平台 API Key' }))
|
||
await user.type(screen.getByPlaceholderText('例如:任务系统'), '自动上架')
|
||
await user.click(screen.getByRole('checkbox', { name: '仅允许拉取小壹素材和回传上架状态' }))
|
||
await user.click(screen.getByRole('button', { name: '创建' }))
|
||
await screen.findByText('isolated-test-key')
|
||
expect(fetchMock).toHaveBeenCalledWith('/api/admin/api-keys', expect.objectContaining({
|
||
method: 'POST',
|
||
body: JSON.stringify({ name: '自动上架', scope: 'xiaoyi_auto_listing' }),
|
||
}))
|
||
})
|
||
|
||
test('超级管理员可按需查看并再次隐藏完整 API Key', async () => {
|
||
const user = userEvent.setup()
|
||
;(global.fetch as jest.Mock).mockImplementation((input: RequestInfo | URL) => {
|
||
const url = String(input)
|
||
if (url === '/api/admin/super-admins') {
|
||
return Promise.resolve({
|
||
ok: true,
|
||
json: async () => ({ superAdmins: [], count: 0, canAdd: true }),
|
||
} as Response)
|
||
}
|
||
if (url === '/api/admin/api-keys/key-1/reveal') {
|
||
return Promise.resolve({
|
||
ok: true,
|
||
json: async () => ({ data: { key: '1234567890abcdef1234567890abcdef' } }),
|
||
} as Response)
|
||
}
|
||
if (url.includes('/api/admin/api-keys')) {
|
||
return Promise.resolve({
|
||
ok: true,
|
||
json: async () => ({
|
||
data: [{
|
||
id: 'key-1',
|
||
name: '外部任务系统',
|
||
key: '12345678...',
|
||
canReveal: true,
|
||
isActive: true,
|
||
createdBy: 'admin-1',
|
||
lastUsedAt: null,
|
||
createdAt: '2026-08-29T00:00:00.000Z',
|
||
}],
|
||
}),
|
||
} as Response)
|
||
}
|
||
return Promise.resolve({ ok: true, json: async () => ({ data: [] }) } as Response)
|
||
})
|
||
|
||
render(<SuperAdminsTab showToast={jest.fn()} />)
|
||
await user.click(await screen.findByRole('button', { name: /权限与凭证/ }))
|
||
await screen.findByText('外部任务系统')
|
||
const revealButton = screen.getByTitle('查看完整 Key')
|
||
await user.click(revealButton)
|
||
|
||
expect(await screen.findByText('1234567890abcdef1234567890abcdef')).toBeInTheDocument()
|
||
await user.click(screen.getByTitle('隐藏完整 Key'))
|
||
expect(screen.queryByText('1234567890abcdef1234567890abcdef')).not.toBeInTheDocument()
|
||
})
|
||
})
|