import { test } from 'node:test' import assert from 'node:assert/strict' import { formatNotificationTime, formatUnreadBadge, formatDayLabel, formatGroupCounts, notificationGroupKey, hasNewNotification, readLastNotifiedId, writeLastNotifiedId, } from '../src/shared/utils/notification-bell.ts' import { NOTIFICATION_CATEGORY_OPTIONS, buildNotificationListQuery, } from '../src/shared/api/types/modules/notification.ts' function createStorage() { const store = new Map() return { getItem: (key: string) => (store.has(key) ? (store.get(key) as string) : null), setItem: (key: string, value: string) => { store.set(key, String(value)) }, removeItem: (key: string) => { store.delete(key) }, } } function setupWindow() { const localStorage = createStorage() ;(globalThis as Record).window = { localStorage } return { localStorage } } test('formatUnreadBadge 徽标文案:0 与非法值为空、超过 99 显示 99+', () => { assert.equal(formatUnreadBadge(0), '') assert.equal(formatUnreadBadge(-1), '') assert.equal(formatUnreadBadge(null), '') assert.equal(formatUnreadBadge(undefined), '') assert.equal(formatUnreadBadge(Number.NaN), '') assert.equal(formatUnreadBadge(1), '1') assert.equal(formatUnreadBadge(99), '99') assert.equal(formatUnreadBadge(100), '99+') assert.equal(formatUnreadBadge(9999), '99+') }) test('hasNewNotification 新通知判定:latestId 需大于已提醒 id', () => { assert.equal(hasNewNotification(0, 0), false) assert.equal(hasNewNotification(null, 0), false) assert.equal(hasNewNotification(5, 5), false) assert.equal(hasNewNotification(5, 7), false) assert.equal(hasNewNotification(5, 3), true) assert.equal(hasNewNotification(5, 0), true) assert.equal(hasNewNotification(5, null), true) }) test('已提醒 id 按用户读写并容错', () => { setupWindow() assert.equal(readLastNotifiedId('42'), 0, '未写入时按 0 处理') writeLastNotifiedId('42', 123) assert.equal(readLastNotifiedId('42'), 123) assert.equal(readLastNotifiedId('43'), 0, '不同用户互不影响') writeLastNotifiedId('42', 0) assert.equal(readLastNotifiedId('42'), 123, '非法 id 不覆盖已有值') window.localStorage.setItem('notification:last-notified-id:42', 'broken') assert.equal(readLastNotifiedId('42'), 0, '损坏值按 0 处理') }) test('formatNotificationTime 今天/昨天/更早展示', () => { const now = new Date(2026, 8, 13, 15, 30) assert.equal( formatNotificationTime(new Date(2026, 8, 13, 9, 5).toISOString(), now), '09:05', '今天只显示时分', ) assert.equal( formatNotificationTime(new Date(2026, 8, 12, 23, 59).toISOString(), now), '昨天 23:59', '昨天带前缀', ) assert.equal( formatNotificationTime(new Date(2026, 8, 1, 8, 0).toISOString(), now), '09-01 08:00', '更早显示月-日', ) assert.equal(formatNotificationTime(null, now), '') assert.equal(formatNotificationTime('not-a-date', now), '') }) test('buildNotificationListQuery:关键字去空白、空条件不下发、分类与日期原样透传', () => { assert.deepEqual(buildNotificationListQuery(), { page: 1, pageSize: 20, onlyUnread: 'false' }) const query = buildNotificationListQuery({ page: 2, pageSize: 10, keyword: ' 余额不足 ', category: 'config_error', startDate: '2026-09-10', endDate: '2026-09-13', }) assert.deepEqual(query, { page: 2, pageSize: 10, onlyUnread: 'false', keyword: '余额不足', category: 'config_error', startDate: '2026-09-10', endDate: '2026-09-13', }) // 面板清空筛选后不能带空串日期(后端日期绑定会 400),空分类同样不下发 const cleared = buildNotificationListQuery({ keyword: ' ', category: ' ', startDate: '', endDate: '', }) assert.equal('keyword' in cleared, false) assert.equal('category' in cleared, false) assert.equal('startDate' in cleared, false) assert.equal('endDate' in cleared, false) }) /** 类型筛选项必须与后端四个大类逐一对应,漏一个用户就永远筛不到那类通知。 */ test('排序参数:默认由后端按严重度,只有切时间排序才下发 sort=time', () => { assert.equal('sort' in buildNotificationListQuery(), false, '默认 severity 不下发,后端默认即严重度') assert.equal(buildNotificationListQuery({ sort: 'severity' }).sort, undefined) assert.equal(buildNotificationListQuery({ sort: 'time' }).sort, 'time') }) test('类型筛选项覆盖后端四个大类且标签非空', () => { assert.deepEqual( NOTIFICATION_CATEGORY_OPTIONS.map((option) => option.value), ['system_error', 'task_error', 'config_error', 'system_notice'], ) for (const option of NOTIFICATION_CATEGORY_OPTIONS) { assert.ok(option.label.trim().length > 0, `分类 ${option.value} 缺中文标签`) } }) test('notificationGroupKey 聚合组键:三维各自都能把两组分开', () => { const base = { scene: 'task_partial', day: '2026-09-13', subjectUserId: 1160 } assert.notEqual(notificationGroupKey({ ...base, subjectUserId: 1196 }), notificationGroupKey(base)) assert.notEqual(notificationGroupKey({ ...base, scene: 'task_failed' }), notificationGroupKey(base)) assert.notEqual(notificationGroupKey({ ...base, day: '2026-09-12' }), notificationGroupKey(base)) assert.equal(notificationGroupKey({ ...base, subjectUserId: null }), notificationGroupKey({ ...base, subjectUserId: undefined })) }) test('formatGroupCounts 组行计数:读完不再挂 0 未读', () => { assert.equal(formatGroupCounts(12, 3), '12 条 · 3 未读') assert.equal(formatGroupCounts(12, 0), '12 条') }) test('formatDayLabel 日期组头:今天/昨天/更早/未知时间', () => { const now = new Date(2026, 8, 13, 15, 30) assert.equal(formatDayLabel('2026-09-13', now), '今天') assert.equal(formatDayLabel('2026-09-12', now), '昨天') assert.equal(formatDayLabel('2026-09-01', now), '2026年9月1日') assert.equal(formatDayLabel('', now), '未知时间') }) test('buildNotificationListQuery 下钻条件:主体用户按值下发、未标注走 IS NULL 旗标', () => { const labelled = buildNotificationListQuery({ scene: 'task_partial', day: '2026-09-13', subjectUserId: 1160, subjectUnlabelled: false }) assert.equal(labelled.subjectUserId, 1160) assert.equal('subjectUnlabelled' in labelled, false, '有主体时不下发 IS NULL 旗标,两个条件互斥') // 存量行没有主体用户:按 null 下发等于把条件丢掉,必须换成旗标,否则展开一组会串进别人那组 const unlabelled = buildNotificationListQuery({ scene: 'task_partial', day: '2026-09-13', subjectUserId: null, subjectUnlabelled: true }) assert.equal('subjectUserId' in unlabelled, false, 'null 主体不下发成空值参数') assert.equal(unlabelled.subjectUnlabelled, 'true') })