54 lines
2.8 KiB
JavaScript
54 lines
2.8 KiB
JavaScript
'use strict'
|
||
// 失败留痕接线守卫(2026-11-10)
|
||
//
|
||
// 背景:全仓 318 处空 catch(占 45%),多数是合理的 best-effort。但**写路径**失败若纯静默,
|
||
// 表现为"界面说保存了,实际丢了",排查时毫无线索。做法不追求全量改造,只要求**写路径**必须留痕。
|
||
//
|
||
// 本守卫钉三件事(都是 2026-11-07~10 期间新写/新改的存储写路径):
|
||
// ① 图片落 IndexedDB 失败 → 计数
|
||
// ② 图库索引回收(删除旧图 blob)失败 → 计数且不中断整次写入
|
||
// ③ 接线方式统一走 IWBMetricsSwallow(不各自造轮子),且模块加载序在其之后
|
||
const assert = require('assert')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const root = path.join(__dirname, '..')
|
||
const read = (p) => fs.readFileSync(path.join(root, p), 'utf8')
|
||
|
||
const blob = read('src/image-blob-store.js')
|
||
const outputs = read('src/storage-outputs.js')
|
||
const metrics = read('src/runtime-metrics.js')
|
||
|
||
// ① 图片写入失败留痕
|
||
assert.ok(/catch \(e\) \{ _swallow\('image', e\); return \{ ok: false, error: 'idb-throw' \} \}/.test(blob),
|
||
'image-blob-store 的 IDB 写入 catch 必须留痕(_swallow(\'image\', e))')
|
||
|
||
// ② 图库回收失败留痕 + 不中断
|
||
assert.ok(/_swallow\('save', e\)/.test(outputs), 'storage-outputs 的失败路径必须留痕')
|
||
assert.ok(!/if \(item && item\.idb\) await indexed\.del\(item\.idb\)/.test(outputs),
|
||
'storage-outputs 回收旧图时不得裸 await(单个删除失败会让整次写入抛错)—— 必须 try/catch 包住')
|
||
|
||
// ③ 统一实现 + 加载序
|
||
assert.ok(/global\.IWBMetricsSwallow = swallow/.test(metrics), 'IWBMetricsSwallow 必须由 runtime-metrics 导出')
|
||
for (const f of ['src/image-blob-store.js', 'src/storage-outputs.js']) {
|
||
const s = read(f)
|
||
assert.ok(/const _swallow = \(area, e\) => \{ try \{ const f = global\.IWBMetricsSwallow/.test(s),
|
||
f + ' 应通过统一的 _swallow 帮助函数接线(不要各自直接调全局)')
|
||
}
|
||
const order = JSON.parse(read('tests/fixtures/iwb-source-modules-order.json'))
|
||
const iMetrics = order.indexOf('runtime-metrics.js')
|
||
for (const f of ['image-blob-store.js', 'storage-outputs.js']) {
|
||
const i = order.indexOf(f)
|
||
assert.ok(i > iMetrics, f + ' 必须登记在 runtime-metrics.js 之后(否则加载期依赖守卫会红)')
|
||
}
|
||
|
||
// ④ 诊断面板必须能看到(否则留痕没人看)。
|
||
// 用统一源文本入口扫描,而非钉死单个文件 —— 2026-11-10 诊断页已抽成
|
||
// src/ui-settings-tab-diag.js,钉死文件会让这条守卫在搬动后假红。
|
||
{
|
||
const { srcAll } = require('./_helpers/source')
|
||
assert.ok(/已忽略错误/.test(srcAll()), '诊断页必须展示「已忽略错误」计数(任一 src 模块均可)')
|
||
}
|
||
|
||
console.log('swallow wiring checks passed(图片写入 / 图库回收 / 统一实现 / 加载序 / 诊断可见)')
|