Skip to content

Commit

Permalink
Merge pull request #3 from sja/issue-2
Browse files Browse the repository at this point in the history
Issue 2
  • Loading branch information
sja authored Jan 15, 2017
2 parents 66d426a + 1a114d3 commit b0f3af3
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 20 deletions.
2 changes: 1 addition & 1 deletion io-package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"common": {
"name": "pilight",
"version": "0.5.0",
"version": "0.5.1",
"news": {
"0.5.0": {
"en": "initial adapter",
Expand Down
5 changes: 3 additions & 2 deletions lib/pilight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ module.exports = {
let result = [];
Object.keys(conf.values).forEach(function (propName) {
if (propName === 'timestamp') return;
let typeAndRole = TypeRoleMap[propName] || {};
result.push({
type: 'state',
common: {
valName: propName,
role: TypeRoleMap[propName].role || '',
type: TypeRoleMap[propName].type || '',
role: typeAndRole.role,
type: typeAndRole.type,
read: true,
write: propName === 'state'
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.pilight",
"version": "0.5.0",
"version": "0.5.1",
"description": "ioBroker pilight Adapter",
"author": {
"name": "Sebastian Janzen",
Expand Down
72 changes: 56 additions & 16 deletions test/testAdapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
"use strict";

var expect = require('chai').expect;
var setup = require(__dirname + '/lib/setup');

Expand All @@ -9,6 +11,21 @@ var onStateChanged = null;
var onObjectChanged = null;
var sendToID = 1;

const http = require('http');
const WebSocketServer = require('websocket').server;
let randomWebsocketPort;
const mockHttpServer = http
.createServer((req, res) => {
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>GOT REQ HERE!');
res.end();
})
.listen(0, 'localhost', (err) => {
if (err) console.error(err);
randomWebsocketPort = mockHttpServer.address().port;
console.log('Started Mock HTTP Server on random port ' + randomWebsocketPort);
});
const wsMockServer = new WebSocketServer({httpServer: mockHttpServer});

var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1);

function checkConnectionOfAdapter(cb, counter) {
Expand Down Expand Up @@ -74,6 +91,30 @@ function sendTo(target, command, message, callback) {
}

describe('Test ' + adapterShortName + ' adapter', function() {
let lastWsConnection = null;
let lastWsMessage = null;

wsMockServer.on('request', (req) => {
var connection = req.accept(null, req.origin);

if (lastWsConnection != null) {
console.log('Connection closed by test.', lastWsConnection);
lastWsConnection.close();
}
lastWsConnection = connection;

lastWsConnection.on('message', (msg) => {
if (msg.type === 'utf8') {
lastWsMessage = msg.utf8Data;
}
});

connection.on('close', function(connection) {
console.log('Connection closed.');
});

});

before('Test ' + adapterShortName + ' adapter: Start js-controller', function (_done) {
this.timeout(600000); // because of first install from npm

Expand All @@ -83,24 +124,21 @@ describe('Test ' + adapterShortName + ' adapter', function() {
config.common.enabled = true;
config.common.loglevel = 'debug';

//config.native.dbtype = 'sqlite';
config.native.port = randomWebsocketPort;

setup.setAdapterConfig(config.common, config.native);

setup.startController(true, function(id, obj) {}, function (id, state) {
setup.startController(true, function objChange(id, obj) {}, function stateChange(id, state) {
if (onStateChanged) onStateChanged(id, state);
},
function (_objects, _states) {
function cb(_objects, _states) {
objects = _objects;
states = _states;
_done();
});
});
});

/*
ENABLE THIS WHEN ADAPTER RUNS IN DEAMON MODE TO CHECK THAT IT HAS STARTED SUCCESSFULLY
*/
it('Test ' + adapterShortName + ' adapter: Check if adapter started', function (done) {
this.timeout(60000);
checkConnectionOfAdapter(function (res) {
Expand All @@ -118,23 +156,25 @@ describe('Test ' + adapterShortName + ' adapter', function() {
});
});
});
/**/

/*
PUT YOUR OWN TESTS HERE USING
it('Testname', function ( done) {
...

it(`Test ${adapterShortName} adapter: Check for established websocket connection`, function () {
expect(lastWsConnection).to.not.be.null;
expect(lastWsMessage).to.be.equal('{"action":"request values"}');
lastWsConnection.send('{}');
});

You can also use "sendTo" method to send messages to the started adapter
*/

after('Test ' + adapterShortName + ' adapter: Stop js-controller', function (done) {
this.timeout(10000);

setup.stopController(function (normalTerminated) {
console.log('Adapter normal terminated: ' + normalTerminated);
done();
if (lastWsConnection) {
lastWsConnection.close();
}
wsMockServer.shutDown();
mockHttpServer.close(() => done());
});

});

});
32 changes: 32 additions & 0 deletions test/testPackageFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
"use strict";

const expect = require('chai').expect;
const fs = require('fs');

describe('Test package.json and io-package.json', function() {
it('Test package files', function (done) {
let fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json');
let ioPackage = JSON.parse(fileContentIOPackage);

let fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json');
let npmPackage = JSON.parse(fileContentNPMPackage);

expect(ioPackage).to.be.an('object');
expect(npmPackage).to.be.an('object');

expect(ioPackage.common.version).to.exist;
expect(npmPackage.version).to.exist;

if (!expect(ioPackage.common.version).to.be.equal(npmPackage.version)) {
console.log('ERROR: Version numbers in package.json and io-package.json differ!!');
}

if (!ioPackage.common.news || !ioPackage.common.news[ioPackage.common.version]) {
console.log('WARNING: No news entry for current version exists in io-package.json, no rollback in Admin possible!');
}

done();
});
});

0 comments on commit b0f3af3

Please sign in to comment.