c363683184
这面此前 0 个挂载测试(@vue/test-utils 不在 package.json、node_modules 是空目录), .vite 页面全靠"读源码做正则断言"钉住,改法一旦不是那个字面写法就失明。 装上 @vue/test-utils + vitest + jsdom 后补了 2 文件 7 例挂载测试,靶子选两处只 console 不给用户提示的失败路径(公司目录没取到时原先只剩"tab 条没画出来")。 source-pin 台账用锚点分段口径:实测这棵树 1,704 个 test(/it( 全部行首出现、无循环生成, 而括号配对切块会被正则字面量里的 ) 提前收口、漏 48 处——分母悄悄变小对"只许减"是沉默的, 所以另加一条"与独立出现次数对平"的断言。基线落在每个文件上:只钉"总数+文件数"两个标量时, A 加一条配 B 减一条就能双双不动地绕过去。 ⚠ 这支 CI job 今天是纸面的:Gitea Actions 只列得出 3 次 run,末次停在 2026-08-11 且全是 failure,此后 origin/master 又推进 1,131 笔没有产生任何 run。同一个 workflow 里 backend 的 mvn test、frontend 的 npm test 也一并不会执行——"CI 强制"这一项要成立,得先恢复 runner。
208 lines
7.2 KiB
TypeScript
208 lines
7.2 KiB
TypeScript
/**
|
||
* 后台铃铛通知纯逻辑:未读徽标文案、新通知判定、「已提醒过」去重记录、时间展示。
|
||
* 轮询调度与渲染在 NotificationBell.vue;这里只放可单测的纯函数与存储读写。
|
||
*/
|
||
|
||
/** 未读轮询间隔(60 秒)。 */
|
||
export const NOTIFICATION_POLL_INTERVAL_MS = 60_000
|
||
|
||
const NOTIFIED_KEY_PREFIX = 'admin-notification:last-notified-id'
|
||
|
||
/** 未读徽标文案:0 或非法值显示空串,>99 显示 99+。 */
|
||
export function formatUnreadBadge(count: number | null | undefined): string {
|
||
const value = Number(count ?? 0)
|
||
if (!Number.isFinite(value) || value <= 0) {
|
||
return ''
|
||
}
|
||
return value > 99 ? '99+' : String(Math.floor(value))
|
||
}
|
||
|
||
/** 是否出现新通知:latestId 大于上次已提醒过的 id。 */
|
||
export function hasNewNotification(
|
||
latestId: number | null | undefined,
|
||
lastNotifiedId: number | null | undefined,
|
||
): boolean {
|
||
const latest = Number(latestId ?? 0)
|
||
const notified = Number(lastNotifiedId ?? 0)
|
||
if (!Number.isFinite(latest) || latest <= 0) {
|
||
return false
|
||
}
|
||
return latest > (Number.isFinite(notified) && notified > 0 ? notified : 0)
|
||
}
|
||
|
||
function storageKey(uid: string | number | null | undefined): string {
|
||
const normalized = uid === null || uid === undefined || String(uid).trim() === '' ? '0' : String(uid)
|
||
return `${NOTIFIED_KEY_PREFIX}:${normalized}`
|
||
}
|
||
|
||
/** 读取当前管理员「已提醒过的最大通知 id」。 */
|
||
export function readLastNotifiedId(uid: string | number | null | undefined): number {
|
||
try {
|
||
const raw = window.localStorage.getItem(storageKey(uid))
|
||
const value = Number(raw ?? 0)
|
||
return Number.isFinite(value) && value > 0 ? value : 0
|
||
} catch {
|
||
return 0
|
||
}
|
||
}
|
||
|
||
export function writeLastNotifiedId(uid: string | number | null | undefined, id: number): void {
|
||
try {
|
||
if (Number.isFinite(id) && id > 0) {
|
||
window.localStorage.setItem(storageKey(uid), String(Math.floor(id)))
|
||
}
|
||
} catch {
|
||
/* 存储不可用时静默忽略(退化为每次都提醒,不影响功能) */
|
||
}
|
||
}
|
||
|
||
/** 通知所属日期键(本地时区 yyyy-MM-dd);时间缺失/非法返回空串。 */
|
||
export function notificationDayKey(value: string | null | undefined): string {
|
||
if (!value) {
|
||
return ''
|
||
}
|
||
const date = new Date(value)
|
||
if (Number.isNaN(date.getTime())) {
|
||
return ''
|
||
}
|
||
const pad = (input: number) => String(input).padStart(2, '0')
|
||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
|
||
}
|
||
|
||
/** 日期组头文案:今天 / 昨天 / 2026年9月11日;空键兜底「未知时间」。 */
|
||
export function formatDayLabel(day: string, now: Date = new Date()): string {
|
||
if (!day) {
|
||
return '未知时间'
|
||
}
|
||
if (day === notificationDayKey(now.toISOString())) {
|
||
return '今天'
|
||
}
|
||
const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)
|
||
if (day === notificationDayKey(yesterday.toISOString())) {
|
||
return '昨天'
|
||
}
|
||
const [year, month, date] = day.split('-').map((part) => Number(part))
|
||
if (!Number.isFinite(year) || !Number.isFinite(month) || !Number.isFinite(date)) {
|
||
return day
|
||
}
|
||
return `${year}年${month}月${date}日`
|
||
}
|
||
|
||
export interface NotificationDayGroup<T> {
|
||
day: string
|
||
label: string
|
||
items: T[]
|
||
}
|
||
|
||
/** 按年月日分组(组内保持入参顺序,列表本身已按 id 倒序)。 */
|
||
export function groupNotificationsByDay<T extends { createdAt: string | null }>(
|
||
items: T[],
|
||
now: Date = new Date(),
|
||
): NotificationDayGroup<T>[] {
|
||
const groups: NotificationDayGroup<T>[] = []
|
||
const indexByDay = new Map<string, NotificationDayGroup<T>>()
|
||
for (const item of items ?? []) {
|
||
const day = notificationDayKey(item.createdAt)
|
||
let group = indexByDay.get(day)
|
||
if (!group) {
|
||
group = { day, label: formatDayLabel(day, now), items: [] }
|
||
indexByDay.set(day, group)
|
||
groups.push(group)
|
||
}
|
||
group.items.push(item)
|
||
}
|
||
return groups
|
||
}
|
||
|
||
/** 通知时间展示:今天只显示 HH:mm;昨天显示「昨天 HH:mm」;更早显示 MM-DD HH:mm。 */
|
||
export function formatNotificationTime(
|
||
value: string | null | undefined,
|
||
now: Date = new Date(),
|
||
): string {
|
||
if (!value) {
|
||
return ''
|
||
}
|
||
const date = new Date(value)
|
||
if (Number.isNaN(date.getTime())) {
|
||
return ''
|
||
}
|
||
const pad = (input: number) => String(input).padStart(2, '0')
|
||
const hourMinute = `${pad(date.getHours())}:${pad(date.getMinutes())}`
|
||
const sameDay =
|
||
date.getFullYear() === now.getFullYear() &&
|
||
date.getMonth() === now.getMonth() &&
|
||
date.getDate() === now.getDate()
|
||
if (sameDay) {
|
||
return hourMinute
|
||
}
|
||
const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)
|
||
const isYesterday =
|
||
date.getFullYear() === yesterday.getFullYear() &&
|
||
date.getMonth() === yesterday.getMonth() &&
|
||
date.getDate() === yesterday.getDate()
|
||
if (isYesterday) {
|
||
return `昨天 ${hourMinute}`
|
||
}
|
||
return `${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${hourMinute}`
|
||
}
|
||
|
||
/**
|
||
* 失败反馈的文案与「同场故障只提示一次」的闸门(四面9分口径 §2 后管「错误静默」)。
|
||
*
|
||
* <p>改前 NotificationBell.vue 的两处 catch 只 console.warn:未读数接口挂了红点静默消失、
|
||
* 点通知标已读失败界面纹丝不动,使用者无从判断是"没有通知"还是"接口没通"(全仓 src 无
|
||
* errorHandler/unhandledrejection 兜底)。文案在这里出、可见出口由组件注入
|
||
* ({@code showAdminFeedback}),模型层不引 element-plus ⇒ 这一族判据能用 node --test 直接跑真函数,
|
||
* 不必退化成读源码的钉子。</p>
|
||
*/
|
||
export type BellFeedbackKind = 'success' | 'error' | 'warning'
|
||
|
||
/** 可见提示的出口:组件把 admin-feedback-ui 的 showAdminFeedback 递进来。 */
|
||
export type BellFeedbackSink = (message: string, kind: BellFeedbackKind) => void
|
||
|
||
function failureReason(error: unknown): string {
|
||
if (error instanceof Error) {
|
||
return error.message.trim()
|
||
}
|
||
return String(error ?? '').trim()
|
||
}
|
||
|
||
/** 未读数刷新失败的提示文案:带上原因,便于区分"接口挂了"与"没权限"。 */
|
||
export function summaryFailureMessage(error: unknown): string {
|
||
const reason = failureReason(error)
|
||
return reason ? `通知未读数刷新失败:${reason}` : '通知未读数刷新失败'
|
||
}
|
||
|
||
/** 标记已读失败的提示文案:点了必须有回应,失败也要有回应。 */
|
||
export function markReadFailureMessage(error: unknown): string {
|
||
const reason = failureReason(error)
|
||
return reason ? `这条通知没能标记为已读:${reason}` : '这条通知没能标记为已读'
|
||
}
|
||
|
||
/**
|
||
* 轮询失败的提示闸门:一轮 60s,同一场故障只提示一次,成功一轮后重新武装。
|
||
* 不设闸等于把「静默失败」换成「每分钟打扰一次」,两个都是错的口径。
|
||
*/
|
||
export function createSummaryFailureAlerter(feedback: BellFeedbackSink): {
|
||
reportFailure(error: unknown): void
|
||
reportSuccess(): void
|
||
isArmed(): boolean
|
||
} {
|
||
let alerted = false
|
||
return {
|
||
reportFailure(error: unknown): void {
|
||
if (alerted) {
|
||
return
|
||
}
|
||
alerted = true
|
||
feedback(summaryFailureMessage(error), 'warning')
|
||
},
|
||
reportSuccess(): void {
|
||
alerted = false
|
||
},
|
||
isArmed(): boolean {
|
||
return alerted
|
||
},
|
||
}
|
||
}
|