| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- 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);
- }
- const bundle = [
- extractFunction('isAddPhoneAuthUrl'),
- extractFunction('isAddPhoneAuthState'),
- extractFunction('getPostStep6AutoRestartDecision'),
- extractFunction('getNextActiveStep'),
- extractFunction('runAutoSequenceFromStep'),
- ].join('\n');
- test('auto-run restarts from step 1 with the same email after step 4 failure', async () => {
- const api = new Function(`
- const STEP_IDS = [1, 2, 3, 4, 5, 6, 7, 8, 9];
- const AUTO_STEP_DELAYS = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0 };
- const LAST_STEP_ID = 9;
- const FINAL_OAUTH_CHAIN_START_STEP = null;
- const chrome = {
- tabs: {
- update: async () => {},
- },
- runtime: {
- sendMessage: async () => {},
- },
- };
- let remainingFailures = 1;
- let currentState = {
- email: 'keep@example.com',
- password: 'Secret123!',
- mailProvider: '163',
- stepStatuses: {
- 1: 'pending',
- 2: 'pending',
- 3: 'pending',
- 4: 'pending',
- 5: 'pending',
- 6: 'pending',
- 7: 'pending',
- 8: 'pending',
- 9: 'pending',
- },
- };
- const events = {
- steps: [],
- emails: [],
- invalidations: [],
- logs: [],
- setStateCalls: [],
- };
- async function addLog(message, level = 'info') {
- events.logs.push({ message, level });
- }
- async function ensureAutoEmailReady() {
- events.emails.push(currentState.email);
- return currentState.email;
- }
- async function broadcastAutoRunStatus() {}
- async function getState() {
- return currentState;
- }
- async function setState(updates) {
- currentState = {
- ...currentState,
- ...updates,
- stepStatuses: updates.stepStatuses ? { ...updates.stepStatuses } : currentState.stepStatuses,
- };
- events.setStateCalls.push(updates);
- }
- function isStopError(error) {
- return (error?.message || String(error || '')) === '流程已被用户停止。';
- }
- function isStepDoneStatus(status) {
- return status === 'completed' || status === 'manual_completed' || status === 'skipped';
- }
- async function executeStepAndWait(step) {
- events.steps.push(step);
- if (step === 4 && remainingFailures > 0) {
- remainingFailures -= 1;
- throw new Error('步骤 4 提交验证码前页面异常。');
- }
- }
- async function getTabId() {
- return 1;
- }
- async function invalidateDownstreamAfterStepRestart(step, options = {}) {
- events.invalidations.push({ step, options });
- currentState = {
- ...currentState,
- password: null,
- stepStatuses: {
- 1: currentState.stepStatuses[1] || 'completed',
- 2: 'pending',
- 3: 'pending',
- 4: 'pending',
- 5: 'pending',
- 6: 'pending',
- 7: 'pending',
- 8: 'pending',
- 9: 'pending',
- },
- };
- }
- function getLoginAuthStateLabel(state) {
- return state || 'unknown';
- }
- function getErrorMessage(error) {
- return error?.message || String(error || '');
- }
- async function getLoginAuthStateFromContent() {
- return { state: 'password_page', url: 'https://auth.openai.com/log-in' };
- }
- ${bundle}
- return {
- async run() {
- await runAutoSequenceFromStep(1, {
- targetRun: 1,
- totalRuns: 1,
- attemptRuns: 1,
- continued: false,
- });
- return { events, currentState };
- },
- };
- `)();
- const { events, currentState } = await api.run();
- assert.deepStrictEqual(events.invalidations, [
- {
- step: 1,
- options: {
- logLabel: '步骤 4 报错后准备回到步骤 1 沿用当前邮箱重试(第 1 次重开)',
- },
- },
- ]);
- assert.deepStrictEqual(events.emails, ['keep@example.com', 'keep@example.com']);
- assert.deepStrictEqual(events.steps, [1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
- assert.equal(currentState.email, 'keep@example.com');
- assert.equal(currentState.password, 'Secret123!');
- assert.equal(events.logs.some(({ message }) => /沿用当前邮箱回到步骤 1 重新开始/.test(message)), true);
- });
|