/** * 浏览器布局告警不许当成页面出错(2026-11-27 客户实况:外观专利数据库页弹 * `页面出错了:ResizeObserver loop completed with undelivered notifications.`)。 * * 两半判据: * ① 纯函数:两条浏览器文案判良性,别的(真异常、截断文案、空串)都不许判良性—— * 清单放宽等于把真异常洗白,收窄等于把误报还给用户; * ② 出口行为:`reportClientError` 对良性告警**不进内存环**(不弹红条、不上报都挂在同一个早返回上), * 对真异常照旧进环——只钉纯函数会漏掉「判了但没接线」。 */ import test from 'node:test' import assert from 'node:assert/strict' import { BENIGN_BROWSER_WARNING_PREFIXES, isBenignBrowserWarning, } from '../src/shared/api/benign-browser-warnings.ts' import { recentClientErrors, reportClientError } from '../src/shared/api/error-report.ts' test('两条浏览器布局告警判良性:Chrome 现行文案(带句点)与旧版/Safari 文案', () => { assert.equal(isBenignBrowserWarning('ResizeObserver loop completed with undelivered notifications.'), true) assert.equal(isBenignBrowserWarning('ResizeObserver loop limit exceeded'), true) // 浏览器有时会在同一句后面再拼一段,前缀匹配要照样认 assert.equal( isBenignBrowserWarning('ResizeObserver loop completed with undelivered notifications. at flush()'), true, ) // 前后空白不影响判定(ErrorEvent.message 可能带) assert.equal(isBenignBrowserWarning(' ResizeObserver loop limit exceeded '), true) }) test('真异常不许被这张清单洗白', () => { const notBenign = [ '', ' ', 'Uncaught TypeError: Cannot read properties of undefined', 'Network Error', 'ResizeObserver loop completed', // 截断文案:不是浏览器原话,不能认 'resizeobserver loop limit exceeded', // 大小写不同:浏览器不会这么发 '页面出了点问题,这一步没能完成。', 'Uncaught ResizeObserver loop limit exceeded', // 前置了别的内容,不是纯告警 ] for (const text of notBenign) { assert.equal(isBenignBrowserWarning(text), false, `不该判良性:${JSON.stringify(text)}`) } }) test('清单只许是 ResizeObserver 那两条(防止有人往里加通用错误文案)', () => { assert.equal(BENIGN_BROWSER_WARNING_PREFIXES.length, 2) for (const prefix of BENIGN_BROWSER_WARNING_PREFIXES) { assert.match(prefix, /^ResizeObserver loop /, `良性清单里混进了非布局告警:${prefix}`) } }) test('出口真接线:良性告警不进内存环,真异常照旧进环', () => { const before = recentClientErrors().length const debugged: string[] = [] const originalDebug = console.debug console.debug = (...args: unknown[]) => { debugged.push(args.map(String).join(' ')) } try { reportClientError({ kind: 'window-error', message: 'ResizeObserver loop completed with undelivered notifications.', route: '/patent-center/clean-items', }) } finally { console.debug = originalDebug } assert.equal(recentClientErrors().length, before, '良性告警进了内存环 ⇒ 红条/上报那条路也会走') assert.equal(debugged.length, 1, '良性告警要留一行 console.debug,不能连留痕都没有') assert.match(debugged[0], /benign/) reportClientError({ kind: 'window-error', message: 'Uncaught TypeError: 良性告警测试用的真异常', route: '/patent-center/clean-items', }) assert.equal(recentClientErrors().length, before + 1, '真异常必须照旧进环(判据自己不能把错误吞掉)') })