f68d1afecf
上一笔(5d2a1c69 通知聚合)推 origin 时被四面闸门连拒两次,两次都不是回归: 第一次是负载把 score_ratchet 的 py_case 挤成负数超时(单跑那格 18 秒绿,带 capture_output 复跑 24 秒 rc=0, 管道阻塞这条已排除);第二次读数清楚——后管 10 格红、客户端 48 格红,红的都是我这笔造成的: ① 可读棘轮按对象点名:后管 NotificationBell.vue 700→877 是"新增超长文件",用户端 1318→1439 是"比基线更长"; ② 后管还有一把 vitest 挂载尺子(tests/mount/**,npm test 那个 glob 不递归 ⇒ 上一笔我漏跑了), 它 mock 的还是单层列表接口,两级改造后 2 条判红——这条是我上一轮汇报里说"后管全绿"的假话,先认。 拆法按尺子自己给的那条路("要么拆,要么逐条点名改基线并写清是谁的哪一笔"):放宽基线要人拍板,所以拆。 只搬 script,样式一行没动——搬模板会把 scoped 样式的后代选择器甩在父作用域外,那是会改画面的拆法。 新层是纯 TS + 一个 composable:后管 src/layout/notification-bell-panel.ts、用户端 src/shared/composables/useNotificationBellPanel.ts, 组件只留面板开合、fixed 定位、外部点击关闭、60s 轮询定时器;行数 后管 877→652、用户端 1439→1213(台账点名的基线是 1318)。 这一层是纯函数,于是判据从"源码里有没有那行字"升级成"喂数据看返回值": 新增 tests/pure/notification-bell-panel.test.ts 两支(后管 9 例 / 用户端 8 例,走 vitest——这层要解析 @ 别名与 vue, 进不了零依赖的 node --test,两面 vitest.config 的 include 因此多收一格,注释同步改掉)。 覆盖:红点按组数、组内没读完不许减组数、下钻三维(主体为空改 IS NULL 旗标)、日期倒挂交换、 聚合页缺字段时的回落、换天才挂日期头、级别兜底、水位只在首页有数据时推进。 变异自证 6 条全 KILLED(A1 组数提前掉/A2 下钻丢主体/A3 天天挂头/D1 组数提前掉/D2 丢 IS NULL 旗标/D3 日期不交换), 还原按注入前内存字节写回并比过(admin 13790 / desktop 13115)。 三处台账跟着实况走,没有一处是放宽: ① 用户端 catch 棘轮 perFile:组件 3→0、新记 useNotificationBellPanel.ts=2,silent 总数 84→83(尺子的注释要求降了就一起改小); blocks 仍 299(搬动不改分母)。 ② 后管 source-pin 台账:notification-bell-failure.test.ts 的钉子从 SFC 复指到新层, readSource 读点数不变(中途加过一枚"组件确实在用这层"的钉子,实测会把台账读点数抬上去, 而这条已由挂载用例在运行时证 ⇒ 撤掉,不往只许减的台账里添)。 ③ 后管挂载用例 4→6 例(补"点聚合行只展开不标已读""红点按组数""下钻带主体")。 验证=后管 node --test 1819/1819、vitest 21/21(12 挂载 + 9 纯函数)、真浏览器 e2e 10/10、vue-tsc rc=0; 用户端 node --test 834/834、vitest 16/16(8 挂载 + 8 纯函数)、vue-tsc rc=0; ops 侧 test_readability_ratchet 9/9 绿(这条是上一笔红的因,现已解)。 ⚠ scripts/tests 整量此刻仍 1 格红,红的不是本笔:scripts/tests/test_无声台账放宽闸.py 于 10-13 09:56 被另一路会话 扩写进 RD2 三记反例(3f55f3b),而配套 app_client/scripts/gen_silent_ledger.py 的 --check 还只判总数轴(那份 10-12 23:12 起没动) ⇒ 逐文件那一条轴此刻必然判不到,本笔不动别人的尺子也不替别人抬基线。
264 lines
10 KiB
TypeScript
264 lines
10 KiB
TypeScript
/**
|
||
* 首批组件挂载用例之二(四面9分口径 §2 后管「0 组件挂载测试」)。
|
||
*
|
||
* 靶子是 NotificationBell.vue 的两处静默 catch(未读数轮询、点明细标已读)。
|
||
* 这里跑真组件:真 pinia 会话、真 `notification-bell-model` 里的提示闸门、真定时器
|
||
* (用 vi 的假时钟推进 60s 轮询),只把两个出口(api 与 admin-feedback-ui)换成可断言的假件
|
||
* ⇒ 判的是运行时行为,不是读源码的形状钉子。
|
||
*
|
||
* 10-13 聚合改造(场景 × 自然日 × 主体用户)后,面板默认视图拿的是 `fetchNotificationGroups`,
|
||
* 明细要展开聚合行才由 `fetchNotificationList` 取 ⇒ 点已读那两支都先展开再点明细行;
|
||
* 顺带钉住「聚合行点击只展开、不替用户标已读」与「红点按组数、组内没读完不许掉」。
|
||
*
|
||
* ⚠ 面板是 `<Teleport to="body">`:每支用例都必须 unmount 并清空 body,
|
||
* 否则上一支留下的那一行 `.bell-item--unread` 会被这一支的 document 查询捡到(实测会串味)。
|
||
* ⚠ 这面还有一支 `npm run test:mount`(vitest,只收 tests/mount/**),
|
||
* 与 `npm test`(node --test,glob 不递归)互不抓取 ⇒ 改铃铛必须两把都跑,只跑一把就是没验。
|
||
*/
|
||
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(),
|
||
fetchNotificationGroups: 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,
|
||
})
|
||
|
||
/** 聚合行:一条组 = 场景 × 自然日 × 主体用户,rowCount 是折叠前的明细条数。 */
|
||
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: new Date().toISOString(),
|
||
...over,
|
||
})
|
||
|
||
const groupPage = (rows: unknown[], over: Record<string, unknown> = {}) => ({
|
||
groups: rows,
|
||
total: rows.length,
|
||
rowCount: rows.length,
|
||
page: 1,
|
||
pageSize: 6,
|
||
unreadGroupCount: rows.filter((g) => (g as { unreadCount: number }).unreadCount > 0).length,
|
||
unreadCount: rows.reduce((sum, g) => sum + (g as { unreadCount: number }).unreadCount, 0),
|
||
...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(selector: string): HTMLElement | null {
|
||
return document.body.querySelector(selector) as HTMLElement | null
|
||
}
|
||
|
||
/** 展开第一个聚合行(聚合行点击只展开,明细行才是能标已读的那一层)。 */
|
||
async function expandFirstGroup() {
|
||
const row = panelRow('.bell-group')
|
||
if (!row) throw new Error('面板 Teleport 到 body 后没渲染出聚合行')
|
||
row.click()
|
||
await flushPromises()
|
||
}
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
useAdminSessionStore().user = { id: 9, username: 'root', role: 'super_admin' }
|
||
api.fetchNotificationGroups.mockResolvedValue(groupPage([]))
|
||
api.fetchNotificationList.mockResolvedValue({ items: [], total: 0, page: 1, pageSize: 50, unreadCount: 0 })
|
||
api.fetchNotificationSummary.mockResolvedValue({ unreadCount: 0, unreadGroupCount: 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, unreadGroupCount: 1, 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.fetchNotificationGroups.mockResolvedValue(groupPage([group({ latestId: 21 })]))
|
||
api.fetchNotificationList.mockResolvedValue({
|
||
items: [notification({ id: 21 })],
|
||
total: 1,
|
||
page: 1,
|
||
pageSize: 50,
|
||
unreadCount: 1,
|
||
})
|
||
api.markNotificationRead.mockRejectedValue(new Error('403 Forbidden'))
|
||
|
||
const bell = mountBell()
|
||
await flushPromises()
|
||
await bell.find('button.bell-trigger').trigger('click')
|
||
await flushPromises()
|
||
|
||
// 展开这一步本身不许标已读:口径是"聚合行只展开,已读进明细逐条点"
|
||
expect(api.markNotificationRead).not.toHaveBeenCalled()
|
||
await expandFirstGroup()
|
||
|
||
const row = panelRow('.bell-item--detail')
|
||
if (!row) throw new Error('展开聚合行后没渲染出明细行')
|
||
row.click()
|
||
await flushPromises()
|
||
|
||
expect(feedback.showAdminFeedback).toHaveBeenCalledWith('这条通知没能标记为已读:403 Forbidden', 'error')
|
||
// 前端不许替后端宣布已读:行仍是未读态、徽标还在
|
||
expect(panelRow('.bell-item--detail')?.className).toContain('bell-item--unread')
|
||
expect(bell.find('.bell-badge').exists()).toBe(true)
|
||
})
|
||
|
||
it('反向钉:标已读成功时一句提示都不许有,行也要真的转为已读', async () => {
|
||
api.fetchNotificationGroups.mockResolvedValue(groupPage([group({ latestId: 22 })]))
|
||
api.fetchNotificationList.mockResolvedValue({
|
||
items: [notification({ id: 22 })],
|
||
total: 1,
|
||
page: 1,
|
||
pageSize: 50,
|
||
unreadCount: 1,
|
||
})
|
||
api.markNotificationRead.mockResolvedValue(true)
|
||
|
||
const bell = mountBell()
|
||
await flushPromises()
|
||
await bell.find('button.bell-trigger').trigger('click')
|
||
await flushPromises()
|
||
await expandFirstGroup()
|
||
|
||
const row = panelRow('.bell-item--detail')
|
||
if (!row) throw new Error('展开聚合行后没渲染出明细行')
|
||
row.click()
|
||
await flushPromises()
|
||
|
||
expect(feedback.showAdminFeedback).not.toHaveBeenCalled()
|
||
expect(panelRow('.bell-item--detail')?.className).not.toContain('bell-item--unread')
|
||
expect(bell.find('.bell-badge').exists()).toBe(false)
|
||
})
|
||
|
||
it('组内还有未读时红点不许掉,读完最后一条才掉', async () => {
|
||
api.fetchNotificationGroups.mockResolvedValue(
|
||
groupPage([group({ latestId: 32, rowCount: 2, unreadCount: 2 })], { unreadCount: 2, unreadGroupCount: 1 }),
|
||
)
|
||
api.fetchNotificationSummary.mockResolvedValue({ unreadCount: 2, unreadGroupCount: 1, latestId: 32 })
|
||
api.fetchNotificationList.mockResolvedValue({
|
||
items: [notification({ id: 31 }), notification({ id: 32 })],
|
||
total: 2,
|
||
page: 1,
|
||
pageSize: 50,
|
||
unreadCount: 2,
|
||
})
|
||
|
||
const bell = mountBell()
|
||
await flushPromises()
|
||
await bell.find('button.bell-trigger').trigger('click')
|
||
await flushPromises()
|
||
await expandFirstGroup()
|
||
|
||
panelRow('.bell-item--detail.bell-item--unread')!.click()
|
||
await flushPromises()
|
||
expect(api.markNotificationRead).toHaveBeenCalledTimes(1)
|
||
expect(bell.find('.bell-badge').exists()).toBe(true, '这一组还剩一条没读,红点不许先掉')
|
||
|
||
panelRow('.bell-item--detail.bell-item--unread')!.click()
|
||
await flushPromises()
|
||
expect(api.markNotificationRead).toHaveBeenCalledTimes(2)
|
||
expect(bell.find('.bell-badge').exists()).toBe(false)
|
||
})
|
||
|
||
it('下钻带上主体用户:同场景同一天是好几组,少一维就把别的主体的行混进来', async () => {
|
||
api.fetchNotificationGroups.mockResolvedValue(
|
||
groupPage([group({ subjectUserId: 1196, latestId: 41 }), group({ subjectUserId: 1160, latestId: 42 })]),
|
||
)
|
||
api.fetchNotificationList.mockResolvedValue({
|
||
items: [notification({ id: 41 })],
|
||
total: 1,
|
||
page: 1,
|
||
pageSize: 50,
|
||
unreadCount: 1,
|
||
})
|
||
|
||
const bell = mountBell()
|
||
await flushPromises()
|
||
await bell.find('button.bell-trigger').trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(document.body.querySelectorAll('.bell-group')).toHaveLength(2)
|
||
await expandFirstGroup()
|
||
|
||
expect(api.fetchNotificationList.mock.calls[0][0]).toMatchObject({
|
||
scene: 'service_down',
|
||
day: '2026-09-13',
|
||
subjectUserId: 1196,
|
||
})
|
||
expect(api.fetchNotificationList.mock.calls[0][0].subjectUnlabelled).toBe(false)
|
||
})
|
||
})
|