gongxiangp2/lib/__tests__/salary-performance-versions.test.ts

90 lines
6.6 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 { requireSalaryAccess } from '../personnel-salary'
import { updateSalaryPosition, createSalaryPosition } from '../salary-positions'
import { INITIAL_SALARY_POSITIONS } from '../salary-position-seeds'
jest.mock('../prisma', () => ({ prisma: { $transaction: jest.fn() } }))
jest.mock('../personnel-salary', () => ({ ...jest.requireActual('../personnel-salary'), requireSalaryAccess: jest.fn() }))
const id = INITIAL_SALARY_POSITIONS[0].id
const versionId = '30000000-0000-4000-8000-000000000001'
const fields = INITIAL_SALARY_POSITIONS[0].value
let row: any
const db = {
salaryPositionConfig: {findUnique: jest.fn(),findUniqueOrThrow: jest.fn(),count:jest.fn(),updateMany:jest.fn(),create:jest.fn()},
salaryPerformanceVersion: {create:jest.fn(),updateMany:jest.fn()},
performanceSurveyTemplate:{count:jest.fn()},salaryPositionConfigAudit:{create:jest.fn()},
}
const request = (extra = {}) => ({operation:'update-version',revision:4,versionId,versionRevision:2,description:'修正说明',dimensions:fields.dimensions,confirmedImpact:{effectiveMonth:'0000-00',endsBeforeMonth:'2026-11'},...extra})
beforeEach(()=>{
jest.resetAllMocks()
row={...fields,id,revision:4,updatedAt:new Date(),deletedAt:null,personnelPositionId:null,jobGrade:'',performanceVersions:[
{id:'30000000-0000-4000-8000-000000000002',effectiveMonth:'2026-11',revision:1,description:'11月',dimensions:fields.dimensions},
{id:versionId,effectiveMonth:'0000-00',revision:2,description:'历史',dimensions:fields.dimensions},
]}
;(prisma.$transaction as jest.Mock).mockImplementation(work=>work(db))
db.salaryPositionConfig.findUnique.mockImplementation(async()=>structuredClone(row))
db.salaryPositionConfig.findUniqueOrThrow.mockImplementation(async()=>structuredClone(row))
db.salaryPositionConfig.count.mockResolvedValue(0)
db.salaryPositionConfig.updateMany.mockResolvedValue({count:1})
db.salaryPerformanceVersion.updateMany.mockImplementation(async({where,data})=>{
Object.assign(row.performanceVersions.find((v:any)=>v.id===where.id),{...data,revision:3})
return {count:1}
})
db.salaryPerformanceVersion.create.mockImplementation(async({data})=>{
row.performanceVersions.push({...data,id:'30000000-0000-4000-8000-000000000003',revision:1})
row.performanceVersions.sort((a: any, b: any) => b.effectiveMonth.localeCompare(a.effectiveMonth))
return data
})
db.performanceSurveyTemplate.count.mockResolvedValue(4)
})
test('历史修正必须确认精确影响区间,未确认不进入写入',async()=>{
await expect(updateSalaryPosition(id,request({confirmedImpact:undefined}),'admin')).rejects.toMatchObject({status:409,code:'PERFORMANCE_IMPACT_REQUIRED'})
await expect(updateSalaryPosition(id,request({confirmedImpact:{effectiveMonth:'0000-00',endsBeforeMonth:null}}),'admin')).rejects.toMatchObject({status:409})
expect(db.salaryPerformanceVersion.updateMany).not.toHaveBeenCalled()
expect(db.salaryPositionConfigAudit.create).not.toHaveBeenCalled()
})
test('指定历史版本保留稳定ID,在同一事务保存完整快照与审计,原停用来源可保留',async()=>{
const saved=await updateSalaryPosition(id,request(),'admin')
expect(saved.performanceVersion).toMatchObject({id:versionId,effectiveMonth:'0000-00',endsBeforeMonth:'2026-11',revision:3})
expect(db.performanceSurveyTemplate.count).not.toHaveBeenCalled()
expect(db.salaryPositionConfigAudit.create).toHaveBeenCalledWith({data:expect.objectContaining({action:'UPDATE_PERFORMANCE_VERSION',before:expect.objectContaining({id:versionId,revision:2}),after:expect.objectContaining({id:versionId,revision:3,description:'修正说明'})})})
expect(prisma.$transaction).toHaveBeenCalledTimes(1)
})
test.each([{revision:3},{versionRevision:1},{versionId:'30000000-0000-4000-8000-000000000099'}])('岗位或版本修订冲突停止写入 %j',async extra=>{
await expect(updateSalaryPosition(id,request(extra),'admin')).rejects.toMatchObject({status:409,code:'REVISION_CONFLICT'})
expect(db.salaryPerformanceVersion.updateMany).not.toHaveBeenCalled()
})
test('重复实施月份被拒绝,未来版本复制来源并在审计记录新实施月',async()=>{
const body={operation:'create-version',revision:4,effectiveMonth:'2026-11',description:'新方案',dimensions:fields.dimensions,copyFromVersionId:versionId}
await expect(updateSalaryPosition(id,body,'admin')).rejects.toMatchObject({status:409,code:'DUPLICATE_PERFORMANCE_MONTH'})
const saved=await updateSalaryPosition(id,{...body,effectiveMonth:'2027-01'},'admin')
expect(saved.performanceVersion?.effectiveMonth).toBe('2027-01')
expect(db.salaryPositionConfigAudit.create).toHaveBeenCalledWith({data:expect.objectContaining({action:'CREATE_PERFORMANCE_VERSION',after:expect.objectContaining({effectiveMonth:'2027-01'})})})
})
test('通用策划可补建切换职级前的历史版本,切换后只允许职级方案', async () => {
row.performanceVersions[0].dimensions = []
db.salaryPositionConfig.count.mockResolvedValue(2)
const body = { operation: 'create-version', revision: 4, copyFromVersionId: versionId,
description: '补建历史方案', dimensions: fields.dimensions }
await expect(updateSalaryPosition(id, { ...body, effectiveMonth: '2026-12' }, 'admin'))
.rejects.toMatchObject({ status: 409, code: 'GRADE_PERFORMANCE_VERSION_REQUIRED' })
const saved = await updateSalaryPosition(id, { ...body, effectiveMonth: '2026-09',
confirmedImpact: { effectiveMonth: '2026-09', endsBeforeMonth: '2026-11' } }, 'admin')
expect(saved.performanceVersion).toMatchObject({ effectiveMonth: '2026-09', endsBeforeMonth: '2026-11' })
})
test('只读权限被拒绝,版本化方案拒绝旧写法覆盖历史',async()=>{
;(requireSalaryAccess as jest.Mock).mockRejectedValueOnce(Object.assign(new Error('denied'),{status:403}))
await expect(updateSalaryPosition(id,request(),'reader')).rejects.toMatchObject({status:403})
expect(db.salaryPositionConfig.findUnique).not.toHaveBeenCalled()
await expect(updateSalaryPosition(id,{...fields,revision:4},'admin')).rejects.toMatchObject({status:409,code:'PERFORMANCE_VERSION_REQUIRED'})
expect(db.salaryPerformanceVersion.updateMany).not.toHaveBeenCalled()
})
test('数据库并发修订失败时不记成功审计,当前新岗位也需确认影响月份',async()=>{
db.salaryPerformanceVersion.updateMany.mockResolvedValue({count:0})
await expect(updateSalaryPosition(id,request(),'admin')).rejects.toMatchObject({status:409})
expect(db.salaryPositionConfigAudit.create).not.toHaveBeenCalled()
await expect(createSalaryPosition({...fields,effectiveMonth:'2026-11'},'admin')).rejects.toMatchObject({status:409,code:'PERFORMANCE_IMPACT_REQUIRED'})
})