249 lines
12 KiB
TypeScript
249 lines
12 KiB
TypeScript
import { render, screen, waitFor, within } 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('交互搜索提交完整 ID,与状态共用,清空后恢复最近记录', 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.includes('q=project-1')) {
|
||
return Promise.resolve({ ok: true, json: async () => ({
|
||
data: { targets: [], recentDeliveries: [{
|
||
id: 'delivery-1', materialId: 'material-1', taskNo: 'TASK1',
|
||
projectId: 'project-1', eventId: 'event-1', status: 'retrying',
|
||
}] },
|
||
}) } as Response)
|
||
}
|
||
return fallback(input, init)
|
||
})
|
||
|
||
render(<SuperAdminsTab showToast={jest.fn()} />)
|
||
await user.click(await screen.findByRole('button', { name: /素材外发/ }))
|
||
const search = screen.getByRole('searchbox')
|
||
await user.type(search, ' project-1 {enter}')
|
||
expect(await screen.findByText('事件 ID event-1')).toBeInTheDocument()
|
||
expect(fetchMock).toHaveBeenCalledWith('/api/admin/auto-listing-targets?status=all&q=project-1', expect.anything())
|
||
|
||
await user.click(screen.getByRole('combobox', { name: '投递状态' }))
|
||
await user.click(screen.getByRole('option', { name: '重试中' }))
|
||
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith(
|
||
'/api/admin/auto-listing-targets?status=retrying&q=project-1', expect.anything(),
|
||
))
|
||
await user.click(screen.getByRole('button', { name: '清空搜索' }))
|
||
expect(await screen.findByText('当前筛选条件下暂无投递记录')).toBeInTheDocument()
|
||
expect(search).toHaveValue('')
|
||
expect(screen.queryByText('事件 ID event-1')).not.toBeInTheDocument()
|
||
})
|
||
|
||
test('允许补发待投递、重试、已耗尽及租约过期的任务,点击后刷新列表', async () => {
|
||
const user = userEvent.setup()
|
||
const fetchMock = jest.mocked(global.fetch)
|
||
const fallback = fetchMock.getMockImplementation()!
|
||
const toast = jest.fn()
|
||
fetchMock.mockImplementation((input, init) => {
|
||
const url = String(input)
|
||
if (url === '/api/admin/external-notification-deliveries/retrying-1/retry') {
|
||
return Promise.resolve({ ok: true } as Response)
|
||
}
|
||
if (url.startsWith('/api/admin/auto-listing-targets')) {
|
||
return Promise.resolve({ ok: true, json: async () => ({
|
||
data: { targets: [], recentDeliveries: [
|
||
{ id: 'pending-1', materialId: 'pending-material', status: 'pending', canReplay: true },
|
||
{ id: 'retrying-1', materialId: 'retrying-material', status: 'retrying', canReplay: true },
|
||
{ id: 'exhausted-1', materialId: 'exhausted-material', status: 'exhausted', canReplay: true },
|
||
{ id: 'expired-1', materialId: 'expired-material', status: 'processing', canReplay: true },
|
||
{ id: 'processing-1', materialId: 'processing-material', status: 'processing', canReplay: false },
|
||
{ id: 'succeeded-1', materialId: 'succeeded-material', status: 'succeeded', canReplay: false },
|
||
] },
|
||
}) } as Response)
|
||
}
|
||
return fallback(input, init)
|
||
})
|
||
|
||
render(<SuperAdminsTab showToast={toast} />)
|
||
await user.click(await screen.findByRole('button', { name: /素材外发/ }))
|
||
const retryingRow = (await screen.findByText('素材编号 retrying-material')).closest('li')!
|
||
expect(screen.getAllByRole('button', { name: '重放投递' })).toHaveLength(4)
|
||
for (const material of ['processing-material', 'succeeded-material']) {
|
||
const row = screen.getByText('素材编号 ' + material).closest('li')!
|
||
expect(within(row).queryByRole('button', { name: '重放投递' })).not.toBeInTheDocument()
|
||
}
|
||
await user.click(within(retryingRow).getByRole('button', { name: '重放投递' }))
|
||
await waitFor(() => expect(toast).toHaveBeenCalledWith('补发任务已重新入队', 'success'))
|
||
expect(fetchMock).toHaveBeenCalledWith('/api/admin/external-notification-deliveries/retrying-1/retry', { method: 'POST' })
|
||
})
|
||
|
||
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()
|
||
})
|
||
})
|