gongxiangp2/lib/__tests__/connect-salary-market-intent.test.ts

86 lines
5.8 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 { connectCreativeMarketIntent, updateCreativeMarketIntentMonthDescription } from '../connect-salary-market-intent'
import { INITIAL_SALARY_POSITIONS } from '../salary-position-seeds'
import { updateSalaryPosition } from '../salary-positions'
import { requireSalaryAccess } from '../personnel-salary'
import { LEGACY_MARKET_INTENT_DESCRIPTION, MARKET_INTENT_DESCRIPTION } from '../salary-market-intent-contract'
jest.mock('../prisma', () => ({ prisma: { salaryPositionConfig: { findUnique: jest.fn() }, salaryPositionConfigAudit: { findMany: jest.fn() } } }))
jest.mock('../personnel-salary', () => {
const actual = jest.requireActual('../personnel-salary')
return { ...actual, requireSalaryAccess: jest.fn() }
})
jest.mock('../salary-positions', () => ({ ...jest.requireActual('../salary-positions'), updateSalaryPosition: jest.fn() }))
const find = prisma.salaryPositionConfig.findUnique as jest.Mock, audit = prisma.salaryPositionConfigAudit.findMany as jest.Mock, update = updateSalaryPosition as jest.Mock
const oldPosition = () => {
const seed = INITIAL_SALARY_POSITIONS[1], value = structuredClone(seed.value)
value.dimensions[1].metrics[0].source = { kind: 'survey', templateCode: 'project_result_validation' }
value.dimensions[1].metrics[0].description = '原有来源'
return { ...value, id: seed.id, revision: 7, updatedAt: new Date(), deletedAt: null }
}
beforeEach(() => { jest.resetAllMocks(); find.mockResolvedValue(oldPosition()); audit.mockResolvedValue([]); update.mockImplementation(async (id, input) => ({ id, revision: input.revision + 1 })) })
test('仅改变稳定ID目标来源及说明,保留其他岗位配置,并走版本/审计服务', async () => {
expect(await connectCreativeMarketIntent('admin')).toMatchObject({ changed: true, revision: 8 })
expect(requireSalaryAccess).toHaveBeenCalledWith('admin', true)
const before = oldPosition(), input = update.mock.calls[0][1]
expect(input.revision).toBe(7)
expect(input.dimensions[1].metrics[0].source).toEqual({ kind: 'market-intent-top3' })
input.dimensions[1].metrics[0].source = before.dimensions[1].metrics[0].source
input.dimensions[1].metrics[0].description = before.dimensions[1].metrics[0].description
expect(input.dimensions).toEqual(before.dimensions)
})
test('重复执行或之后人工切回调研,不再次覆盖已应用的配置', async () => {
audit.mockResolvedValue([{ after: INITIAL_SALARY_POSITIONS[1].value }])
expect(await connectCreativeMarketIntent('admin')).toMatchObject({ changed: false, reason: 'already-applied' })
expect(update).not.toHaveBeenCalled()
})
test('人工选择其他来源、并发版本冲突及权限拒绝不绕过', async () => {
const position = oldPosition(); position.dimensions[1].metrics[0].source = { kind: 'survey', templateCode: 'different' }; find.mockResolvedValue(position)
await expect(connectCreativeMarketIntent('admin')).rejects.toMatchObject({ status: 409 })
find.mockResolvedValue(oldPosition()); update.mockRejectedValue({ status: 409 })
await expect(connectCreativeMarketIntent('admin')).rejects.toMatchObject({ status: 409 })
;(requireSalaryAccess as jest.Mock).mockRejectedValue({ status: 403 }); find.mockClear()
await expect(connectCreativeMarketIntent('reader')).rejects.toMatchObject({ status: 403 })
expect(find).not.toHaveBeenCalled()
})
const legacyMonthPosition = () => {
const value = oldPosition()
value.dimensions[1].metrics[0].source = { kind: 'market-intent-top3' }
value.dimensions[1].metrics[0].description = LEGACY_MARKET_INTENT_DESCRIPTION
return value
}
test('上月口径仅更新稳定目标的标准说明,保留来源、权重与其他配置并递增审计版本', async () => {
const before = legacyMonthPosition()
find.mockResolvedValue(structuredClone(before))
expect(await updateCreativeMarketIntentMonthDescription('admin')).toMatchObject({ changed: true, revision: 8 })
expect(requireSalaryAccess).toHaveBeenCalledWith('admin', true)
const input = update.mock.calls[0][1]
expect(input.revision).toBe(7)
expect(input.dimensions[1].metrics[0].description).toBe(MARKET_INTENT_DESCRIPTION)
input.dimensions[1].metrics[0].description = LEGACY_MARKET_INTENT_DESCRIPTION
expect(input.dimensions).toEqual(before.dimensions)
})
test('已更新、人工说明、其他来源及移除目标均跳过,不创建或重建内容', async () => {
const updated = legacyMonthPosition(); updated.dimensions[1].metrics[0].description = MARKET_INTENT_DESCRIPTION
const custom = legacyMonthPosition(); custom.dimensions[1].metrics[0].description = '人工维护说明'
const otherSource = oldPosition()
const removed = legacyMonthPosition(); removed.dimensions[1].metrics.shift()
for (const value of [null, { ...legacyMonthPosition(), deletedAt: new Date() }, updated, custom, otherSource, removed]) {
find.mockResolvedValue(value)
expect(await updateCreativeMarketIntentMonthDescription('admin')).toMatchObject({ changed: false })
}
expect(update).not.toHaveBeenCalled()
})
test('说明曾应用后人工改回旧文案也不覆盖;权限和并发冲突继续生效', async () => {
find.mockResolvedValue(legacyMonthPosition())
audit.mockResolvedValue([{ after: INITIAL_SALARY_POSITIONS[1].value }])
expect(await updateCreativeMarketIntentMonthDescription('admin')).toMatchObject({ changed: false, reason: 'already-applied' })
expect(update).not.toHaveBeenCalled()
audit.mockResolvedValue([]); update.mockRejectedValue({ status: 409 })
await expect(updateCreativeMarketIntentMonthDescription('admin')).rejects.toMatchObject({ status: 409 })
;(requireSalaryAccess as jest.Mock).mockRejectedValue({ status: 403 }); find.mockClear()
await expect(updateCreativeMarketIntentMonthDescription('reader')).rejects.toMatchObject({ status: 403 })
expect(find).not.toHaveBeenCalled()
})