feat: 添加步骤 5 提交状态验证和重试页面恢复逻辑,更新相关测试用例

This commit is contained in:
QLHazyCoder
2026-05-12 00:26:54 +08:00
parent c6a37005e3
commit 745010d6c9
6 changed files with 457 additions and 5 deletions
@@ -0,0 +1,140 @@
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('step 5 post-completion validation recovers about-you retry page before allowing success', async () => {
const api = new Function(`
const logs = [];
const messages = [];
let stateReadCount = 0;
const chrome = {
tabs: {
async get() {
return { url: 'https://auth.openai.com/about-you' };
},
},
};
async function sendToContentScriptResilient(source, message) {
messages.push({ source, type: message.type });
if (message.type === 'GET_STEP5_SUBMIT_STATE') {
stateReadCount += 1;
if (stateReadCount === 1) {
return {
retryPage: true,
retryEnabled: true,
maxCheckAttemptsBlocked: false,
userAlreadyExistsBlocked: false,
successState: '',
profileVisible: false,
errorText: '',
unknownAuthPage: false,
url: 'https://auth.openai.com/about-you',
};
}
return {
retryPage: false,
retryEnabled: false,
maxCheckAttemptsBlocked: false,
userAlreadyExistsBlocked: false,
successState: 'logged_in_home',
profileVisible: false,
errorText: '',
unknownAuthPage: false,
url: 'https://chatgpt.com/',
};
}
if (message.type === 'RECOVER_STEP5_SUBMIT_RETRY_PAGE') {
return { recovered: true, clickCount: 1 };
}
throw new Error('unexpected message type: ' + message.type);
}
async function addLog(message, level, meta) {
logs.push({ message, level, meta });
}
async function waitForTabStableComplete() {}
${extractFunction('parseUrlSafely')}
${extractFunction('isSignupEntryHost')}
${extractFunction('isLikelyLoggedInChatgptHomeUrl')}
${extractFunction('getStep5SubmitStateFromContent')}
${extractFunction('recoverStep5SubmitRetryPageOnTab')}
${extractFunction('validateStep5PostCompletion')}
return {
async run() {
return validateStep5PostCompletion(99, {});
},
snapshot() {
return { logs, messages, stateReadCount };
},
};
`)();
const result = await api.run();
const snapshot = api.snapshot();
assert.equal(result.successState, 'logged_in_home');
assert.deepStrictEqual(
snapshot.messages.map(({ type }) => type),
['GET_STEP5_SUBMIT_STATE', 'RECOVER_STEP5_SUBMIT_RETRY_PAGE', 'GET_STEP5_SUBMIT_STATE']
);
assert.equal(snapshot.stateReadCount, 2);
assert.equal(
snapshot.logs.some(({ message }) => /检测到认证重试页/.test(message)),
true
);
});
+108
View File
@@ -875,3 +875,111 @@ return {
});
assert.equal(api.snapshot().recoverCalls, 1);
});
test('step 5 profile url helper treats about-you as profile page', () => {
const api = new Function(`
${extractFunction('isSignupProfilePageUrl')}
return {
run(url) {
return isSignupProfilePageUrl(url);
},
};
`)();
assert.equal(api.run('https://auth.openai.com/about-you'), true);
assert.equal(api.run('https://auth.openai.com/create-account/profile'), true);
assert.equal(api.run('https://chatgpt.com/about-you'), false);
});
test('step 5 recovers about-you auth retry page after profile submit', async () => {
const api = new Function(`
let retryVisible = true;
let recoverCalls = 0;
const location = {
href: 'https://auth.openai.com/about-you',
pathname: '/about-you',
};
const document = {
querySelector() { return null; },
querySelectorAll() { return []; },
};
function throwIfStopped() {}
function log() {}
async function sleep() {}
async function humanPause() {}
function simulateClick() {}
function isVisibleElement() { return true; }
function isActionEnabled() { return true; }
function getActionText(el) { return el?.textContent || ''; }
function getSignupAuthRetryPathPatterns() { return []; }
function getAuthTimeoutErrorPageState(options) {
const matchesAboutYou = Array.isArray(options?.pathPatterns)
&& options.pathPatterns.some((pattern) => pattern.test(location.pathname));
if (!retryVisible || !matchesAboutYou) return null;
return {
retryEnabled: true,
userAlreadyExistsBlocked: false,
maxCheckAttemptsBlocked: false,
};
}
async function recoverCurrentAuthRetryPage() {
recoverCalls += 1;
retryVisible = false;
location.href = 'https://chatgpt.com/';
location.pathname = '/';
}
function createSignupUserAlreadyExistsError() { return new Error('user already exists'); }
function createAuthMaxCheckAttemptsError() { return new Error('max_check_attempts'); }
function getStep5ErrorText() { return ''; }
function isStep5Ready() { return /^https:\\/\\/auth\\.openai\\.com\\//.test(location.href); }
function isLikelyLoggedInChatgptHomeUrl() { return /^https:\\/\\/chatgpt\\.com\\//.test(location.href); }
function isOAuthConsentPage() { return false; }
function isAddPhonePageReady() { return false; }
${extractFunction('isSignupProfilePageUrl')}
${getStep5OutcomeBundle()}
return {
run() {
return waitForStep5SubmitOutcome({ timeoutMs: 1000 });
},
snapshot() {
return { recoverCalls };
},
};
`)();
const result = await api.run();
assert.deepStrictEqual(result, {
state: 'logged_in_home',
url: 'https://chatgpt.com/',
});
assert.equal(api.snapshot().recoverCalls, 1);
});
test('step 5 does not treat unknown auth page as left_profile success', () => {
const api = new Function(`
const location = {
href: 'https://auth.openai.com/unexpected-state',
};
function getStep5AuthRetryPageState() { return null; }
function isLikelyLoggedInChatgptHomeUrl() { return false; }
function isOAuthConsentPage() { return false; }
function isAddPhonePageReady() { return false; }
function isStep5ProfileStillVisible() { return false; }
${extractFunction('getStep5PostSubmitSuccessState')}
return {
run() {
return getStep5PostSubmitSuccessState();
},
};
`)();
assert.equal(api.run(), null);
});