auto-run-step4-restart.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('background.js', 'utf8');
  5. function extractFunction(name) {
  6. const markers = [`async function ${name}(`, `function ${name}(`];
  7. const start = markers
  8. .map((marker) => source.indexOf(marker))
  9. .find((index) => index >= 0);
  10. if (start < 0) {
  11. throw new Error(`missing function ${name}`);
  12. }
  13. let parenDepth = 0;
  14. let signatureEnded = false;
  15. let braceStart = -1;
  16. for (let i = start; i < source.length; i += 1) {
  17. const ch = source[i];
  18. if (ch === '(') {
  19. parenDepth += 1;
  20. } else if (ch === ')') {
  21. parenDepth -= 1;
  22. if (parenDepth === 0) {
  23. signatureEnded = true;
  24. }
  25. } else if (ch === '{' && signatureEnded) {
  26. braceStart = i;
  27. break;
  28. }
  29. }
  30. if (braceStart < 0) {
  31. throw new Error(`missing body for function ${name}`);
  32. }
  33. let depth = 0;
  34. let end = braceStart;
  35. for (; end < source.length; end += 1) {
  36. const ch = source[end];
  37. if (ch === '{') depth += 1;
  38. if (ch === '}') {
  39. depth -= 1;
  40. if (depth === 0) {
  41. end += 1;
  42. break;
  43. }
  44. }
  45. }
  46. return source.slice(start, end);
  47. }
  48. const bundle = [
  49. extractFunction('isAddPhoneAuthUrl'),
  50. extractFunction('isAddPhoneAuthState'),
  51. extractFunction('getPostStep6AutoRestartDecision'),
  52. extractFunction('getNextActiveStep'),
  53. extractFunction('runAutoSequenceFromStep'),
  54. ].join('\n');
  55. test('auto-run restarts from step 1 with the same email after step 4 failure', async () => {
  56. const api = new Function(`
  57. const STEP_IDS = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  58. const AUTO_STEP_DELAYS = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0 };
  59. const LAST_STEP_ID = 9;
  60. const FINAL_OAUTH_CHAIN_START_STEP = null;
  61. const chrome = {
  62. tabs: {
  63. update: async () => {},
  64. },
  65. runtime: {
  66. sendMessage: async () => {},
  67. },
  68. };
  69. let remainingFailures = 1;
  70. let currentState = {
  71. email: 'keep@example.com',
  72. password: 'Secret123!',
  73. mailProvider: '163',
  74. stepStatuses: {
  75. 1: 'pending',
  76. 2: 'pending',
  77. 3: 'pending',
  78. 4: 'pending',
  79. 5: 'pending',
  80. 6: 'pending',
  81. 7: 'pending',
  82. 8: 'pending',
  83. 9: 'pending',
  84. },
  85. };
  86. const events = {
  87. steps: [],
  88. emails: [],
  89. invalidations: [],
  90. logs: [],
  91. setStateCalls: [],
  92. };
  93. async function addLog(message, level = 'info') {
  94. events.logs.push({ message, level });
  95. }
  96. async function ensureAutoEmailReady() {
  97. events.emails.push(currentState.email);
  98. return currentState.email;
  99. }
  100. async function broadcastAutoRunStatus() {}
  101. async function getState() {
  102. return currentState;
  103. }
  104. async function setState(updates) {
  105. currentState = {
  106. ...currentState,
  107. ...updates,
  108. stepStatuses: updates.stepStatuses ? { ...updates.stepStatuses } : currentState.stepStatuses,
  109. };
  110. events.setStateCalls.push(updates);
  111. }
  112. function isStopError(error) {
  113. return (error?.message || String(error || '')) === '流程已被用户停止。';
  114. }
  115. function isStepDoneStatus(status) {
  116. return status === 'completed' || status === 'manual_completed' || status === 'skipped';
  117. }
  118. async function executeStepAndWait(step) {
  119. events.steps.push(step);
  120. if (step === 4 && remainingFailures > 0) {
  121. remainingFailures -= 1;
  122. throw new Error('步骤 4 提交验证码前页面异常。');
  123. }
  124. }
  125. async function getTabId() {
  126. return 1;
  127. }
  128. async function invalidateDownstreamAfterStepRestart(step, options = {}) {
  129. events.invalidations.push({ step, options });
  130. currentState = {
  131. ...currentState,
  132. password: null,
  133. stepStatuses: {
  134. 1: currentState.stepStatuses[1] || 'completed',
  135. 2: 'pending',
  136. 3: 'pending',
  137. 4: 'pending',
  138. 5: 'pending',
  139. 6: 'pending',
  140. 7: 'pending',
  141. 8: 'pending',
  142. 9: 'pending',
  143. },
  144. };
  145. }
  146. function getLoginAuthStateLabel(state) {
  147. return state || 'unknown';
  148. }
  149. function getErrorMessage(error) {
  150. return error?.message || String(error || '');
  151. }
  152. async function getLoginAuthStateFromContent() {
  153. return { state: 'password_page', url: 'https://auth.openai.com/log-in' };
  154. }
  155. ${bundle}
  156. return {
  157. async run() {
  158. await runAutoSequenceFromStep(1, {
  159. targetRun: 1,
  160. totalRuns: 1,
  161. attemptRuns: 1,
  162. continued: false,
  163. });
  164. return { events, currentState };
  165. },
  166. };
  167. `)();
  168. const { events, currentState } = await api.run();
  169. assert.deepStrictEqual(events.invalidations, [
  170. {
  171. step: 1,
  172. options: {
  173. logLabel: '步骤 4 报错后准备回到步骤 1 沿用当前邮箱重试(第 1 次重开)',
  174. },
  175. },
  176. ]);
  177. assert.deepStrictEqual(events.emails, ['keep@example.com', 'keep@example.com']);
  178. assert.deepStrictEqual(events.steps, [1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
  179. assert.equal(currentState.email, 'keep@example.com');
  180. assert.equal(currentState.password, 'Secret123!');
  181. assert.equal(events.logs.some(({ message }) => /沿用当前邮箱回到步骤 1 重新开始/.test(message)), true);
  182. });