obniz
Version:
obniz sdk for javascript
332 lines (263 loc) • 8.99 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('uart.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 = [{ uart0: { tx: 0, rx: 1, baud: 9600, bits: 7 } }];
let expecteBinaryStrings = [
'04 00 0d 00 00 01 00 00 25 80 01 07 00 00 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 = [{ uart0: null }];
let expecteBinaryStrings = ['04 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.2', function() {
let requestJson = [{ uart1: { tx: 0, rx: 1 } }];
let expecteBinaryStrings = [
'04 00 0d 01 00 01 00 01 C2 00 01 08 00 00 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.3', function() {
let requestJson = [{ uart1: { data: [17] } }];
let expecteBinaryStrings = ['04 02 02 01 11'];
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 = [{ uart1: { data: [17, 69, 68] } }];
let expecteBinaryStrings = ['04 02 04 01 11 45 44'];
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 = [{ uart0: { tx: 2, rx: 3 } }];
let expecteBinaryStrings = [
'04 00 0d 00 02 03 00 01 C2 00 01 08 00 00 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('response test no.6', function() {
let responseBinaryString = '04 03 0b 01 00 69 61 73 64 66 61 73 64 66';
let expectJson = [
{ uart1: { data: [0, 105, 97, 115, 100, 102, 97, 115, 100, 102] } },
];
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.7', function() {
let requestJson = [
{
uart0: {
tx: 0,
rx: 1,
baud: 115200,
stop: 1.5,
bits: 7,
parity: 'odd',
flowcontrol: 'rts',
rts: 10,
},
},
];
let expecteBinaryStrings = [
'04 00 0d 00 00 01 00 01 c2 00 02 07 03 02 0a 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.8', function() {
let requestJson = [
{
uart1: {
tx: 2,
rx: 3,
baud: 115200,
stop: 1.5,
bits: 7,
parity: 'odd',
flowcontrol: 'rts',
rts: 11,
},
},
];
let expecteBinaryStrings = [
'04 00 0d 01 02 03 00 01 c2 00 02 07 03 02 0b 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.9', function() {
let requestJson = [{ uart0: null }];
let expecteBinaryStrings = ['04 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.10', function() {
let requestJson = [{ uart1: null }];
let expecteBinaryStrings = ['04 01 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);
});
});