verification-stop-propagation.test.js 4.8 KB

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