import { INITIAL_SALARY_POSITIONS } from '../salary-position-seeds' import { percentageSchema, positionFieldsSchema, sourceFromKey, sourceKey, sourceLabel, positionAtMonth, resolvePersonnelPerformance, performancePositions, performanceVersionLabel, performanceVersionRange, type SalaryPosition } from '../salary-position-contract' import { calculatePositionScore } from '../performance-salary-pay' import { getPositionSalaryTemplate } from '../performance-salary-templates' const copy = () => JSON.parse(JSON.stringify(INITIAL_SALARY_POSITIONS[1].value)) test('经营来源可在岗位配置选择、保存与导出名称解析,主管及技术岗原来源保留', () => { const source = sourceFromKey('market-intent-top3') expect(source).toEqual({ kind: 'market-intent-top3' }) expect(sourceKey(source)).toBe('market-intent-top3') expect(sourceLabel(source, [])).toBe('业绩转化 · 上月最高3条意向率环比') expect(INITIAL_SALARY_POSITIONS[0].value.dimensions[1].metrics[0].source.kind).toBe('unavailable') expect(INITIAL_SALARY_POSITIONS[2].value.dimensions[0].metrics[1].source).toEqual({ kind: 'survey', templateCode: 'project_result_validation' }) }) test('三岗初始化完整权重,包括零权重指标', () => { for (const seed of INITIAL_SALARY_POSITIONS) expect(positionFieldsSchema.safeParse(seed.value).success).toBe(true) expect(INITIAL_SALARY_POSITIONS.map(seed => seed.value.dimensions.map(d => [d.weight, d.metrics.map(m => m.weight)]))).toEqual([ [['70', ['80', '20']], ['20', ['40', '40', '20']], ['10', ['100']]], [['25', ['100', '0']], ['70', ['10', '90', '0']], ['5', ['100']]], [['25', ['50', '50']], ['70', ['0', '60', '40']], ['5', ['100']]], ]) }) test('百分比精确边界、空配置与无自动归一化', () => { for (const value of ['0', '0.00', '0.01', '99.99', '100.00']) expect(percentageSchema.safeParse(value).success).toBe(true) for (const value of ['-1', '100.01', '1.001', 'NaN', '', '1e2', 50]) expect(percentageSchema.safeParse(value).success).toBe(false) const value = copy(); value.dimensions = []; expect(positionFieldsSchema.safeParse(value).success).toBe(true) const invalid = copy(); invalid.dimensions[0].weight = '24.99'; expect(positionFieldsSchema.safeParse(invalid).success).toBe(false) invalid.dimensions[0].weight = '25'; invalid.dimensions[0].metrics[0].weight = '99.99'; expect(positionFieldsSchema.safeParse(invalid).success).toBe(false) }) test('重复稳定ID和名称、非法来源及岗位入口被拒绝', () => { const value = copy(); value.dimensions[0].metrics[0].id = value.dimensions[1].metrics[0].id expect(positionFieldsSchema.safeParse(value).success).toBe(false) value.dimensions = []; value.salaryRole = 'director'; expect(positionFieldsSchema.safeParse(value).success).toBe(false) const source = copy(); source.dimensions[0].metrics[0].source = { kind: 'manual', score: 100 } expect(positionFieldsSchema.safeParse(source).success).toBe(false) }) test('两层权重逐项舍入后求和,正权重缺失阻断,零权重不阻断', () => { const dimensions = copy().dimensions const scores: Record = {} for (const d of dimensions) for (const m of d.metrics) scores[m.id] = m.weight === '0' ? null : 88.885 expect(calculatePositionScore(dimensions, scores).totalScore).toBe(88.88) scores[dimensions[0].metrics[0].id] = null expect(calculatePositionScore(dimensions, scores).totalScore).toBeNull() dimensions[0].weight = '0'; dimensions[1].weight = '95' expect(calculatePositionScore(dimensions, scores).totalScore).toBe(88.88) expect(calculatePositionScore([], {}).totalScore).toBeNull() }) test.each(['ai-planning', 'ai-technical', 'management', 'photography', 'spatial-visual'] as const)('动态%s模板字段唯一,含基础字段及唯一绩效系数', salaryRole => { const template = getPositionSalaryTemplate({ ...copy(), salaryRole, id: 'job', revision: 1, updatedAt: '', personnelCount: 0 }, '2027-01') expect(new Set(template.columns.map(c => c.key)).size).toBe(template.columns.length) expect(template.columns.map(c => c.key)).toEqual(expect.arrayContaining(['name', 'nickname', 'department', 'baseSalary', 'gradeSalary', 'performanceSalary', 'performanceCoefficient', 'currentTotal'])) expect(template.columns.filter(c => c.key === 'performanceCoefficient')).toHaveLength(1) expect(template.columns.some(c => c.key === 'performanceScore' || c.label.includes('绩效得分'))).toBe(false) expect(template.columns.some(c => c.key.endsWith('Weighted') || c.label.includes('加权得分'))).toBe(false) expect(template.columns.filter(c => c.key.endsWith('Score') && c.key !== 'performanceScore')).toHaveLength(4) }) test('各项考核得分直接乘综合占比,总分等于显示值相加且无二次加权', () => { const dimensions = copy().dimensions const scores: Record = {} for (const d of dimensions) for (const m of d.metrics) scores[m.id] = 100 const result = calculatePositionScore(dimensions, scores) expect(result.contributions[dimensions[1].metrics[1].id]).toBe(63) expect(result.totalScore).toBe(100) expect(result).not.toHaveProperty('weighted') for (const d of dimensions) for (const m of d.metrics) scores[m.id] = 88.885 const rounded = calculatePositionScore(dimensions, scores) expect(rounded.totalScore).toBe(88.88) expect(rounded.totalScore).toBe(Object.values(rounded.contributions).reduce((sum, value) => sum + Math.round((value ?? 0) * 100), 0) / 100) scores[dimensions[0].metrics[1].id] = null expect(calculatePositionScore(dimensions, scores).contributions[dimensions[0].metrics[1].id]).toBeNull() scores[dimensions[0].metrics[1].id] = 0 expect(calculatePositionScore(dimensions, scores).contributions[dimensions[0].metrics[1].id]).toBe(0) }) test('14%综合占比恰好落在半分位时精确四舍五入', () => { const dimension = INITIAL_SALARY_POSITIONS[0].value.dimensions[0] const metric = dimension.metrics[1] expect(calculatePositionScore([dimension], { [metric.id]: 116.25 }).contributions[metric.id]).toBe(16.28) expect(calculatePositionScore([dimension], { [metric.id]: 116.24999 }).contributions[metric.id]).toBe(16.27) }) function versionedPlanner(): SalaryPosition { const fields = copy() return { ...fields, id: 'parent', revision: 3, updatedAt: '', personnelCount: 7, performanceVersions: [ { id:'closed',effectiveMonth:'2026-11',endsBeforeMonth:null,revision:1,description:'按职级匹配',dimensions:[] }, { id:'baseline',effectiveMonth:'0000-00',endsBeforeMonth:'2026-11',revision:1,description:'原策划方案',dimensions:fields.dimensions }, ], } } test('历史基线覆盖10月及以前,11月关闭通用策划方案并要求职级匹配', () => { const parent = versionedPlanner() const child = { ...parent, id:'p3',personnelPositionId:'parent',jobGrade:'P3',performanceVersions:[{ id:'nov',effectiveMonth:'2026-11',endsBeforeMonth:null,revision:1,description:'P3方案',dimensions:copy().dimensions, }] } for (const month of ['2026-08','2026-10']) { const positions = [parent,child].map(p=>positionAtMonth(p,month)) for (const grade of ['', 'P3', 'P4']) expect(resolvePersonnelPerformance('parent',grade,positions)?.id).toBe('parent') expect(performancePositions(positions).map(p=>p.id)).toEqual(['parent']) expect(getPositionSalaryTemplate(positions[0],month).columns.some(c=>c.label.includes('知识分享'))).toBe(false) } const november = [parent,child].map(p=>positionAtMonth(p,'2026-11')) expect(resolvePersonnelPerformance('parent','',november)).toBeNull() expect(resolvePersonnelPerformance('parent','P4',november)).toBeNull() expect(resolvePersonnelPerformance('parent','P3',november)?.id).toBe('p3') expect(performancePositions(november).map(p=>p.id)).toEqual(['p3']) }) test('版本持续至下一实施月,跨年及历史范围展示准确', () => { const parent = versionedPlanner() const dimensions = copy().dimensions parent.performanceVersions!.unshift({id:'future',effectiveMonth:'2027-01',endsBeforeMonth:null,revision:2,description:'未来方案',dimensions}) parent.performanceVersions![1].endsBeforeMonth='2027-01' expect(positionAtMonth(parent,'2026-12').performanceVersion?.id).toBe('closed') expect(positionAtMonth(parent,'2027-01').performanceVersion?.id).toBe('future') expect(positionAtMonth(parent,'2028-12').description).toBe('未来方案') expect(performanceVersionLabel(parent.performanceVersions![2])).toBe('2026年10月及以前') expect(performanceVersionRange({effectiveMonth:'2026-11',endsBeforeMonth:'2027-01'})).toBe('2026年11月至2026年12月') expect(performanceVersionRange({effectiveMonth:'2026-11',endsBeforeMonth:'2026-12'})).toBe('2026年11月') expect(positionAtMonth({...parent,performanceVersions:[]},'2026-10').dimensions).toEqual([]) })