refactor(通知): 铃铛面板状态机从 SFC 拆成独立一层——是上一笔把两个组件顶到可读棘轮 800 行上限的账,不是新功能

上一笔(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 起没动)
⇒ 逐文件那一条轴此刻必然判不到,本笔不动别人的尺子也不替别人抬基线。
This commit is contained in:
2026-10-13 10:38:34 +08:00
parent 5d2a1c6974
commit f68d1afecf
11 changed files with 1257 additions and 590 deletions
@@ -35,7 +35,11 @@ const BASELINE = {
// ② guardRun 挡的是"处理器自己崩了把页面带白屏",兜法是 console.error + 一次中文提示,
// 且刻意不向上重抛(重抛会让 Vue 再进一次同一个处理器)。
// silent 一格没涨(两支都有用户可见出口),台账无需新增条目。
silent: 84,
// 83 = 10-13 面板状态机拆进 useNotificationBellPanel.ts 后的实扫值(原 84)。
// 拆分是为了 SFC 顶到可读棘轮的 800 行上限(1439 行 > 台账点名的 1318),不是冲静默来的:
// 铃铛那两处"有意静默降级"(轮询失败不亮红点、列表失败在面板内给文案)整块搬进了新文件,
// 逐文件台账跟着实况走——组件 3→0、新记 useNotificationBellPanel.ts=2,总数还比基线少 1。
silent: 83,
/** 逐文件台账:只钉总数与文件数两个标量的棘轮,会被「A 加一条 + B 减一条」整段绕过。 */
perFile: {
'src/pages/brand/components/BrandCollectDataTab.vue': 4,
@@ -64,8 +68,9 @@ const BASELINE = {
'src/shared/bridges/web-fallback.ts': 2,
'src/shared/components/ApiSecretSettingsPanel.vue': 1,
'src/shared/components/ExpiryNotice.vue': 2,
'src/shared/components/NotificationBell.vue': 3,
'src/shared/components/NotificationBell.vue': 0,
'src/shared/composables/useLiveTaskBoard.ts': 1,
'src/shared/composables/useNotificationBellPanel.ts': 2,
'src/shared/composables/useTaskProgressLoop.ts': 3,
'src/shared/composables/useVersionUpdate.ts': 1,
'src/shared/dispatch-guard-ui.ts': 1,
@@ -0,0 +1,115 @@
/**
* 面板层纯函数的行为用例(10-13 从 NotificationBell.vue 拆出这一层的唯一目的就在这)。
*
* 与后管同一形状各一份(两端是两个 app,没有共享包):这里判的是聚合两级的口径本身——
* 红点按组数、组内没读完不许掉、下钻三维、日期倒挂交换、换天挂组头。
* 组件那一面由 tests/mount 的挂载用例跑真 DOM,两边不重复。
*
* 跑法:`npx vitest run --config vitest.config.ts`(`npm test` 那把 node --test 零依赖,
* 解析不了 @ 别名与 vue,所以这一支走 vitest 的 tests/pure/**)。
*/
import { describe, expect, it } from 'vitest'
import {
PANEL_DETAIL_LIMIT,
PANEL_PAGE_SIZES,
PANEL_PAGE_SIZE,
applyGroupsPage,
applyMarkReadToCounts,
detailQueryFor,
itemLevel,
normalizedDayRange,
startsNewDay,
} from '@/shared/composables/useNotificationBellPanel'
const group = (over: Record<string, unknown> = {}) => ({
latestId: 7,
scene: 'task_partial',
category: 'task_error',
day: '2026-09-13',
subjectUserId: 1160,
rowCount: 1,
unreadCount: 1,
level: 'error',
title: '用户采集数据任务部分结果缺失',
content: '缺 6 行',
createdAt: '2026-09-13T09:00:00.000Z',
...over,
})
describe('用户端通知面板层纯函数', () => {
it('分页档位与默认每页组数:面板高度按量级设计,别把组数默认成明细数', () => {
expect(PANEL_PAGE_SIZE).toBe(10)
expect(PANEL_PAGE_SIZES).toEqual([10, 20, 50, 100])
})
it('未选日期时不下发日期条件;倒挂自动交换', () => {
expect(normalizedDayRange(null)).toEqual({})
expect(normalizedDayRange(['2026-09-14', '2026-09-13'])).toEqual({
startDate: '2026-09-13',
endDate: '2026-09-14',
})
})
it('下钻条件带满三维,主体为空改走 IS NULL 旗标', () => {
const labelled = detailQueryFor(group({ subjectUserId: 1196 }) as never)
expect(labelled.scene).toBe('task_partial')
expect(labelled.day).toBe('2026-09-13')
expect(labelled.subjectUserId).toBe(1196)
expect(labelled.subjectUnlabelled).toBe(false)
expect(labelled.pageSize).toBe(PANEL_DETAIL_LIMIT)
const unlabelled = detailQueryFor(group({ subjectUserId: null }) as never)
expect(unlabelled.subjectUserId).toBeNull()
expect(unlabelled.subjectUnlabelled).toBe(true)
})
it('组内还有未读时组数不许减,读完最后一条才减', () => {
const groups = [group({ unreadCount: 2 })]
const key = 'task_partial|2026-09-13|1160'
const first = applyMarkReadToCounts(groups, key, 2, 1)
expect(groups[0].unreadCount).toBe(1)
expect(first.unreadCount).toBe(1)
expect(first.unreadGroupCount).toBe(1, '这一组还剩一条没读,红点不许先掉')
const last = applyMarkReadToCounts(groups, key, 1, 1)
expect(groups[0].unreadCount).toBe(0)
expect(last.unreadGroupCount).toBe(0)
})
it('没展开任何一组时只减明细数;两个计数都不许越界成负数', () => {
const groups = [group({ unreadCount: 1 })]
expect(applyMarkReadToCounts(groups, '', 1, 1)).toEqual({ unreadCount: 0, unreadGroupCount: 1 })
expect(applyMarkReadToCounts([], '', 0, 0)).toEqual({ unreadCount: 0, unreadGroupCount: 0 })
})
it('聚合页缺字段时回落:组数按实收、明细条数按清单长度', () => {
const applied = applyGroupsPage(
{ groups: [group(), group({ subjectUserId: 1196, day: '2026-09-12' })] } as never,
{ unreadCount: 4, unreadGroupCount: 2 },
)
expect(applied.total).toBe(2)
expect(applied.rowCount).toBe(2)
expect(applied.unreadCount).toBe(4)
expect(applied.unreadGroupCount).toBe(2)
const empty = applyGroupsPage({ groups: null } as never, { unreadCount: 1, unreadGroupCount: 1 })
expect(empty.groups).toEqual([])
expect(empty.total).toBe(0)
})
it('日期头只在换天时出现:同一天多组(多用户/多场景)不重复挂头', () => {
const list = [group(), group({ subjectUserId: 1196 }), group({ scene: 'task_failed' })]
expect(startsNewDay(list, 0)).toBe(true)
expect(startsNewDay(list, 1)).toBe(false)
expect(startsNewDay(list, 2)).toBe(false, '同一天不同场景仍是两组,但不重复挂日期头')
})
it('级别兜底:非法/缺失都按 warning,组行与明细行配色不分家', () => {
expect(itemLevel({ level: 'error' })).toBe('error')
expect(itemLevel({ level: 'info' })).toBe('info')
expect(itemLevel({ level: 'warn' })).toBe('warning')
expect(itemLevel({ level: '' })).toBe('warning')
})
})