Files
crawler-plugin/admin-frontend-vue/tests/mount/notification-bell-failure.mount.test.ts
T
huangzd1997 f142744ed4 feat(护栏): 后管补挂载测试与 readSource 逐文件台账,另加一支 CI job(runner 现状见正文)
这面此前 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。
2026-10-08 17:37:00 +08:00

158 lines
5.9 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.
/**
* 首批组件挂载用例之二(四面9分口径 §2 后管「0 组件挂载测试」)。
*
* 靶子是 NotificationBell.vue 的两处静默 catch(未读数轮询、点通知标已读)。
* 这里跑真组件:真 pinia 会话、真 `notification-bell-model` 里的提示闸门、真定时器
* (用 vi 的假时钟推进 60s 轮询),只把两个出口(api 与 admin-feedback-ui)换成可断言的假件
* ⇒ 判的是运行时行为,不是读源码的形状钉子。
*
* ⚠ 面板是 `<Teleport to="body">`:每支用例都必须 unmount 并清空 body,
* 否则上一支留下的那一行 `.bell-item--unread` 会被这一支的 document 查询捡到(实测会串味)。
*
* 跑法:`npm run test:mount`。
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { createPinia, setActivePinia, type Pinia } from 'pinia'
import NotificationBell from '@/components/NotificationBell.vue'
import { useAdminSessionStore } from '@/stores/admin-session'
import { NOTIFICATION_POLL_INTERVAL_MS } from '@/layout/notification-bell-model'
const feedback = vi.hoisted(() => ({ showAdminFeedback: vi.fn(), runBusy: vi.fn() }))
const api = vi.hoisted(() => ({
fetchNotificationSummary: vi.fn(),
fetchNotificationList: vi.fn(),
markNotificationRead: vi.fn(),
markAllNotificationsRead: vi.fn(),
}))
vi.mock('@/components/admin-feedback-ui', () => feedback)
vi.mock('@/api/notifications', () => api)
const notification = (over: Record<string, unknown> = {}) => ({
id: 7,
scene: 'service_down',
level: 'error',
title: '品牌检测服务挂了',
content: '上游 503',
read: false,
readAt: null,
createdAt: new Date().toISOString(),
...over,
})
describe('NotificationBell:两处失败必须可见', () => {
let pinia: Pinia
let wrapper: VueWrapper | null = null
function mountBell() {
wrapper = mount(NotificationBell, { attachTo: document.body, global: { plugins: [pinia] } })
return wrapper
}
/** 只看当前这一棵树里的面板:Teleport 出去的内容挂在 document.body 上。 */
function panelRow(): HTMLElement | null {
return document.body.querySelector('.bell-item') as HTMLElement | null
}
beforeEach(() => {
vi.clearAllMocks()
pinia = createPinia()
setActivePinia(pinia)
useAdminSessionStore().user = { id: 9, username: 'root', role: 'super_admin' }
api.fetchNotificationList.mockResolvedValue({ items: [], total: 0, page: 1, pageSize: 10, unreadCount: 0 })
api.fetchNotificationSummary.mockResolvedValue({ unreadCount: 0, latestId: 0 })
api.markNotificationRead.mockResolvedValue(true)
vi.spyOn(console, 'warn').mockImplementation(() => {})
})
afterEach(() => {
wrapper?.unmount()
wrapper = null
document.body.innerHTML = ''
vi.useRealTimers()
})
it('未读数接口挂了:立刻给一条带原因的 warning,而不是红点无声消失', async () => {
api.fetchNotificationSummary.mockRejectedValue(new Error('502 Bad Gateway'))
mountBell()
await flushPromises()
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(1)
expect(feedback.showAdminFeedback.mock.calls[0]).toEqual(['通知未读数刷新失败:502 Bad Gateway', 'warning'])
})
it('轮询闸门:同一场故障连打三轮也只提示一次,恢复后再坏一次要再提示', async () => {
vi.useFakeTimers()
api.fetchNotificationSummary.mockRejectedValue(new Error('500'))
mountBell()
await vi.advanceTimersByTimeAsync(0)
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(1)
await vi.advanceTimersByTimeAsync(NOTIFICATION_POLL_INTERVAL_MS * 3)
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(1)
api.fetchNotificationSummary.mockResolvedValue({ unreadCount: 2, latestId: 11 })
await vi.advanceTimersByTimeAsync(NOTIFICATION_POLL_INTERVAL_MS)
api.fetchNotificationSummary.mockRejectedValue(new Error('503'))
await vi.advanceTimersByTimeAsync(NOTIFICATION_POLL_INTERVAL_MS)
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(2)
expect(feedback.showAdminFeedback.mock.calls[1][0]).toContain('503')
})
it('点通知标已读失败:提示带原因,且这条不许被前端冒充成"已读"', async () => {
api.fetchNotificationList.mockResolvedValue({
items: [notification({ id: 21 })],
total: 1,
page: 1,
pageSize: 10,
unreadCount: 1,
})
api.markNotificationRead.mockRejectedValue(new Error('403 Forbidden'))
const bell = mountBell()
await flushPromises()
await bell.find('button.bell-trigger').trigger('click')
await flushPromises()
const row = panelRow()
if (!row) throw new Error('面板 Teleport 到 body 后没渲染出那一行通知')
row.click()
await flushPromises()
expect(feedback.showAdminFeedback).toHaveBeenCalledWith('这条通知没能标记为已读:403 Forbidden', 'error')
// 前端不许替后端宣布已读:行仍是未读态、徽标还在
expect(panelRow()?.className).toContain('bell-item--unread')
expect(bell.find('.bell-badge').exists()).toBe(true)
})
it('反向钉:标已读成功时一句提示都不许有,行也要真的转为已读', async () => {
api.fetchNotificationList.mockResolvedValue({
items: [notification({ id: 22 })],
total: 1,
page: 1,
pageSize: 10,
unreadCount: 1,
})
api.markNotificationRead.mockResolvedValue(true)
const bell = mountBell()
await flushPromises()
await bell.find('button.bell-trigger').trigger('click')
await flushPromises()
const row = panelRow()
if (!row) throw new Error('面板 Teleport 到 body 后没渲染出那一行通知')
row.click()
await flushPromises()
expect(feedback.showAdminFeedback).not.toHaveBeenCalled()
expect(panelRow()?.className).not.toContain('bell-item--unread')
expect(bell.find('.bell-badge').exists()).toBe(false)
})
})