feat: add Luban SMS provider
This commit is contained in:
+91
-1
@@ -25,7 +25,7 @@ function load(extra = {}) {
|
||||
Set,
|
||||
...extra,
|
||||
};
|
||||
const fn = new Function('globalThis', 'self', `with (globalThis) { ${source}; return { collectPriceEntries, filterPriceEntries, normalizeActivation, extractVerificationCode, isPhoneNumberUsedError, isPhoneNumberDeliveryRefusedError, getPhoneReplacementReleaseAction, fetchNextPhone, normalizeSettings, buildUrl }; }`);
|
||||
const fn = new Function('globalThis', 'self', `with (globalThis) { ${source}; return { collectPriceEntries, filterPriceEntries, normalizeActivation, extractVerificationCode, isPhoneNumberUsedError, isPhoneNumberDeliveryRefusedError, getPhoneReplacementReleaseAction, fetchNextPhone, normalizeSettings, buildUrl, fetchActiveCode }; }`);
|
||||
return fn(sandbox, sandbox);
|
||||
}
|
||||
test('collectPriceEntries parses keyed price inventory', () => {
|
||||
@@ -112,3 +112,93 @@ test('fetchNextPhone releases previous activation then acquires replacement', as
|
||||
['getNumberV2', null, null, '6'],
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
test('buildUrl uses Luban apikey parameter and base endpoint', () => {
|
||||
const api = load();
|
||||
const url = new URL(api.buildUrl(
|
||||
{ provider: 'luban-sms', lubanBaseUrl: 'https://lubansms.com/v2/api/getNumber', apiKey: 'luban-key' },
|
||||
{ service_id: '121949' }
|
||||
));
|
||||
assert.equal(url.searchParams.get('apikey'), 'luban-key');
|
||||
assert.equal(url.searchParams.get('api_key'), null);
|
||||
assert.equal(url.searchParams.get('service_id'), '121949');
|
||||
});
|
||||
|
||||
test('normalizeActivation parses Luban getNumber response', () => {
|
||||
const api = load();
|
||||
const activation = api.normalizeActivation(
|
||||
{ code: 0, msg: '', number: '79781901206', request_id: 230698 },
|
||||
{ provider: 'luban-sms', serviceCode: '121949' }
|
||||
);
|
||||
assert.equal(activation.provider, 'luban-sms');
|
||||
assert.equal(activation.activationId, '230698');
|
||||
assert.equal(activation.phoneNumber, '79781901206');
|
||||
assert.equal(activation.serviceCode, '121949');
|
||||
});
|
||||
|
||||
test('fetchNextPhone acquires Luban number and rejects previous request', async () => {
|
||||
const requests = [];
|
||||
const stored = {
|
||||
provider: 'luban-sms',
|
||||
apiKey: 'luban-key',
|
||||
lubanBaseUrl: 'https://lubansms.com/v2/api/getNumber',
|
||||
lubanServiceId: '121949',
|
||||
activeActivation: { activationId: 'old-request', phoneNumber: '79780000000', provider: 'luban-sms' },
|
||||
};
|
||||
const api = load({
|
||||
chrome: {
|
||||
action: { onClicked: { addListener() {} } },
|
||||
tabs: { async sendMessage() {} },
|
||||
scripting: { async executeScript() {} },
|
||||
runtime: { onMessage: { addListener() {} } },
|
||||
storage: { local: { async get() { return stored; }, async set(update) { Object.assign(stored, update); } } }
|
||||
},
|
||||
fetch: async (url) => {
|
||||
const parsed = new URL(url);
|
||||
requests.push([parsed.pathname, parsed.searchParams.get('apikey'), parsed.searchParams.get('request_id'), parsed.searchParams.get('status'), parsed.searchParams.get('service_id')]);
|
||||
if (parsed.pathname.endsWith('/setStatus')) return new Response(JSON.stringify({ code: 0, msg: 'success' }), { status: 200 });
|
||||
if (parsed.pathname.endsWith('/List')) return new Response(JSON.stringify({ code: 0, msg: [] }), { status: 200 });
|
||||
if (parsed.pathname.endsWith('/getNumber')) return new Response(JSON.stringify({ code: 0, msg: '', number: '79781901206', request_id: 230698 }), { status: 200 });
|
||||
throw new Error(`unexpected ${url}`);
|
||||
},
|
||||
Response,
|
||||
});
|
||||
const result = await api.fetchNextPhone({ reason: 'manual_replace', releaseAction: 'cancel' });
|
||||
assert.equal(result.activation.provider, 'luban-sms');
|
||||
assert.equal(result.activation.activationId, '230698');
|
||||
assert.equal(stored.activeActivation.previousActivationId, 'old-request');
|
||||
assert.deepEqual(requests, [
|
||||
['/v2/api/setStatus', 'luban-key', 'old-request', 'reject', null],
|
||||
['/v2/api/List', 'luban-key', null, null, null],
|
||||
['/v2/api/getNumber', 'luban-key', null, null, '121949'],
|
||||
]);
|
||||
});
|
||||
|
||||
test('fetchActiveCode parses Luban success SMS code', async () => {
|
||||
const stored = {
|
||||
provider: 'luban-sms',
|
||||
apiKey: 'luban-key',
|
||||
lubanBaseUrl: 'https://lubansms.com/v2/api/getSms',
|
||||
activeActivation: { activationId: '230698', phoneNumber: '79781901206', provider: 'luban-sms' },
|
||||
};
|
||||
const api = load({
|
||||
chrome: {
|
||||
action: { onClicked: { addListener() {} } },
|
||||
tabs: { async sendMessage() {} },
|
||||
scripting: { async executeScript() {} },
|
||||
runtime: { onMessage: { addListener() {} } },
|
||||
storage: { local: { async get() { return stored; }, async set(update) { Object.assign(stored, update); } } }
|
||||
},
|
||||
fetch: async (url) => {
|
||||
const parsed = new URL(url);
|
||||
assert.equal(parsed.pathname, '/v2/api/getSms');
|
||||
assert.equal(parsed.searchParams.get('request_id'), '230698');
|
||||
return new Response(JSON.stringify({ code: 0, msg: 'success', sms_code: '380682' }), { status: 200 });
|
||||
},
|
||||
Response,
|
||||
});
|
||||
const result = await api.fetchActiveCode();
|
||||
assert.equal(result.code, '380682');
|
||||
assert.equal(stored.activeActivation.lastCode, '380682');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user