修复首页风险预告与实际发送的排序不一致问题
atRisk.js 之前只按 fireInDays 排序,同一天内多条提醒的先后完全未定义, 和 reminder.js 实际发送时「当天>提前、重要程度降序」的规则对不上,导致 额度卡在中间时首页提示的风险人名和定时任务实际跳过的人不一致。 - 把 importanceRank 从 reminder.js 抽到共享的 src/importance.js,两处复用 - atRisk.js 的 computeAtRisk 排序改为三段式:fireInDays 升序 → 同天内 kind(当天优先于提前)→ 同 kind 内重要程度降序 - 补充 atRisk.test.js 用例覆盖「同一天多条事件、额度只够一部分」场景
This commit is contained in:
@@ -76,3 +76,50 @@ test('未开启提醒的纪念日不参与计算', () => {
|
||||
const r = computeAtRisk([{ ...near, remindEnabled: 0 }], 0, TODAY)
|
||||
assert.strictEqual(r.atRiskCount, 0)
|
||||
})
|
||||
|
||||
// ------ 同一天内排序需与 reminder.js 的 runForUser 完全对齐 ------
|
||||
// 四条纪念日都恰好在第 5 天产生一个事件:
|
||||
// 甲(当天/high) 乙(当天/low) 丙(提前/high) 丁(提前/medium)
|
||||
// 丙、丁的「当天」事件被安排在第 65 天(超出 60 天窗口),不会进入计算,
|
||||
// 这样每人恰好只贡献一个事件,排序结果可以精确断言。
|
||||
const onDayHigh = {
|
||||
id: 'e1', personName: '甲', remindEnabled: 1, importance: 'high',
|
||||
isLunar: false, solarMonth: 8, solarDay: 20, remindDays: 0
|
||||
}
|
||||
const onDayLow = {
|
||||
id: 'e2', personName: '乙', remindEnabled: 1, importance: 'low',
|
||||
isLunar: false, solarMonth: 8, solarDay: 20, remindDays: 0
|
||||
}
|
||||
const aheadHigh = {
|
||||
id: 'e3', personName: '丙', remindEnabled: 1, importance: 'high',
|
||||
isLunar: false, solarMonth: 10, solarDay: 19, remindDays: 60
|
||||
}
|
||||
const aheadMedium = {
|
||||
id: 'e4', personName: '丁', remindEnabled: 1, importance: 'medium',
|
||||
isLunar: false, solarMonth: 10, solarDay: 19, remindDays: 60
|
||||
}
|
||||
// 刻意打乱输入顺序(不按「当天>提前、重要程度降序」排列),
|
||||
// 这样如果排序逻辑退化成只按 fireInDays 排(Array.sort 是稳定排序,
|
||||
// 同值会保留输入顺序),测试才能真正暴露出排序规则失效,而不是被输入顺序碰巧掩盖。
|
||||
const sameDayGroup = [aheadMedium, aheadHigh, onDayLow, onDayHigh]
|
||||
|
||||
test('同一天内,当天事件排在提前事件之前(与 reminder.js 一致)', () => {
|
||||
// 额度只够 2 条:应先消耗「当天」的甲、乙,风险留给「提前」的丙、丁
|
||||
const r = computeAtRisk(sameDayGroup, 2, TODAY)
|
||||
assert.strictEqual(r.atRiskCount, 2)
|
||||
assert.deepStrictEqual(r.atRiskNames, ['丙', '丁'])
|
||||
})
|
||||
|
||||
test('同一天内,当天事件按重要程度降序(high 先于 low)', () => {
|
||||
// 额度只够 1 条:当天里 high 的甲应排在 low 的乙前面,先被消耗
|
||||
const r = computeAtRisk(sameDayGroup, 1, TODAY)
|
||||
assert.strictEqual(r.atRiskCount, 3)
|
||||
assert.deepStrictEqual(r.atRiskNames, ['乙', '丙', '丁'])
|
||||
})
|
||||
|
||||
test('同一天内,提前事件按重要程度降序(high 先于 medium)', () => {
|
||||
// 额度够 3 条:甲、乙(当天)先消耗,提前事件里 high 的丙应先于 medium 的丁被消耗
|
||||
const r = computeAtRisk(sameDayGroup, 3, TODAY)
|
||||
assert.strictEqual(r.atRiskCount, 1)
|
||||
assert.deepStrictEqual(r.atRiskNames, ['丁'])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user