Files
crawler-plugin/frontend-vue/tests/catch-silence-ratchet.test.ts
T
huangzd1997 aea5614a8b fix(终态): 消掉第二套任务终态词表,并把判据从正则换成 AST
useTaskProgressLoop 的 DEFAULT_TERMINAL 只认 SUCCESS|FAILED,而 COMPLETED≡SUCCESS
(task-status-text 把两者都映射成「已完成」,看板 SUCCESS_STATUSES_ORDERED 也含两者)。
收到 COMPLETED 不退出的轮询会一直空转到瞬断预算耗尽——这形状先前在跟价与风控两页踩过。
唯一被这次改宽影响的页面是 BrandCollectDataTab(它不传 isTerminal);BrandPublishTab
传自己那份含 COMPLETED+CANCELLED 的判定,行为不变,已用单独一条钉子钉住。

判据原先是"变量名恰为 status"的源码正则,同形异名全 src 有 16 处抓不到,等于恒绿;
现在用 typescript(node_modules 里已有)做 AST 尺子,并把"旧正则对三条反例失明"写进断言。
ShopMatch 的 waitForScheduledTaskStageExit 等的是阶段不是任务终态,为此给骨架加了可选
exitOn,保住它原有语义、未知状态不会挂死。

口径更正:窄集合"全 src 应为 0"不成立——实测 20 处/8 文件是 fileStatus(结果文件维度)、
loop-run 带 STOPPED 的词表、显示与下载判定,不同维度不该混进一本账;改成迁移面硬 0 +
逐文件台账只许减。catch 同理:grep 'catch (' 的 292 数不到无绑定 catch {,AST 口径 295/84。
2026-10-08 17:36:47 +08:00

120 lines
6.6 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 = {
blocks: 295,
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}`)
}
})