@jambonz/realtimedb-helpers
Version:
utility functions for querying jambonz redis database
186 lines (147 loc) • 5.89 kB
JavaScript
const test = require('tape').test ;
const config = require('config');
const opts = config.get('redis');
function sleep(secs) {
return new Promise((resolve) => {
setTimeout(resolve, secs * 1000);
});
}
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
test('calls tests', async(t) => {
const fn = require('..');
const {updateCallStatus, retrieveCall, deleteCall, listCalls, purgeCalls, client} = fn(opts);
//wait 1 sec for connection
//await sleep(1);
try {
let status = await updateCallStatus({
callId: 'xxxx',
sipStatus: 100,
callStatus: 'Trying'
}, 'http://127.0.0.1:3000');
t.ok(!status, 'fails to add new call when callSid is missing');
status = await updateCallStatus({
callSid: 'callSid-1',
callId: 'xxxx',
sipStatus: 100,
callStatus: 'Trying'
}, 'http://127.0.0.1:3000');
t.ok(!status, 'fails to add new call when accountSid is missing');
status = await updateCallStatus({
callSid: 'callSid-1',
sipStatus: 100,
callStatus: 'Trying'
}, 'http://127.0.0.1:3000');
t.ok(!status, 'fails to add new call when callId is missing');
status = await updateCallStatus({
callSid: 'callSid-1',
callId: 'xxxx',
callStatus: 'Trying'
}, 'http://127.0.0.1:3000');
t.ok(!status, 'fails to add new call when sipStatus is missing');
status = await updateCallStatus({
callSid: 'callSid-1',
callId: 'xxxx',
sipStatus: 100
}, 'http://127.0.0.1:3000');
t.ok(!status, 'fails to add new call when callStatus is missing');
status = await updateCallStatus({
callSid: 'callSid-1',
callId: 'xxxx',
sipStatus: 100,
callStatus: 'foobar'
}, 'http://127.0.0.1:3000');
t.ok(!status, 'fails to add new call when callStatus is invalid');
status = await updateCallStatus({
callSid: 'callSid-1',
callId: 'xxxx',
sipStatus: 100,
callStatus: 'trying'
});
t.ok(!status, 'fails to add new call when serviceUrl is not provided');
status = await updateCallStatus({
callSid: 'callSid-1',
accountSid: 'account-1',
callId: 'xxxx',
sipStatus: 100,
callStatus: 'trying'
}, 'http://127.0.0.1:3000');
t.ok(status, 'successfully adds new call when status is 100 Trying');
status = await updateCallStatus({
callSid: 'callSid-2',
accountSid: 'account-1',
applicationSid: null,
callId: 'xxxx',
sipStatus: 100,
callStatus: 'trying'
}, 'http://127.0.0.1:3000');
t.ok(status, 'successfully adds second new call');
status = await updateCallStatus({
callSid: 'callSid-3',
accountSid: 'account-1',
direction: 'inbound',
from: '111',
to: '222',
applicationSid: null,
callId: 'xxxx',
sipStatus: 100,
callStatus: 'completed'
}, 'http://127.0.0.1:3000');
t.ok(status, 'successfully adds third new call');
let callInfo = await retrieveCall('account-1', 'callSid-1');
t.ok(callInfo.callId === 'xxxx', 'successfully retrieves call');
callInfo = await retrieveCall('account-1', 'callSid-2');
t.ok(callInfo.callId === 'xxxx', 'successfully retrieves second call');
callInfo = await retrieveCall('account-1', 'callSid-X');
t.ok(!callInfo, 'fails to retrieve unknown call');
let calls = await listCalls('account-1');
t.ok(calls.length === 3, 'successfully listed all calls');
calls = await listCalls({ accountSid: 'account-1' });
t.ok(calls.length === 3, 'successfully listed all calls with opts');
calls = await listCalls({ accountSid: 'account-1', callStatus: 'completed' });
t.ok(calls.length === 1, 'successfully listed filtered completed calls');
calls = await listCalls({ accountSid: 'account-1', from: '111' });
t.ok(calls.length === 1, 'successfully listed filtered calls from "111"');
calls = await listCalls({ accountSid: 'account-1', to: '222' });
t.ok(calls.length === 1, 'successfully listed filtered calls to "222"');
calls = await listCalls({ accountSid: 'account-1', direction: 'inbound' });
t.ok(calls.length === 1, 'successfully listed filtered inbound calls');
let result = await deleteCall('account-1', 'callSid-2');
t.ok(result === true, 'successfully deleted callSid-2');
result = await deleteCall('account-1', 'callSid-2');
t.ok(result === false, 'failed to delete non-existent call sid');
let count = await purgeCalls();
t.ok(count === 0, 'no calls purged');
// force age out by removing key
count = await client.del(`call:account-1:callSid-3`);
t.ok(count === 1, 'forced call 3 to age out');
count = await purgeCalls();
t.ok(count === 1, '1 call purged');
await client.flushall();
for( let i = 0; i < 1000; i++) {
await updateCallStatus({
callSid: `callSid-${i}`,
accountSid: 'account-1',
callId: 'xxxx',
sipStatus: 100,
callStatus: 'trying'
}, 'http://127.0.0.1:3000');
}
t.pass('successfully added 1,000 calls');
count = await purgeCalls();
t.ok(count === 0, 'no calls purged');
/* commenting out for now, as it's not clear how to test this given the race condition where the first purge finishes before the second purge is called */
//const [res1, res2] = await Promise.all([purgeCalls(), new Promise(resolve => setTimeout(resolve, 10)).then(() => purgeCalls())]);
//t.ok(res1 === 0 && res2 == undefined, 'successfully handle simultaneous purgeCalls run');
calls = await listCalls('account-1');
t.ok(calls.length === 1000, 'successfully retrieved all 1,000 calls');
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});