Files
image-workbench-desktop/tests/sync-facts-guard-test.js
T

43 lines
2.7 KiB
JavaScript
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.
'use strict'
// 事实一致性守卫(2026-11-07)
//
// 背景:新增/移动一个 src 模块要手工同步 9 处,漏一处门禁才红(且只报"应为 N")。
// 本守卫钉住**可机械判定**的三件事,配合 `npm run sync:facts` 一条命令修复:
// ① src/*.js 与 scripts/build.js 的 sourceModules 一一对应(不许漏登记/幽灵条目)
// ② 顺序快照 == build.js 实际顺序(加载序即安全边界)
// ③ 统一源文本 helper 可用且覆盖 app.js + 全部 src(防止 helper 退化成空壳)
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const S = require('./_helpers/source')
const root = path.join(__dirname, '..')
const srcFiles = fs.readdirSync(path.join(root, 'src')).filter((n) => n.endsWith('.js')).sort()
const buildSrc = fs.readFileSync(path.join(root, 'scripts', 'build.js'), 'utf8')
const buildList = [...buildSrc.matchAll(/^ '([a-z0-9-]+\.js)',$/gm)].map((m) => m[1])
const orderSnapshot = JSON.parse(fs.readFileSync(path.join(root, 'tests', 'fixtures', 'iwb-source-modules-order.json'), 'utf8'))
// ① 一一对应
const missing = srcFiles.filter((f) => !buildList.includes(f))
const extra = buildList.filter((f) => !srcFiles.includes(f))
assert.deepStrictEqual(missing, [], 'src 里有模块未登记到 scripts/build.js(会不打进产物):' + missing.join(', '))
assert.deepStrictEqual(extra, [], 'scripts/build.js 登记了 src 里不存在的模块:' + extra.join(', '))
assert.strictEqual(buildList.length, srcFiles.length, '模块数不一致:build ' + buildList.length + ' vs src ' + srcFiles.length)
// ② 顺序快照
assert.deepStrictEqual(orderSnapshot, buildList,
'sourceModules 顺序快照与 build.js 不一致 —— 跑 `npm run sync:facts` 查看,或确认顺序后 `npm run sync:facts:write`')
// ③ helper 可用性(防退化成空壳后所有跨模块断言静默通过)
const appSrc = S.appSrc()
const srcAll = S.srcAll()
assert.ok(appSrc.length > 100000, 'appSrc() 过短(' + appSrc.length + '),helper 可能读错文件')
assert.ok(srcAll.length > 100000, 'srcAll() 过短(' + srcAll.length + '),helper 可能没扫到模块')
assert.ok(S.appAllSrc().length === appSrc.length + srcAll.length + 1, 'appAllSrc() 长度应等于 app + src 拼接')
// 抽查:helper 真能读到已知内容
assert.ok(/function Workbench\(\)/.test(S.appSrc()), 'appSrc() 应包含 Workbench 定义')
assert.ok(/IWBStorageLocal/.test(S.srcAll()), 'srcAll() 应包含 IWBStorageLocal')
console.log('sync-facts guard passed(' + srcFiles.length + ' 个模块一一对应;helper 覆盖 app ' +
Math.round(appSrc.length / 1024) + 'KB + src ' + Math.round(srcAll.length / 1024) + 'KB)')