modems.js 6.24 KB
/* eslint-disable no-console */
/* global describe it */
const should = require('should');
const modems = require('../lib/modems');

describe('#modems', () => {
    describe('#enabledModems', () => {
        it('should return correctly', () => {
            const test1 = modems.enabledModems([
                {
                    name: 'SENDER-AS13',
                    disabled: false,
                    outgoing: true,
                    imsi: null,
                    prefix: [],
                },
            ]);

            test1.should.ok();
        });
    });

    describe('#prefixes', () => {
        it('should return correctly', () => {
            const config = [
                {
                    name: 'XL1',
                    outgoing: true,
                    prefix: ['0818', '081'],
                },
                {
                    name: 'TSEL1',
                    outgoing: true,
                    prefix: ['0812', '0811'],
                },
                {
                    name: 'TSEL2',
                    outgoing: true,
                    prefix: ['0812', '0811'],
                },
                {
                    name: 'ISAT1',
                    outgoing: true,
                    prefix: ['0815', '0816'],
                },
            ];

            const prefixes = modems.prefixes(config);

            prefixes[0].prefix.should.equal('62811');
            prefixes[1].prefix.should.equal('62812');
            prefixes[2].prefix.should.equal('62815');
            prefixes[3].prefix.should.equal('62816');
            prefixes[4].prefix.should.equal('62818');
            prefixes[5].prefix.should.equal('6281');

            prefixes[0].modems[0].should.equal('TSEL1');
            prefixes[0].modems[1].should.equal('TSEL2');
            prefixes[1].modems[0].should.equal('TSEL1');
            prefixes[1].modems[1].should.equal('TSEL2');
            prefixes[2].modems[0].should.equal('ISAT1');
            prefixes[3].modems[0].should.equal('ISAT1');
            prefixes[4].modems[0].should.equal('XL1');
            prefixes[5].modems[0].should.equal('XL1');
        });
    });

    describe('#findPrefixForNumber', () => {
        it('should return correct prefix', () => {
            const config = [
                {
                    name: 'XL1',
                    outgoing: true,
                    prefix: ['0818', '081'],
                },
                {
                    name: 'TSEL1',
                    outgoing: true,
                    prefix: ['0812', '0811'],
                },
                {
                    name: 'TSEL2',
                    outgoing: true,
                    prefix: ['0812', '0811'],
                },
                {
                    name: 'ISAT1',
                    outgoing: true,
                    prefix: ['0815', '0816'],
                },
                {
                    name: 'FREE1',
                    outgoing: true,
                    prefix: [],
                },
                {
                    name: 'FREE2',
                    outgoing: true,
                    prefix: [],
                },
            ];

            const prefixes = modems.prefixes(config);
            modems.findPrefixForNumber('628188188', prefixes).prefix.should.equal('62818');
            modems.findPrefixForNumber('08188188', prefixes).prefix.should.equal('62818');
            modems.findPrefixForNumber('62812812', prefixes).prefix.should.equal('62812');
            modems.findPrefixForNumber('62810810', prefixes).prefix.should.equal('6281');
            should.not.exists(modems.findPrefixForNumber('6221212121', prefixes));
        });
    });

    describe('#randomModemByPrefix', () => {
        const config = [
            {
                name: 'XL1',
                outgoing: true,
                prefix: ['0818', '081'],
            },
            {
                name: 'TSEL1',
                outgoing: true,
                prefix: ['0812', '0811'],
            },
            {
                name: 'TSEL2',
                outgoing: true,
                prefix: ['0812', '0811'],
            },
            {
                name: 'ISAT1',
                outgoing: true,
                prefix: ['0815', '0816'],
            },
            {
                name: 'ISAT2',
                outgoing: false,
                prefix: ['0815', '0816'],
            },
            {
                name: 'FREE1',
                outgoing: true,
                prefix: [],
            },
            {
                name: 'FREE2',
                outgoing: true,
                prefix: [],
            },
        ];

        const loopCount = 100;

        it('should return correctly for XL', () => {
            for (let i = 0; i < loopCount; i += 1) {
                modems.randomModemByPrefix('6281881818', config).name.should.equal('XL1');
            }
        });

        it('should return correctly for TSEL', () => {
            for (let i = 0; i < loopCount; i += 1) {
                modems.randomModemByPrefix('62812812', config).name.should.equalOneOf('TSEL1', 'TSEL2');
            }
        });

        it('should return correctly for INDOSAT', () => {
            for (let i = 0; i < loopCount; i += 1) {
                modems.randomModemByPrefix('62815815', config).name.should.equal('ISAT1');
            }
        });

        it('should return correctly for UNKNOWN PREFIX', () => {
            for (let i = 0; i < loopCount; i += 1) {
                modems.randomModemByPrefix('628888888', config).name.should.equalOneOf('FREE1', 'FREE2');
            }
        });

        it('should handle UNKNOWN PREFIX without free modem', () => {
            // eslint-disable-next-line no-shadow
            const config = [
                {
                    name: 'XL1',
                    outgoing: true,
                    prefix: ['0818', '081'],
                },
                {
                    name: 'FREE1',
                    outgoing: false,
                    prefix: [],
                },
                {
                    name: 'FREE2',
                    outgoing: false,
                    prefix: [],
                },
            ];

            should.not.exists(modems.randomModemByPrefix('628888888', config));
        });
    });
});