Files
crawler-plugin/admin-frontend-vue/tests/pure/notification-bell-panel.test.ts
T
huangzd1997 86d78160fd feat(通知): 四级严重度与严重度/时间双排序,客户端与后台管理同步
通知表新增 severity(紧急/高危/中危/低危,V161),由 scene + level 推导且
不回改 level:老客户端配色不受影响,secret_balance 的余量偏低与欠费也能分档。
列表与聚合接口默认按严重度倒序(同档按时间倒序),sort=time 切纯时间排序;
聚合组取组内最高严重度。两端铃铛加彩色等级标签与排序切换,展开明细沿用
同一排序,严重度排序下隐藏会跨天误导的日期头。
2026-10-21 13:33:07 +08:00

145 lines
6.2 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.
/**
* 面板层纯函数的行为用例(10-13 从 NotificationBell.vue 拆出这一层的唯一目的就在这)。
*
* 组件那一层在本面只能挂载跑(vitest+jsdom),而"红点按组数、组内没读完不许掉、
* 下钻三维、换页清展开态"这几条口径其实都是纯计算——放这一层就能逐条喂数据判它,
* 不必退化成 readSource 钉源码字形(那种钉子在这仓已被记进只许减的台账)。
*
* 跑法:`npx vitest run --config vitest.config.ts`(`npm test` 那把 node --test 零依赖,
* 解析不了 @ 别名与 vue,所以这一支走 vitest 的 tests/pure/**)。
*/
import { describe, expect, it } from 'vitest'
import {
PANEL_DETAIL_LIMIT,
applyGroupsPage,
applyMarkReadToCounts,
detailQueryFor,
itemLevel,
itemSeverity,
latestIdWorthRecording,
normalizedDayRange,
severityLabel,
startsNewDay,
} from '@/layout/notification-bell-panel'
const group = (over: Record<string, unknown> = {}) => ({
latestId: 7,
scene: 'service_down',
category: 'system_error',
day: '2026-09-13',
subjectUserId: 1160,
rowCount: 1,
unreadCount: 1,
level: 'error',
title: '品牌检测服务挂了',
content: '上游 503',
createdAt: '2026-09-13T09:00:00.000Z',
...over,
})
describe('通知面板层纯函数', () => {
it('起止日期倒挂自动交换;单边留空不下发该条件', () => {
expect(normalizedDayRange('2026-09-14', '2026-09-13')).toEqual({
startDate: '2026-09-13',
endDate: '2026-09-14',
})
expect(normalizedDayRange('', '')).toEqual({ startDate: undefined, endDate: undefined })
expect(normalizedDayRange('2026-09-13', '')).toEqual({ startDate: '2026-09-13', endDate: undefined })
})
it('下钻条件带满三维,主体为空改走 IS NULL 旗标', () => {
const labelled = detailQueryFor(group({ subjectUserId: 1196 }) as never)
expect(labelled.scene).toBe('service_down')
expect(labelled.day).toBe('2026-09-13')
expect(labelled.subjectUserId).toBe(1196)
expect(labelled.subjectUnlabelled).toBe(false)
expect(labelled.pageSize).toBe(PANEL_DETAIL_LIMIT)
// 改动上线前的存量行没有主体用户:按 null 下发等于把条件丢掉,必须换成旗标
const unlabelled = detailQueryFor(group({ subjectUserId: null }) as never)
expect(unlabelled.subjectUserId).toBeNull()
expect(unlabelled.subjectUnlabelled).toBe(true)
})
it('组内还有未读时组数不许减,读完最后一条才减', () => {
const groups = [group({ unreadCount: 2 }), group({ subjectUserId: 1196, unreadCount: 1 })]
const key = 'service_down|2026-09-13|1160'
const first = applyMarkReadToCounts(groups, key, 3, 2)
expect(groups[0].unreadCount).toBe(1, '第一条读完,组内还剩一条')
expect(first.unreadCount).toBe(2, '明细条数照实减')
expect(first.unreadGroupCount).toBe(2, '这一组还没读完,未读组数不许动')
const last = applyMarkReadToCounts(groups, key, 2, 2)
expect(groups[0].unreadCount).toBe(0)
expect(last.unreadGroupCount).toBe(1, '组内清零,红点(按组数)才掉这一格')
})
it('没展开任何一组时只减明细数,不减组数', () => {
const groups = [group({ unreadCount: 1 })]
const next = applyMarkReadToCounts(groups, '', 1, 1)
expect(groups[0].unreadCount).toBe(1, '找不到归属组就不该改组内计数')
expect(next.unreadCount).toBe(0)
expect(next.unreadGroupCount).toBe(1)
})
it('两个计数不许越界成负数', () => {
const next = applyMarkReadToCounts([], 'nothing-expanded', 0, 0)
expect(next).toEqual({ unreadCount: 0, unreadGroupCount: 0 })
})
it('聚合页缺字段时回落:组数按实收、明细条数按清单长度', () => {
const applied = applyGroupsPage(
{ groups: [group(), group({ subjectUserId: 1196 })] } as never,
{ unreadCount: 5, unreadGroupCount: 3 },
)
expect(applied.total).toBe(2)
expect(applied.rowCount).toBe(2)
expect(applied.unreadCount).toBe(5, '后端没给这一格就保留上一次读数,别凭空归零')
expect(applied.unreadGroupCount).toBe(3)
const empty = applyGroupsPage({ groups: null } as never, { unreadCount: 1, unreadGroupCount: 1 })
expect(empty.groups).toEqual([])
expect(empty.total).toBe(0)
})
it('日期头只在换天时出现:同一天多组(多用户/多场景)不重复挂头', () => {
const list = [group(), group({ subjectUserId: 1196 }), group({ day: '2026-09-12' })]
expect(startsNewDay(list, 0)).toBe(true)
expect(startsNewDay(list, 1)).toBe(false)
expect(startsNewDay(list, 2)).toBe(true)
})
it('级别兜底:非法/缺失都按 warning,组行与明细行不会配色分家', () => {
expect(itemLevel({ level: 'error' })).toBe('error')
expect(itemLevel({ level: 'info' })).toBe('info')
expect(itemLevel({ level: 'fatal' })).toBe('warning')
expect(itemLevel({ level: '' })).toBe('warning')
})
it('四级严重度:后端给值原样用,旧后端缺字段按 level 兜底,绝不出现空标签', () => {
expect(itemSeverity({ severity: 'urgent', level: 'warning' })).toBe('urgent')
expect(itemSeverity({ severity: 'high' })).toBe('high')
expect(itemSeverity({ severity: 'medium' })).toBe('medium')
expect(itemSeverity({ severity: 'low' })).toBe('low')
expect(itemSeverity({ level: 'error' })).toBe('urgent')
expect(itemSeverity({ level: 'info' })).toBe('low')
expect(itemSeverity({ level: 'warning' })).toBe('high')
expect(itemSeverity({ level: '' })).toBe('high')
expect(itemSeverity({ severity: 'fatal', level: 'warning' })).toBe('high')
expect(severityLabel({ severity: 'urgent' })).toBe('紧急')
expect(severityLabel({ severity: 'high' })).toBe('高危')
expect(severityLabel({ severity: 'medium' })).toBe('中危')
expect(severityLabel({ severity: 'low' })).toBe('低危')
})
it('已提醒水位只在首页真有数据时推进,取不到 id 就什么也不写', () => {
expect(latestIdWorthRecording([])).toBe(0)
expect(latestIdWorthRecording([group({ latestId: 0 })])).toBe(0)
expect(latestIdWorthRecording([group({ latestId: 'x' })])).toBe(0)
expect(latestIdWorthRecording([group({ latestId: 88 })])).toBe(88)
})
})