checkout-api-utils.test.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. function loadCheckoutApiUtils() {
  5. const source = fs.readFileSync('background/checkout-api-utils.js', 'utf8');
  6. const scope = {};
  7. new Function('self', `${source}; return self.MultiPageCheckoutApiUtils;`)(scope);
  8. return scope.MultiPageCheckoutApiUtils;
  9. }
  10. test('extractPaypalSmsCode reads code from json or plain sms text', () => {
  11. const api = loadCheckoutApiUtils();
  12. assert.equal(
  13. api.extractPaypalSmsCode(JSON.stringify({ msg: 'PayPal code: 482913. Do not share.' })),
  14. '482913'
  15. );
  16. assert.equal(
  17. api.extractPaypalSmsCode('暂无短信'),
  18. ''
  19. );
  20. assert.equal(
  21. api.extractPaypalSmsCode('旧码 111111 新码 222222', { excludeCodes: ['111111'] }),
  22. '222222'
  23. );
  24. assert.equal(
  25. api.extractPaypalSmsCode("yes|PayPal: 804999 is your security code. Don't share it.|(PayPal)|到期时间:2026-06-29 00:00:00"),
  26. '804999'
  27. );
  28. });
  29. test('fetchPaypalSmsCode polls until the sms code appears', async () => {
  30. const api = loadCheckoutApiUtils();
  31. const calls = [];
  32. const result = await api.fetchPaypalSmsCode({
  33. smsApiUrl: 'http://a.62-us.com/api/get_sms?key=test',
  34. maxAttempts: 2,
  35. intervalMs: 1,
  36. }, {
  37. fetchImpl: async (url) => {
  38. calls.push(url);
  39. return {
  40. ok: true,
  41. status: 200,
  42. text: async () => calls.length === 1 ? 'not ready' : '{"data":"PayPal verification code 654321"}',
  43. };
  44. },
  45. sleep: async () => {},
  46. });
  47. assert.equal(result.code, '654321');
  48. assert.equal(result.attempt, 2);
  49. assert.equal(calls.length, 2);
  50. });