background-step7-recovery.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/steps/fetch-login-code.js', 'utf8');
  5. const globalScope = {};
  6. const api = new Function('self', `${source}; return self.MultiPageBackgroundStep7;`)(globalScope);
  7. test('step 7 refreshes CPA oauth via step 6 replay before submitting verification code', async () => {
  8. const calls = {
  9. ensureReady: 0,
  10. executeStep6: [],
  11. sleep: [],
  12. resolveOptions: null,
  13. };
  14. const executor = api.createStep7Executor({
  15. addLog: async () => {},
  16. chrome: {
  17. tabs: {
  18. update: async () => {},
  19. },
  20. },
  21. CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email',
  22. confirmCustomVerificationStepBypass: async () => {},
  23. ensureStep7VerificationPageReady: async () => {
  24. calls.ensureReady += 1;
  25. return { state: 'verification_page' };
  26. },
  27. executeStep6: async (_state, options = {}) => {
  28. calls.executeStep6.push(options);
  29. },
  30. getMailConfig: () => ({
  31. provider: 'qq',
  32. label: 'QQ 邮箱',
  33. source: 'mail-qq',
  34. url: 'https://mail.qq.com',
  35. navigateOnReuse: false,
  36. }),
  37. getPanelMode: () => 'cpa',
  38. getState: async () => ({ email: 'user@example.com', password: 'secret' }),
  39. getTabId: async (sourceName) => (sourceName === 'signup-page' ? 1 : 2),
  40. HOTMAIL_PROVIDER: 'hotmail-api',
  41. isTabAlive: async () => true,
  42. isVerificationMailPollingError: () => false,
  43. LUCKMAIL_PROVIDER: 'luckmail-api',
  44. resolveVerificationStep: async (_step, _state, _mail, options) => {
  45. calls.resolveOptions = options;
  46. await options.beforeSubmit({ code: '654321' });
  47. },
  48. reuseOrCreateTab: async () => {},
  49. setState: async () => {},
  50. setStepStatus: async () => {},
  51. shouldSkipLoginVerificationForCpaCallback: () => false,
  52. shouldUseCustomRegistrationEmail: () => false,
  53. sleepWithStop: async (ms) => {
  54. calls.sleep.push(ms);
  55. },
  56. STANDARD_MAIL_VERIFICATION_RESEND_INTERVAL_MS: 25000,
  57. STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS: 8,
  58. throwIfStopped: () => {},
  59. });
  60. await executor.executeStep7({
  61. email: 'user@example.com',
  62. password: 'secret',
  63. oauthUrl: 'https://oauth.example/latest',
  64. });
  65. assert.equal(typeof calls.resolveOptions.beforeSubmit, 'function');
  66. assert.equal(calls.ensureReady, 2);
  67. assert.deepStrictEqual(calls.executeStep6, [{ skipPreLoginCleanup: true }]);
  68. assert.deepStrictEqual(calls.sleep, [1200]);
  69. assert.equal(calls.resolveOptions.resendIntervalMs, 25000);
  70. });
  71. test('step 7 disables resend interval for 2925 mailbox polling', async () => {
  72. let capturedOptions = null;
  73. const executor = api.createStep7Executor({
  74. addLog: async () => {},
  75. chrome: {
  76. tabs: {
  77. update: async () => {},
  78. },
  79. },
  80. CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email',
  81. confirmCustomVerificationStepBypass: async () => {},
  82. ensureStep7VerificationPageReady: async () => ({ state: 'verification_page' }),
  83. executeStep6: async () => {},
  84. getMailConfig: () => ({
  85. provider: '2925',
  86. label: '2925 邮箱',
  87. source: 'mail-2925',
  88. url: 'https://2925.com',
  89. navigateOnReuse: false,
  90. }),
  91. getPanelMode: () => 'sub2api',
  92. getState: async () => ({ email: 'user@example.com', password: 'secret' }),
  93. getTabId: async (sourceName) => (sourceName === 'signup-page' ? 1 : 2),
  94. HOTMAIL_PROVIDER: 'hotmail-api',
  95. isTabAlive: async () => true,
  96. isVerificationMailPollingError: () => false,
  97. LUCKMAIL_PROVIDER: 'luckmail-api',
  98. resolveVerificationStep: async (_step, _state, _mail, options) => {
  99. capturedOptions = options;
  100. },
  101. reuseOrCreateTab: async () => {},
  102. setState: async () => {},
  103. setStepStatus: async () => {},
  104. shouldSkipLoginVerificationForCpaCallback: () => false,
  105. shouldUseCustomRegistrationEmail: () => false,
  106. sleepWithStop: async () => {},
  107. STANDARD_MAIL_VERIFICATION_RESEND_INTERVAL_MS: 25000,
  108. STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS: 8,
  109. throwIfStopped: () => {},
  110. });
  111. await executor.executeStep7({
  112. email: 'user@example.com',
  113. password: 'secret',
  114. oauthUrl: 'https://oauth.example/latest',
  115. });
  116. assert.equal(capturedOptions.resendIntervalMs, 0);
  117. assert.equal(capturedOptions.beforeSubmit, undefined);
  118. });