verification-stop-propagation.test.js 4.9 KB

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