test: add verification stop propagation tests for error handling
This commit is contained in:
+19
-1
@@ -1712,23 +1712,27 @@ async function reuseOrCreateTab(source, url, options = {}) {
|
||||
// ============================================================
|
||||
|
||||
async function sendToContentScript(source, message, options = {}) {
|
||||
throwIfStopped();
|
||||
const { responseTimeoutMs = getContentScriptResponseTimeoutMs(message) } = options;
|
||||
const registry = await getTabRegistry();
|
||||
const entry = registry[source];
|
||||
|
||||
if (!entry || !entry.ready) {
|
||||
throwIfStopped();
|
||||
console.log(LOG_PREFIX, `${source} not ready, queuing command`);
|
||||
return queueCommand(source, message);
|
||||
}
|
||||
|
||||
// Verify tab is still alive
|
||||
const alive = await isTabAlive(source);
|
||||
throwIfStopped();
|
||||
if (!alive) {
|
||||
// Tab was closed — queue the command, it will be sent when tab is reopened
|
||||
console.log(LOG_PREFIX, `${source} tab was closed, queuing command`);
|
||||
return queueCommand(source, message);
|
||||
}
|
||||
|
||||
throwIfStopped();
|
||||
console.log(LOG_PREFIX, `Sending to ${source} (tab ${entry.tabId}):`, message.type);
|
||||
return sendTabMessageWithTimeout(entry.tabId, source, message, responseTimeoutMs);
|
||||
}
|
||||
@@ -3810,13 +3814,17 @@ function getVerificationPollPayload(step, state, overrides = {}) {
|
||||
}
|
||||
|
||||
async function requestVerificationCodeResend(step) {
|
||||
throwIfStopped();
|
||||
const signupTabId = await getTabId('signup-page');
|
||||
if (!signupTabId) {
|
||||
throw new Error('认证页面标签页已关闭,无法重新请求验证码。');
|
||||
}
|
||||
|
||||
throwIfStopped();
|
||||
await chrome.tabs.update(signupTabId, { active: true });
|
||||
throwIfStopped();
|
||||
await addLog(`步骤 ${step}:正在请求新的${getVerificationCodeLabel(step)}验证码...`, 'warn');
|
||||
throwIfStopped();
|
||||
|
||||
const result = await sendToContentScript('signup-page', {
|
||||
type: 'RESEND_VERIFICATION_CODE',
|
||||
@@ -3863,6 +3871,7 @@ async function pollFreshVerificationCode(step, state, mail, pollOverrides = {})
|
||||
const maxRounds = pollOverrides.maxRounds || VERIFICATION_POLL_MAX_ROUNDS;
|
||||
|
||||
for (let round = 1; round <= maxRounds; round++) {
|
||||
throwIfStopped();
|
||||
if (round > 1) {
|
||||
await requestVerificationCodeResend(step);
|
||||
}
|
||||
@@ -3902,6 +3911,9 @@ async function pollFreshVerificationCode(step, state, mail, pollOverrides = {})
|
||||
|
||||
return result;
|
||||
} catch (err) {
|
||||
if (isStopError(err)) {
|
||||
throw err;
|
||||
}
|
||||
lastError = err;
|
||||
await addLog(`步骤 ${step}:${err.message}`, 'warn');
|
||||
if (round < maxRounds) {
|
||||
@@ -3963,7 +3975,7 @@ async function resolveVerificationStep(step, state, mail, options = {}) {
|
||||
await requestVerificationCodeResend(step);
|
||||
await addLog(`步骤 ${step}:已先请求一封新的${getVerificationCodeLabel(step)}验证码,再开始轮询邮箱。`, 'warn');
|
||||
} catch (err) {
|
||||
if (step === 7 && isStep7RestartFromStep6Error(err)) {
|
||||
if (isStopError(err) || (step === 7 && isStep7RestartFromStep6Error(err))) {
|
||||
throw err;
|
||||
}
|
||||
await addLog(`步骤 ${step}:首次重新获取验证码失败:${err.message},将继续使用当前时间窗口轮询。`, 'warn');
|
||||
@@ -3984,7 +3996,9 @@ async function resolveVerificationStep(step, state, mail, options = {}) {
|
||||
filterAfterTimestamp: nextFilterAfterTimestamp ?? undefined,
|
||||
});
|
||||
|
||||
throwIfStopped();
|
||||
await addLog(`步骤 ${step}:已获取${getVerificationCodeLabel(step)}验证码:${result.code}`);
|
||||
throwIfStopped();
|
||||
const submitResult = await submitVerificationCode(step, result.code);
|
||||
|
||||
if (submitResult.invalidCode) {
|
||||
@@ -4023,6 +4037,7 @@ async function executeStep4(state) {
|
||||
}
|
||||
|
||||
await chrome.tabs.update(signupTabId, { active: true });
|
||||
throwIfStopped();
|
||||
await addLog('步骤 4:正在确认注册验证码页面是否就绪,必要时自动恢复密码页超时报错...');
|
||||
const prepareResult = await sendToContentScriptResilient(
|
||||
'signup-page',
|
||||
@@ -4050,6 +4065,7 @@ async function executeStep4(state) {
|
||||
return;
|
||||
}
|
||||
|
||||
throwIfStopped();
|
||||
if (mail.provider === HOTMAIL_PROVIDER) {
|
||||
await addLog(`步骤 4:正在通过 ${mail.label} 轮询验证码...`);
|
||||
} else {
|
||||
@@ -4161,6 +4177,7 @@ async function runStep7Attempt(state) {
|
||||
await reuseOrCreateTab('signup-page', state.oauthUrl);
|
||||
}
|
||||
|
||||
throwIfStopped();
|
||||
await addLog('步骤 7:正在准备认证页,必要时切换到一次性验证码登录...');
|
||||
const prepareResult = await sendToContentScript('signup-page', {
|
||||
type: 'PREPARE_LOGIN_CODE',
|
||||
@@ -4178,6 +4195,7 @@ async function runStep7Attempt(state) {
|
||||
throw new Error(prepareResult.error);
|
||||
}
|
||||
|
||||
throwIfStopped();
|
||||
if (mail.provider === HOTMAIL_PROVIDER) {
|
||||
await addLog(`步骤 7:正在通过 ${mail.label} 轮询验证码...`);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user