'use strict'; const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const test = require('node:test'); const vm = require('node:vm'); const root = path.resolve(__dirname, '..', '..'); const read = (file) => fs.readFileSync(path.join(root, file), 'utf8'); function sourceFiles(directory) { const files = []; for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { if (['.git', 'build', 'node_modules', 'runtime', 'third-party', 'vendor'].includes(entry.name)) continue; if (/\.min\.js$/.test(entry.name)) continue; const full = path.join(directory, entry.name); if (entry.isDirectory()) files.push(...sourceFiles(full)); else if (/\.(?:js|cjs|mjs|php)$/.test(entry.name)) files.push(full); } return files; } const routed = [ 'assets/js/auth.js', 'assets/js/author.js', 'assets/js/credits.js', 'assets/js/order-center.js', 'assets/js/reward.js', 'assets/js/routes/user-center.js', 'assets/js/signin.js', 'assets/js/section-navs.js', ]; test('loader maps and boots the single main module', () => { const loader = read('assets/js/runtime/loader.js'); assert.match(loader, /['"]main['"]\s*:\s*['"]main\.min['"]/); assert.match(loader, /tbquire\(\[['"]main['"]\]\)/); assert.equal((loader.match(/tbquire\(\[['"]main['"]\]\)/g) || []).length, 1); }); test('main owns the one global auto_fun boot', () => { const main = read('assets/js/runtime/main.js'); assert.match(main, /window\.auto_fun\s*=\s*debounce\(/); assert.match(main, /jQuery\(document\)\.trigger\(['"]auto_fun['"]\)/); }); test('theme and comments do not depend on the removed aksrt namespace APIs', () => { const theme = read('assets/js/core/theme.js'); const comments = read('assets/js/comment-ajax.js'); const runtime = read('assets/js/runtime/main.min.js'); assert.match(theme, /window\.aksrtData\.themeMode/); assert.doesNotMatch(theme, /(?:window\.)?aksrt\.toggleTheme/); assert.doesNotMatch(comments, /(?:window\.)?aksrt\.(?:__|confirm)/); assert.doesNotMatch(runtime, /(?:window\.)?aksrt\.(?:toggleTheme|__|confirm)/); }); test('theme initializes when the removed aksrt namespace is absent', () => { const classList = { toggle() {}, contains() { return false; } }; const document = { readyState: 'complete', cookie: '', body: { classList, setAttribute() {} }, documentElement: { classList, setAttribute() {} }, head: { appendChild() {} }, querySelector() { return null; }, querySelectorAll() { return []; }, getElementById() { return null; }, createElement() { return { setAttribute() {} }; }, dispatchEvent() {}, }; const window = { document, location: { protocol: 'https:' }, localStorage: { getItem() { return null; }, setItem() {} }, matchMedia() { return { matches: false }; }, setTimeout, innerWidth: 1280, innerHeight: 720, }; assert.doesNotThrow(() => vm.runInNewContext(read('assets/js/core/theme.js'), { window, document, localStorage: window.localStorage, CustomEvent: class {}, Date, Math, })); assert.equal(window.aksrt, undefined); }); test('routed modules use one namespaced auto_fun owner and no registry', () => { for (const file of routed) { const source = read(file); assert.match(source, /\.off\(['"]auto_fun\.[^'"]+['"]\)\.on\(['"]auto_fun\.[^'"]+['"]/, file); assert.doesNotMatch(source, /AksrtFrontend|aksrt\.autoFun|aksrt:(?:ready|rehydrated)/, file); } }); test('HTML fragment injection announces post_ajax before auto_fun', () => { for (const file of ['assets/js/core/pagination.js', 'assets/js/author.js', 'assets/js/credits.js', 'assets/js/routes/user-center.js']) { const source = read(file); assert.match(source, /post_ajax\.ed[\s\S]*?window\.auto_fun\(\)/, file); } }); test('modal fragment lifecycle reruns auto_fun after loaded.bs.modal', () => { const source = read('assets/js/runtime/main.js'); assert.match(source, /loaded\.bs\.modal[\s\S]*?auto_fun/); }); test('JSON success lifecycle remains zib_ajax.success', () => { const source = read('assets/js/runtime/main.js'); assert.match(source, /zib_ajax\.success/); }); test('Miniimgbox owns data-imgbox and lazySizes has one integration owner', () => { const imgbox = read('assets/js/runtime/imgbox.js'); const lazy = read('assets/js/runtime/main.js'); assert.match(imgbox, /Miniimgbox/); assert.match(imgbox, /data-imgbox/); assert.match(lazy, /lazybeforeunveil/); assert.match(lazy, /MutationObserver/); }); test('non-generated first-party sources contain no deprecated lifecycle API', () => { const files = sourceFiles(root).filter((file) => { const relative = path.relative(root, file).replaceAll('\\', '/'); return relative !== 'tests/widget-system/frontend-lifecycle-contract.test.cjs'; }); for (const file of files) { const source = fs.readFileSync(file, 'utf8'); assert.doesNotMatch(source, /AksrtFrontend|aksrt\.autoFun|aksrt:(?:ready|rehydrated)/, path.relative(root, file)); } });