UNPKG

obniz

Version:

obniz sdk for javascript

551 lines (442 loc) 14.9 kB
let chai = require('chai'); let expect = chai.expect; let testUtil = require(global.appRoot + '/test/testUtil.js'); chai.use(require('chai-like')); chai.use(testUtil.obnizAssert); describe('i2c.log', function() { beforeEach(function(done) { return testUtil.setupObnizPromise(this, done, { binary: true }); }); afterEach(function(done) { return testUtil.releaseObnizePromise(this, done); }); it('request test no.0', function() { let requestJson = [{ io0: false }]; let expecteBinaryStrings = ['02 00 02 00 00']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.1', function() { let requestJson = [{ io1: true }]; let expecteBinaryStrings = ['02 00 02 01 01']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.2', function() { let requestJson = [ { i2c0: { mode: 'master', sda: 4, scl: 3, clock: 400000 } }, ]; let expecteBinaryStrings = ['06 00 08 00 00 04 03 00 06 1a 80']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.3', function() { let requestJson = [{ i2c0: { address: 80, data: [0, 0, 1] } }]; let expecteBinaryStrings = ['06 02 06 00 00 50 00 00 01']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.4', function() { let requestJson = [{ system: { wait: 10 } }]; let expecteBinaryStrings = ['00 04 02 00 0a']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.5', function() { let requestJson = [{ i2c0: { address: 80, data: [0, 0] } }]; let expecteBinaryStrings = ['06 02 05 00 00 50 00 00']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.6', function() { let requestJson = [{ i2c0: { address: 80, read: 2 } }]; let expecteBinaryStrings = ['06 03 07 00 00 50 00 00 00 02']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('response test no.7', function() { let responseBinaryString = '06 03 05 00 00 50 01 f5'; let expectJson = [ { i2c0: { address: 80, data: [1, 245], mode: 'master' } }, ]; let binaryArray = responseBinaryString.split(' ').map(function(val, index) { return parseInt(val, 16); }); let binary = new Uint8Array(binaryArray); let json = this.obniz.binary2Json(binary); let isValidCommand = testUtil.isValidCommandResponseJson(json); expect(isValidCommand.valid).to.be.true; expect(json).to.be.deep.equal(expectJson); }); it('request test no.8', function() { let requestJson = [{ i2c0: { address: 80, read: 2 } }]; let expecteBinaryStrings = ['06 03 07 00 00 50 00 00 00 02']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('response test no.9', function() { let responseBinaryString = '06 03 05 00 00 50 ff ff'; let expectJson = [ { i2c0: { address: 80, data: [255, 255], mode: 'master' } }, ]; let binaryArray = responseBinaryString.split(' ').map(function(val, index) { return parseInt(val, 16); }); let binary = new Uint8Array(binaryArray); let json = this.obniz.binary2Json(binary); let isValidCommand = testUtil.isValidCommandResponseJson(json); expect(isValidCommand.valid).to.be.true; expect(json).to.be.deep.equal(expectJson); }); it('request test no.10', function() { let requestJson = [{ i2c0: null }]; let expecteBinaryStrings = ['06 01 01 00']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.11', function() { let requestJson = [ { i2c0: { mode: 'master', sda: 4, scl: 3, clock: 1000 } }, ]; let expecteBinaryStrings = ['06 00 08 00 00 04 03 00 00 03 e8']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.12', function() { let requestJson = [{ i2c0: { address: 592, data: [0, 0, 1] } }]; let expecteBinaryStrings = ['06 02 06 00 82 50 00 00 01']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.13', function() { let requestJson = [{ system: { wait: 10 } }]; let expecteBinaryStrings = ['00 04 02 00 0a']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.14', function() { let requestJson = [{ i2c0: { address: 592, data: [0, 0] } }]; let expecteBinaryStrings = ['06 02 05 00 82 50 00 00']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.15', function() { let requestJson = [{ i2c0: { address: 592, read: 2 } }]; let expecteBinaryStrings = ['06 03 07 00 82 50 00 00 00 02']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('request test no.16', function() { let requestJson = [ { i2c0: { sda: 0, scl: 1, mode: 'slave', slave_address: 1, slave_address_length: 7, }, }, ]; let expecteBinaryStrings = ['6 0 b 0 1 0 1 0 0 0 0 7 0 1']; expect(requestJson.length).to.be.equal(1); let isValidCommand = testUtil.isValidCommandRequestJson(requestJson); expect(isValidCommand.valid).to.be.true; let compress = this.obniz.constructor.WSCommand.compress( this.obniz.wscommands, requestJson[0] ); let binaryArray = expecteBinaryStrings .join(' ') .split(' ') .map(function(val, index) { return parseInt(val, 16); }); expect(binaryArray.length).to.be.above(2); let binary = new Uint8Array(binaryArray); expect(compress).to.be.deep.equal(binary); }); it('response test no.17', function() { let responseBinaryString = '6 4 40 44 0 7 0 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f'; let expectJson = [ { i2c0: { mode: 'slave', is_fragmented: true, address: 1, data: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, ], }, }, ]; let binaryArray = responseBinaryString.split(' ').map(function(val, index) { return parseInt(val, 16); }); let binary = new Uint8Array(binaryArray); let json = this.obniz.binary2Json(binary); let isValidCommand = testUtil.isValidCommandResponseJson(json); expect(isValidCommand.valid).to.be.true; expect(json).to.be.deep.equal(expectJson); }); });