Files
crawler-plugin/admin-frontend-vue/tests/notification-bell.test.ts
T
huangzd1997 d268ecdf6b feat(通知): task_failed 带 ref_json 定位锚点,明细可点直达任务详情
用户原话「都没体现哪个任务id/店铺/用户,咋排查」:通知表只有 title/content,
失败任务又按「用户×模块×自然日」聚合成一条,明细被 5 个 taskNo 预览 + 512 字截断吃掉。

- V162 给 biz_user_notification 加 ref_json(TEXT,可重跑迁移,存量不回填)
- 扫描器把桶内每个失败任务写成锚点(taskId/taskNo/moduleType/reason/updatedAt/
  deviceId),上限 20 条;品牌任务无 task_no 用 #<id> 兜底,保证至少一条可定位
- 详情接口回传 refs;同键刷新时锚点变化也更新,且不重新点亮已读
- 后台铃铛「展开明细」渲染成可点行,跳任务中心 ?taskNo= 自动开详情抽屉
- 新增判据 taskFailedAdminNotificationCarriesUidAndTaskNoAnchor:管理端正文必须含
  uid、每个桶的 ref_json 至少一条非空 taskNo
2026-10-22 10:28:12 +08:00

129 lines
5.0 KiB
TypeScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import {
formatNotificationTime,
formatUnreadBadge,
formatDayLabel,
formatGroupCounts,
notificationGroupKey,
hasNewNotification,
readLastNotifiedId,
writeLastNotifiedId,
taskCenterRouteForRef,
} from '../src/layout/notification-bell-model.ts'
function createStorage() {
const store = new Map<string, string>()
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()
const globalScope = globalThis as Record<string, unknown>
const previous = globalScope.window
globalScope.window = { localStorage }
return {
localStorage,
restore: () => {
globalScope.window = previous
},
}
}
test('test_未读徽标:0 与非法值不显示、超过 99 显示 99+', () => {
assert.equal(formatUnreadBadge(0), '')
assert.equal(formatUnreadBadge(-3), '')
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+')
})
test('test_新通知判定:latestId 需大于已提醒 id', () => {
assert.equal(hasNewNotification(0, 0), false)
assert.equal(hasNewNotification(null, 5), false)
assert.equal(hasNewNotification(5, 5), false)
assert.equal(hasNewNotification(4, 5), false)
assert.equal(hasNewNotification(6, 5), true)
assert.equal(hasNewNotification(6, 0), true)
assert.equal(hasNewNotification(6, null), true)
})
test('test_已提醒 id 按管理员读写并容错', () => {
setupWindow()
assert.equal(readLastNotifiedId(7), 0, '未写入时按 0 处理')
writeLastNotifiedId(7, 88)
assert.equal(readLastNotifiedId(7), 88)
assert.equal(readLastNotifiedId(8), 0, '不同管理员互不影响')
writeLastNotifiedId(7, 0)
assert.equal(readLastNotifiedId(7), 88, '非法 id 不覆盖已有值')
window.localStorage.setItem('admin-notification:last-notified-id:7', 'broken')
assert.equal(readLastNotifiedId(7), 0, '损坏值按 0 处理')
assert.equal(readLastNotifiedId(null), readLastNotifiedId('0'), '空 uid 归一为 0 号键')
})
test('test_时间展示:今天/昨天/更早', () => {
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('test_日期组头文案:今天/昨天/更早/未知时间', () => {
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('test_聚合组键:三维各自都能把两组分开', () => {
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 }),
'null 与 undefined 都是"未标注主体",必须落进同一组')
})
test('test_组行计数文案:读完不再挂 0 未读', () => {
assert.equal(formatGroupCounts(12, 3), '12 条 · 3 未读')
assert.equal(formatGroupCounts(12, 0), '12 条')
assert.equal(formatGroupCounts(1, null), '1 条')
})
test('test_任务锚点深链:按 taskNo 跳任务中心,空锚点不给链接', () => {
assert.deepEqual(taskCenterRouteForRef({ taskNo: 'DELETE_BRAND-101' }), {
path: '/task-center/board',
query: { taskNo: 'DELETE_BRAND-101' },
})
assert.deepEqual(taskCenterRouteForRef({ taskNo: ' #201 ' }), {
path: '/task-center/board',
query: { taskNo: '#201' },
})
assert.equal(taskCenterRouteForRef({ taskNo: '' }), null)
assert.equal(taskCenterRouteForRef({ taskNo: ' ' }), null)
assert.equal(taskCenterRouteForRef(null), null)
assert.equal(taskCenterRouteForRef(undefined), null)
})