4d729733a4
- 此前全树 0 处全局错误处理器:装 app.config.errorHandler + window error + unhandledrejection, 每条都留可诊断记录、界面只出中文、处理器自身异常不重抛也不白屏(9 例判据)。 - safeHttpUrl 未覆盖的 3 处(两个品牌 Tab 与 useVersionUpdate 直装服务端 file_url)收口; 令牌键 aiimage_auth_token 由 4 处各自定义 + 2 处内联收成唯一定义点。 - 「localStorage 有 uid 就免校验」改走 auth-gate 单点,剩余例外台账化且同口径必须能抓到 评审点名的原写法(6 例);卸载配对 3 处补齐 + 全树判据(7 例)。 - task-status-text 把 CANCELLED 显示成"执行中"改对,CANCELED/CANCELLED 两拼写都映射(4 例)。 - 主审计补的四处修复:新判据里两处拿数组和 0 比(怎么写都红);静默 catch 的"用户可见出口" 符号表认不出本仓 userNotice 与注入式 notify,把真报了用户的 catch 判成静默; 新错误出口带来的 2 个 try/catch 按棘轮合同写理由进基线(296→298,silent 一格没涨)。 - 验收:node --test 820 例 0 红、npx vue-tsc --noEmit rc=0。
133 lines
7.9 KiB
TypeScript
133 lines
7.9 KiB
TypeScript
// catch 静默棘轮:只许减,不许增(四面9分口径 §2 用户端「错误静默」)。
|
||
//
|
||
// 口径与扫描器都在 tests/helpers/catch-silence-scanner.ts:按 AST 数 CatchClause,
|
||
// 静默=空块,或首条语句是 console.* 且整块没有 throw / 没有 ElMessage 这类用户可见出口。
|
||
// 与文档基线 292/81 的差别写在扫描器头注释里(grep 数不到无绑定的 `catch {`),这里钉实扫的 295/84。
|
||
// 跑法:npm test
|
||
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { fileURLToPath } from 'node:url'
|
||
import { dirname, resolve, relative, sep } from 'node:path'
|
||
|
||
import { listSourceFiles, readSource } from './helpers/task-terminal-ast-ruler.ts'
|
||
import { scanCatchSilence, toLedger, totals } from './helpers/catch-silence-scanner.ts'
|
||
|
||
const here = dirname(fileURLToPath(import.meta.url))
|
||
const root = resolve(here, '..')
|
||
const rel = (p: string) => relative(root, p).split(sep).join('/')
|
||
const SRC_FILES = listSourceFiles(resolve(root, 'src')).map(rel)
|
||
|
||
const BASELINE = {
|
||
// 296 = 实扫基线 295 + 1 处评审过的新增:
|
||
// kick-handler.ts 的 setAuthNotice()(67eb88cb)——localStorage.setItem/removeItem 在隐私模式
|
||
// 与配额满时真的会抛,兜法是先不落提示、返回「没提示」,属于可接受降级而不是吞掉错误;
|
||
// 同一批另两处(pywebview addEventListener 的外层兜底、isSessionExpiredNav 的外层兜底)挡的是
|
||
// 不可能发生的事(内层 lsGet 已自带兜底、URLSearchParams 对字符串永不抛),本轮直接摘掉,
|
||
// 所以这里只 +1 而不是 +3。静默数 silent 一格没动(仍 <=84),逐文件台账也对平。
|
||
blocks: 298,
|
||
// 298 = 296 + 全局错误出口 app-error.ts 的两支新 try/catch(10-09 装 app.config.errorHandler /
|
||
// window.onerror / unhandledrejection 时引入),两支都不是"吞":
|
||
// ① String(reason) 对无 toString 的对象/Symbol 会抛,兜法返回一句中文占位文本,
|
||
// 该文本随后可见(进界面提示 + 内存环 + 控制台);
|
||
// ② guardRun 挡的是"处理器自己崩了把页面带白屏",兜法是 console.error + 一次中文提示,
|
||
// 且刻意不向上重抛(重抛会让 Vue 再进一次同一个处理器)。
|
||
// silent 一格没涨(两支都有用户可见出口),台账无需新增条目。
|
||
silent: 84,
|
||
/** 逐文件台账:只钉总数与文件数两个标量的棘轮,会被「A 加一条 + B 减一条」整段绕过。 */
|
||
perFile: {
|
||
'src/pages/brand/components/BrandCollectDataTab.vue': 4,
|
||
'src/pages/brand/components/BrandConvertTab.vue': 1,
|
||
'src/pages/brand/components/BrandDedupeTab.vue': 1,
|
||
'src/pages/brand/components/BrandPriceTrackTab.vue': 8,
|
||
'src/pages/brand/components/BrandProductRiskTab.vue': 8,
|
||
'src/pages/brand/components/BrandPublishTab.vue': 2,
|
||
'src/pages/brand/components/BrandQueryAsinTab.vue': 1,
|
||
'src/pages/brand/components/BrandShopDataCrawlTab.vue': 1,
|
||
'src/pages/brand/components/BrandShopMatchTab.vue': 1,
|
||
'src/pages/brand/components/BrandSimilarAsinTab.vue': 1,
|
||
'src/pages/brand/components/BrandSplitTab.vue': 1,
|
||
'src/pages/brand/components/BrandVariantTab.vue': 2,
|
||
'src/pages/home/DesktopHomePage.vue': 1,
|
||
'src/pages/image-video/components/DeliveryVideoWorkspace.vue': 1,
|
||
'src/pages/login/DesktopLoginPage.vue': 12,
|
||
'src/pages/setup/DesktopSecretSetupPage.vue': 1,
|
||
'src/router/index.ts': 3,
|
||
'src/shared/api/http.ts': 1,
|
||
'src/shared/api/task-progress-polling.ts': 1,
|
||
'src/shared/api/types/modules/permission.ts': 1,
|
||
'src/shared/auth/account-expiry.ts': 1,
|
||
'src/shared/auth/ensure-auth.ts': 1,
|
||
'src/shared/auth/kick-handler.ts': 4,
|
||
'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/composables/useLiveTaskBoard.ts': 1,
|
||
'src/shared/composables/useTaskProgressLoop.ts': 3,
|
||
'src/shared/composables/useVersionUpdate.ts': 1,
|
||
'src/shared/dispatch-guard-ui.ts': 1,
|
||
'src/shared/route-prefetch.ts': 1,
|
||
'src/shared/utils/api-secret-store.ts': 9,
|
||
'src/shared/utils/notification-bell.ts': 1,
|
||
'src/shared/utils/row-id.ts': 1,
|
||
} as Record<string, number>,
|
||
}
|
||
|
||
const scans = SRC_FILES.map((f) => scanCatchSilence(readSource(f, root), f))
|
||
const now = totals(scans)
|
||
const ledger = toLedger(scans)
|
||
|
||
test('分母自证:扫描器真的看见了整棵 src(分母塌了的话"≤基线"就是恒绿)', () => {
|
||
assert.ok(SRC_FILES.length > 100, `只扫到 ${SRC_FILES.length} 个文件,分母不对`)
|
||
assert.ok(SRC_FILES.some((f) => f.endsWith('.vue')), '.vue 没进分母就是漏扫')
|
||
assert.ok(now.blocks >= 250, `catch 块只数到 ${now.blocks} 个,与基线 ${BASELINE.blocks} 差得离谱 ⇒ 扫描器坏了,不是代码变好了`)
|
||
// 非零对照:已知藏着静默 catch 的文件必须被扫到,否则"0 处"没有意义
|
||
assert.ok(ledger['src/shared/utils/api-secret-store.ts'] > 0, 'api-secret-store.ts 的静默 catch 没被数到,扫描器口径失效')
|
||
assert.ok(ledger['src/pages/login/DesktopLoginPage.vue'] > 0, 'DesktopLoginPage.vue 的静默 catch 没被数到,扫描器口径失效')
|
||
})
|
||
|
||
test('静默 catch 只许减:总数与逐文件台账都不许超过基线', () => {
|
||
const worse: string[] = []
|
||
for (const [file, count] of Object.entries(ledger)) {
|
||
const allowed = BASELINE.perFile[file]
|
||
if (allowed === undefined) worse.push(`${file} 是新增静默 catch 的文件(${count} 处)`)
|
||
else if (count > allowed) worse.push(`${file} 从 ${allowed} 处涨到 ${count} 处`)
|
||
}
|
||
assert.deepEqual(worse, [], `新增了静默 catch,要么把错误报给用户、要么显式上抛:\n${worse.join('\n')}`)
|
||
assert.ok(now.silent <= BASELINE.silent,
|
||
`静默 catch 总数 ${now.silent} > 基线 ${BASELINE.silent};减下去了就把基线与台账一起改小`)
|
||
assert.ok(now.blocks <= BASELINE.blocks, `catch 块总数从 ${BASELINE.blocks} 涨到 ${now.blocks},新增 try/catch 要一并说明理由`)
|
||
})
|
||
|
||
test('扫描器先自证:新增一个空 catch 一定被数出来', () => {
|
||
const target = 'src/pages/brand/components/BrandCollectDataTab.vue'
|
||
const source = readSource(target, root).replace(/\r\n/g, '\n')
|
||
const anchor = 'function selectCountry(code: string) {'
|
||
assert.equal(source.split(anchor).length - 1, 1, '锚点不唯一,注入自证不作数')
|
||
const before = scanCatchSilence(source, target)
|
||
const injected = source.replace(anchor, 'function injectedProbeForRuler() {\n try { throw new Error("x") } catch { }\n}\n' + anchor)
|
||
assert.notEqual(injected, source, '注入没生效,这条自证是假的')
|
||
|
||
const after = scanCatchSilence(injected, target)
|
||
assert.equal(after.total, before.total + 1, '多出来的 catch 块没被数到分母')
|
||
assert.equal(after.silent.length, before.silent.length + 1, '多出来的空 catch 体没被数成静默')
|
||
assert.deepEqual(Object.entries(ledger).map(([f, c]) => [f, c]).filter(([f]) => f === target).map(([, c]) => c), [before.silent.length],
|
||
'台账与单文件复扫对不上')
|
||
})
|
||
|
||
test('扫描器的口径边界:console + throw、console + ElMessage 都不算静默', () => {
|
||
const cases: Array<[string, number]> = [
|
||
['try { f() } catch { }', 1],
|
||
['try { f() } catch (e) { console.log(e) }', 1],
|
||
['try { f() } catch (e) { console.error(e); throw e }', 0],
|
||
['try { f() } catch (e) { console.error(e); ElMessage.error("失败了") }', 0],
|
||
['try { f() } catch (e) { ElMessage.error(String(e)) }', 0],
|
||
['try { f() } catch (e) { retry() }', 0],
|
||
]
|
||
for (const [src, expected] of cases) {
|
||
const r = scanCatchSilence(src, '<内存口径>')
|
||
assert.equal(r.silent.length, expected, `口径判错:${src}(期望静默 ${expected},实测 ${r.silent.length})`)
|
||
assert.equal(r.total, 1, `catch 块分母判错:${src}`)
|
||
}
|
||
})
|