383 lines
17 KiB
JavaScript
383 lines
17 KiB
JavaScript
'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');
|
|
|
|
class FakeElement {
|
|
constructor(classes = [], id = '') {
|
|
this.classes = new Set(classes);
|
|
this.id = id;
|
|
this.children = [];
|
|
this.parent = null;
|
|
this.innerHTML = '';
|
|
this.delegated = null;
|
|
}
|
|
|
|
append(child) {
|
|
child.parent = this;
|
|
this.children.push(child);
|
|
}
|
|
}
|
|
|
|
function descendants(element) {
|
|
return element.children.flatMap((child) => [child, ...descendants(child)]);
|
|
}
|
|
|
|
function createRuntime() {
|
|
const body = new FakeElement(['body']);
|
|
const document = { body };
|
|
const timers = [];
|
|
const window = {
|
|
setTimeout(callback, delay) {
|
|
timers.push({ callback, delay });
|
|
return timers.length;
|
|
},
|
|
};
|
|
|
|
class FakeQuery {
|
|
constructor(elements) {
|
|
this.elements = elements;
|
|
this.length = elements.length;
|
|
}
|
|
|
|
append(value) {
|
|
let elements = value instanceof FakeQuery ? value.elements : [];
|
|
if (typeof value === 'string' && value === '<div class="notyn"></div>') {
|
|
elements = [new FakeElement(['notyn'])];
|
|
}
|
|
this.elements.forEach((parent) => elements.forEach((element) => parent.append(element)));
|
|
return this;
|
|
}
|
|
|
|
addClass(classes) {
|
|
this.elements.forEach((element) => classes.split(/\s+/).forEach((name) => element.classes.add(name)));
|
|
return this;
|
|
}
|
|
|
|
removeClass() {
|
|
this.elements.forEach((element) => element.classes.clear());
|
|
return this;
|
|
}
|
|
|
|
find(selector) {
|
|
return new FakeQuery(this.elements.flatMap(descendants).filter((element) => matches(element, selector)));
|
|
}
|
|
|
|
html(value) {
|
|
this.elements.forEach((element) => { element.innerHTML = value; });
|
|
return this;
|
|
}
|
|
|
|
remove() {
|
|
this.elements.forEach((element) => {
|
|
if (!element.parent) return;
|
|
element.parent.children = element.parent.children.filter((child) => child !== element);
|
|
element.parent = null;
|
|
});
|
|
return this;
|
|
}
|
|
|
|
on(event, selector, handler) {
|
|
this.elements.forEach((element) => { element.delegated = { event, selector, handler }; });
|
|
return this;
|
|
}
|
|
}
|
|
|
|
function matches(element, selector) {
|
|
if (selector.startsWith('.')) return element.classes.has(selector.slice(1));
|
|
if (selector.startsWith('#')) return element.id === selector.slice(1);
|
|
return false;
|
|
}
|
|
|
|
function parseNotification(html) {
|
|
const id = (html.match(/ id="([^"]+)"/) || [])[1] || '';
|
|
const content = html.match(/<div class="notyf ([^"]+)">([\s\S]*)<\/div><\/div>$/);
|
|
assert.ok(content, 'notification markup should follow the Zibll contract');
|
|
const wrapper = new FakeElement(['noty1'], id);
|
|
const message = new FakeElement(['notyf', content[1]]);
|
|
message.innerHTML = content[2];
|
|
wrapper.append(message);
|
|
return wrapper;
|
|
}
|
|
|
|
function jQuery(value) {
|
|
if (value === document.body || value instanceof FakeElement) return new FakeQuery([value]);
|
|
if (typeof value === 'string' && value.startsWith('<div class="noty1"')) {
|
|
return new FakeQuery([parseNotification(value)]);
|
|
}
|
|
if (typeof value === 'string') {
|
|
return new FakeQuery([body, ...descendants(body)].filter((element) => matches(element, value)));
|
|
}
|
|
return new FakeQuery([]);
|
|
}
|
|
|
|
vm.runInNewContext(read('assets/js/notyf.js'), { jQuery, window, document });
|
|
return { body, timers, window };
|
|
}
|
|
|
|
test('notyf follows Zibll defaults, type normalization, and timing', () => {
|
|
const runtime = createRuntime();
|
|
|
|
runtime.window.notyf('完成');
|
|
const container = runtime.body.children[0];
|
|
const first = container.children[0];
|
|
assert.ok(container.classes.has('notyn'));
|
|
assert.ok(first.classes.has('noty1'));
|
|
assert.ok(first.children[0].classes.has('success'));
|
|
assert.equal(first.children[0].innerHTML, '完成');
|
|
assert.equal(runtime.timers[0].delay, 5000);
|
|
|
|
runtime.window.notyf('失败', 'error', 2);
|
|
const second = container.children[1];
|
|
assert.ok(second.children[0].classes.has('danger'));
|
|
assert.equal(runtime.timers[1].delay, 2000);
|
|
});
|
|
|
|
test('fixed-id notifications stay open until an in-place update', () => {
|
|
const runtime = createRuntime();
|
|
|
|
runtime.window.notyf('处理中', 'load', '', 'request-state');
|
|
const container = runtime.body.children[0];
|
|
const notification = container.children[0];
|
|
assert.equal(runtime.timers.length, 0);
|
|
assert.equal(notification.id, 'request-state');
|
|
assert.ok(notification.children[0].classes.has('load'));
|
|
|
|
runtime.window.notyf('<strong>完成</strong>', 'success', 3, 'request-state');
|
|
assert.equal(container.children.length, 1);
|
|
assert.equal(notification.children[0].innerHTML, '<strong>完成</strong>');
|
|
assert.ok(notification.children[0].classes.has('success'));
|
|
assert.equal(runtime.timers[0].delay, 3000);
|
|
});
|
|
|
|
test('clicking a notification uses the Zibll exit class and removal delay', () => {
|
|
const runtime = createRuntime();
|
|
runtime.window.notyf('可关闭');
|
|
const notification = runtime.body.children[0].children[0];
|
|
const delegated = runtime.body.delegated;
|
|
|
|
assert.deepEqual({ event: delegated.event, selector: delegated.selector }, { event: 'click', selector: '.noty1' });
|
|
delegated.handler.call(notification);
|
|
assert.ok(notification.classes.has('notyn-out'));
|
|
assert.equal(runtime.timers.at(-1).delay, 1000);
|
|
runtime.timers.at(-1).callback();
|
|
assert.equal(runtime.body.children[0].children.length, 0);
|
|
});
|
|
|
|
test('notyf CSS and WordPress enqueue match the reference contract', () => {
|
|
const css = read('assets/css/frontend/notyf.css');
|
|
const scripts = read('src/Core/ScriptsPart1c.php');
|
|
|
|
assert.match(css, /\.notyn\s*\{[\s\S]*?bottom:\s*6em;[\s\S]*?max-width:\s*320px;[\s\S]*?z-index:\s*10000000;/);
|
|
assert.match(css, /\.notyf\s*\{[\s\S]*?position:\s*relative;[\s\S]*?font-size:\s*14px;[\s\S]*?line-height:\s*1\.42857143;[\s\S]*?min-width:\s*200px;[\s\S]*?padding:\s*1em 3em 1em 3\.4em;/);
|
|
assert.doesNotMatch(css, /\.notyf\s*\{[^}]*white-space:\s*nowrap;/);
|
|
assert.match(css, /\.notyf::before\s*\{[\s\S]*?font-family:\s*"Font Awesome 5 Free";[\s\S]*?font-weight:\s*900;[\s\S]*?line-height:\s*1;/);
|
|
assert.match(css, /rgba\(15, 147, 249, 0\.7\)[\s\S]*?rgba\(61, 189, 249, 0\.8\)/);
|
|
assert.match(css, /rgba\(58, 162, 54, 0\.8\)[\s\S]*?rgba\(89, 247, 131, 0\.8\)/);
|
|
assert.match(css, /rgba\(253, 170, 71, 0\.7\)[\s\S]*?rgba\(247, 154, 13, 0\.8\)/);
|
|
assert.match(css, /rgba\(253, 69, 28, 0\.7\)[\s\S]*?rgba\(251, 110, 75, 0\.8\)/);
|
|
assert.match(css, /content:\s*'\\f058'/);
|
|
assert.match(css, /content:\s*'\\f0a1'/);
|
|
assert.match(css, /content:\s*'\\f05a'/);
|
|
assert.match(css, /content:\s*'\\f057'/);
|
|
assert.match(css, /\.notyf\.load::before[\s\S]*?huan-rotate 1s/);
|
|
|
|
assert.match(scripts, /'aksrt-notyf',[\s\S]*?'\/assets\/js\/notyf\.js',[\s\S]*?\['jquery'\]/);
|
|
assert.match(scripts, /'aksrt-runtime',[\s\S]*?\['aksrt-rehydrate', 'aksrt-notyf'\]/);
|
|
assert.match(scripts, /'\/assets\/css\/frontend\/notyf\.css',[\s\S]*?'aksrt-font-awesome-5-v4-shims'/);
|
|
});
|
|
|
|
test('balance center actions use direct fixed-id notyf flows', () => {
|
|
const credits = read('assets/js/credits.js');
|
|
const scripts = read('src/Core/ScriptsPart1b.php');
|
|
const css = read('assets/css/routes/user-center/credits.css');
|
|
const renderers = [
|
|
'inc/helpers/user-center-renderers/credits-balance.php',
|
|
'inc/helpers/user-center-renderers/credits-wallet.php',
|
|
'inc/helpers/user-center-renderers/transfer-modal.php',
|
|
'inc/helpers/user-center-renderers/withdraw-modal.php',
|
|
].map(read).join('\n');
|
|
|
|
function section(start, end) {
|
|
const from = credits.indexOf('function ' + start + '(');
|
|
const to = credits.indexOf('function ' + end + '(', from + 1);
|
|
assert.notEqual(from, -1, start + ' should exist');
|
|
assert.notEqual(to, -1, end + ' should exist');
|
|
return credits.slice(from, to);
|
|
}
|
|
|
|
const flows = [
|
|
[section('initSignin', 'initSigninGoto'), 'aksrt-balance-signin-toast'],
|
|
[section('initBalanceCharge', 'initTransfer'), 'aksrt-balance-charge-toast'],
|
|
[section('initTransfer', 'initPointsExchange'), 'aksrt-balance-transfer-toast'],
|
|
[section('initPointsExchange', 'initWithdraw'), 'aksrt-balance-points-toast'],
|
|
[section('initWithdraw', 'initAssetTabs'), 'aksrt-balance-withdraw-toast'],
|
|
];
|
|
|
|
flows.forEach(([source, id]) => {
|
|
assert.match(source, new RegExp("notyf\\([^;]+?'load'[^;]+?'" + id + "'\\);"));
|
|
assert.match(source, new RegExp("notyf\\([^;]+?'danger'[^;]+?'" + id + "'\\);"));
|
|
assert.match(source, new RegExp("notyf\\([^;]+?'success'[^;]+?'" + id + "'\\);"));
|
|
assert.doesNotMatch(source, /setTip|data-(?:signin|aksrt-(?:charge|transfer|points-exchange|withdraw))-tip/);
|
|
});
|
|
|
|
assert.match(credits, /notyf\('等待支付结果…', 'load', '', 'aksrt-balance-charge-payment-toast'\)/);
|
|
assert.match(credits, /onTimeout:[\s\S]*?notyf\([^;]+?'danger'[^;]+?'aksrt-balance-charge-payment-toast'\)/);
|
|
assert.match(credits, /register\('credits',[\s\S]*?initSignin\(\);[\s\S]*?initBalanceCharge\(\);[\s\S]*?initTransfer\(\);[\s\S]*?initPointsExchange\(\);[\s\S]*?initWithdraw\(\);/);
|
|
assert.match(credits, /aksrtSigninInitialized[\s\S]*?aksrtPointsExchangeInitialized[\s\S]*?aksrtWithdrawInitialized/);
|
|
assert.match(scripts, /'aksrt-credits',[\s\S]*?\/assets\/js\/credits\.js',[\s\S]*?\[[^\]]*'aksrt-notyf'[^\]]*\]/);
|
|
assert.doesNotMatch(renderers, /data-(?:signin|aksrt-(?:charge|transfer|points-exchange|withdraw))-tip/);
|
|
assert.doesNotMatch(css, /\.uc-(?:modal|balance-card)__tip/);
|
|
});
|
|
|
|
test('VIP purchase and points exchange use direct fixed-id notyf flows', () => {
|
|
const vip = read('assets/js/pay-vip.js');
|
|
const css = read('assets/css/frontend/vip-modal.css');
|
|
const scripts = read('src/Core/ScriptsPart1b.php');
|
|
|
|
assert.match(vip, /notyf\("正在兑换会员…", "load", "", "aksrt-vip-points-toast"\)/);
|
|
assert.match(vip, /notyf\([^;]+?"success"[^;]+?"aksrt-vip-points-toast"\)/);
|
|
assert.match(vip, /notyf\([^;]+?"danger"[^;]+?"aksrt-vip-points-toast"\)/);
|
|
|
|
assert.match(vip, /activePurchaseToastId = "aksrt-vip-purchase-toast";[\s\S]*?notyf\("正在创建会员订单…", "load"/);
|
|
assert.match(vip, /onPending:[\s\S]*?"info"[\s\S]*?activePurchaseToastId = "aksrt-vip-payment-toast";[\s\S]*?notyf\("等待支付结果…", "load"/);
|
|
assert.match(vip, /onSuccess:[\s\S]*?notyf\([^;]+?"success"/);
|
|
assert.match(vip, /onFailure:[\s\S]*?notyf\([^;]+?"danger"[^;]+?"aksrt-vip-payment-toast"/);
|
|
assert.match(vip, /onTimeout:[\s\S]*?notyf\([^;]+?"danger"[^;]+?"aksrt-vip-payment-toast"/);
|
|
assert.match(vip, /已取消会员购买", "warning", 3, activePurchaseToastId/);
|
|
assert.match(vip, /会员套餐加载失败,请重试[\s\S]*?"danger"/);
|
|
assert.match(vip, /会员兑换信息加载失败,请重试[\s\S]*?"danger"/);
|
|
|
|
assert.doesNotMatch(vip, /setTip|aksrt-vip-modal-tip/);
|
|
assert.doesNotMatch(css, /\.aksrt-vip-modal-tip/);
|
|
assert.match(scripts, /'aksrt-pay-vip',[\s\S]*?\/assets\/js\/pay-vip\.js',[\s\S]*?\[[^\]]*'aksrt-notyf'[^\]]*\]/);
|
|
});
|
|
|
|
test('profile save uses AJAX rehydrate and a fixed-id notyf flow without query feedback', () => {
|
|
const profile = read('assets/js/routes/user-center.js');
|
|
const page = read('templates/pages/page-profile.php');
|
|
const form = read('inc/helpers/user-center-renderers/profile-form.php');
|
|
const layout = read('assets/css/routes/profile/layout.css');
|
|
const lists = read('assets/css/routes/profile/lists.css');
|
|
const scripts = read('src/Core/ScriptsPart1b.php');
|
|
|
|
assert.match(profile, /function initProfileForm\(root\)[\s\S]*?aksrtProfileInitialized[\s\S]*?addEventListener\('submit'/);
|
|
assert.match(profile, /register\('aksrt-profile-form',[\s\S]*?initProfileForm\(root\)/);
|
|
assert.match(profile, /notyf\([^;]+?'load'[^;]+?'aksrt-profile-save-toast'\);/);
|
|
assert.match(profile, /notyf\([^;]+?'success'[^;]+?'aksrt-profile-save-toast'\);/);
|
|
assert.match(profile, /notyf\([^;]+?'danger'[^;]+?'aksrt-profile-save-toast'\);/);
|
|
assert.match(profile, /fetch\(ajaxUrl, \{ method: 'POST', credentials: 'same-origin', body: data \}\)/);
|
|
assert.match(scripts, /\$user_center_deps[\s\S]*?\[[^\]]*'aksrt-notyf'[^\]]*\][\s\S]*?\/assets\/js\/routes\/user-center\.js/);
|
|
|
|
assert.doesNotMatch(page, /REQUEST_METHOD|profile_error|['"]saved['"]|add_query_arg/);
|
|
assert.doesNotMatch([profile, form, layout, lists].join('\n'), /profile-save-msg|profile-form__submit-msg/);
|
|
});
|
|
|
|
test('settings actions use rehydrated fixed-id notyf flows', () => {
|
|
const userCenter = read('assets/js/routes/user-center.js');
|
|
const signRegister = read('assets/js/sign-register.js');
|
|
const settings = read('inc/helpers/user-center-renderers/profile-settings.php');
|
|
const context = read('inc/helpers/aksrt-user-center.php');
|
|
const page = read('templates/pages/page-profile.php');
|
|
|
|
[
|
|
'aksrt-settings-password-code-toast',
|
|
'aksrt-settings-email-code-toast',
|
|
'aksrt-settings-password-toast',
|
|
'aksrt-settings-email-toast',
|
|
'aksrt-settings-oauth-unbind-toast',
|
|
'aksrt-settings-phone-bind-toast',
|
|
].forEach((id) => assert.match(userCenter, new RegExp("['\"]" + id + "['\"]")));
|
|
|
|
assert.match(userCenter, /notyf\([^;]+?'load'[^;]+?toastId\);/);
|
|
assert.match(userCenter, /notyf\([^;]+?'success'[^;]+?toastId\);/);
|
|
assert.match(userCenter, /notyf\([^;]+?'danger'[^;]+?toastId\);/);
|
|
assert.match(userCenter, /register\('aksrt-settings',[\s\S]*?initSettings\(root\);[\s\S]*?initBindPhone\(root\);/);
|
|
assert.match(userCenter, /aksrtSettingsInitialized/);
|
|
assert.match(userCenter, /oauth_bound[\s\S]*?notyf\([^;]+?'success'\)[\s\S]*?history\.replaceState/);
|
|
assert.doesNotMatch(userCenter, /oauth_unbound/);
|
|
|
|
assert.match(signRegister, /scene === 'bind_phone'[\s\S]*?'aksrt-settings-phone-code-toast'/);
|
|
assert.match(signRegister, /notyf\('正在发送验证码…', 'load', '', toastId\)/);
|
|
assert.match(signRegister, /notyf\([^;]+?'success', 5, toastId\)/);
|
|
assert.match(signRegister, /notyf\([^;]+?'danger', 5, toastId\)/);
|
|
assert.match(signRegister, /register\('aksrt-sms-buttons',[\s\S]*?initSmsButtons\(root\)/);
|
|
assert.match(signRegister, /aksrtSmsInitialized/);
|
|
|
|
assert.match(settings, /require \$dir \. '\/profile-account-modals\.php';/);
|
|
assert.doesNotMatch(context, /account_modals_html|'account-modals'/);
|
|
assert.doesNotMatch(page, /user-center\/account-modals/);
|
|
assert.equal(fs.existsSync(path.join(root, 'templates/parts/user-center/account-modals.php')), false);
|
|
});
|
|
|
|
test('front submit actions use direct fixed-id notyf flows', () => {
|
|
const submit = read('assets/js/front-submit.js');
|
|
const page = read('templates/pages/page-submit.php');
|
|
|
|
[
|
|
'aksrt-front-submit-draft-toast',
|
|
'aksrt-front-submit-publish-toast',
|
|
'aksrt-front-submit-delete-draft-toast',
|
|
'aksrt-front-submit-featured-upload-toast',
|
|
'aksrt-front-submit-slide-upload-toast',
|
|
'aksrt-front-submit-video-cover-toast',
|
|
'aksrt-front-submit-resource-upload-toast',
|
|
'aksrt-front-submit-editor-image-toast',
|
|
'aksrt-front-submit-ai-tags-toast',
|
|
'aksrt-front-submit-ai-summary-toast',
|
|
].forEach((id) => assert.match(submit, new RegExp("['\"]" + id + "['\"]")));
|
|
|
|
assert.match(submit, /submit: function[\s\S]*?notyf\([^;]+?'load'[^;]+?toastId[\s\S]*?notyf\([^;]+?'success'[^;]+?toastId[\s\S]*?notyf\([^;]+?'danger'[^;]+?toastId/);
|
|
assert.match(submit, /bindDraftDelete: function[\s\S]*?'aksrt-front-submit-delete-draft-toast'[\s\S]*?'load'[\s\S]*?'success'[\s\S]*?'danger'/);
|
|
|
|
[
|
|
'imageToastId',
|
|
'slideToastId',
|
|
'videoToastId',
|
|
].forEach((variable) => {
|
|
assert.match(submit, new RegExp("notyf\\([^;]+?'load'[^;]+?" + variable + "\\);"));
|
|
assert.match(submit, new RegExp("notyf\\([^;]+?'success'[^;]+?" + variable + "\\);"));
|
|
assert.match(submit, new RegExp("notyf\\([^;]+?'danger'[^;]+?" + variable + "\\);"));
|
|
});
|
|
|
|
assert.match(submit, /aksrt-front-submit-resource-upload-toast[\s\S]*?'load'[\s\S]*?'success'[\s\S]*?'danger'/);
|
|
assert.match(submit, /aksrt-front-submit-editor-image-toast[\s\S]*?'load'[\s\S]*?'success'[\s\S]*?'danger'/);
|
|
assert.match(submit, /aksrt-front-submit-ai-tags-toast[\s\S]*?aksrt-front-submit-ai-summary-toast[\s\S]*?'load'[\s\S]*?'success'[\s\S]*?'danger'/);
|
|
assert.match(submit, /needContent[^;]+?'warning'/);
|
|
assert.doesNotMatch(submit, /\b(?:window\.)?alert\s*\(/);
|
|
|
|
assert.match(page, /'aksrt-front-submit',[\s\S]*?'\/assets\/js\/front-submit\.js',[\s\S]*?\[[^\]]*'aksrt-notyf'[^\]]*\]/);
|
|
});
|
|
|
|
test('legacy frontend toast APIs and selectors are fully removed', () => {
|
|
const files = [
|
|
'assets/js',
|
|
'assets/css/frontend',
|
|
'assets/css/routes',
|
|
'assets/css/build/frontend-base.min.css.disabled',
|
|
'page.php',
|
|
'templates/pages/page-profile.php',
|
|
];
|
|
const legacy = /AksrtToast|aksrtToast|aksrtShowToast|aksrt\.(?:toast|notyf)|showToast|toast-container|toast-item|aksrt-toast/;
|
|
|
|
function sourceFiles(target) {
|
|
const absolute = path.join(root, target);
|
|
if (!fs.statSync(absolute).isDirectory()) return [absolute];
|
|
return fs.readdirSync(absolute, { withFileTypes: true }).flatMap((entry) => {
|
|
if (entry.name === 'admin' || entry.name.endsWith('.min.js')) return [];
|
|
const child = path.join(absolute, entry.name);
|
|
return entry.isDirectory() ? sourceFiles(path.relative(root, child)) : [child];
|
|
});
|
|
}
|
|
|
|
const scanned = files.flatMap(sourceFiles);
|
|
const offenders = scanned.filter((file) => legacy.test(fs.readFileSync(file, 'utf8')));
|
|
assert.deepEqual(offenders.map((file) => path.relative(root, file)), []);
|
|
});
|