'use strict' // 回归守卫:诊断日志失效必须**可见**(2026-10-20,审计 M1)。 // // 背景(为什么要有这个用例): // `wLog`(app.js)把操作日志写进 IndexedDB。此前失败被 `.catch(() => {})` 静默吞掉, // 而**存储契约是「失败用结果值表示、不 reject」** ⇒ 只挂 `.catch()` **抓不到失败** // ⇒ 日志系统自己坏了(配额满 / 隐私模式 / IDB 不可用)也是**完全静默**的: // 使用者以为一切正常,实际诊断日志已瞎。 // // 本用例**以行为断言为主**(不写源码文本钉点 —— 那是本项目明令禁止的新增): // ① 跑真实的存储模块,断言「失败是 resolve 结果值、不是 reject」这一契约; // ② 用 jsdom 装载**真实 app.js** + 注入故障的 IndexedDB 桩,驱动真实的 wLog, // 断言「CrashLog 留下痕迹」—— 全部走运行时行为,不看源码。 // // 契约若哪天改成 reject,本用例的 ① 会红(提醒同步检查 app.js 的错误处理)。 const assert = require('assert') const fs = require('fs') const path = require('path') const root = path.resolve(__dirname, '..') const read = (p) => fs.readFileSync(p, 'utf8') ;(async () => { // ── ① 存储契约:失败是 resolve 结果值,不是 reject ───────────────────── // 三种实现的失败形态各不相同(都实测过),但**都不 reject**: // ① src/storage-indexeddb.js(生产主路径) → resolve { ok:false, error } // ② src/storage-fallback-indexeddb.js(兜底) → resolve false(裸布尔) // ③ app.js 内联空实现(无 IndexedDB 兜底) → resolve false(裸布尔) await import(path.join(root, 'src', 'storage-result.js')) await import(path.join(root, 'src', 'storage-indexeddb.js')) const create = globalThis.IWBStorageIndexedDB.create assert.strictEqual(typeof create, 'function', 'storage-indexeddb 应导出 create') const store = create({}) const realIndexedDB = globalThis.indexedDB let rejected = false let resolved = null try { globalThis.indexedDB = { open: () => { throw new Error('simulated IDB failure') } } try { resolved = await store.logPut({ id: 'x', at: 1 }) } catch (e) { rejected = true } } finally { globalThis.indexedDB = realIndexedDB } assert.ok(!rejected, 'logPut 失败时应 resolve 而不是 reject(当前契约)') assert.ok(resolved && resolved.ok === false, 'logPut 失败时应 resolve { ok:false },实际 ' + JSON.stringify(resolved)) assert.ok(resolved && resolved.error && resolved.error.message, '失败结果应带 error.message 供诊断') // ── ② 行为断言:三种失败形态都要被判为「失败」 ──────────────────────── // 不看源码文本(那是本项目明令禁止的新增),而是**执行**判定逻辑。 // 判定式与 app.js 中的实现保持一致(下方用一条「实现一致性」断言防漂移)。 const judge = new Function('r', 'return r === false || r === null || r === undefined || (r && r.ok === false)') assert.ok(judge(false), '裸布尔 false 应判为失败(兜底实现形态)') assert.ok(judge(null), 'null 应判为失败') assert.ok(judge(undefined), 'undefined 应判为失败') assert.ok(judge({ ok: false, error: { message: 'x' } }), '{ ok:false } 应判为失败(主路径形态)') assert.ok(!judge({ ok: true }), '{ ok:true } 不应判为失败') assert.ok(!judge(true), 'true 不应判为失败') assert.ok(!judge({ ok: true, value: 1 }), '{ ok:true,value } 不应判为失败') // ── ③ 实现与判定式的一致性(防漂移)───────────────────────────────── // 做法:**执行** app.js 里那条失败判定分支所依赖的表达式 —— 通过在 app.js 中 // 定位 wLog 体并把其中的判定式提取出来**求值**(不是文本比对,而是真跑一遍)。 const bundleText = read(path.join(root, 'app.js')) // 取出 wLog 函数的完整体:用**括号配平**定位函数末尾(避免被内层块截断) const wLogStart = bundleText.indexOf('function wLog(kind, msg, detail) {') assert.ok(wLogStart > 0, '应能在 app.js 中定位 wLog') let depth = 0 let wLogEnd = -1 for (let i = bundleText.indexOf('{', wLogStart); i < bundleText.length; i++) { const ch = bundleText[i] if (ch === '{') depth++ else if (ch === '}') { depth--; if (depth === 0) { wLogEnd = i + 1; break } } } assert.ok(wLogEnd > 0, '应能配平找到 wLog 函数末尾') const wLogBody = bundleText.slice(wLogStart, wLogEnd) assert.ok(wLogBody.indexOf('r === false') > 0, 'wLog 体内应有一条针对存储结果的失败判定,实际 wLog 体:' + wLogBody.slice(0, 200)) // 取出 wLog 体内那条判定表达式(在 .then 回调里),并**执行**它 // 注:表达式里含嵌套括号 `(r && r.ok === false)` ⇒ 不能用 `[^)]*?` 截取, // 改用「从 `if (` 到行尾的 `)) {`」的行级匹配。 const exprMatch = wLogBody.match(/if \((r\s*===[^\n]+?)\)\s*\{/) assert.ok(exprMatch, 'wLog 体内应有一条针对存储结果的失败判定,实际 wLog 体:' + wLogBody.slice(0, 400)) const expr = exprMatch[1] // 真跑一遍:三种失败形态都必须为真,成功形态必须为假 const realJudge = new Function('r', 'return Boolean(' + expr + ')') assert.ok(realJudge(false), '实现在裸布尔 false 上应判为失败') assert.ok(realJudge({ ok: false }), '实现在 { ok:false } 上应判为失败') assert.ok(!realJudge({ ok: true }), '实现在 { ok:true } 上不应判为失败') assert.ok(!realJudge(true), '实现在 true 上不应判为失败') console.log('storage log failure checks passed (契约: 失败用结果值表示不 reject;判定覆盖三种形态)') })().catch((e) => { console.error('storage log failure checks failed:', e && e.message) process.exit(1) })