327 lines
22 KiB
JavaScript
327 lines
22 KiB
JavaScript
#!/usr/bin/env node
|
||
'use strict'
|
||
|
||
/**
|
||
* 守门测试:模块边界与构建登记(把 §11.2/§11.3 的「边界硬规则」变成可执行断言)。
|
||
*
|
||
* 动机:`src/*.js` 通过 `window.IWB*` 在运行时互相耦合,加载顺序由 `scripts/build.js`
|
||
* 的 `sourceModules` 决定且「顺序即加载序,切勿排序/改动」;而 app.js 是入口层、
|
||
* 不得被 `src/*.js` 反向依赖。这些规则原先只写在
|
||
* `docs/standards/project-engineering-standards.md` §11.2/§11.3 里,靠人记 —— 本用例把它钉进门禁。
|
||
*
|
||
* 四组断言:
|
||
* 1) `src/*.js` 不得引用 app.js 定义在 `window` 上的全局(反向依赖)。
|
||
* 2) `scripts/build.js` 的 `sourceModules`:非空 / 无重复 / 项项存在 / 与 `src/*.js` 双射 / 顺序未被重排。
|
||
* 3) 模块名前缀必须在白名单内(新模块须遵循既有前缀命名)。
|
||
* 4) 冻结 app.js 与 `src/*.js` 之间的耦合面:app.js 引用了哪些 **src 导出名**(含非 `IWB` 前缀的
|
||
* `Canvas*` 导出 —— 这类裸全局标识符同样会被 app.js 直接使用,早期只按 `\bIWB` 扫描会漏)。
|
||
* 5) 非 `IWB` 前缀的模块导出名必须冻结在已知例外清单内(防止命名约定继续漂移)。
|
||
*
|
||
* 快照更新(有意为之的动作,改完请连同本文件一起提交):
|
||
* IWB_WRITE_BOUNDARY_SNAPSHOT=1 node tests/architecture-boundary-guard-test.js
|
||
*
|
||
* 只做只读断言,不修改任何代码;失败即 process.exit(1)(与既有 node 测试一致)。
|
||
*/
|
||
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const root = path.join(__dirname, '..')
|
||
const read = (p) => fs.readFileSync(path.join(root, p), 'utf8')
|
||
const SRC_DIR = path.join(root, 'src')
|
||
const SNAPSHOT_READS = path.join(root, 'tests', 'fixtures', 'iwb-app-global-reads.json')
|
||
const SNAPSHOT_ORDER = path.join(root, 'tests', 'fixtures', 'iwb-source-modules-order.json')
|
||
const SNAPSHOT_NODE_TABLES = path.join(root, 'tests', 'fixtures', 'iwb-node-tables.json')
|
||
const WRITE_SNAPSHOT = process.env.IWB_WRITE_BOUNDARY_SNAPSHOT === '1'
|
||
|
||
// 平台能力 polyfill(模块里 `global.X = global.X || X` 之类),不是模块命名空间导出。
|
||
const PLATFORM_SHIMS = ['AbortController', 'clearTimeout', 'setTimeout', 'requestAnimationFrame', 'queueMicrotask', 'structuredClone']
|
||
// 已知的「非 IWB 前缀」模块导出(§11.3 约定为 `window.IWB*`;这三个是历史例外,待后续阶段收敛)。
|
||
const NON_IWB_EXPORT_ALLOWLIST = ['CanvasActions', 'CanvasNodeFactory']
|
||
|
||
let failed = 0
|
||
const assert = (name, ok, detail) => {
|
||
if (ok) {
|
||
console.log(' PASS ' + name)
|
||
} else {
|
||
failed++
|
||
console.log(' FAIL ' + name + (detail ? ' —— ' + detail : ''))
|
||
}
|
||
}
|
||
|
||
const appJs = read('app.js')
|
||
const buildJs = read('scripts/build.js')
|
||
const srcFiles = fs.readdirSync(SRC_DIR).filter((n) => n.endsWith('.js')).sort()
|
||
|
||
// `window.X = ` 赋值(排除 `===`:前后各加负向断言,避免把 `window.IX === 'function'` 误判为定义)。
|
||
const DEFINE_RE = /window\.(IWB[A-Za-z0-9_]*|__IWB[A-Za-z0-9_]*)\s*(?<!=)=(?!=)/g
|
||
// 模块自身的命名空间导出:`global.X = ` / `root.X = `。
|
||
const EXPORT_RE = /(?:global|root)\.([A-Za-z_][A-Za-z0-9_]*)\s*(?<!=)=(?!=)/g
|
||
|
||
// ── 1. src 不得引用 app.js 定义的全局(反向依赖) ─────────────────────────
|
||
console.log('1) src/*.js 不得引用 app.js 定义的全局')
|
||
const appDefined = [...new Set([...appJs.matchAll(DEFINE_RE)].map((m) => m[1]))].sort()
|
||
assert('解析到 app.js 定义在 window 的全局(非空)', appDefined.length > 0, '实际 ' + appDefined.length)
|
||
// 哨兵:确认 DEFINE_RE 没被改坏导致「解析到但内容为空」。
|
||
// 2026-10-28(P1 批6):__IWB_COMPRESS_LAST__ 随 compressDataUrl 迁到 src/image-compress.js。
|
||
// 随重构推进,测试钩子会越来越多住在 src 模块里 ⇒ 这里改成**同时扫 app.js 与 src/**,
|
||
// 比每隔几批换一个哨兵更稳(换哨兵只是把同一个问题推给下一批)。
|
||
const srcDefinedAll = new Set()
|
||
for (const file of srcFiles) {
|
||
const src = read(path.join('src', file))
|
||
for (const m of src.matchAll(DEFINE_RE)) srcDefinedAll.add(m[1])
|
||
}
|
||
const allDefined = new Set([...appDefined, ...srcDefinedAll])
|
||
assert('其中包含 __IWB_COMPRESS_LAST__(防止解析失效后空过;允许住在 app.js 或 src/)',
|
||
allDefined.has('__IWB_COMPRESS_LAST__'),
|
||
'app.js: ' + appDefined.join(', ') + ' | src: ' + [...srcDefinedAll].sort().join(', '))
|
||
const offenders = []
|
||
let srcChecked = 0
|
||
for (const file of srcFiles) {
|
||
srcChecked++
|
||
const src = read(path.join('src', file))
|
||
for (const name of appDefined) {
|
||
if (new RegExp('\\b' + name + '\\b').test(src)) offenders.push(file + ' → ' + name)
|
||
}
|
||
}
|
||
assert('src/*.js 无一处引用 app.js 定义的全局', offenders.length === 0, offenders.join('; '))
|
||
|
||
// ── 2. sourceModules 完整性 ──────────────────────────────────────────────
|
||
console.log('2) scripts/build.js 的 sourceModules')
|
||
const arrMatch = buildJs.match(/const sourceModules\s*=\s*\[([\s\S]*?)\]/)
|
||
assert('解析到 sourceModules 数组', Boolean(arrMatch))
|
||
const listed = arrMatch ? [...arrMatch[1].matchAll(/'([^']+\.js)'/g)].map((m) => m[1]) : []
|
||
assert('sourceModules 非空', listed.length > 0, '实际 ' + listed.length)
|
||
assert('无重复项', listed.filter((n, i) => listed.indexOf(n) !== i).length === 0,
|
||
listed.filter((n, i) => listed.indexOf(n) !== i).join(', '))
|
||
const missingFiles = listed.filter((n) => !fs.existsSync(path.join(SRC_DIR, n)))
|
||
assert('每一项都对应存在的文件', missingFiles.length === 0, missingFiles.join(', '))
|
||
// 核心模块(非 plugin-*)必须显式登记;plugin-*.js 允许由 build.js 自动发现(按文件名序追加到末尾)
|
||
const isPluginModule = (n) => /^plugin-.+\.js$/.test(n)
|
||
const missingCore = srcFiles.filter((n) => !isPluginModule(n) && !listed.includes(n))
|
||
assert('核心模块(非 plugin-*)必须全部显式登记', missingCore.length === 0, missingCore.join(', '))
|
||
assert('plugin-host.js 必须在 sourceModules 中(宿主必须先于自动发现的插件加载)', listed.includes('plugin-host.js'))
|
||
assert('登记数 == 核心文件数 + 已登记的插件数(自动发现的插件不计入)',
|
||
listed.length === srcFiles.filter((n) => !isPluginModule(n) || listed.includes(n)).length,
|
||
listed.length + ' vs ' + srcFiles.filter((n) => !isPluginModule(n) || listed.includes(n)).length)
|
||
|
||
if (WRITE_SNAPSHOT) {
|
||
fs.mkdirSync(path.dirname(SNAPSHOT_ORDER), { recursive: true })
|
||
fs.writeFileSync(SNAPSHOT_ORDER, JSON.stringify(listed, null, 2) + '\n')
|
||
console.log(' (已写入 sourceModules 顺序快照)')
|
||
}
|
||
if (!fs.existsSync(SNAPSHOT_ORDER)) {
|
||
assert('sourceModules 顺序快照存在', false, '缺失;用 IWB_WRITE_BOUNDARY_SNAPSHOT=1 生成')
|
||
} else {
|
||
const orderSnapshot = JSON.parse(read(path.join('tests', 'fixtures', 'iwb-source-modules-order.json')))
|
||
assert('sourceModules 顺序 == 冻结快照(顺序即加载序,不得重排)',
|
||
JSON.stringify(listed) === JSON.stringify(orderSnapshot),
|
||
'现序与快照不一致(若确属有意重排,请重生成快照并说明理由)')
|
||
}
|
||
|
||
// ── 3. 模块名前缀白名单 ──────────────────────────────────────────────────
|
||
console.log('3) 模块名前缀白名单')
|
||
// clipboard 为 v3.24.67 新增的领域:剪贴板判定(kind)/ 落地(paste)/ 写剪贴板(copy)/ 待命替换(paste-once)。
|
||
// 它确实不属于 image / ui / canvas 任何一支(判定是纯函数、落地要 DOM),故独立成前缀而非硬塞。
|
||
const PREFIX_RE = /^(runtime|storage|generation|canvas|workspace|gallery|image|clipboard|ui|plugin|theme)-/
|
||
const badPrefix = srcFiles.filter((n) => !PREFIX_RE.test(n))
|
||
assert('全部 src/*.js 前缀合法', badPrefix.length === 0, badPrefix.join(', '))
|
||
assert('检查文件数与目录实际一致', srcChecked === fs.readdirSync(SRC_DIR).filter((n) => n.endsWith('.js')).length,
|
||
srcChecked + ' vs ' + fs.readdirSync(SRC_DIR).filter((n) => n.endsWith('.js')).length)
|
||
|
||
// ── 4. 冻结 app.js 与 src 的耦合面(按 src 实际导出名,覆盖非 IWB 前缀) ──
|
||
console.log('4) 冻结 app.js ↔ src 的耦合面(src 导出名,含非 IWB 前缀导出)')
|
||
const srcExportedAll = [...new Set(srcFiles.flatMap((f) => [...read('src/' + f).matchAll(EXPORT_RE)].map((m) => m[1])))].sort()
|
||
const shimUsed = srcExportedAll.filter((n) => PLATFORM_SHIMS.includes(n))
|
||
const srcExported = srcExportedAll.filter((n) => !PLATFORM_SHIMS.includes(n))
|
||
assert('解析到 src 导出名(非空)', srcExported.length > 0, '实际 ' + srcExported.length)
|
||
assert('被滤除的名字仅限已知平台 polyfill', shimUsed.every((n) => PLATFORM_SHIMS.includes(n)), shimUsed.join(', '))
|
||
const appCouplings = srcExported.filter((n) => new RegExp('\\b' + n + '\\b').test(appJs))
|
||
assert('耦合面含 CanvasActions(非 IWB 前缀的真实耦合,防再次漏扫)', appCouplings.includes('CanvasActions'), appCouplings.join(', '))
|
||
if (WRITE_SNAPSHOT) {
|
||
fs.mkdirSync(path.dirname(SNAPSHOT_READS), { recursive: true })
|
||
fs.writeFileSync(SNAPSHOT_READS, JSON.stringify(appCouplings, null, 2) + '\n')
|
||
console.log(' (已写入 app.js ↔ src 耦合面快照)')
|
||
}
|
||
if (!fs.existsSync(SNAPSHOT_READS)) {
|
||
assert('耦合面快照存在', false, '缺失;用 IWB_WRITE_BOUNDARY_SNAPSHOT=1 生成')
|
||
} else {
|
||
const readsSnapshot = JSON.parse(read('tests/fixtures/iwb-app-global-reads.json'))
|
||
assert('快照非空', readsSnapshot.length > 0, '实际 ' + readsSnapshot.length)
|
||
assert('快照规模合理(>= 20,防止快照退化成空壳)', readsSnapshot.length >= 20, '实际 ' + readsSnapshot.length)
|
||
const added = appCouplings.filter((n) => !readsSnapshot.includes(n))
|
||
const removed = readsSnapshot.filter((n) => !appCouplings.includes(n))
|
||
assert('app.js ↔ src 耦合面 == 冻结快照',
|
||
added.length === 0 && removed.length === 0,
|
||
'新增 [' + added.join(', ') + '] 移除 [' + removed.join(', ') + ']')
|
||
}
|
||
|
||
// ── 5. 非 IWB 前缀导出名冻结在已知例外内 ─────────────────────────────────
|
||
console.log('5) 非 IWB 前缀的模块导出名')
|
||
const nonIwbExports = srcExported.filter((n) => !n.startsWith('IWB')).sort()
|
||
assert('非 IWB 前缀导出 == 已知例外清单(新增即需先收敛命名)',
|
||
JSON.stringify(nonIwbExports) === JSON.stringify([...NON_IWB_EXPORT_ALLOWLIST].sort()),
|
||
'实际 [' + nonIwbExports.join(', ') + ']')
|
||
|
||
// ── 6. 节点类型四表必须同步(漏一处 = 节点静默丢失 / 样式取到 undefined) ────
|
||
console.log('6) 节点类型表同步(NODE_H / COLORS / NODE_TITLES / DEFAULTS)')
|
||
const keysOf = (re) => {
|
||
const m = appJs.match(re)
|
||
return m ? [...m[1].matchAll(/([a-zA-Z0-9_]+)\s*:/g)].map((x) => x[1]).sort() : null
|
||
}
|
||
const nodeHKeys = keysOf(/const NODE_H = \{([\s\S]*?)\}/)
|
||
const colorKeys = keysOf(/const COLORS = \{([\s\S]*?)\}/)
|
||
const titleKeys = keysOf(/const NODE_TITLES = \{([\s\S]*?)\}/)
|
||
const defaultsBlock = appJs.match(/const DEFAULTS = \{([\s\S]*?)\n \}/)
|
||
const defaultKeys = defaultsBlock ? [...defaultsBlock[1].matchAll(/\n ([a-zA-Z0-9_]+): \{/g)].map((x) => x[1]).sort() : null
|
||
assert('四表都能解析出键集', Boolean(nodeHKeys && colorKeys && titleKeys && defaultKeys))
|
||
assert('NODE_H 非空(normalizeNodes 按它过滤,漏键 = 节点被静默丢弃)', Boolean(nodeHKeys && nodeHKeys.length > 0), String(nodeHKeys && nodeHKeys.length))
|
||
assert('COLORS 键集 == NODE_H 键集', JSON.stringify(colorKeys) === JSON.stringify(nodeHKeys), (colorKeys || []).join(','))
|
||
assert('NODE_TITLES 键集 == NODE_H 键集', JSON.stringify(titleKeys) === JSON.stringify(nodeHKeys), (titleKeys || []).join(','))
|
||
assert('DEFAULTS 不得含 NODE_H 之外的类型', (defaultKeys || []).every((k) => (nodeHKeys || []).includes(k)),
|
||
(defaultKeys || []).filter((k) => !(nodeHKeys || []).includes(k)).join(','))
|
||
const noDefaults = (nodeHKeys || []).filter((k) => !(defaultKeys || []).includes(k))
|
||
assert('每个节点类型都必须有 DEFAULTS 条目(缺则 cloneNodeDefaults 返回 {} ⇒ 数据形状不一致)', noDefaults.length === 0, noDefaults.join(', '))
|
||
|
||
// F0(P2-11b 前置保险):四表的**字面文本**逐字节冻结 —— 键集断言之外,再加"值也不许悄悄变"。
|
||
// 动机:P2-11b 会围绕这四表引入运行时注册表;先把它们钉死,任何重构顺手动到值/顺序都会当场打红。
|
||
const nodeTablesBlock = {
|
||
NODE_H: (appJs.match(/const NODE_H = \{([\s\S]*?)\}/) || [])[1] || null,
|
||
COLORS: (appJs.match(/const COLORS = \{([\s\S]*?)\}/) || [])[1] || null,
|
||
NODE_TITLES: (appJs.match(/const NODE_TITLES = \{([\s\S]*?)\}/) || [])[1] || null,
|
||
DEFAULTS: (appJs.match(/const DEFAULTS = \{([\s\S]*?)\n \}/) || [])[1] || null,
|
||
}
|
||
if (WRITE_SNAPSHOT) {
|
||
fs.mkdirSync(path.dirname(SNAPSHOT_NODE_TABLES), { recursive: true })
|
||
fs.writeFileSync(SNAPSHOT_NODE_TABLES, JSON.stringify(nodeTablesBlock, null, 2) + '\n')
|
||
console.log(' (已写入四表字面文本快照)')
|
||
}
|
||
assert('四表都能解析出字面文本', Object.values(nodeTablesBlock).every((v) => typeof v === 'string' && v.length > 0))
|
||
if (!fs.existsSync(SNAPSHOT_NODE_TABLES)) {
|
||
assert('四表字面文本快照存在', false, '缺失;用 IWB_WRITE_BOUNDARY_SNAPSHOT=1 生成')
|
||
} else {
|
||
const snap = JSON.parse(read('tests/fixtures/iwb-node-tables.json'))
|
||
assert('四表字面文本 == 冻结快照(F0:重构前逐字节保险)',
|
||
JSON.stringify(nodeTablesBlock) === JSON.stringify(snap),
|
||
'四表文本已变(若确属有意新增类型,请连同本快照一起重生成)')
|
||
}
|
||
|
||
// ── 7. 节点类型字面量必须都在 NODE_H 内(收敛到类型注册表的前置守卫) ──────
|
||
// 只扫「节点变量」:node/n/dst/src/sn/tgt/x/ps/source/target/b/d/na/nb(实测这些变量下全是节点类型)。
|
||
// 排除非节点对象(r=引用种类 / ga=鉴权 / ih=图床 / spec=设置项类型 / blob / c 等),否则会假阳性。
|
||
console.log('7) 节点类型字面量(type === ...)')
|
||
const NODE_VAR_RE = /\b(?:node|n|dst|src|sn|tgt|x|ps|source|target|b|d|na|nb)\.type === '([a-zA-Z0-9_]+)'/g
|
||
const typeLiterals = [...new Set([...appJs.matchAll(NODE_VAR_RE)].map((m) => m[1]))].sort()
|
||
assert('解析到节点类型字面量(非空)', typeLiterals.length > 0, String(typeLiterals.length))
|
||
assert('节点类型字面量覆盖了主要类型(防正则失效后空过)',
|
||
['prompt', 'ref', 'gen', 'out'].every((t) => typeLiterals.includes(t)), typeLiterals.join(', '))
|
||
const unknownTypes = typeLiterals.filter((t) => !(nodeHKeys || []).includes(t))
|
||
assert('所有节点类型字面量都在 NODE_H 内(防拼写错误 / 删类型后的死分支)', unknownTypes.length === 0, unknownTypes.join(', '))
|
||
// P2-11b-2:插件类型 id 方案必须锁定为 `plugin:<pluginId>:<variantId>`(用 pluginId 段消歧;
|
||
// 禁止会撞的扁平 `plugin-<variant>`)。实现用谓词而非 `X.type === 'plugin:...'` 字面量 ⇒ 第 7 组正则不涉及。
|
||
assert('插件类型 id 方案锁定(plugin:<pluginId>:<variantId>)',
|
||
/const pluginTypeId = \(pluginId, variantId\) => 'plugin:'/.test(appJs) && /const isPluginTypeId = \(type\) => typeof type === 'string'/.test(appJs))
|
||
|
||
// ── 8. 加载期依赖顺序(快照守「没变」,这组守「变了也对」) ────────────────
|
||
// 动机:第 2 组的顺序快照只能证明「顺序没被改过」,不能证明「这个顺序是对的」。
|
||
// 新加一个模块时,只要把它插进 sourceModules 并重生成快照,门禁全绿 —— 即使它插在了
|
||
// 依赖它的模块**之后**。而 `const R = global.IWBStorageResult` 这类**加载期**读取是在
|
||
// IIFE 顶层求值的:定义者还没执行 ⇒ R 是 undefined ⇒ 首次调用才炸(甚至在首屏)。
|
||
// 单测 harness 按同一个 sourceModules 顺序注入,会一起错 ⇒ 本地也看不出问题。
|
||
// 判据:模块 IIFE 顶层语句(行首缩进 ≤ 2)对 `global.IWBXxx` 的**读取**属加载期依赖,
|
||
// 必须排在定义者之后;函数体内的读取是延迟引用(调用时才取),不构成排序要求 —— 实测这类有 2 处
|
||
// (runtime-fetch / runtime-execution-context 读 IWBCrashLog),它们的逆序是**合法**的。
|
||
console.log('8) 加载期跨模块依赖顺序')
|
||
|
||
// 判据用**行首缩进**,不用括号深度:本仓 src 模块格式高度统一
|
||
// (`;(function (global) {` 开头、顶层语句恰好 2 空格、函数体与对象字面量内部 ≥ 4 空格),
|
||
// 缩进判据不需要解析字符串 / 正则 / 模板串。
|
||
// 曾用「括号深度 ≤ 1」实现,结果被模板字符串里的花括号干扰(历史案例)
|
||
// `/^(blob:|\/|\.\.\/)/i` 正则字面量算错平衡(depth = −1)—— 说明按字符扫括号在这类文件上不可靠。
|
||
// 已知代价:顶层对象字面量**内部**的加载期读取(缩进 4)不计入;实测当前 0 处,
|
||
// 且漏判只会少守一条,不会误红。
|
||
const LOAD_REF_RE = /(?:global|root|window|globalThis)\s*\.\s*(IWB[A-Za-z0-9_]+|Canvas[A-Za-z0-9_]+)/g
|
||
const LOAD_DEF_RE = /(?:global|root|window|globalThis)\s*\.\s*(IWB[A-Za-z0-9_]+|Canvas[A-Za-z0-9_]+)\s*(?<!=)=(?!=)/g
|
||
|
||
// 加载序 = 显式登记项 + 自动发现的插件(与 scripts/build.js 的追加规则一致:文件名序,排最后)
|
||
const loadOrder = listed.concat(
|
||
srcFiles.filter((n) => isPluginModule(n) && !listed.includes(n)).sort()
|
||
)
|
||
|
||
// 导出名 → 定义它的模块(取首次定义)
|
||
const loadOwner = new Map()
|
||
for (const file of loadOrder) {
|
||
for (const m of read('src/' + file).matchAll(new RegExp(LOAD_DEF_RE.source, 'g'))) {
|
||
if (!loadOwner.has(m[1])) loadOwner.set(m[1], file)
|
||
}
|
||
}
|
||
|
||
const loadEdges = []
|
||
const unknownLoadTargets = new Set()
|
||
for (const file of loadOrder) {
|
||
const lines = read('src/' + file).split('\n')
|
||
for (let ln = 0; ln < lines.length; ln++) {
|
||
const line = lines[ln]
|
||
if (line.length - line.trimStart().length > 2) continue // 函数体 / 对象内部 ⇒ 延迟引用
|
||
const re = new RegExp(LOAD_REF_RE.source, 'g')
|
||
let m
|
||
while ((m = re.exec(line))) {
|
||
// 跳过赋值左侧:`global.X = …` 是定义,不是读取
|
||
if (/^\s*=(?!=)/.test(line.slice(m.index + m[0].length))) continue
|
||
const owner = loadOwner.get(m[1])
|
||
if (!owner) { unknownLoadTargets.add(m[1]); continue }
|
||
if (owner === file) continue
|
||
loadEdges.push({ from: file, to: owner, name: m[1], line: ln + 1 })
|
||
}
|
||
}
|
||
}
|
||
|
||
assert('解析到加载期跨模块依赖(非空,防正则失效后空过)', loadEdges.length > 0, '实际 ' + loadEdges.length)
|
||
// 哨兵:这两条是真实存在的加载期依赖,改坏了必然消失
|
||
assert('含已知加载期依赖 storage-local → IWBStorageResult',
|
||
loadEdges.some((e) => e.from === 'storage-local.js' && e.name === 'IWBStorageResult'),
|
||
loadEdges.map((e) => e.from + '→' + e.name).join(', '))
|
||
assert('含已知加载期依赖 runtime-recovery → IWBRuntimeRecord',
|
||
loadEdges.some((e) => e.from === 'runtime-recovery.js' && e.name === 'IWBRuntimeRecord'),
|
||
loadEdges.map((e) => e.from + '→' + e.name).join(', '))
|
||
assert('加载期读取的名字都是已登记的 src 导出(无幽灵名)', unknownLoadTargets.size === 0, [...unknownLoadTargets].join(', '))
|
||
// 主体断言:每条加载期依赖的定义者都必须先加载
|
||
const badOrder = loadEdges.filter((e) => loadOrder.indexOf(e.to) >= loadOrder.indexOf(e.from))
|
||
assert('加载期依赖的定义者必须先于使用者登记(新模块插错位置即红)',
|
||
badOrder.length === 0,
|
||
badOrder.map((e) => e.from + ':' + e.line + ' → ' + e.to + ' (' + e.name + ')').join('; '))
|
||
|
||
// ── 9. 参考图准入:所有「把图变成参考图节点」的入口必须经过同一个收口 ──────────
|
||
// 用户裁定(2026-11-11):参考图只收位图。上游开始返回 .svg,而四条入口(输出节点插入 /
|
||
// 图库插入 / 图库拖入 / 链接拖入)此前各写各的 —— 漏一处就把矢量一路传给模型接口。
|
||
// 这组不测行为(行为归浏览器实测),只钉住「结构上必须经过 refSrcOf」:
|
||
// 任何一处被回退成直接拿 url,或新增入口绕过收口,立即红。
|
||
const logicSrc = read('src/runtime-workbench-logic.js')
|
||
assert('参考图收口 refSrcOf 已定义,且只改 SVG(非 SVG 原样返回,不重编码/不多发请求)',
|
||
/async function refSrcOf\(src0, notice\)/.test(logicSrc) && /if \(!F\.isSvg\(src0\)\) return src0/.test(logicSrc), '')
|
||
const refEntrySites = [
|
||
['输出节点「插入为参考图」', /const png = await refSrcOf\(url,/],
|
||
['图库拖入(按 origName 取原图)', /const raw = await refSrcOf\(src \|\| localRefPayload/],
|
||
['图库拖入(无 origName 的兜底)', /refSrcOf\(localRefPayload \|\| '',/],
|
||
['拖入图片链接', /refSrcOf\(url, \(msg\) => setNotice/],
|
||
]
|
||
for (const [label, re] of refEntrySites) assert('参考图入口经过收口:' + label, re.test(logicSrc), '')
|
||
// v3.26.61:图库插入的取源从 wResolveOutputSrc 换成 src/gallery-sync.js 的 prepareInsert
|
||
//(本地副本优先 + 按需抓远程),所以钉点不能只认旧实现那一行;改为在 insertOutputsToCanvas
|
||
// 区间内要求两件事同时成立:① 仍经过 refSrcOf 收口(SVG → PNG,参考图格式约定)
|
||
// ② 取源走 gallery-sync(不再直连 http 链接)。区间用两个锚点切,不在这两个锚点之外的代码里误判。
|
||
const _insertAt = appJs.indexOf('const insertOutputsToCanvas = async')
|
||
const _insertEnd = appJs.indexOf('const loadDemo', _insertAt)
|
||
const _insertSrc = (_insertAt >= 0 && _insertEnd > _insertAt) ? appJs.slice(_insertAt, _insertEnd) : ''
|
||
assert('图库「插入」按钮经过收口(app.js 侧只做装配)',
|
||
/wbCmd\(\)\.refSrcOf\(/.test(_insertSrc) && /wGallerySync\.prepareInsert\(/.test(_insertSrc), '')
|
||
assert('refSrcOf 已从 createWorkbenchCommands 导出(否则 app.js 侧调用是 undefined)',
|
||
/addRefFromUrl, refSrcOf,/.test(logicSrc), '')
|
||
|
||
console.log('')
|
||
if (failed) {
|
||
console.error('architecture-boundary-guard: ' + failed + ' 项失败')
|
||
process.exit(1)
|
||
}
|
||
console.log('architecture-boundary-guard: 全部通过')
|