二七王:前端单测框架与 shared 同步守卫

测试侧用 vm.runInThisContext 加载浏览器全局脚本,正式代码不为测试加任何导出。
test_shared_sync 断言前后端 shared 逐字节一致,已做反向验证(改一个空格即红)。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-26 19:51:31 +08:00
co-authored by Claude Opus 5
parent 147a28805e
commit 656066b1a5
4 changed files with 82 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
// 把前端 ES5 全局脚本加载进 Node global。
// 正式代码是浏览器脚本(var X = {...},无 module.exports),
// 这里用 vm.runInThisContext 还原浏览器语义——顶层 var 挂到 globalThis。
// 【关键】不得为了测试而给正式代码加 module.exports(client 05 §10)。
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const ROOT = path.resolve(__dirname, '..', '..'); // 仓库根
function load(rel) {
const abs = path.join(ROOT, rel);
const code = fs.readFileSync(abs, 'utf8');
vm.runInThisContext(code, { filename: rel });
}
function loadAll(rels) {
rels.forEach(load);
}
// 断言抛错的小工具:期望 fn 抛异常
function throws(fn) {
try { fn(); } catch (e) { return true; }
return false;
}
module.exports = { ROOT, load, loadAll, throws };