'use strict'; 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('comment REST admission is request-local and preprocess keeps native captcha enforcement', () => { const controller = read('src/Core/CommentActions.php'); const moderation = read('src/Auth/Moderation.php'); assert.match(controller, /aksrt_require_captcha\('comment', \$params\)/); assert.match(controller, /begin_captcha_verified_comment_request\(\)[\s\S]*?try \{[\s\S]*?wp_new_comment[\s\S]*?finally \{[\s\S]*?end_captcha_verified_comment_request\(\)/); assert.match(moderation, /private \$captcha_verified_comment_request_depth = 0/); assert.match(moderation, /if \(\$this->captcha_verified_comment_request_depth === 0\) \{/); assert.doesNotMatch(controller + moderation, /function_exists\('aksrt_require_captcha'\)/); assert.doesNotMatch(controller + moderation, /\$_(?:POST|REQUEST)\[[^\]]*captcha[^\]]*verified|set_transient\([^\n]*comment/i); }); test('captcha business failures preserve WP_Error codes and never stringify opaque results', () => { const sources = [ read('src/Core/CommentActions.php'), read('src/Auth/Moderation.php'), read('src/Admin/UserControllerAuthPhoneOperations.php'), read('inc/modules/user-parts/account-ajax.php'), read('src/Core/FrontSubmit.php'), ].join('\n'); assert.match(sources, /get_error_code\(\)/); assert.match(sources, /get_error_message\(\)/); assert.doesNotMatch(sources, /\(string\)\s*\$(?:captcha|verify|result)/); }); test('password and email account flows verify captcha once per distinct HTTP request', () => { const account = read('inc/modules/user-parts/account-ajax.php'); const methodBody = (action, nextAction = '') => { const start = account.indexOf(`add_action('${action}'`); const end = nextAction ? account.indexOf(`add_action('${nextAction}'`, start + 1) : account.length; return account.slice(start, end === -1 ? account.length : end); }; const send = methodBody('wp_ajax_aksrt_send_account_email_code', 'wp_ajax_aksrt_verify_account_email'); const password = methodBody('wp_ajax_aksrt_change_password', 'wp_ajax_aksrt_change_email'); const email = methodBody('wp_ajax_aksrt_change_email'); assert.equal((send.match(/aksrt_require_captcha\(/g) || []).length, 1); assert.match(send, /aksrt_require_captcha\(\s*'change_email'/); assert.equal((password.match(/aksrt_require_captcha\(/g) || []).length, 1); assert.match(password, /aksrt_require_captcha\(\s*'change_password'/); assert.equal((email.match(/aksrt_require_captcha\(/g) || []).length, 1); assert.match(email, /aksrt_require_captcha\(\s*'change_email'/); }); test('legacy public SMS-code AJAX surface is removed while SMS service remains', () => { const sms = read('src/Sms/SMS.php'); assert.doesNotMatch(sms, /aksrt_send_sms_code/); assert.doesNotMatch(sms, /function ajax_send_code\s*\(/); assert.match(sms, /public function send_code\(string \$phone, string \$code\): array/); });