refactor: 移除网络超时相关逻辑,简化错误处理
This commit is contained in:
@@ -79,7 +79,6 @@ test('auth page recovery detects retry page state', () => {
|
||||
assert.equal(snapshot.titleMatched, true);
|
||||
assert.equal(snapshot.detailMatched, false);
|
||||
assert.equal(snapshot.maxCheckAttemptsBlocked, false);
|
||||
assert.equal(snapshot.operationTimedOutBlocked, false);
|
||||
});
|
||||
|
||||
test('auth page recovery clicks retry and waits until page recovers', async () => {
|
||||
@@ -157,21 +156,3 @@ test('auth page recovery throws cloudflare security blocked error on max_check_a
|
||||
);
|
||||
});
|
||||
|
||||
test('auth page recovery throws network timeout block error on operation timed out page', async () => {
|
||||
const state = {
|
||||
clickCount: 0,
|
||||
pageText: 'Something went wrong. Operation timed out.',
|
||||
retryVisible: true,
|
||||
};
|
||||
const api = createRecoveryApi(state);
|
||||
|
||||
await assert.rejects(
|
||||
() => api.recoverAuthRetryPage({
|
||||
logLabel: '步骤 7:检测到登录超时报错,正在点击“重试”恢复当前页面',
|
||||
pathPatterns: [/\/log-in(?:[/?#]|$)/i],
|
||||
step: 7,
|
||||
timeoutMs: 1000,
|
||||
}),
|
||||
/NETWORK_TIMEOUT_BLOCKED::/
|
||||
);
|
||||
});
|
||||
|
||||
@@ -60,11 +60,11 @@ function createRouter(overrides = {}) {
|
||||
handleCloudflareSecurityBlocked: overrides.handleCloudflareSecurityBlocked || (async (error) => {
|
||||
const message = typeof error === 'string' ? error : error?.message || '';
|
||||
events.securityBlocks.push(message);
|
||||
return message.replace(/^(?:CF_SECURITY_BLOCKED|NETWORK_TIMEOUT_BLOCKED)::/, '') || message;
|
||||
return message.replace(/^CF_SECURITY_BLOCKED::/, '') || message;
|
||||
}),
|
||||
importSettingsBundle: async () => {},
|
||||
invalidateDownstreamAfterStepRestart: async () => {},
|
||||
isCloudflareSecurityBlockedError: overrides.isCloudflareSecurityBlockedError || ((error) => /^(?:CF_SECURITY_BLOCKED|NETWORK_TIMEOUT_BLOCKED)::/.test(typeof error === 'string' ? error : error?.message || '')),
|
||||
isCloudflareSecurityBlockedError: overrides.isCloudflareSecurityBlockedError || ((error) => /^CF_SECURITY_BLOCKED::/.test(typeof error === 'string' ? error : error?.message || '')),
|
||||
isAutoRunLockedState: () => false,
|
||||
isHotmailProvider: () => false,
|
||||
isLocalhostOAuthCallbackUrl: () => true,
|
||||
@@ -226,26 +226,3 @@ test('message router stops the flow and surfaces cloudflare security block error
|
||||
error: '您已触发Cloudflare 安全防护系统',
|
||||
});
|
||||
});
|
||||
test('message router stops the flow and surfaces network timeout block errors', async () => {
|
||||
const { router, events } = createRouter();
|
||||
|
||||
const response = await router.handleMessage({
|
||||
type: 'STEP_ERROR',
|
||||
step: 7,
|
||||
source: 'signup-page',
|
||||
payload: {},
|
||||
error: 'NETWORK_TIMEOUT_BLOCKED::请检查当前网络节点是否稳定',
|
||||
}, {});
|
||||
|
||||
assert.deepStrictEqual(events.securityBlocks, ['NETWORK_TIMEOUT_BLOCKED::请检查当前网络节点是否稳定']);
|
||||
assert.deepStrictEqual(events.notifyErrors, [
|
||||
{
|
||||
step: 7,
|
||||
error: '流程已被用户停止。',
|
||||
},
|
||||
]);
|
||||
assert.deepStrictEqual(response, {
|
||||
ok: true,
|
||||
error: '请检查当前网络节点是否稳定',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,74 +1,13 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
|
||||
const source = fs.readFileSync('background.js', 'utf8');
|
||||
|
||||
function extractFunction(name) {
|
||||
const markers = [`async function ${name}(`, `function ${name}(`];
|
||||
const start = markers
|
||||
.map((marker) => source.indexOf(marker))
|
||||
.find((index) => index >= 0);
|
||||
if (start < 0) {
|
||||
throw new Error(`missing function ${name}`);
|
||||
}
|
||||
|
||||
let parenDepth = 0;
|
||||
let signatureEnded = false;
|
||||
let braceStart = -1;
|
||||
for (let i = start; i < source.length; i += 1) {
|
||||
const ch = source[i];
|
||||
if (ch === '(') {
|
||||
parenDepth += 1;
|
||||
} else if (ch === ')') {
|
||||
parenDepth -= 1;
|
||||
if (parenDepth === 0) {
|
||||
signatureEnded = true;
|
||||
}
|
||||
} else if (ch === '{' && signatureEnded) {
|
||||
braceStart = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (braceStart < 0) {
|
||||
throw new Error(`missing body for function ${name}`);
|
||||
}
|
||||
|
||||
let depth = 0;
|
||||
let end = braceStart;
|
||||
for (; end < source.length; end += 1) {
|
||||
const ch = source[end];
|
||||
if (ch === '{') depth += 1;
|
||||
if (ch === '}') {
|
||||
depth -= 1;
|
||||
if (depth === 0) {
|
||||
end += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return source.slice(start, end);
|
||||
}
|
||||
|
||||
test('security blocked alert title distinguishes cloudflare and network timeout', () => {
|
||||
const test = require('node:test');const assert = require('node:assert/strict');const fs = require('node:fs');const source = fs.readFileSync('background.js', 'utf8');function extractFunction(name) { const markers = [`async function ${name}(`, `function ${name}(`]; const start = markers .map((marker) => source.indexOf(marker)) .find((index) => index >= 0); if (start < 0) { throw new Error(`missing function ${name}`); } let parenDepth = 0; let signatureEnded = false; let braceStart = -1; for (let i = start; i < source.length; i += 1) { const ch = source[i]; if (ch === '(') { parenDepth += 1; } else if (ch === ')') { parenDepth -= 1; if (parenDepth === 0) { signatureEnded = true; } } else if (ch === '{' && signatureEnded) { braceStart = i; break; } } if (braceStart < 0) { throw new Error(`missing body for function ${name}`); } let depth = 0; let end = braceStart; for (; end < source.length; end += 1) { const ch = source[end]; if (ch === '{') depth += 1; if (ch === '}') { depth -= 1; if (depth === 0) { end += 1; break; } } } return source.slice(start, end);}test('security blocked alert title distinguishes cloudflare and network timeout', () => {
|
||||
const api = new Function(`
|
||||
const CLOUDFLARE_SECURITY_BLOCK_ERROR_PREFIX = 'CF_SECURITY_BLOCKED::';
|
||||
const NETWORK_TIMEOUT_BLOCK_ERROR_PREFIX = 'NETWORK_TIMEOUT_BLOCKED::';
|
||||
${extractFunction('getErrorMessage')}
|
||||
${extractFunction('isCloudflareSecurityBlockedError')}
|
||||
${extractFunction('isNetworkTimeoutBlockedError')}
|
||||
${extractFunction('getTerminalSecurityBlockedTitle')}
|
||||
return { getTerminalSecurityBlockedTitle };
|
||||
`)();
|
||||
|
||||
assert.equal(
|
||||
api.getTerminalSecurityBlockedTitle(new Error('CF_SECURITY_BLOCKED::blocked')),
|
||||
'Cloudflare 风控拦截'
|
||||
);
|
||||
assert.equal(
|
||||
api.getTerminalSecurityBlockedTitle(new Error('NETWORK_TIMEOUT_BLOCKED::timeout')),
|
||||
'网络节点异常'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -87,8 +87,6 @@ test('ensureStep8VerificationPageReady throws cloudflare security block error on
|
||||
const api = new Function(`
|
||||
const CLOUDFLARE_SECURITY_BLOCK_ERROR_PREFIX = 'CF_SECURITY_BLOCKED::';
|
||||
const CLOUDFLARE_SECURITY_BLOCK_USER_MESSAGE = 'cloudflare blocked';
|
||||
const NETWORK_TIMEOUT_BLOCK_ERROR_PREFIX = 'NETWORK_TIMEOUT_BLOCKED::';
|
||||
const NETWORK_TIMEOUT_BLOCK_USER_MESSAGE = 'network timeout blocked';
|
||||
|
||||
function getLoginAuthStateLabel(state) {
|
||||
return state === 'login_timeout_error_page' ? 'login timeout page' : 'unknown page';
|
||||
@@ -117,40 +115,6 @@ return {
|
||||
);
|
||||
});
|
||||
|
||||
test('ensureStep8VerificationPageReady throws network timeout block error on operation timed out page', async () => {
|
||||
const api = new Function(`
|
||||
const CLOUDFLARE_SECURITY_BLOCK_ERROR_PREFIX = 'CF_SECURITY_BLOCKED::';
|
||||
const CLOUDFLARE_SECURITY_BLOCK_USER_MESSAGE = 'cloudflare blocked';
|
||||
const NETWORK_TIMEOUT_BLOCK_ERROR_PREFIX = 'NETWORK_TIMEOUT_BLOCKED::';
|
||||
const NETWORK_TIMEOUT_BLOCK_USER_MESSAGE = 'network timeout blocked';
|
||||
|
||||
function getLoginAuthStateLabel(state) {
|
||||
return state === 'login_timeout_error_page' ? 'login timeout page' : 'unknown page';
|
||||
}
|
||||
|
||||
async function getLoginAuthStateFromContent() {
|
||||
return {
|
||||
state: 'login_timeout_error_page',
|
||||
url: 'https://auth.openai.com/log-in',
|
||||
operationTimedOutBlocked: true,
|
||||
};
|
||||
}
|
||||
|
||||
${extractFunction(backgroundSource, 'ensureStep8VerificationPageReady')}
|
||||
|
||||
return {
|
||||
run() {
|
||||
return ensureStep8VerificationPageReady({});
|
||||
},
|
||||
};
|
||||
`)();
|
||||
|
||||
await assert.rejects(
|
||||
() => api.run(),
|
||||
/NETWORK_TIMEOUT_BLOCKED::/
|
||||
);
|
||||
});
|
||||
|
||||
test('step 8 reruns step 7 when auth page enters login timeout retry state', async () => {
|
||||
const calls = {
|
||||
executeStep7: 0,
|
||||
|
||||
Reference in New Issue
Block a user