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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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('runAutoSequenceFromStep'),
  53. ].join('\n');
  54. test('auto-run restarts from step 1 with the same email after step 4 failure', async () => {
  55. const api = new Function(`
  56. const AUTO_STEP_DELAYS = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0 };
  57. const LAST_STEP_ID = 10;
  58. const FINAL_OAUTH_CHAIN_START_STEP = 7;
  59. const chrome = {
  60. tabs: {
  61. update: async () => {},
  62. },
  63. runtime: {
  64. sendMessage: async () => {},
  65. },
  66. };
  67. let remainingFailures = 1;
  68. let currentState = {
  69. email: 'keep@example.com',
  70. password: 'Secret123!',
  71. mailProvider: '163',
  72. stepStatuses: {
  73. 1: 'pending',
  74. 2: 'pending',
  75. 3: 'pending',
  76. 4: 'pending',
  77. 5: 'pending',
  78. 6: 'pending',
  79. 7: 'pending',
  80. 8: 'pending',
  81. 9: 'pending',
  82. 10: 'pending',
  83. },
  84. };
  85. const events = {
  86. steps: [],
  87. emails: [],
  88. invalidations: [],
  89. logs: [],
  90. setStateCalls: [],
  91. };
  92. async function addLog(message, level = 'info') {
  93. events.logs.push({ message, level });
  94. }
  95. async function ensureAutoEmailReady() {
  96. events.emails.push(currentState.email);
  97. return currentState.email;
  98. }
  99. async function broadcastAutoRunStatus() {}
  100. async function getState() {
  101. return currentState;
  102. }
  103. async function setState(updates) {
  104. currentState = {
  105. ...currentState,
  106. ...updates,
  107. stepStatuses: updates.stepStatuses ? { ...updates.stepStatuses } : currentState.stepStatuses,
  108. };
  109. events.setStateCalls.push(updates);
  110. }
  111. function isStopError(error) {
  112. return (error?.message || String(error || '')) === '流程已被用户停止。';
  113. }
  114. function isStepDoneStatus(status) {
  115. return status === 'completed' || status === 'manual_completed' || status === 'skipped';
  116. }
  117. async function executeStepAndWait(step) {
  118. events.steps.push(step);
  119. if (step === 4 && remainingFailures > 0) {
  120. remainingFailures -= 1;
  121. throw new Error('步骤 4 提交验证码前页面异常。');
  122. }
  123. }
  124. async function getTabId() {
  125. return 1;
  126. }
  127. function shouldSkipLoginVerificationForCpaCallback() {
  128. return false;
  129. }
  130. async function invalidateDownstreamAfterStepRestart(step, options = {}) {
  131. events.invalidations.push({ step, options });
  132. currentState = {
  133. ...currentState,
  134. password: null,
  135. stepStatuses: {
  136. 1: currentState.stepStatuses[1] || 'completed',
  137. 2: 'pending',
  138. 3: 'pending',
  139. 4: 'pending',
  140. 5: 'pending',
  141. 6: 'pending',
  142. 7: 'pending',
  143. 8: 'pending',
  144. 9: 'pending',
  145. 10: 'pending',
  146. },
  147. };
  148. }
  149. function getLoginAuthStateLabel(state) {
  150. return state || 'unknown';
  151. }
  152. function getErrorMessage(error) {
  153. return error?.message || String(error || '');
  154. }
  155. async function getLoginAuthStateFromContent() {
  156. return { state: 'password_page', url: 'https://auth.openai.com/log-in' };
  157. }
  158. ${bundle}
  159. return {
  160. async run() {
  161. await runAutoSequenceFromStep(1, {
  162. targetRun: 1,
  163. totalRuns: 1,
  164. attemptRuns: 1,
  165. continued: false,
  166. });
  167. return { events, currentState };
  168. },
  169. };
  170. `)();
  171. const { events, currentState } = await api.run();
  172. assert.deepStrictEqual(events.invalidations, [
  173. {
  174. step: 1,
  175. options: {
  176. logLabel: '步骤 4 报错后准备回到步骤 1 沿用当前邮箱重试(第 1 次重开)',
  177. },
  178. },
  179. ]);
  180. assert.deepStrictEqual(events.emails, ['keep@example.com', 'keep@example.com']);
  181. assert.deepStrictEqual(events.steps, [1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  182. assert.equal(currentState.email, 'keep@example.com');
  183. assert.equal(currentState.password, 'Secret123!');
  184. assert.equal(events.logs.some(({ message }) => /沿用当前邮箱回到步骤 1 重新开始/.test(message)), true);
  185. });