Files
crawler-plugin/frontend-vue/tests/catch-silence-ratchet.test.ts
T
huangzd1997 0877d41262 fix(用户端护栏): 摘掉两处挡"不可能发生"的兜底 catch,棘轮基线只 +1 并记名
把用户端全量套件并进实跑分母时跑出 1 红:`catch-silence-ratchet` 的 catch 块总数从 295
涨到 298,逐文件台账又点名 kick-handler.ts 从 4 涨到 5。三笔新增都来自 67eb88cb
(19:04 那批"登录态失效前端会真退出"),逐处看过之后分两类处理,而不是把基线一次抬 3:

 * kick-handler.ts:`window.addEventListener('pywebviewready', …)` 外面那层 try/catch 摘掉——
   这个调用在任何受支持浏览器里都不抛,真正的失败面(桥尚未就绪)已经由内层 apply() 的
   try/catch 兜住;
 * DesktopLoginPage.vue:`isSessionExpiredNav()` 的外层 try/catch 摘掉——`URLSearchParams`
   对字符串永不抛,而它调用的 `lsGet()` 自己已经带兜底,这层是第二份对同一件事的防御;
 * 保留 `setAuthNotice()`:`localStorage.setItem/removeItem` 在隐私模式与配额满时**真会抛**,
   兜法是"这条提示不落",是可接受降级而不是吞错——这一处记进 blocks 基线(295→296)并写明出处。

静默数(空块或首条 console.* 且无 throw/ElMessage)一格没动,仍 ≤84;逐文件台账对平。
验证:node --test tests/catch-silence-ratchet.test.ts ⇒ 4/4 绿;
npm test(93 个文件)⇒ tests 762, pass 762, fail 0(改前是 761 pass / 1 fail)。

纯前端源码,未重新构建发布 ⇒ 现网零影响;改的都是 HEAD 已提交内容,不涉及并行会话在飞文件。
2026-10-09 00:41:21 +08:00

126 lines
7.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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: 296,
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}`)
}
})