gongxiangp2/lib/__tests__/connect-salary-management-survey.test.ts

54 lines
3.9 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 } from '../salary-positions'
import { connectManagementVisualSurvey } from '../connect-salary-management-survey'
import { INITIAL_SALARY_POSITIONS } from '../salary-position-seeds'
jest.mock('../prisma', () => ({ prisma: { salaryPositionConfig: { findUnique: jest.fn() }, salaryPositionConfigAudit: { findMany: jest.fn() }, performanceSurveyTemplate: { findMany: jest.fn() } } }))
jest.mock('../personnel-salary', () => ({ ...jest.requireActual('../personnel-salary'), requireSalaryAccess: jest.fn() }))
jest.mock('../salary-positions', () => ({ ...jest.requireActual('../salary-positions'), updateSalaryPosition: jest.fn() }))
const db = prisma as unknown as Record<string, any>, update = updateSalaryPosition as jest.Mock
const before = () => ({ ...structuredClone(INITIAL_SALARY_POSITIONS[0].value), id: INITIAL_SALARY_POSITIONS[0].id, revision: 2, updatedAt: new Date(), deletedAt: null })
beforeEach(() => {
jest.resetAllMocks()
db.salaryPositionConfig.findUnique.mockResolvedValue(before())
db.performanceSurveyTemplate.findMany.mockResolvedValue([{ code: 'supervisor-visual' }])
db.salaryPositionConfigAudit.findMany.mockResolvedValue([])
update.mockImplementation(async (id, input) => ({ id, revision: input.revision + 1 }))
})
test('只改稳定主管视觉指标的来源,保留ID、权重和所有其他字段并使用版本审计服务', async () => {
expect(await connectManagementVisualSurvey('admin', 2)).toMatchObject({ changed: true, revision: 3 })
expect(requireSalaryAccess).toHaveBeenCalledWith('admin', true)
const input = update.mock.calls[0][1], original = before()
expect(input.revision).toBe(2)
expect(input.dimensions[0].metrics[0].source).toEqual({ kind: 'survey', templateCode: 'supervisor-visual' })
input.dimensions[0].metrics[0].source = original.dimensions[0].metrics[0].source
expect(input.dimensions).toEqual(original.dimensions)
})
test('重复执行跳过,曾修正后人工改回则冲突停止', async () => {
const row = before(); row.dimensions[0].metrics[0].source = { kind: 'survey', templateCode: 'supervisor-visual' }
db.salaryPositionConfig.findUnique.mockResolvedValue(row)
expect(await connectManagementVisualSurvey('admin', 2)).toMatchObject({ changed: false, revision: 2 })
db.salaryPositionConfig.findUnique.mockResolvedValue(before()); db.salaryPositionConfigAudit.findMany.mockResolvedValue([{ after: row }])
await expect(connectManagementVisualSurvey('admin', 2)).rejects.toMatchObject({ status: 409 })
expect(update).not.toHaveBeenCalled()
})
test('版本变化、来源人工变更、模板缺失或同名冲突均不写入', async () => {
await expect(connectManagementVisualSurvey('admin', 1)).rejects.toMatchObject({ status: 409 })
for (const templates of [[], [{ code: 'a' }, { code: 'b' }]]) {
db.performanceSurveyTemplate.findMany.mockResolvedValue(templates)
await expect(connectManagementVisualSurvey('admin', 2)).rejects.toMatchObject({ status: 409 })
}
db.performanceSurveyTemplate.findMany.mockResolvedValue([{ code: 'supervisor-visual' }])
const row = before(); row.dimensions[0].metrics[0].source = { kind: 'unavailable' }; db.salaryPositionConfig.findUnique.mockResolvedValue(row)
await expect(connectManagementVisualSurvey('admin', 2)).rejects.toMatchObject({ status: 409 })
expect(update).not.toHaveBeenCalled()
})
test('权限和保存时并发冲突不绕过', async () => {
update.mockRejectedValue({ status: 409 })
await expect(connectManagementVisualSurvey('admin', 2)).rejects.toMatchObject({ status: 409 })
;(requireSalaryAccess as jest.Mock).mockRejectedValue({ status: 403 }); db.salaryPositionConfig.findUnique.mockClear()
await expect(connectManagementVisualSurvey('reader', 2)).rejects.toMatchObject({ status: 403 })
expect(db.salaryPositionConfig.findUnique).not.toHaveBeenCalled()
})