gongxiangp2/components/SuperAdminsTab.test.tsx

125 lines
5.2 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)
}
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).not.toBeChecked()
await user.click(choice)
expect(choice).toBeChecked()
})
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()
})
})