gongxiangp2/lib/__tests__/performance-survey-templates.test.ts

200 lines
7.8 KiB
TypeScript

/** @jest-environment node */
import {
DEFAULT_PERFORMANCE_SURVEY_TEMPLATES,
calculateAggregateScore,
calculateResponseScores,
getPerformanceSurveyQuestionScoreDetails,
getPerformanceSurveyTemplateForCreator,
} from '@/lib/performance-survey-templates'
describe('performance survey templates', () => {
test('综合支持部发起的视觉评分去掉文案表达并将单项满分调整为 10 分', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'visual_brand_tone')!
const customized = getPerformanceSurveyTemplateForCreator(template, {
departmentName: '综合支持部',
departmentPath: '集团/品牌中心/综合支持部',
})
expect(customized.questions).toHaveLength(4)
expect(customized.questions.map((question) => question.id)).not.toContain('copyExpression')
expect(customized.questions.every((question) => question.maxScore === 10)).toBe(true)
expect(customized.questions.reduce((sum, question) => sum + Number(question.maxScore), 0)).toBe(40)
expect(customized.maxTotal).toBe(120)
expect(calculateResponseScores(
customized,
Object.fromEntries(customized.questions.map((question) => [question.id, 10])),
).totalScore).toBe(40)
expect(template.questions).toHaveLength(5)
expect(template.questions.find((question) => question.id === 'copyExpression')?.maxScore).toBe(8)
})
test('非综合支持部的视觉评分仍保留原有五个维度', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'visual_brand_tone')!
const unchanged = getPerformanceSurveyTemplateForCreator(template, {
departmentName: '策划部',
departmentPath: '集团/品牌中心/策划部',
})
expect(unchanged).toBe(template)
expect(unchanged.questions).toHaveLength(5)
expect(unchanged.questions.find((question) => question.id === 'copyExpression')).toBeDefined()
})
test('course share template uses the legacy 100-point and 30% weighted scoring rule', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'course_share')!
const scores = Object.fromEntries(template.questions.map((question) => [question.id, 20]))
const result = calculateResponseScores(template, scores)
expect(template.questions).toHaveLength(5)
expect(template.maxTotal).toBe(100)
expect(template.weight).toBe(0.3)
expect(template.aggregationStrategy).toBe('trim_extremes')
expect(template.itemName).toBe('课程')
expect(template.respondentNameFallback).toBe('匿名学员')
expect(template.belowBand).toBe('满意度不足 77%,此项考核为 0')
expect(result.totalScore).toBe(100)
expect(result.kpiScore).toBe(120)
expect(result.weightedScore).toBe(36)
})
test('person-period templates use the legacy 120-point and 20% weighted scoring rule', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'cross_department')!
const scores = Object.fromEntries(template.questions.map((question) => [question.id, 20]))
const result = calculateResponseScores(template, scores)
expect(template.questions).toHaveLength(6)
expect(template.maxTotal).toBe(120)
expect(template.weight).toBe(0.2)
expect(result.totalScore).toBe(120)
expect(result.kpiScore).toBe(120)
expect(result.weightedScore).toBe(24)
})
test('question score details keep every item title, score and maximum', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'course_share')!
const details = getPerformanceSurveyQuestionScoreDetails(template, {
usefulness: 18,
insight: 16.5,
})
expect(details).toHaveLength(template.questions.length)
expect(details[0]).toEqual({
id: 'usefulness',
title: '内容实用度',
score: 18,
maxScore: 20,
})
expect(details[1]).toEqual(expect.objectContaining({
id: 'insight',
score: 16.5,
maxScore: 20,
}))
expect(details[2].score).toBeNull()
})
test('aggregate scoring drops one highest and one lowest response when there are at least three', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'cross_department')!
const result = calculateAggregateScore(
[
{
totalScore: 60,
createdAt: '2026-07-04T00:00:00.000Z',
scores: Object.fromEntries(template.questions.map((question) => [question.id, 10])),
respondentName: '最低分',
},
{
totalScore: 100,
createdAt: '2026-07-04T00:01:00.000Z',
scores: Object.fromEntries(template.questions.map((question) => [question.id, question.id === 'requirementClarity' ? 18 : 16])),
respondentName: '有效分',
},
{
totalScore: 120,
createdAt: '2026-07-04T00:02:00.000Z',
scores: Object.fromEntries(template.questions.map((question) => [question.id, 20])),
respondentName: '最高分',
},
],
template.scoreBands,
template.weight,
template,
)
expect(result.droppedHighestLowest).toBe(true)
expect(result.effectiveCount).toBe(1)
expect(result.rawAverage).toBe(93.33)
expect(result.trimNote).toBe('已剔除 1 个最高总分和 1 个最低总分')
expect(result.removedHigh?.respondentName).toBe('最高分')
expect(result.removedLow?.respondentName).toBe('最低分')
expect(result.dimensionAverages.requirementClarity).toBe(18)
expect(result.achievementScore).toBe(100)
expect(result.kpiScore).toBe(100)
expect(result.weightedScore).toBe(20)
})
test('visual scoring keeps a single evaluator score as the direct average', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'visual_brand_tone')!
const result = calculateAggregateScore(
[{ totalScore: 88, createdAt: '2026-07-04T00:00:00.000Z' }],
template.scoreBands,
template.weight,
template,
)
expect(template.aggregationStrategy).toBe('direct_average')
expect(result.droppedHighestLowest).toBe(false)
expect(result.effectiveCount).toBe(1)
expect(result.achievementScore).toBe(88)
expect(result.removedHigh).toBeNull()
expect(result.removedLow).toBeNull()
})
test('visual scoring directly averages all three evaluator scores without trimming extremes', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'visual_brand_tone')!
const legacyTemplateSnapshot = { ...template, aggregationStrategy: undefined }
const result = calculateAggregateScore(
[60, 90, 120].map((totalScore, index) => ({
totalScore,
createdAt: `2026-07-04T00:0${index}:00.000Z`,
})),
template.scoreBands,
template.weight,
legacyTemplateSnapshot,
)
expect(result.droppedHighestLowest).toBe(false)
expect(result.effectiveCount).toBe(3)
expect(result.rawAverage).toBe(90)
expect(result.achievementScore).toBe(90)
expect(result.removedHigh).toBeNull()
expect(result.removedLow).toBeNull()
})
test('visual scoring directly averages every evaluator score when there are more than three', () => {
const template = DEFAULT_PERFORMANCE_SURVEY_TEMPLATES.find((item) => item.code === 'visual_brand_tone')!
const result = calculateAggregateScore(
[60, 70, 80, 100, 120].map((totalScore, index) => ({
totalScore,
createdAt: `2026-07-04T00:0${index}:00.000Z`,
})),
template.scoreBands,
template.weight,
template,
)
expect(result.droppedHighestLowest).toBe(false)
expect(result.effectiveCount).toBe(5)
expect(result.rawAverage).toBe(86)
expect(result.achievementScore).toBe(86)
expect(result.removedHigh).toBeNull()
expect(result.removedLow).toBeNull()
})
})