const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const source = fs.readFileSync('background/verification-flow.js', 'utf8'); const globalScope = {}; const api = new Function('self', `${source}; return self.MultiPageBackgroundVerificationFlow;`)(globalScope); test('verification flow keeps 2925 polling cadence in the default payload', () => { const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {} } }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async () => ({}), sendToMailContentScriptResilient: async () => ({}), setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); const step4Payload = helpers.getVerificationPollPayload(4, { email: 'user@example.com', mailProvider: '2925' }); const step8Payload = helpers.getVerificationPollPayload(8, { email: 'user@example.com', mailProvider: '2925' }); assert.equal(step4Payload.maxAttempts, 15); assert.equal(step4Payload.intervalMs, 15000); assert.equal(step8Payload.maxAttempts, 15); assert.equal(step8Payload.intervalMs, 15000); }); test('verification flow only enables 2925 target email matching in receive mode', () => { const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {} } }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async () => ({}), sendToMailContentScriptResilient: async () => ({}), setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); const providePayload = helpers.getVerificationPollPayload(4, { email: 'user@example.com', mailProvider: '2925', mail2925Mode: 'provide', }); const receivePayload = helpers.getVerificationPollPayload(4, { email: 'user@example.com', mailProvider: '2925', mail2925Mode: 'receive', }); assert.equal(providePayload.mail2925MatchTargetEmail, false); assert.equal(receivePayload.mail2925MatchTargetEmail, true); }); test('verification flow runs beforeSubmit hook before filling the code', async () => { const events = []; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {}, }, }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async (_step, payload) => { events.push(['complete', payload.code]); }, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'FILL_CODE') { events.push(['submit', message.payload.code]); return {}; } return {}; }, sendToMailContentScriptResilient: async () => ({ code: '654321', emailTimestamp: 123, }), setState: async (payload) => { events.push(['state', payload.lastLoginCode || payload.lastSignupCode]); }, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 7, { email: 'user@example.com', lastLoginCode: null }, { provider: 'qq', label: 'QQ 邮箱' }, { beforeSubmit: async (result) => { events.push(['beforeSubmit', result.code]); }, } ); assert.deepStrictEqual(events, [ ['beforeSubmit', '654321'], ['submit', '654321'], ['state', '654321'], ['complete', '654321'], ]); }); test('verification flow skips 2925 mailbox preclear when using a fixed login mail window and still clears after success', async () => { const mailMessages = []; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {}, }, }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'FILL_CODE') { return {}; } return {}; }, sendToMailContentScriptResilient: async (_mail, message) => { mailMessages.push(message.type); if (message.type === 'POLL_EMAIL') { return { code: '654321', emailTimestamp: 123 }; } return { ok: true }; }, setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 8, { email: 'user@example.com', mailProvider: '2925', lastLoginCode: null, }, { provider: '2925', label: '2925 邮箱' }, { filterAfterTimestamp: 123456 } ); await new Promise((resolve) => setTimeout(resolve, 0)); assert.deepStrictEqual(mailMessages, ['POLL_EMAIL', 'DELETE_ALL_EMAILS']); }); test('verification flow skips 2925 mailbox preclear when using a fixed signup mail window and still clears after success', async () => { const mailMessages = []; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {}, }, }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'FILL_CODE') { return {}; } if (message.type === 'RESEND_VERIFICATION_CODE') { return {}; } return {}; }, sendToMailContentScriptResilient: async (_mail, message) => { mailMessages.push(message.type); if (message.type === 'POLL_EMAIL') { return { code: '654321', emailTimestamp: 123 }; } return { ok: true, deleted: true }; }, setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 4, { email: 'user@example.com', mailProvider: '2925', lastSignupCode: null, }, { provider: '2925', label: '2925 邮箱' }, { filterAfterTimestamp: 123456, requestFreshCodeFirst: false, } ); await new Promise((resolve) => setTimeout(resolve, 0)); assert.deepStrictEqual(mailMessages, ['POLL_EMAIL', 'DELETE_ALL_EMAILS']); }); test('verification flow treats add-phone after login code submit as fatal instead of completing step 8', async () => { const events = []; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {}, }, }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async (_step, payload) => { events.push(['complete', payload.code]); }, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'FILL_CODE') { events.push(['submit', message.payload.code]); return { addPhonePage: true, url: 'https://auth.openai.com/add-phone', }; } return {}; }, sendToMailContentScriptResilient: async () => ({ code: '654321', emailTimestamp: 123, }), setState: async (payload) => { events.push(['state', payload.lastLoginCode || payload.lastSignupCode]); }, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await assert.rejects( () => helpers.resolveVerificationStep( 8, { email: 'user@example.com', lastLoginCode: null }, { provider: 'qq', label: 'QQ 邮箱' }, {} ), /验证码提交后页面进入手机号页面/ ); assert.deepStrictEqual(events, [ ['submit', '654321'], ]); }); test('verification flow caps mail polling timeout to the remaining oauth budget', async () => { const mailPollCalls = []; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {}, }, }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'FILL_CODE') { return {}; } return {}; }, sendToMailContentScriptResilient: async (_mail, message, options) => { mailPollCalls.push({ payload: message.payload, options, }); return { code: '654321', emailTimestamp: 123 }; }, setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 8, { email: 'user@example.com', lastLoginCode: null, }, { provider: 'qq', label: 'QQ 邮箱' }, { getRemainingTimeMs: async () => 5000, resendIntervalMs: 0, } ); assert.ok(mailPollCalls.length >= 1); assert.equal(mailPollCalls[0].options.timeoutMs, 5000); assert.equal(mailPollCalls[0].options.responseTimeoutMs, 5000); assert.equal(mailPollCalls[0].payload.maxAttempts, 2); }); test('verification flow keeps 2925 mailbox polling at 15 refresh attempts even when oauth budget is smaller', async () => { const mailPollCalls = []; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {}, }, }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'FILL_CODE') { return {}; } return {}; }, sendToMailContentScriptResilient: async (_mail, message, options) => { mailPollCalls.push({ type: message.type, payload: message.payload, options, }); return { code: '654321', emailTimestamp: 123 }; }, setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 8, { email: 'user@example.com', mailProvider: '2925', lastLoginCode: null, }, { provider: '2925', label: '2925 邮箱' }, { getRemainingTimeMs: async () => 5000, resendIntervalMs: 0, disableTimeBudgetCap: true, } ); const pollCall = mailPollCalls.find((entry) => entry.type === 'POLL_EMAIL'); assert.ok(pollCall); assert.equal(pollCall.payload.maxAttempts, 15); assert.ok(pollCall.options.timeoutMs >= 250000); }); test('verification flow keeps Hotmail request timestamp filtering on the first poll', async () => { const pollPayloads = []; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {} } }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 87654, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async (_step, _state, payload) => { pollPayloads.push(payload); return { code: '654321', emailTimestamp: 123 }; }, pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'FILL_CODE') { return {}; } return {}; }, sendToMailContentScriptResilient: async () => ({}), setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 7, { email: 'user@example.com', loginVerificationRequestedAt: 100000, lastLoginCode: null, }, { provider: 'hotmail-api', label: 'Hotmail' }, {} ); assert.equal(pollPayloads.length, 1); assert.equal(pollPayloads[0].filterAfterTimestamp, 87654); }); test('verification flow keeps fixed filter timestamp after step 4 resend', async () => { const pollPayloads = []; let submitCount = 0; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {} } }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: (_step, state) => Math.max(0, Number(state.signupVerificationRequestedAt || 0) - 15000), getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async (_step, _state, payload) => { pollPayloads.push(payload); return { code: pollPayloads.length === 1 ? '111111' : '222222', emailTimestamp: pollPayloads.length, }; }, pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'FILL_CODE') { submitCount += 1; return submitCount === 1 ? { invalidCode: true, errorText: '旧验证码' } : {}; } if (message.type === 'RESEND_VERIFICATION_CODE') { return {}; } return {}; }, sendToMailContentScriptResilient: async () => ({}), setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 4, { email: 'user@example.com', signupVerificationRequestedAt: 100000, lastSignupCode: null, }, { provider: 'hotmail-api', label: 'Hotmail' }, { filterAfterTimestamp: 123456, } ); assert.equal(pollPayloads.length, 2); assert.equal(pollPayloads[0].filterAfterTimestamp, 123456); assert.equal(pollPayloads[1].filterAfterTimestamp, 123456); }); test('verification flow uses configured signup resend count for step 4', async () => { const resendSteps = []; let pollCalls = 0; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {} } }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'RESEND_VERIFICATION_CODE') { resendSteps.push(message.step); } return {}; }, sendToMailContentScriptResilient: async () => { pollCalls += 1; return pollCalls === 2 ? { code: '654321', emailTimestamp: 123 } : {}; }, setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 4, { email: 'user@example.com', verificationResendCount: 2, lastSignupCode: null, }, { provider: 'qq', label: 'QQ 邮箱' }, { requestFreshCodeFirst: true, resendIntervalMs: 0, } ); assert.deepStrictEqual(resendSteps, [4, 4]); assert.equal(pollCalls, 2); }); test('verification flow uses configured login resend count for step 8', async () => { const resendSteps = []; let pollCalls = 0; const helpers = api.createVerificationFlowHelpers({ addLog: async () => {}, chrome: { tabs: { update: async () => {} } }, CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email', completeStepFromBackground: async () => {}, confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }), getHotmailVerificationPollConfig: () => ({}), getHotmailVerificationRequestTimestamp: () => 0, getState: async () => ({}), getTabId: async () => 1, HOTMAIL_PROVIDER: 'hotmail-api', isStopError: () => false, LUCKMAIL_PROVIDER: 'luckmail-api', MAIL_2925_VERIFICATION_INTERVAL_MS: 15000, MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15, pollCloudflareTempEmailVerificationCode: async () => ({}), pollHotmailVerificationCode: async () => ({}), pollLuckmailVerificationCode: async () => ({}), sendToContentScript: async (_source, message) => { if (message.type === 'RESEND_VERIFICATION_CODE') { resendSteps.push(message.step); } return {}; }, sendToMailContentScriptResilient: async () => { pollCalls += 1; return pollCalls === 3 ? { code: '654321', emailTimestamp: 123 } : {}; }, setState: async () => {}, setStepStatus: async () => {}, sleepWithStop: async () => {}, throwIfStopped: () => {}, VERIFICATION_POLL_MAX_ROUNDS: 5, }); await helpers.resolveVerificationStep( 8, { email: 'user@example.com', verificationResendCount: 2, lastLoginCode: null, }, { provider: 'qq', label: 'QQ 邮箱' }, { requestFreshCodeFirst: false, resendIntervalMs: 0, } ); assert.deepStrictEqual(resendSteps, [8, 8]); assert.equal(pollCalls, 3); });