112 lines
3.3 KiB
JavaScript
112 lines
3.3 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('assert');
|
|
const { _private } = require('../src/services/chatgpt_service');
|
|
const { getFailureLabel } = require('../src/utils/error_classifier');
|
|
|
|
assert.strictEqual(
|
|
getFailureLabel('CHATGPT_CLOUDFLARE_TIMEOUT', { taskType: 'chatgpt_register' }),
|
|
'未手动完成 Cloudflare 验证超时',
|
|
'CF等待超时应显示明确的处理结论',
|
|
);
|
|
|
|
function missingLocator() {
|
|
return { count: async () => 0 };
|
|
}
|
|
|
|
(async () => {
|
|
const challengePage = {
|
|
url: () => 'https://auth.openai.com/log-in',
|
|
title: async () => 'auth.openai.com',
|
|
locator: selector => {
|
|
if (selector === 'body') {
|
|
return {
|
|
innerText: async () => 'Performing security verification Verify you are human',
|
|
};
|
|
}
|
|
return missingLocator();
|
|
},
|
|
};
|
|
assert.strictEqual(
|
|
await _private.detectCloudflareChallenge(challengePage),
|
|
true,
|
|
'应识别 auth.openai.com 上的 Verify you are human',
|
|
);
|
|
|
|
const unrelatedPage = {
|
|
...challengePage,
|
|
url: () => 'https://example.com/',
|
|
};
|
|
assert.strictEqual(
|
|
await _private.detectCloudflareChallenge(unrelatedPage),
|
|
false,
|
|
'相同文案出现在非 OpenAI 域名时不应误判',
|
|
);
|
|
|
|
const normalPageWithResidualScript = {
|
|
url: () => 'https://chatgpt.com/',
|
|
title: async () => 'ChatGPT',
|
|
locator: selector => {
|
|
if (selector === 'body') return { innerText: async () => 'ChatGPT New chat Library' };
|
|
if (selector === 'script[src*="challenge-platform"]') {
|
|
return {
|
|
count: async () => 1,
|
|
nth() { return this; },
|
|
isVisible: async () => false,
|
|
};
|
|
}
|
|
return missingLocator();
|
|
},
|
|
};
|
|
assert.strictEqual(
|
|
await _private.detectCloudflareChallenge(normalPageWithResidualScript),
|
|
false,
|
|
'页面残留Cloudflare脚本但没有可见挑战时不能误判',
|
|
);
|
|
|
|
const state = { challenge: true, waits: 0, clicks: 0 };
|
|
const transitioningPage = {
|
|
url: () => state.challenge
|
|
? 'https://auth.openai.com/log-in'
|
|
: 'https://accounts.google.com/o/oauth2/auth',
|
|
title: async () => state.challenge ? 'Just a moment...' : 'Sign in',
|
|
waitForTimeout: async () => {
|
|
state.waits++;
|
|
state.challenge = false;
|
|
},
|
|
locator: selector => {
|
|
if (selector === 'body') {
|
|
return {
|
|
innerText: async () => state.challenge
|
|
? 'Performing security verification Verify you are human'
|
|
: 'Sign in with Google',
|
|
};
|
|
}
|
|
return {
|
|
count: async () => 0,
|
|
click: async () => { state.clicks++; },
|
|
};
|
|
},
|
|
};
|
|
|
|
let detectedNotices = 0;
|
|
let clearedNotices = 0;
|
|
const result = await _private.waitForCloudflareChallenge(transitioningPage, () => {}, {
|
|
timeoutMs: 5000,
|
|
onDetected: async () => { detectedNotices++; },
|
|
onCleared: async () => { clearedNotices++; },
|
|
});
|
|
|
|
assert.strictEqual(result.detected, true);
|
|
assert.strictEqual(result.passed, true);
|
|
assert.strictEqual(detectedNotices, 1);
|
|
assert.strictEqual(clearedNotices, 1);
|
|
assert.strictEqual(state.clicks, 0, '兼容流程不得自动操作人机验证控件');
|
|
assert.ok(state.waits >= 2, '挑战消失后应连续确认两次再继续');
|
|
|
|
console.log('chatgpt_cloudflare_wait.test.js: PASS');
|
|
})().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|