Files
wordpress-theme-aksrt/tests/widget-system/homepage-dom-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

86 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 read = (file) => fs.readFileSync(path.join(root, file), 'utf8');
test('homepage uses the global DOM lifecycle without a second ready path', () => {
const runtime = read('assets/js/aksrt-runtime.js');
const homepage = read('assets/js/homepage-fixed.js');
assert.match(runtime, /aksrt\.autoFun\s*=\s*function\s*\(root, reason\)/);
assert.match(runtime, /function initRuntime\(\)\s*\{\s*aksrt\.autoFun\(document, 'init'\);/s);
assert.doesNotMatch(runtime, /function initRuntime\(\)\s*\{\s*aksrt\.autoFun\(document\);/s);
assert.match(homepage, /window\.AksrtFrontend\.register\('aksrt-homepage', init\)/);
assert.doesNotMatch(homepage, /DOMContentLoaded/);
assert.doesNotMatch(homepage, /document\.querySelectorAll\('\[data-sort-dropdown-trigger\]'\)/);
assert.doesNotMatch(homepage, /document\.querySelectorAll\('\.aksrt-homepage__stream'\)/);
assert.doesNotMatch(homepage, /document\.querySelectorAll\('\.aksrt-homepage__tabs\.home-tab-nav-box'\)/);
assert.match(homepage, /dataset\.aksrtHomeTabsBound === '1'/);
assert.match(homepage, /dataset\.aksrtSortDropdownBound === '1'/);
assert.match(homepage, /dataset\.aksrtSortbarBound === '1'/);
assert.match(homepage, /dataset\.aksrtStickyTabsBound === '1'/);
});
test('homepage initializer is registered once and remains idempotent across rehydrate calls', () => {
const source = read('assets/js/homepage-fixed.js');
const registrations = [];
let sortbarClickBindings = 0;
const sortbar = {
dataset: {},
getAttribute() {
return null;
},
addEventListener(type) {
if (type === 'click') sortbarClickBindings++;
},
};
const stream = {
querySelector(selector) {
return selector === '.aksrt-homepage__sortbar' ? sortbar : null;
},
};
const scope = {
querySelectorAll(selector) {
return selector === '.aksrt-homepage__stream' ? [stream] : [];
},
};
const context = {
window: {
AksrtFrontend: {
register(name, init) {
registrations.push({ name, init });
},
},
},
document: scope,
console,
URLSearchParams,
URL,
setTimeout,
clearTimeout,
};
context.window.window = context.window;
context.window.document = scope;
vm.runInNewContext(source, context, { filename: 'homepage-fixed.js' });
assert.equal(registrations.length, 1);
assert.equal(registrations[0].name, 'aksrt-homepage');
assert.equal(typeof registrations[0].init, 'function');
registrations[0].init(scope, 'init');
registrations[0].init(scope, 'ajax');
assert.equal(sortbarClickBindings, 1);
assert.equal(sortbar.dataset.aksrtSortbarBound, '1');
});
test('homepage AJAX content continues to use the shared ajax rehydrate contract', () => {
const homepage = read('assets/js/homepage-fixed.js');
assert.match(homepage, /window\.AksrtFrontend\.rehydrate\(panel, 'ajax'\)/);
});