91 lines
5.2 KiB
TypeScript
91 lines
5.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { http } from '@/shared/api/http'
|
|
import {
|
|
cancelTask,
|
|
changeTemplateStatus,
|
|
createNotification,
|
|
getMyNotification,
|
|
getMyUnreadNotificationCount,
|
|
getTask,
|
|
listMyNotifications,
|
|
listTasks,
|
|
listTemplates,
|
|
markAllMyNotificationsRead,
|
|
markMyNotificationRead,
|
|
retryTask,
|
|
updateTemplate,
|
|
} from './notificationCenter'
|
|
|
|
vi.mock('@/shared/api/http', () => ({
|
|
http: { get: vi.fn(), post: vi.fn(), put: vi.fn() },
|
|
unwrapApiResponse: <T>(response: { data: T }) => response.data,
|
|
}))
|
|
|
|
const envelope = <T>(data: T) => ({ code: '200', data })
|
|
const template = { id: 12, templateCode: 'ORDER_SENT', templateName: '订单已发货', category: 'ORDER', channel: 'IN_APP' as const, titleTemplate: '已发货', bodyTemplate: '正文', variableWhitelist: [], status: 'DRAFT' as const, version: 1 }
|
|
const task = { id: 13, taskNo: 'NT-13', category: 'ORDER', title: '已发货', status: 'CREATED' as const, recipientCount: 2, scheduledAt: null, sentAt: null, duplicated: false }
|
|
const taskDetail = { id: 13, taskNo: 'NT-13', category: 'ORDER', title: '已发货', status: 'DEAD' as const, recipientCount: 1, attemptCount: 3, scheduledAt: null, sentAt: null, lastError: 'gateway timeout', recipients: [{ id: 21, displayName: '客户成员', appCode: 'MERCHANT', status: 'FAILED', failureReason: 'gateway timeout', deliveredAt: null }] }
|
|
|
|
describe('notificationCenter API', () => {
|
|
beforeEach(() => vi.clearAllMocks())
|
|
|
|
it('posts the frozen create-notification DTO unchanged', async () => {
|
|
vi.mocked(http.post).mockResolvedValueOnce(envelope(task))
|
|
const payload = { tenantId: '0', templateCode: 'ORDER_SENT', recipientType: 'PLATFORM_ROLE' as const, roleCode: 'PLATFORM_ADMIN', variables: {}, scheduledAt: null, idempotencyKey: 'notification-1' }
|
|
expect((await createNotification(payload)).id).toBe('13')
|
|
expect(http.post).toHaveBeenCalledWith('/platform/v1/notifications', payload)
|
|
})
|
|
|
|
it('uses template list, update, and one-way status paths', async () => {
|
|
vi.mocked(http.get).mockResolvedValueOnce(envelope({ records: [template], page: 1, size: 20, total: 1, pages: 1 }))
|
|
vi.mocked(http.put).mockResolvedValue(envelope(template))
|
|
await listTemplates({ page: 1, size: 20, status: 'DRAFT' })
|
|
const update = { templateName: '新名称', category: 'ORDER', titleTemplate: '标题', bodyTemplate: '正文', variableWhitelist: [], version: 1 }
|
|
await updateTemplate('12', update)
|
|
await changeTemplateStatus('12', 'ACTIVE', 1)
|
|
expect(http.get).toHaveBeenCalledWith('/platform/v1/notification-templates', { params: { page: 1, size: 20, status: 'DRAFT' } })
|
|
expect(http.put).toHaveBeenNthCalledWith(1, '/platform/v1/notification-templates/12', update)
|
|
expect(http.put).toHaveBeenNthCalledWith(2, '/platform/v1/notification-templates/12/status', { targetStatus: 'ACTIVE', version: 1 })
|
|
})
|
|
|
|
it('uses task list, cancel, and retry paths without request bodies', async () => {
|
|
vi.mocked(http.get).mockResolvedValueOnce(envelope({ records: [task], page: 1, size: 20, total: 1, pages: 1 }))
|
|
vi.mocked(http.put).mockResolvedValue(envelope(task))
|
|
await listTasks({ page: 1, size: 20, status: 'CREATED' })
|
|
await cancelTask('13')
|
|
await retryTask('13')
|
|
expect(http.get).toHaveBeenCalledWith('/platform/v1/notification-tasks', { params: { page: 1, size: 20, status: 'CREATED' } })
|
|
expect(http.put).toHaveBeenNthCalledWith(1, '/platform/v1/notification-tasks/13/cancel')
|
|
expect(http.put).toHaveBeenNthCalledWith(2, '/platform/v1/notification-tasks/13/retry')
|
|
})
|
|
|
|
it('loads task failure evidence and normalizes nested long ids', async () => {
|
|
vi.mocked(http.get).mockResolvedValueOnce(envelope(taskDetail))
|
|
const result = await getTask('13')
|
|
expect(http.get).toHaveBeenCalledWith('/platform/v1/notification-tasks/13')
|
|
expect(result.id).toBe('13')
|
|
expect(result.recipients[0]?.id).toBe('21')
|
|
expect(result.lastError).toBe('gateway timeout')
|
|
})
|
|
it('uses platform inbox paths and normalizes inbox ids', async () => {
|
|
const inbox = { id: 14, category: 'ORDER', title: 'order notice', summary: 'summary', status: 'UNREAD' as const, sentAt: '2026-08-27T10:00:00', readAt: null }
|
|
const detail = { ...inbox, body: 'body', sourceType: null, sourceId: null }
|
|
vi.mocked(http.get)
|
|
.mockResolvedValueOnce(envelope({ records: [inbox], page: 1, size: 20, total: 1, pages: 1 }))
|
|
.mockResolvedValueOnce(envelope(detail))
|
|
.mockResolvedValueOnce(envelope({ unreadCount: 1 }))
|
|
vi.mocked(http.put)
|
|
.mockResolvedValueOnce(envelope(detail))
|
|
.mockResolvedValueOnce(envelope({ updatedCount: 1 }))
|
|
const result = await listMyNotifications({ page: 1, size: 20, status: 'UNREAD' })
|
|
await getMyNotification('14')
|
|
await markMyNotificationRead('14')
|
|
await markAllMyNotificationsRead()
|
|
expect(await getMyUnreadNotificationCount()).toBe(1)
|
|
expect(result.records[0]?.id).toBe('14')
|
|
expect(http.get).toHaveBeenNthCalledWith(1, '/platform/v1/notifications', { params: { page: 1, size: 20, status: 'UNREAD' } })
|
|
expect(http.put).toHaveBeenNthCalledWith(1, '/platform/v1/notifications/14/read')
|
|
expect(http.put).toHaveBeenNthCalledWith(2, '/platform/v1/notifications/read-all')
|
|
})
|
|
})
|