/** * 铃铛失败可见性(四面9分口径 §2 后管「0 组件挂载测试 / 错误静默」)。 * * 前 4 支是**行为断言**:直接 import `src/layout/notification-bell-model.ts` 跑真函数, * 提示出口是注入进去的假 sink——不挂载组件也测得到运行时行为。 * 第 5 支是**源码钉子**(readSource):钉的是「面板层那两处 catch 接的是这条出口」这个形状, * (10-13 起 catch 在 notification-bell-panel.ts,同支还钉住组件确实 import 了这层。) * 不是运行时行为;运行时那一面由 `tests/mount/notification-bell-failure.mount.test.ts` * (`npm run test:mount`,vitest + @vue/test-utils + jsdom)覆盖。 * 两者都要:钉子跑在零依赖的 `node --test` 里,挂载例要装 node_modules。 * 第 6 支给那枚钉子做变异自证:把提示出口在内存里摘掉,钉子必须变红(否则第 5 支恒绿)。 */ import test from 'node:test' import assert from 'node:assert/strict' import { readSource } from './helpers.ts' import { createSummaryFailureAlerter, markReadFailureMessage, summaryFailureMessage, type BellFeedbackKind, } from '../src/layout/notification-bell-model.ts' const BELL = 'src/components/NotificationBell.vue' const PANEL = 'src/layout/notification-bell-panel.ts' /** 钉子判据:两处失败路径的 console.warn 之后 160 字符窗口里必须有用户可见出口。 */ function bellFailureExits(src: string): string[] { const missing: string[] = [] for (const token of ['未读数刷新失败', '标记已读失败']) { const at = src.indexOf(`console.warn('[admin-notification] ${token}`) if (at < 0) { missing.push(`${token}:console.warn 本体都不在了(catch 被挪走或整段删除)`) continue } const window = src.slice(at, at + 160) if (!/showAdminFeedback\(|summaryFailures\.reportFailure\(/.test(window)) { missing.push(`${token}:console.warn 之后 160 字符内没有提示出口`) } } return missing } test('标记已读失败:文案带原因,非 Error 抛值也不塌成 undefined', () => { assert.equal(markReadFailureMessage(new Error('500 Internal Server Error')), '这条通知没能标记为已读:500 Internal Server Error') assert.equal(markReadFailureMessage('网关超时'), '这条通知没能标记为已读:网关超时') assert.equal(markReadFailureMessage(undefined), '这条通知没能标记为已读') assert.equal(markReadFailureMessage(new Error(' ')), '这条通知没能标记为已读') }) test('未读数刷新失败:文案带原因,且与标记已读的文案分得开', () => { assert.equal(summaryFailureMessage(new Error('401 Unauthorized')), '通知未读数刷新失败:401 Unauthorized') assert.equal(summaryFailureMessage(null), '通知未读数刷新失败') assert.notEqual(summaryFailureMessage(new Error('boom')), markReadFailureMessage(new Error('boom')), '两类失败共用一句文案时,用户与排障者都分不清是轮询挂了还是点击没生效') }) test('轮询提示闸门:同场故障只提示一次,成功一轮后重新武装', () => { const seen: Array<{ message: string; kind: BellFeedbackKind }> = [] const alerter = createSummaryFailureAlerter((message, kind) => seen.push({ message, kind })) assert.equal(alerter.isArmed(), false, '闸门初始应为未提示状态') alerter.reportFailure(new Error('502')) alerter.reportFailure(new Error('502')) alerter.reportFailure(new Error('503')) assert.equal(seen.length, 1, `60s 一轮的轮询不设闸就是每分钟打扰一次,实测提示 ${seen.length} 次`) assert.deepEqual(seen[0], { message: '通知未读数刷新失败:502', kind: 'warning' }) assert.equal(alerter.isArmed(), true) alerter.reportSuccess() assert.equal(alerter.isArmed(), false, '成功一轮后必须重新武装,否则恢复期之后的新故障又变回静默') alerter.reportFailure(new Error('500')) assert.equal(seen.length, 2, '重新武装后下一次失败要再提示一次') assert.equal(seen[1].message, '通知未读数刷新失败:500') }) test('闸门不会把成功当失败:reportSuccess 之后再报错误仍是可见的', () => { const seen: string[] = [] const alerter = createSummaryFailureAlerter((message) => seen.push(message)) alerter.reportFailure(new Error('第 1 次')) alerter.reportSuccess() alerter.reportSuccess() alerter.reportFailure(new Error('第 2 次')) assert.deepEqual(seen, ['通知未读数刷新失败:第 1 次', '通知未读数刷新失败:第 2 次'], '重复 reportSuccess 不该把闸门永久卸掉,也不该把第二次故障吞掉') }) test('源码钉子(非行为断言):两处 catch 都接到 admin-feedback-ui 的统一出口', () => { // 10-13 两级改造后这两处 catch 长在面板模块里(SFC 顶到可读棘轮的 800 行上限,状态机整块拆出); // 钉子跟着搬;「组件在用这一层」由 tests/mount 那支挂载用例在运行时证(这里再加一枚 readSource 就是往只许减的台账里添读点)。 const src = readSource(PANEL) assert.ok(src.length > 0, '取样集非空自检:notification-bell-panel.ts 读到了') assert.ok(src.includes(`import { showAdminFeedback } from '@/components/admin-feedback-ui'`), '铃铛没接 admin-feedback-ui 的统一出口') assert.ok(src.includes('createSummaryFailureAlerter'), '轮询闸门没在面板层实例化') assert.deepEqual(bellFailureExits(src), [], '两处静默 catch 里有一处又退回只 console 了') }) test('判据自证(内存变异):摘掉提示出口,上面那枚钉子必须变红', () => { const src = readSource(PANEL) assert.deepEqual(bellFailureExits(src), [], '基线就该是干净的,否则这条自证测不出任何东西') const variants: Array<[string, string]> = [ ['摘掉轮询提示', " summaryFailures.reportFailure(error)\n"], ['摘掉标记已读提示', " showAdminFeedback(markReadFailureMessage(error), 'error')\n"], ] for (const [label, dropped] of variants) { assert.equal(src.split(dropped).length - 1, 1, `「${label}」的锚串在源码里不唯一,变异不作数`) const mutated = src.replace(dropped, '') assert.notEqual(mutated, src, `「${label}」没删掉任何东西`) assert.ok(bellFailureExits(mutated).length > 0, `摘掉提示出口后钉子仍然全绿=判据恒绿`) } })