index.test.js 8.13 KB
import lib from '../../src';
import expect from 'expect.js';
import muk from 'muk';
import rewire from 'rewire';
import {
  ret, orderName, setRet, setOrderName, uhf as FakeUhf
}
from '../mock/fakeUhf';
import FakeConnector from '../mock/fakeConnector';
import FakeConfig from '../mock/fakeConfig';

describe('index.js', function() {
  let index;
  before(function() {
    index = muk('../../src', {
      './lib/connector': FakeConnector,
      './lib/directive/uhf': FakeUhf
    });
  });
  describe('open UHF', function() {
    it('open expect return', function() {
      expect(function() {
        lib.open(null, null, null);
      }).to.throwException('错误:ip,端口,回调必须要传');
      expect(function() {
        lib.open('1.1.1.1', null, null);
      }).to.throwException('错误:ip,端口,回调必须要传');
      expect(function() {
        lib.open('1.1.1.1', '123', null);
      }).to.throwException('错误:ip,端口,回调必须要传');
    });


    it('open expect success', function(done) {
      index.open('1.1.1.1', '123', function(err, data) {
        expect(data).to.be.a(FakeConnector);
        done();
      });
    });

    it('open expect error', function(done) {
      let val = {
        code: 0,
        message: 'fake error'
      };
      setRet(val);
      index.open('1.1.1.1', '123', function(err, data) {
        expect(err.message).to.be('fake error');
        done();
      });

    });
  });

  describe('start inventory', function() {
    it('start expect error', function() {
      expect(function() {
        index.start();
      }).to.throwException('错误:ip,端口,回调必须要传');

      expect(function() {
        index.start({
          'port': '123'
        });
      }).to.throwException('错误:ip,端口,回调必须要传');

    });

    it('start expect asyn error', function(done) {
      index.start({
        'port': '123',
        'host': '1.1.1.1'
      }, function(err, data) {
        expect(err.message).to.be('Error:灵敏度,定频,盘点时长,功率必须要传!')
        done();
      });
    });


    it('start epxect ok', function(done) {
      let val = {
        code: 1
      };


      setRet(val);

      let index = rewire("../../src");

      index.__set__('setFilter', function() {});
      index.__set__('setAntStayTime', function(connector, param) {
        process.nextTick(function() {
            connector.done('setAntStayTime', ret);
        });
      });
      index.__set__('setFrequencyRegion', function(connector, param) {
        process.nextTick(function() {
            connector.done('setFrequencyRegion', ret);
        });
      });
      index.__set__('setSpeedMode', function(connector, param) {
        process.nextTick(function() {
            connector.done('setSpeedMode', ret);
        });
      });
      index.__set__('setTagGroup', function(connector, param) {
        process.nextTick(function() {
            connector.done('setTagGroup', ret);
        });
      });
      index.__set__('setFrequency', function(connector, param) {
        process.nextTick(function() {
            connector.done('setFrequency', ret);
        });
      });
      index.__set__('setInventoryTime', function(connector, param) {
        process.nextTick(function() {
          connector.done('setInventoryTime', ret);
        });
      });

      index.__set__('setPower', function(connector, param) {
        process.nextTick(function() {
          connector.done('setPower', ret);
        });
      });
      let startIndex = 0;
      index.__set__('startInventory', function(connector, param) {
        process.nextTick(function() {
          if (startIndex > 2) {
            return;
          }
          connector.done('startInventory', {
            code: 2,
            data: [{
              epc: '123'
            }]
          });
          startIndex++;
        });
        process.nextTick(function() {
          if (startIndex > 2) {
            return;
          }
          connector.done('startInventory', {
            code: 1
          });
          startIndex++;
        });
      });

      index.__set__('Connector', FakeConnector);
      index.__set__('uhf', new FakeUhf());

      index.start({
          'port': '123',
          'host': '1.1.1.1',
          filter: { //设置灵敏度的过滤
            'on': '1',
            rssi: -60
          },
          frequency: 10,
          antStayTime: 100,
          inventoryTime: 400, //设置盘点时长
          power: [15, 0, 0, 0] //功率设置
        },
        function(err, connected, data) {
          expect(data).to.have.length(1);
          done();
        })

    });
  });

  describe('stop inventory', function() {
    it('stop ok', function() {
      expect(index.stop).to.be.ok();
    });
  });

  describe('procTagInfo', function() {
    it('procTagInfo exit proc', function() {
      let proc = rewire("../../src").__get__('procTagInfo');
      let countMap = {
        '1': 1
      };
      let rets = [{
        epc: '1',
        rssi: '2',
        updateTime: 1 * new Date(),
        state: 'on',
        ant: 1
      }];
      let data = [{
        epc: '1',
        rssi: '3',
        ant: 2
      }];
      rets = proc(rets, data, 1, '1.1.1.1');
      expect(rets[0].ant).to.be(2);
      expect(rets[0].rssi).to.be('3');
      expect(rets[0].epc).to.be('1');
      expect(rets).to.have.length(1);
    });

    it('procTagInfo noExist proc', function() {
      let proc = rewire("../../src").__get__('procTagInfo');
      let rets = proc([], [{
        epc: '1',
        rssi: '3',
        ant: 2
      }], 1, '1.1.1.1');

      expect(rets[0].speed).to.be(1);
      expect(rets[0].epc).to.be('1');
      expect(rets[0].rssi).to.be('3');
      expect(rets[0].ant).to.be(2);
      expect(rets).to.have.length(1);
    });

  });

  describe('compTags', function() {
    it('lastRets empty rets a record expect lastRets a record', function() {
      let compTags = rewire("../../src").__get__('compTags');
      let ret = compTags([], [{
        epc: '1',
        rssi: '3',
        ant: 2
      }], []);
      expect(ret.last).to.eql([{
        epc: '1',
        rssi: '3',
        ant: 2
      }]);
      expect(ret.gone).to.eql([]);
    });

    it('lastRets a record rets empty expect gone a record', function() {
      let compTags = rewire("../../src").__get__('compTags');
      let ret = compTags([{
        epc: '1',
        rssi: '3',
        speed: 2,
        ant: 2
      }], [], []);
      expect(ret.last).to.eql([{
        epc: '1',
        rssi: '3',
        speed: 2,
        ant: 2
      }]);
      expect(ret.gone).to.eql([{
        epc: '1',
        rssi: '3',
        speed: 2,
        ant: 2
      }]);
    });
  });

  describe('receive', function() {
    it('receive expect uhf and config and inventory ret', function() {
      let indexM = rewire("../../src");
      let expectClassNames = ['uhf', 'config', 'inventory'];

      for (let i = 0; i < expectClassNames.length; i++) {
        indexM.__set__('locateOrder', function() {
          return {
            val: {
              directive: expectClassNames[i]
            }
          }
        });
        indexM.__set__(expectClassNames[i], {
          receive: function() {
            return expectClassNames[i];
          }
        });
        let receive = indexM.__get__('receive');

        let retData = receive(true, {});

        expect(retData).to.be(expectClassNames[i]);
      }
    });
    it('receive param data null expect null', function() {
      let indexM = rewire("../../src");
      let retData = indexM.__get__('receive')(true, null);
      expect(retData).to.be(null);
    });

    it('receive param err  expect error', function() {
      let indexM = rewire("../../src");
      expect(function() {
        indexM.__get__('receive')(true, {}, new Error('mock error'));
      }).to.throwException(/mock error/);
    });
  });
  after(function() {
    muk.restore();
  });
});