Files
wordpress-theme-aksrt/tests/widget-system/rehydrate-runtime-contract.test.cjs
T
kate522 1f4a39d8f8 feat(runtime): 全站前端脚本统一接入 AksrtFrontend 全局 DOM 生命周期
- rehydrate.js 新增 boot()/aksrt:ready 事件,支持 boot 后晚注册立即以 init 触发
- aksrt-runtime.js 初始加载改走 AksrtFrontend.boot,tooltip/倒计时注册为组件
- 约 30 个前端脚本(首页、评论、支付、路由、页面叶子、widgets)
  从直接 DOMContentLoaded 迁移为 AksrtFrontend.register,并加幂等标记
- section-navs 依赖改为 aksrt-runtime,保证注册时序
- 清理死代码:删除孤儿 loader.js/main.js 及 runtime/page-navs 遗留副本
- loader.js 移除已迁移模块的 tbquire 路径映射,main.js 相应瘦身
- build-public-assets.mjs 增加 runtime/main.min.js 自动压缩步骤
- 新增 4 个契约测试(homepage-dom/rehydrate-runtime/global-dom-init/
  public-bundle-parity),3 个既有 harness 适配注册表初始化方式
2026-09-02 21:48:56 +08:00

98 lines
3.1 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const test = require('node:test');
const root = path.resolve(__dirname, '..', '..');
const source = fs.readFileSync(path.join(root, 'assets/js/rehydrate.js'), 'utf8');
function createRuntime() {
const events = [];
const document = {
dispatchEvent(event) {
events.push(event);
return true;
},
};
function CustomEvent(type, options) {
this.type = type;
this.bubbles = Boolean(options && options.bubbles);
this.detail = options && options.detail;
}
const errors = [];
const context = {
window: {},
document,
CustomEvent,
console: {
error(...args) {
errors.push(args);
},
},
};
context.window.window = context.window;
context.window.document = document;
vm.runInNewContext(source, context, { filename: 'rehydrate.js' });
return { api: context.window.AksrtFrontend, document, events, errors };
}
test('global DOM boot runs registered components once with init reason', () => {
const runtime = createRuntime();
const calls = [];
runtime.api.register('first', (rootNode, reason) => calls.push([rootNode, reason]));
runtime.api.boot(runtime.document);
runtime.api.boot(runtime.document);
assert.equal(runtime.api.isBooted(), true);
assert.deepEqual(calls, [[runtime.document, 'init']]);
assert.deepEqual(runtime.events.map((event) => event.type), ['aksrt:rehydrated', 'aksrt:ready']);
assert.equal(runtime.events[0].detail.reason, 'init');
assert.equal(runtime.events[1].detail.reason, 'init');
});
test('late registrations initialize immediately after global boot', () => {
const runtime = createRuntime();
const calls = [];
runtime.api.boot(runtime.document);
runtime.api.register('late', (rootNode, reason) => calls.push([rootNode, reason]));
assert.deepEqual(calls, [[runtime.document, 'init']]);
assert.deepEqual(Array.from(runtime.api.getRegistered()), ['late']);
});
test('same-name registration replaces the owner without duplicating it', () => {
const runtime = createRuntime();
const calls = [];
runtime.api.register('owner', () => calls.push('old'));
runtime.api.register('owner', () => calls.push('new'));
runtime.api.boot(runtime.document);
assert.deepEqual(Array.from(runtime.api.getRegistered()), ['owner']);
assert.deepEqual(calls, ['new']);
});
test('rehydrate preserves root and reason while isolating component failures', () => {
const runtime = createRuntime();
const fragment = {
dispatchEvent(event) {
runtime.events.push(event);
return true;
},
};
const calls = [];
runtime.api.register('broken', () => { throw new Error('broken'); });
runtime.api.register('healthy', (rootNode, reason) => calls.push([rootNode, reason]));
runtime.api.rehydrate(fragment, 'modal');
assert.deepEqual(calls, [[fragment, 'modal']]);
assert.equal(runtime.errors.length, 1);
assert.equal(runtime.events.at(-1).type, 'aksrt:rehydrated');
assert.equal(runtime.events.at(-1).detail.reason, 'modal');
assert.equal(runtime.events.at(-1).bubbles, true);
});