verification-stop-propagation.test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 VERIFICATION_POLL_MAX_ROUNDS = 5;
  59. const logs = [];
  60. let resendCalls = 0;
  61. function getHotmailVerificationPollConfig() {
  62. return {};
  63. }
  64. async function pollHotmailVerificationCode() {
  65. throw new Error('hotmail path should not run in this test');
  66. }
  67. async function pollLuckmailVerificationCode() {
  68. throw new Error('luckmail path should not run in this test');
  69. }
  70. function getVerificationCodeStateKey(step) {
  71. return step === 4 ? 'lastSignupCode' : 'lastLoginCode';
  72. }
  73. function getVerificationPollPayload(step, state, overrides = {}) {
  74. return {
  75. filterAfterTimestamp: 123,
  76. ...overrides,
  77. };
  78. }
  79. async function sendToMailContentScriptResilient() {
  80. throw new Error(STOP_ERROR_MESSAGE);
  81. }
  82. async function requestVerificationCodeResend() {
  83. resendCalls += 1;
  84. }
  85. async function addLog(message, level) {
  86. logs.push({ message, level });
  87. }
  88. ${bundle}
  89. return {
  90. pollFreshVerificationCode,
  91. snapshot() {
  92. return { logs, resendCalls };
  93. },
  94. };
  95. `)();
  96. let error = null;
  97. try {
  98. await api.pollFreshVerificationCode(7, {}, { provider: 'qq' }, {});
  99. } catch (err) {
  100. error = err;
  101. }
  102. const state = api.snapshot();
  103. assert.strictEqual(error?.message, '流程已被用户停止。', 'Stop 错误应原样向上抛出');
  104. assert.strictEqual(state.resendCalls, 0, 'Stop 后不应继续请求新的验证码');
  105. assert.deepStrictEqual(state.logs, [], 'Stop 后不应再记录普通失败或重试日志');
  106. }
  107. async function testResolveVerificationStepRethrowsStopFromFreshRequest() {
  108. const bundle = [
  109. extractFunction('isStopError'),
  110. extractFunction('resolveVerificationStep'),
  111. ].join('\n');
  112. const api = new Function(`
  113. const STOP_ERROR_MESSAGE = '流程已被用户停止。';
  114. const HOTMAIL_PROVIDER = 'hotmail-api';
  115. const LUCKMAIL_PROVIDER = 'luckmail-api';
  116. const logs = [];
  117. let pollCalls = 0;
  118. function getVerificationCodeStateKey(step) {
  119. return step === 4 ? 'lastSignupCode' : 'lastLoginCode';
  120. }
  121. function getHotmailVerificationPollConfig() {
  122. return {};
  123. }
  124. function getVerificationCodeLabel(step) {
  125. return step === 4 ? '注册' : '登录';
  126. }
  127. function isStep7RestartFromStep6Error() {
  128. return false;
  129. }
  130. async function requestVerificationCodeResend() {
  131. throw new Error(STOP_ERROR_MESSAGE);
  132. }
  133. async function addLog(message, level) {
  134. logs.push({ message, level });
  135. }
  136. async function pollFreshVerificationCode() {
  137. pollCalls += 1;
  138. return { code: '123456', emailTimestamp: Date.now() };
  139. }
  140. async function submitVerificationCode() {
  141. throw new Error('submit should not run in this test');
  142. }
  143. async function setState() {}
  144. async function completeStepFromBackground() {}
  145. ${bundle}
  146. return {
  147. resolveVerificationStep,
  148. snapshot() {
  149. return { logs, pollCalls };
  150. },
  151. };
  152. `)();
  153. let error = null;
  154. try {
  155. await api.resolveVerificationStep(7, {}, { provider: 'qq' }, { requestFreshCodeFirst: true });
  156. } catch (err) {
  157. error = err;
  158. }
  159. const state = api.snapshot();
  160. assert.strictEqual(error?.message, '流程已被用户停止。', '首次请求新验证码收到 Stop 后应立即终止');
  161. assert.strictEqual(state.pollCalls, 0, 'Stop 后不应继续进入邮箱轮询');
  162. assert.deepStrictEqual(state.logs, [], 'Stop 后不应追加降级日志');
  163. }
  164. (async () => {
  165. await testPollFreshVerificationCodeRethrowsStop();
  166. await testResolveVerificationStepRethrowsStopFromFreshRequest();
  167. console.log('verification stop propagation tests passed');
  168. })().catch((error) => {
  169. console.error(error);
  170. process.exit(1);
  171. });