fix: enhance step 7 login identifier resolution and add tests for phone/email handling

This commit is contained in:
QLHazyCoder
2026-05-08 10:31:20 +08:00
parent b56bde0c2e
commit 83c5a6237c
4 changed files with 219 additions and 42 deletions
+100
View File
@@ -358,6 +358,106 @@ test('step 7 forwards phone login identity payload when account identifier is ph
]);
});
test('step 7 keeps Plus email login even when phone sms runtime exists', async () => {
const source = fs.readFileSync('background/steps/oauth-login.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundStep7;`)(globalScope);
const events = {
payloads: [],
};
const executor = api.createStep7Executor({
addLog: async () => {},
completeStepFromBackground: async () => {},
getErrorMessage: (error) => error?.message || String(error || ''),
getLoginAuthStateLabel: (state) => state || 'unknown',
getState: async () => ({
plusModeEnabled: true,
phoneVerificationEnabled: true,
signupMethod: 'phone',
email: 'plus.user@example.com',
password: 'secret',
signupPhoneNumber: '+441111111111',
}),
isStep6RecoverableResult: (result) => result?.step6Outcome === 'recoverable',
isStep6SuccessResult: (result) => result?.step6Outcome === 'success',
refreshOAuthUrlBeforeStep6: async () => 'https://oauth.example/latest',
reuseOrCreateTab: async () => {},
sendToContentScriptResilient: async (_sourceName, message) => {
events.payloads.push(message.payload);
return {
step6Outcome: 'success',
state: 'verification_page',
loginVerificationRequestedAt: 123456,
};
},
STEP6_MAX_ATTEMPTS: 3,
throwIfStopped: () => {},
});
await executor.executeStep7({
plusModeEnabled: true,
phoneVerificationEnabled: true,
signupMethod: 'phone',
email: 'plus.user@example.com',
password: 'secret',
signupPhoneNumber: '+441111111111',
visibleStep: 10,
});
assert.equal(events.payloads[0].loginIdentifierType, 'email');
assert.equal(events.payloads[0].email, 'plus.user@example.com');
assert.equal(events.payloads[0].phoneNumber, '');
assert.equal(events.payloads[0].accountIdentifier, 'plus.user@example.com');
});
test('step 7 can infer phone login from an available phone signup configuration before step 2 finishes', async () => {
const source = fs.readFileSync('background/steps/oauth-login.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundStep7;`)(globalScope);
const events = {
payloads: [],
};
const executor = api.createStep7Executor({
addLog: async () => {},
completeStepFromBackground: async () => {},
getErrorMessage: (error) => error?.message || String(error || ''),
getLoginAuthStateLabel: (state) => state || 'unknown',
getState: async () => ({
phoneVerificationEnabled: true,
signupMethod: 'phone',
signupPhoneNumber: '+447780579093',
}),
isStep6RecoverableResult: (result) => result?.step6Outcome === 'recoverable',
isStep6SuccessResult: (result) => result?.step6Outcome === 'success',
refreshOAuthUrlBeforeStep6: async () => 'https://oauth.example/latest',
reuseOrCreateTab: async () => {},
sendToContentScriptResilient: async (_sourceName, message) => {
events.payloads.push(message.payload);
return {
step6Outcome: 'success',
state: 'phone_verification_page',
loginVerificationRequestedAt: 987654,
};
},
STEP6_MAX_ATTEMPTS: 3,
throwIfStopped: () => {},
});
await executor.executeStep7({
phoneVerificationEnabled: true,
signupMethod: 'phone',
signupPhoneNumber: '+447780579093',
});
assert.equal(events.payloads[0].loginIdentifierType, 'phone');
assert.equal(events.payloads[0].phoneNumber, '+447780579093');
assert.equal(events.payloads[0].email, '');
});
test('step 7 can start from a manually filled signup phone without completed step 2 or step 3 state', async () => {
const source = fs.readFileSync('background/steps/oauth-login.js', 'utf8');
const globalScope = {};
+44
View File
@@ -184,6 +184,50 @@ test('step 7 treats unified OpenAI login page as email input despite phone optio
assert.equal(api.getLoginPhoneInput(), null, 'phone option button must not turn email input into phone input');
});
test('step 7 clicks the generic other-account entry before phone entry', async () => {
const api = new Function(`
const clicks = [];
const genericEntry = { id: 'generic', textContent: '\\u767b\\u5f55\\u81f3\\u53e6\\u4e00\\u4e2a\\u5e10\\u6237' };
const phoneEntry = { id: 'phone', textContent: '\\u4f7f\\u7528\\u7535\\u8bdd\\u53f7\\u7801\\u7ee7\\u7eed' };
function normalizeStep6Snapshot(snapshot) { return snapshot; }
function inspectLoginAuthState() {
return { state: 'entry_page', loginEntryTrigger: genericEntry, phoneEntryTrigger: phoneEntry };
}
function findLoginEntryTrigger() { return genericEntry; }
function findLoginPhoneEntryTrigger() { return phoneEntry; }
function isActionEnabled() { return true; }
function getActionText(el) { return el.textContent || ''; }
function log() {}
async function humanPause() {}
function simulateClick(el) { clicks.push(el.id); }
async function waitForLoginEntryOpenTransition() { return { state: 'phone_entry_page' }; }
async function switchFromEmailPageToPhoneLogin() { return { routed: 'switch-phone' }; }
async function step6LoginFromEmailPage() { return { routed: 'email' }; }
async function step6LoginFromPasswordPage() { return { routed: 'password' }; }
async function step6LoginFromPhonePage() { return { routed: 'phone' }; }
async function finalizeStep6VerificationReady() { return { routed: 'verification' }; }
function createStep6OAuthConsentSuccessResult() { return { routed: 'oauth' }; }
function createStep6AddEmailSuccessResult() { return { routed: 'add-email' }; }
async function createStep6LoginTimeoutRecoveryTransition() { return { action: 'recoverable', result: { routed: 'recoverable' } }; }
function createStep6RecoverableResult(reason, snapshot, options = {}) {
return { step6Outcome: 'recoverable', reason, state: snapshot?.state, message: options.message || '' };
}
${extractFunction('step6OpenLoginEntry')}
return { clicks, step6OpenLoginEntry };
`)();
const result = await api.step6OpenLoginEntry(
{ loginIdentifierType: 'phone', phoneNumber: '+441111111111' },
{ state: 'entry_page', loginEntryTrigger: { id: 'generic', textContent: '\u767b\u5f55\u81f3\u53e6\u4e00\u4e2a\u5e10\u6237' }, phoneEntryTrigger: { id: 'phone', textContent: '\u4f7f\u7528\u7535\u8bdd\u53f7\u7801\u7ee7\u7eed' } }
);
assert.deepStrictEqual(api.clicks, ['generic']);
assert.equal(result.routed, 'phone');
});
test('step 7 detects username text input when usernameKind is phone_number', () => {
const api = createPhoneLoginEntryApi({
phoneUsernameKind: true,