131 lines
7.1 KiB
TypeScript
131 lines
7.1 KiB
TypeScript
/** @jest-environment node */
|
||
import { collectSalaryDashboardPoints, readSalaryDashboardPoints } from '../salary-dashboard-points'
|
||
|
||
jest.mock('../prisma', () => ({ prisma: {} }))
|
||
const people = [
|
||
{ id: 'a', realName: '甲', flowerName: '小甲' },
|
||
{ id: 'b', realName: '乙', flowerName: '小乙' },
|
||
{ id: 'c', realName: '丙', flowerName: '小丙' },
|
||
]
|
||
const contribution = (workflowKey: string, finalPoints: number, extra = {}) => ({ workflowKey, name: '甲', finalPoints, ...extra })
|
||
const flow = (businessId: string, reviewFinishTime: string | null, scores = [contribution(businessId, 10)]) => ({
|
||
businessId, reviewFinishTime, scoreContributions: scores,
|
||
})
|
||
const payload = (workflows: Array<ReturnType<typeof flow> & { finishTime?: string }>, extra = {}) => ({ generatedAt: '2026-11-26 12:00:00', workflows, ...extra })
|
||
const collect = (data: unknown, month = '2026-09') => collectSalaryDashboardPoints(data, [month], people).get(month)!
|
||
|
||
test('只汇总原看板最终贡献,不依赖旧快照表单、最终完结时间或本地重算', () => {
|
||
const data = payload([
|
||
flow('a', '2026-09-01 00:00:00', [contribution('a', 1.23), contribution('a', -4.56, { name: ' 小甲 ' }), contribution('a', 0, { name: '乙' })]),
|
||
{ ...flow('b', '2026-08-31 23:59:59'), finishTime: '2026-09-01 01:00:00' },
|
||
{ ...flow('c', null), finishTime: '2026-09-01 01:00:00' },
|
||
flow('d', '2026-09-02', []),
|
||
])
|
||
expect(collect(data).points).toEqual(new Map([['a', -3.33], ['b', 0]]))
|
||
expect(collect(data).points.has('c')).toBe(false)
|
||
})
|
||
|
||
test('源日历月份与带时区时间均使用上海月界,跨年正确', () => {
|
||
const data = payload([
|
||
flow('a', '2026-12-31 23:59:59'),
|
||
flow('b', '2026-12-31T16:00:00Z'),
|
||
flow('c', '2027-01-31T23:59:59+08:00'),
|
||
flow('d', '2027-02-01T00:00:00+08:00'),
|
||
])
|
||
const results = collectSalaryDashboardPoints(data, ['2026-12', '2027-01', '2027-02'], people)
|
||
expect(Array.from(results, ([month, result]) => [month, result.points.get('a')])).toEqual([['2026-12', 10], ['2027-01', 20], ['2027-02', 10]])
|
||
})
|
||
|
||
test('人工加扣分按原因和金额扣除已嵌入项,余项按审核月或未知流程编号日期纳入', () => {
|
||
const key = '202609010001'
|
||
const data = payload([
|
||
flow(key, '2026-09-10', [contribution(key, 11, { manualAdditionPoints: 3, manualAdditionReasons: '奖励', manualDeductionPoints: 2 })]),
|
||
flow('20260902-known-other', '2026-08-01'),
|
||
flow('20260903-known-unreviewed', null),
|
||
], { manualDeductions: [
|
||
{ workflowKey: key, name: '甲', type: 'add', points: 3, reason: '奖励' },
|
||
{ workflowKey: key, name: '甲', points: 1, reason: '扣1' },
|
||
{ workflowKey: key, name: '甲', points: 1, reason: '扣2' },
|
||
{ workflowKey: key, name: '甲', type: '加分', points: 4 },
|
||
{ workflowKey: '20260904-independent', name: '小甲', points: 5 },
|
||
{ workflowKey: '20260902-known-other', name: '甲', points: 50 },
|
||
{ workflowKey: '20260903-known-unreviewed', name: '甲', points: 50 },
|
||
{ workflowKey: '20260804-other', name: '甲', points: 50 },
|
||
{ workflowKey: key, name: '甲', points: 50, enabled: false },
|
||
{ workflowKey: '20260530-before-cutoff', name: '甲', points: 50 },
|
||
] })
|
||
expect(collect(data).points.get('a')).toBe(10)
|
||
})
|
||
|
||
test('同一人工调整被多个角色引用时只在独立调整阶段去重,保留源最终贡献', () => {
|
||
const key = '20260801-review-september'
|
||
const data = payload([
|
||
flow(key, '2026-09-10', [
|
||
contribution(key, 10, { manualAdditionPoints: 2, manualAdditionReasons: '奖1;奖2' }),
|
||
contribution(key, 20, { manualAdditionPoints: 2, manualAdditionReasons: '奖1;奖2' }),
|
||
]),
|
||
], { meta: { manualDeductions: [
|
||
{ workflowKey: key, name: '甲', type: 'plus', score: '1', reason: '奖1' },
|
||
{ workflowKey: key, name: '甲', type: 'addition', score: '1', reason: '奖2' },
|
||
{ workflowKey: key, name: '乙', deductionPoints: '2.25' },
|
||
] } })
|
||
expect(collect(data).points).toEqual(new Map([['a', 30], ['b', -2.25]]))
|
||
})
|
||
|
||
test('重复人工配置按原排行逐项参与,已嵌入的同键配置均跳过', () => {
|
||
const key = '20260901-repeat'
|
||
const adjustment = { workflowKey: key, name: '甲', type: 'add', points: 2 }
|
||
expect(collect(payload([flow(key, '2026-09-01')], { manualDeductions: [adjustment, adjustment] })).points.get('a')).toBe(14)
|
||
expect(collect(payload([flow(key, '2026-09-01', [contribution(key, 12, { manualAdditionPoints: 2 })])], { manualDeductions: [adjustment, adjustment] })).points.get('a')).toBe(12)
|
||
})
|
||
|
||
test('四月及更早月份使用源历史净分,五月起不加入,负历史净分截为0', () => {
|
||
const data = payload([], { historicalScores: { 甲: 10, 乙: 2 }, historicalOvertimePenalties: { 甲: 3.25, 乙: 3 } })
|
||
expect(collect(data, '2026-04').points).toEqual(new Map([['a', 6.75], ['b', 0]]))
|
||
expect(collect(data, '2026-05').points.size).toBe(0)
|
||
})
|
||
|
||
test('只按去空格后的姓名或花名精确匹配,冲突人员整月留空且不覆盖其他人员', () => {
|
||
const data = payload([flow('a', '2026-09-01', [
|
||
contribution('a', 10), contribution('a', 20, { name: '小甲' }),
|
||
contribution('a', 30, { name: ' 小乙 ' }), contribution('a', 99, { name: '丙丁' }),
|
||
contribution('a', 99, { name: 'display-only' }),
|
||
])])
|
||
const result = collectSalaryDashboardPoints(data, ['2026-09'], [
|
||
...people, { id: 'd', realName: '丁', flowerName: '甲' },
|
||
]).get('2026-09')!
|
||
expect(result.points).toEqual(new Map([['b', 30]]))
|
||
expect(result.ambiguousEmployeeIds).toEqual(new Set(['a', 'd']))
|
||
})
|
||
|
||
test('84条审核流程的142条最终贡献全部汇总,空贡献与缺失人员不补零', () => {
|
||
const data = payload(Array.from({ length: 84 }, (_, index) => {
|
||
const key = `flow-${index}`
|
||
return flow(key, '2026-09-20', [
|
||
contribution(key, .1), ...(index < 58 ? [contribution(key, .2, { name: '乙' })] : []),
|
||
])
|
||
}))
|
||
expect(collect(data).points).toEqual(new Map([['a', 8.4], ['b', 11.6]]))
|
||
expect(collect(payload([])).points.size).toBe(0)
|
||
})
|
||
|
||
test.each([
|
||
payload([flow('duplicate', '2026-09-01'), flow('duplicate', '2026-09-02')]),
|
||
payload([flow('key', '2026-09-01', [contribution('other', 2)])]),
|
||
payload([flow('key', 'invalid date')]),
|
||
payload([flow('key', '2026-02-30')]),
|
||
payload([flow('key', '2026-09-01', [contribution('key', NaN)])]),
|
||
payload([flow('key', '2026-09-01', [contribution('key', Infinity)])]),
|
||
{ generatedAt: 'now', workflows: [{ businessId: 'key' }] },
|
||
])('错误或重复源记录使本次积分失败,不能跳过记录后返回部分结果 %#', async data => {
|
||
await expect(readSalaryDashboardPoints(['2026-09'], Promise.resolve(people), Promise.resolve(data))).rejects.toMatchObject({
|
||
status: 503, code: 'SALARY_POINTS_UNAVAILABLE',
|
||
})
|
||
})
|
||
|
||
test('源读取失败返回可重试503,恢复后使用新结果', async () => {
|
||
await expect(readSalaryDashboardPoints(['2026-09'], Promise.resolve(people), Promise.reject(new Error('private URL')))).rejects.toMatchObject({ status: 503, code: 'SALARY_POINTS_UNAVAILABLE' })
|
||
const results = await readSalaryDashboardPoints(['2026-09'], Promise.resolve(people), Promise.resolve(payload([flow('a', '2026-09-01')])))
|
||
expect(results.get('2026-09')!.points.get('a')).toBe(10)
|
||
})
|