const assert = require('assert'); const fs = require('fs'); const path = require('path'); const CaptchaPages = require('../public/js/captcha-pages.js'); const fakeApi = { clientId: 'client-x', tenantId: 'tenant-x' }; function run() { const captchaSource = fs.readFileSync(path.join(__dirname, '..', 'public', 'js', 'captcha-pages.js'), 'utf8'); assert.strictEqual(captchaSource.includes('auth-captcha-modal'), false, '验证码适配层不应控制自定义弹窗样式'); assert.strictEqual(captchaSource.includes('bgUrl:'), false, '验证码适配层不应覆盖 TAC 背景样式'); assert.strictEqual(captchaSource.includes('logoUrl:'), false, '验证码适配层不应覆盖 TAC Logo 样式'); assert.strictEqual(captchaSource.includes('root.layui.layer.open'), true, '验证码适配层应使用 Layui 默认弹层承载 TAC'); assert.strictEqual(captchaSource.includes('root.layui.layer.close'), true, '验证码结束时应关闭对应 Layui 弹层'); assert.deepStrictEqual(CaptchaPages.buildChallengeBody({ sceneCode: 'WEB_H5_LOGIN', subject: '13800000000' }, fakeApi), { tenantId: 'tenant-x', clientId: 'client-x', sceneCode: 'WEB_H5_LOGIN', subject: '13800000000' }); const challenge = CaptchaPages.adaptChallengeResponse({ code: 200, data: { required: true, providerCode: 'tianai', captchaType: 'SLIDER', challengeId: 'challenge-1', payload: { id: 'tianai-1', backgroundImage: 'bg.png', templateImage: 'tpl.png', backgroundImageWidth: 340 } } }); assert.strictEqual(challenge.code, 200); assert.strictEqual(challenge.data.type, 'SLIDER'); assert.strictEqual(challenge.data.id, 'tianai-1'); assert.strictEqual(challenge.data.challengeId, 'challenge-1'); assert.strictEqual(challenge.data.backgroundImage, 'bg.png'); assert.strictEqual(challenge.data.templateImage, 'tpl.png'); assert.deepStrictEqual(CaptchaPages.buildVerifyBody({ id: 'tianai-1', data: { bgImageWidth: 340, bgImageHeight: 180, templateImageWidth: 50, templateImageHeight: 50, startTime: 1720000000000, stopTime: 1720000001500, trackList: [ { x: 0, y: 0, t: 0, type: 'down' }, { x: 120, y: 0, t: 650, type: 'move' }, { x: 120, y: 0, t: 700, type: 'up' } ] } }, { sceneCode: 'WEB_H5_LOGIN', subject: '13800000000', providerCode: 'tianai', captchaType: 'SLIDER', challengeId: 'challenge-1' }, fakeApi), { tenantId: 'tenant-x', clientId: 'client-x', sceneCode: 'WEB_H5_LOGIN', subject: '13800000000', challengeId: 'challenge-1', providerCode: 'tianai', captchaType: 'SLIDER', payload: { track: { bgImageWidth: 340, bgImageHeight: 180, templateImageWidth: 50, templateImageHeight: 50, startTime: 1720000000000, stopTime: 1720000001500, left: 120, top: 0, trackList: [ { x: 0, y: 0, t: 0, type: 'down' }, { x: 120, y: 0, t: 650, type: 'move' }, { x: 120, y: 0, t: 700, type: 'up' } ] } } }); assert.strictEqual(CaptchaPages.extractValidToken({ code: 200, data: { passed: true, validToken: 'captcha-ticket' } }), 'captcha-ticket'); assert.strictEqual(CaptchaPages.shouldRequireCaptcha({ required: false }), false); assert.strictEqual(CaptchaPages.shouldRequireCaptcha({ required: true }), true); const originalDocument = global.document; const originalLayui = global.layui; const mountedBox = { innerHTML: '
' }; let layerOptions; let closedLayerIndex; global.document = { querySelector(selector) { assert.strictEqual(selector, '[data-captcha-layer-box="captcha-layer-1"]'); return mountedBox; } }; global.layui = { layer: { open(options) { layerOptions = options; return 7; }, close(index) { closedLayerIndex = index; } } }; const captchaLayer = CaptchaPages.createCaptchaLayer(); assert.strictEqual(captchaLayer.box, mountedBox); assert.strictEqual(captchaLayer.layerIndex, 7); assert.strictEqual(layerOptions.type, 1); assert.strictEqual(layerOptions.title, false); assert.strictEqual(layerOptions.closeBtn, 0); assert.deepStrictEqual(layerOptions.area, ['318px', '318px']); assert.strictEqual(layerOptions.offset, 'auto'); assert.strictEqual(typeof layerOptions.content, 'string'); assert.strictEqual(layerOptions.content.includes('data-captcha-layer-box="captcha-layer-1"'), true); CaptchaPages.closeCaptchaLayer(captchaLayer); assert.strictEqual(mountedBox.innerHTML, ''); assert.strictEqual(closedLayerIndex, 7); global.document = originalDocument; global.layui = originalLayui; console.log('captcha-pages tests passed'); } run();