obniz
Version:
obniz sdk for javascript
196 lines (141 loc) • 5.88 kB
JavaScript
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('switch', function() {
beforeEach(function(done) {
return testUtil.setupObnizPromise(this, done, { binary: true });
});
afterEach(function(done) {
return testUtil.releaseObnizePromise(this, done);
});
it('response test no.1', function() {
let responseBinaryString = '09 00 01 01';
let expectJson = [{ switch: { state: 'push' } }];
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('response test no.3', function() {
let responseBinaryString = '09 00 01 00';
let expectJson = [{ switch: { state: 'none' } }];
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('response test no.5', function() {
let responseBinaryString = '09 00 01 03';
let expectJson = [{ switch: { state: 'right' } }];
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('response test no.7', function() {
let responseBinaryString = '09 00 01 00';
let expectJson = [{ switch: { state: 'none' } }];
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('response test no.9', function() {
let responseBinaryString = '09 00 01 02';
let expectJson = [{ switch: { state: 'left' } }];
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('response test no.11', function() {
let responseBinaryString = '09 00 01 00';
let expectJson = [{ switch: { state: 'none' } }];
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('response test no.13', function() {
let responseBinaryString = '09 00 01 01';
let expectJson = [{ switch: { state: 'push' } }];
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('response test no.15', function() {
let responseBinaryString = '09 00 01 00';
let expectJson = [{ switch: { state: 'none' } }];
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.18', function() {
let requestJson = [{ switch: 'get' }];
let expecteBinaryStrings = ['09 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('response test no.19', function() {
let responseBinaryString = '09 01 01 00';
let expectJson = [{ switch: { state: 'none', action: 'get' } }];
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);
});
});