verification-stop-propagation.test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. const assert = require('assert');
  2. const fs = require('fs');
  3. const source = fs.readFileSync('background.js', 'utf8');
  4. function extractFunction(name) {
  5. const markers = [`async function ${name}(`, `function ${name}(`];
  6. const start = markers
  7. .map(marker => source.indexOf(marker))
  8. .find(index => index >= 0);
  9. if (start < 0) {
  10. throw new Error(`missing function ${name}`);
  11. }
  12. let parenDepth = 0;
  13. let signatureEnded = false;
  14. let braceStart = -1;
  15. for (let i = start; i < source.length; i += 1) {
  16. const ch = source[i];
  17. if (ch === '(') {
  18. parenDepth += 1;
  19. } else if (ch === ')') {
  20. parenDepth -= 1;
  21. if (parenDepth === 0) {
  22. signatureEnded = true;
  23. }
  24. } else if (ch === '{' && signatureEnded) {
  25. braceStart = i;
  26. break;
  27. }
  28. }
  29. if (braceStart < 0) {
  30. throw new Error(`missing body for function ${name}`);
  31. }
  32. let depth = 0;
  33. let end = braceStart;
  34. for (; end < source.length; end += 1) {
  35. const ch = source[end];
  36. if (ch === '{') depth += 1;
  37. if (ch === '}') {
  38. depth -= 1;
  39. if (depth === 0) {
  40. end += 1;
  41. break;
  42. }
  43. }
  44. }
  45. return source.slice(start, end);
  46. }
  47. async function testPollFreshVerificationCodeRethrowsStop() {
  48. const bundle = [
  49. extractFunction('isStopError'),
  50. extractFunction('throwIfStopped'),
  51. extractFunction('pollFreshVerificationCode'),
  52. ].join('\n');
  53. const api = new Function(`
  54. let stopRequested = false;
  55. const STOP_ERROR_MESSAGE = '流程已被用户停止。';
  56. const HOTMAIL_PROVIDER = 'hotmail-api';
  57. const LUCKMAIL_PROVIDER = 'luckmail-api';
  58. const CLOUDFLARE_TEMP_EMAIL_PROVIDER = 'cloudflare-temp-email';
  59. const VERIFICATION_POLL_MAX_ROUNDS = 5;
  60. const logs = [];
  61. let resendCalls = 0;
  62. function getHotmailVerificationPollConfig() {
  63. return {};
  64. }
  65. async function pollHotmailVerificationCode() {
  66. throw new Error('hotmail path should not run in this test');
  67. }
  68. async function pollLuckmailVerificationCode() {
  69. throw new Error('luckmail path should not run in this test');
  70. }
  71. function getVerificationCodeStateKey(step) {
  72. return step === 4 ? 'lastSignupCode' : 'lastLoginCode';
  73. }
  74. function getVerificationPollPayload(step, state, overrides = {}) {
  75. return {
  76. filterAfterTimestamp: 123,
  77. ...overrides,
  78. };
  79. }
  80. async function sendToMailContentScriptResilient() {
  81. throw new Error(STOP_ERROR_MESSAGE);
  82. }
  83. async function requestVerificationCodeResend() {
  84. resendCalls += 1;
  85. }
  86. async function addLog(message, level) {
  87. logs.push({ message, level });
  88. }
  89. ${bundle}
  90. return {
  91. pollFreshVerificationCode,
  92. snapshot() {
  93. return { logs, resendCalls };
  94. },
  95. };
  96. `)();
  97. let error = null;
  98. try {
  99. await api.pollFreshVerificationCode(7, {}, { provider: 'qq' }, {});
  100. } catch (err) {
  101. error = err;
  102. }
  103. const state = api.snapshot();
  104. assert.strictEqual(error?.message, '流程已被用户停止。', 'Stop 错误应原样向上抛出');
  105. assert.strictEqual(state.resendCalls, 0, 'Stop 后不应继续请求新的验证码');
  106. assert.deepStrictEqual(state.logs, [], 'Stop 后不应再记录普通失败或重试日志');
  107. }
  108. async function testResolveVerificationStepRethrowsStopFromFreshRequest() {
  109. const bundle = [
  110. extractFunction('isStopError'),
  111. extractFunction('resolveVerificationStep'),
  112. ].join('\n');
  113. const api = new Function(`
  114. const STOP_ERROR_MESSAGE = '流程已被用户停止。';
  115. const HOTMAIL_PROVIDER = 'hotmail-api';
  116. const LUCKMAIL_PROVIDER = 'luckmail-api';
  117. const CLOUDFLARE_TEMP_EMAIL_PROVIDER = 'cloudflare-temp-email';
  118. const logs = [];
  119. let pollCalls = 0;
  120. function getVerificationCodeStateKey(step) {
  121. return step === 4 ? 'lastSignupCode' : 'lastLoginCode';
  122. }
  123. function getHotmailVerificationPollConfig() {
  124. return {};
  125. }
  126. function getVerificationCodeLabel(step) {
  127. return step === 4 ? '注册' : '登录';
  128. }
  129. function isStep7RestartFromStep6Error() {
  130. return false;
  131. }
  132. async function requestVerificationCodeResend() {
  133. throw new Error(STOP_ERROR_MESSAGE);
  134. }
  135. async function addLog(message, level) {
  136. logs.push({ message, level });
  137. }
  138. async function pollFreshVerificationCode() {
  139. pollCalls += 1;
  140. return { code: '123456', emailTimestamp: Date.now() };
  141. }
  142. async function submitVerificationCode() {
  143. throw new Error('submit should not run in this test');
  144. }
  145. async function setState() {}
  146. async function completeStepFromBackground() {}
  147. ${bundle}
  148. return {
  149. resolveVerificationStep,
  150. snapshot() {
  151. return { logs, pollCalls };
  152. },
  153. };
  154. `)();
  155. let error = null;
  156. try {
  157. await api.resolveVerificationStep(7, {}, { provider: 'qq' }, { requestFreshCodeFirst: true });
  158. } catch (err) {
  159. error = err;
  160. }
  161. const state = api.snapshot();
  162. assert.strictEqual(error?.message, '流程已被用户停止。', '首次请求新验证码收到 Stop 后应立即终止');
  163. assert.strictEqual(state.pollCalls, 0, 'Stop 后不应继续进入邮箱轮询');
  164. assert.deepStrictEqual(state.logs, [], 'Stop 后不应追加降级日志');
  165. }
  166. (async () => {
  167. await testPollFreshVerificationCodeRethrowsStop();
  168. await testResolveVerificationStepRethrowsStopFromFreshRequest();
  169. console.log('verification stop propagation tests passed');
  170. })().catch((error) => {
  171. console.error(error);
  172. process.exit(1);
  173. });