const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); function loadCheckoutApiUtils() { const source = fs.readFileSync('background/checkout-api-utils.js', 'utf8'); const scope = {}; new Function('self', `${source}; return self.MultiPageCheckoutApiUtils;`)(scope); return scope.MultiPageCheckoutApiUtils; } test('extractPaypalSmsCode reads code from json or plain sms text', () => { const api = loadCheckoutApiUtils(); assert.equal( api.extractPaypalSmsCode(JSON.stringify({ msg: 'PayPal code: 482913. Do not share.' })), '482913' ); assert.equal( api.extractPaypalSmsCode('暂无短信'), '' ); assert.equal( api.extractPaypalSmsCode('旧码 111111 新码 222222', { excludeCodes: ['111111'] }), '222222' ); assert.equal( api.extractPaypalSmsCode("yes|PayPal: 804999 is your security code. Don't share it.|(PayPal)|到期时间:2026-06-29 00:00:00"), '804999' ); }); test('fetchPaypalSmsCode polls until the sms code appears', async () => { const api = loadCheckoutApiUtils(); const calls = []; const result = await api.fetchPaypalSmsCode({ smsApiUrl: 'http://a.62-us.com/api/get_sms?key=test', maxAttempts: 2, intervalMs: 1, }, { fetchImpl: async (url) => { calls.push(url); return { ok: true, status: 200, text: async () => calls.length === 1 ? 'not ready' : '{"data":"PayPal verification code 654321"}', }; }, sleep: async () => {}, }); assert.equal(result.code, '654321'); assert.equal(result.attempt, 2); assert.equal(calls.length, 2); });