'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'\)/); });