gongxiangp2/lib/__tests__/on-time-delivery-exemptions.test.ts

47 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** @jest-environment node */
import { prisma } from '../prisma'
import { getOnTimeDeliveryInsights, getOnTimeDeliverySummary, normalizeDetailFormFields } from '../ding-workflow-sync'
import { readArtPointConfiguration } from '../art-workflow-point-configuration'
jest.mock('../prisma', () => ({ prisma: { user: { findUnique: jest.fn() }, dingWorkflowSnapshot: { findMany: jest.fn() }, dingWorkflowSyncState: { findUnique: jest.fn() } } }))
jest.mock('../brand-center-access', () => ({ resolveBrandCenterAccessForUser: jest.fn().mockResolvedValue(true) }))
jest.mock('../art-workflow-point-configuration', () => ({ readArtPointConfiguration: jest.fn() }))
const session = { user: { id: 'reader', email: 'reader@example.test', name: 'Reader', role: 'USER' as const }, expires: '' }
const config = readArtPointConfiguration as jest.Mock
const row = (id: string, finish = '2026-10-24T08:20:00Z') => ({
processInstanceId: id, businessId: id, originatorUserId: null, status: 'COMPLETED', result: 'agree',
processCode: 'PROC-3571C915-0506-4490-A91A-8AA7EA33F838', sourceCreatedAtRaw: '2026-10-15T05:36:00Z',
formFields: normalizeDetailFormFields({ planningTaskStats: JSON.stringify([{ rowValue: [{ label: '策划类型', value: '新品/优化策划全案(策划方案+主图sku详情全套)' }, { label: '策划数量', value: '1' }] }]) }),
activeTasks: [
{ taskId: 'assign', nodeName: '品牌策划主管分配任务', createdAt: '2026-10-15T05:36:00Z' },
{ taskId: 'review', nodeName: '品牌策划主管审核', reviewPassed: true, isActive: false, finishedAt: finish },
],
})
beforeEach(() => {
jest.clearAllMocks()
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({ isActive: true, status: 'active' })
;(prisma.dingWorkflowSyncState.findUnique as jest.Mock).mockResolvedValue(null)
config.mockResolvedValue({ deductions: [], overtimeExemptions: [{ workflowKey: 'exempt', name: 'someone', role: 'planner' }] })
})
test('中心总览和明细采用相同整流程免扣判定,撤销可恢复,期间和分母不扩大', async () => {
const pending = { ...row('pending'), activeTasks: [] }
;(prisma.dingWorkflowSnapshot.findMany as jest.Mock).mockResolvedValue([
row('exempt'), row('late'), pending, { ...row('running'), status: 'RUNNING' },
{ ...row('rejected'), result: 'REFUSE' }, row('november', '2026-11-02T01:00:00Z'),
])
const details = await getOnTimeDeliveryInsights(session, { month: '2026-10' })
expect(details.summary).toMatchObject({ onTime: 1, overtime: 1, total: 2, rate: 50 })
expect(details.items.find(item => item.businessId === 'exempt')?.assessment).toMatchObject({ status: 'on_time', overtimeExempted: true, actualDuration: 9.11, standardDuration: 7 })
expect(await getOnTimeDeliverySummary(session, { month: '2026-10' })).toEqual(details.summary)
config.mockResolvedValue({ deductions: [], overtimeExemptions: [] })
expect(await getOnTimeDeliverySummary(session, { month: '2026-10' })).toMatchObject({ onTime: 0, overtime: 2, total: 2, rate: 0 })
})
test('免扣读取失败返回可重试503,权限失败前不读取配置和业务数据', async () => {
config.mockRejectedValue(new Error('offline'))
await expect(getOnTimeDeliverySummary(session)).rejects.toMatchObject({ status: 503 })
expect(prisma.dingWorkflowSnapshot.findMany).not.toHaveBeenCalled()
config.mockClear()
await expect(getOnTimeDeliverySummary(null)).rejects.toMatchObject({ status: 401 })
expect(config).not.toHaveBeenCalled()
})