const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const test = require('node:test'); const root = path.resolve(__dirname, '..', '..'); const read = (file) => fs.readFileSync(path.join(root, file), 'utf8'); test('badges use server-rendered admin-ajax modal actions only', () => { const badges = read('src/User/Badges.php'); const identity = read('inc/helpers/jio-identity.php'); const scripts = read('src/Core/ScriptsPart1b.php'); for (const action of [ 'jio_user_medal_info_modal', 'jio_single_medal_info_modal', 'jio_user_medal_manually_add_modal', 'jio_user_medal_wear', 'jio_user_medal_manually_add', 'jio_user_medal_manually_remove', ]) { assert.match(badges, new RegExp(`wp_ajax_${action}`)); } assert.match(badges, /wp_ajax_nopriv_jio_user_medal_info_modal/); assert.match(badges, /wp_ajax_nopriv_jio_single_medal_info_modal/); assert.match(identity, /data-jio-modal-cache="false"/); assert.match(identity, /data-jio-modal-loading="spinner"/); assert.match(identity, /data-jio-modal-swipe-close="true"/); assert.match(identity, /admin-ajax\.php/); assert.match(identity, /jio-badge-modal--overview/); assert.match(identity, /jio-badge-modal--grant/); assert.match(badges, /jio-badge-modal--single/); assert.match(badges, /data-jio-modal-loading="spinner"/); assert.match(badges, /data-jio-modal-swipe-close="true"/); assert.doesNotMatch(identity, /data-jio-modal-class="full-sm"/); assert.doesNotMatch(badges, /data-jio-modal-class="modal-mini"/); assert.match(scripts, /user-badge-actions\.js/); assert.doesNotMatch(scripts, /user-badges\.js/); assert.doesNotMatch(badges, /register_rest_route|rest_wear|rest_list|rest_grant|rest_revoke/); assert.doesNotMatch(identity, /data-jio-badges-open|data-jio-badges-can-wear/); }); test('badge mutations enforce the public nonce, login, ownership, and manual grant rules', () => { const badges = read('src/User/Badges.php'); assert.match(badges, /ajax_nonce\(\): string \{ return 'jio_public_nonce'; \}/); assert.match(badges, /wp_verify_nonce\(\$nonce, self::ajax_nonce\(\)\)/); assert.match(badges, /if \(!is_user_logged_in\(\)\) self::ajax_error/); assert.match(badges, /if \(\$operation !== 'wear' && !self::can_manually_set\(\)\)/); assert.match(badges, /\$operation === 'grant' && \(\$item\['get_type'\] \?\? ''\) !== 'manually_add'/); assert.match(badges, /\$operation === 'revoke' && empty\(\$item\['owned'\]\)/); assert.match(badges, /if \(!in_array\(\$slug, \$owned, true\)\)/); assert.match(badges, /'show_unearned'\s*=>\s*\$show_unearned/); }); test('badge actions close their HTML dialog without reloading the page', () => { const actions = read('assets/js/user-badge-actions.js'); assert.match(actions, /jio_user_medal_wear/); assert.match(actions, /jio_user_medal_manually_add/); assert.match(actions, /jio_user_medal_manually_remove/); assert.match(actions, /JioModalRuntime\.close\('badge-action'\)/); assert.match(actions, /getActive\(\) === modal/); assert.doesNotMatch(actions, /window\.location\.reload|setTimeout/); assert.doesNotMatch(actions, /JioRequest|fetchJson|restUrl/); }); test('the retired badge rollout and JSON modal CSS are absent', () => { const retired = [ 'src/Settings/FrontendModalRuntime.php', 'src/Admin/Schema/SchemaToolsFrontendModalRollout.php', 'assets/admin/src/schema/sections/tools/frontend-modal-rollout.js', ]; for (const file of retired) { assert.equal(fs.existsSync(path.join(root, file)), false, `${file} should be deleted`); } const sources = [ 'src/Core/ScriptsPart1c.php', 'assets/css/frontend/medal.css', ].map(read).join('\n'); assert.doesNotMatch(sources, /frontend_modal_rollout_badge/); assert.doesNotMatch(sources, /\.uc-badges-/); assert.doesNotMatch(sources, /grid-template-columns|\.medal-card__|\.medal-modal__|\.medal-grant-item/); const main = read('assets/css/main.css'); assert.match(main, /\.medal-list\s*\{\s*width:\s*calc\(20% - 14px\);\s*margin:\s*10px 7px/); assert.match(main, /\.medal-list\s*\{\s*width:\s*calc\(25% - 14px\)/); });