microsoft-email.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const {
  4. extractVerificationCodeFromMessages,
  5. fetchMicrosoftMailboxMessages,
  6. fetchMicrosoftVerificationCode,
  7. } = require('../microsoft-email.js');
  8. test('extractVerificationCodeFromMessages 只命中过滤时间后的 OpenAI 微软邮件', () => {
  9. const result = extractVerificationCodeFromMessages([
  10. {
  11. From: { EmailAddress: { Address: 'noreply@openai.com' } },
  12. Subject: 'Your code is 112233',
  13. BodyPreview: '112233',
  14. ReceivedDateTime: '2026-04-14T09:00:00.000Z',
  15. Id: 'too-old',
  16. },
  17. {
  18. From: { EmailAddress: { Address: 'alerts@example.com' } },
  19. Subject: 'Your code is 223344',
  20. BodyPreview: '223344',
  21. ReceivedDateTime: '2026-04-14T10:00:00.000Z',
  22. Id: 'wrong-sender',
  23. },
  24. {
  25. From: { EmailAddress: { Address: 'account-security@openai.com' } },
  26. Subject: 'OpenAI verification',
  27. BodyPreview: 'Use 334455 to continue',
  28. ReceivedDateTime: '2026-04-14T10:05:00.000Z',
  29. Id: 'matched',
  30. },
  31. ], {
  32. filterAfterTimestamp: Date.UTC(2026, 3, 14, 9, 30, 0),
  33. });
  34. assert.deepEqual(result, {
  35. code: '334455',
  36. emailTimestamp: Date.UTC(2026, 3, 14, 10, 5, 0),
  37. messageId: 'matched',
  38. sender: 'account-security@openai.com',
  39. subject: 'OpenAI verification',
  40. });
  41. });
  42. test('fetchMicrosoftMailboxMessages 使用 refresh token 拉取微软邮箱列表并返回新 token', async () => {
  43. const requests = [];
  44. const fetchImpl = async (url, options = {}) => {
  45. requests.push({ url, options });
  46. if (String(url).includes('/oauth2/v2.0/token')) {
  47. return {
  48. ok: true,
  49. json: async () => ({
  50. access_token: 'access-token-1',
  51. refresh_token: 'refresh-token-next',
  52. }),
  53. };
  54. }
  55. return {
  56. ok: true,
  57. json: async () => ({
  58. value: [
  59. {
  60. From: { EmailAddress: { Address: 'noreply@openai.com' } },
  61. Subject: 'OpenAI verification',
  62. BodyPreview: 'Use 445566 to continue',
  63. ReceivedDateTime: '2026-04-14T10:06:00.000Z',
  64. Id: 'mail-1',
  65. },
  66. ],
  67. }),
  68. };
  69. };
  70. const result = await fetchMicrosoftMailboxMessages({
  71. clientId: 'client-1',
  72. refreshToken: 'refresh-token-1',
  73. top: 5,
  74. fetchImpl,
  75. });
  76. assert.equal(requests.length, 2);
  77. assert.equal(result.nextRefreshToken, 'refresh-token-next');
  78. assert.equal(result.messages.length, 1);
  79. assert.equal(result.messages[0].id, 'mail-1');
  80. });
  81. test('fetchMicrosoftVerificationCode 会重试直到命中最新验证码', async () => {
  82. let messageRequestCount = 0;
  83. const logs = [];
  84. const fetchImpl = async (url) => {
  85. if (String(url).includes('/oauth2/v2.0/token')) {
  86. return {
  87. ok: true,
  88. json: async () => ({
  89. access_token: 'access-token-2',
  90. refresh_token: 'refresh-token-next-2',
  91. }),
  92. };
  93. }
  94. messageRequestCount += 1;
  95. if (messageRequestCount === 1) {
  96. return {
  97. ok: true,
  98. json: async () => ({
  99. value: [{
  100. From: { EmailAddress: { Address: 'alerts@example.com' } },
  101. Subject: 'Nothing useful',
  102. BodyPreview: 'No code',
  103. ReceivedDateTime: '2026-04-14T10:00:00.000Z',
  104. Id: 'mail-ignore',
  105. }],
  106. }),
  107. };
  108. }
  109. return {
  110. ok: true,
  111. json: async () => ({
  112. value: [{
  113. From: { EmailAddress: { Address: 'noreply@openai.com' } },
  114. Subject: 'Your verification code',
  115. BodyPreview: '667788',
  116. ReceivedDateTime: '2026-04-14T10:10:00.000Z',
  117. Id: 'mail-hit',
  118. }],
  119. }),
  120. };
  121. };
  122. const result = await fetchMicrosoftVerificationCode({
  123. token: 'refresh-token-2',
  124. clientId: 'client-2',
  125. maxRetries: 2,
  126. retryDelayMs: 0,
  127. fetchImpl,
  128. log: (message) => logs.push(message),
  129. filterAfterTimestamp: Date.UTC(2026, 3, 14, 9, 0, 0),
  130. });
  131. assert.equal(result.code, '667788');
  132. assert.equal(result.messageId, 'mail-hit');
  133. assert.equal(result.nextRefreshToken, 'refresh-token-next-2');
  134. assert.equal(logs.some((message) => /retrying/i.test(message)), true);
  135. });