Skip to content

Commit

Permalink
added legacy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Sep 6, 2024
1 parent 3af0bef commit 535744f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 63 deletions.
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@
"main.js"
],
"scripts": {
"test:js": "mocha --config test/mocharc.custom.json \"{!(node_modules|test)/**/*.test.js,*.test.js,test/**/test!(PackageFiles|Startup).js}\"",
"test:package": "mocha test/package --exit",
"test:integration": "mocha test/integration --exit",
"test:legacy": "mocha test/legacy --exit",
"test": "npm run test:js && npm run test:package",
"test": "npm run test:integration && npm run test:package && npm run test:legacy",
"check": "tsc --noEmit -p tsconfig.check.json",
"lint": "eslint .",
"translate": "translate-adapter",
Expand Down
113 changes: 64 additions & 49 deletions test/legacy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
const expect = require('chai').expect;
const setup = require('@iobroker/legacy-testing');
const { startServer, stopServer } = require('./simulate');
Expand Down Expand Up @@ -46,6 +43,38 @@ function checkValueOfState(id, value, cb, counter) {
});
}

function checkValueOfStateAsync(id, value) {
return new Promise((resolve, reject) =>
checkValueOfState(id, value, error =>
error ? reject(error) : resolve()));
}

function checkValueOfStateAcknowledged(id, value, cb, counter) {
counter = counter || 0;
if (counter > 20) {
return cb && cb(`Cannot check value Of State ${id}`);
}

states.getState(id, (err, state) => {
err && console.error(err);
if (value === null && !state) {
cb && cb();
} else if (state && state.ack && (value === undefined || state.val === value)) {
cb && cb();
} else {
setTimeout(() => checkValueOfState(id, value, cb, counter + 1), 500);
}
});
}

function checkValueOfStateAcknowledgedAsync(id, value) {
return new Promise((resolve, reject) =>
checkValueOfStateAcknowledged(id, value, error =>
error ? reject(error) : resolve()));
}

const deviceState = { r: 10, g: 20, b: 30, w: 50, progOn: false, on: true };

describe(`Test ${adapterShortName} adapter`, function () {
before(`Test ${adapterShortName} adapter: Start js-controller`, function (_done) {
this.timeout(600000); // because of first install from npm
Expand All @@ -58,6 +87,7 @@ describe(`Test ${adapterShortName} adapter`, function () {

config.native.devices = [
{
type: 'LD382A',
ip: '127.0.0.1',
port: 5577,
name: 'LD382A',
Expand All @@ -67,7 +97,8 @@ describe(`Test ${adapterShortName} adapter`, function () {

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

startServer({ r: 10, g: 20, b: 30, w: 50, progOn: false, on: true })
// start wifilight device with predefined values
startServer(deviceState)
.then(() => {
setup.startController(
true,
Expand All @@ -83,60 +114,44 @@ describe(`Test ${adapterShortName} adapter`, function () {
});

it(`Test ${adapterShortName} adapter: Check if adapter started`, function (done) {
this.timeout(60000);
checkConnectionOfAdapter(res => {
res && console.log(res);
expect(res).not.to.be.equal('Cannot check connection');
objects.setObject('system.adapter.test.0', {
common: {

},
type: 'instance'
},
() => {
states.subscribeMessage('system.adapter.test.0');
done();
});
done();
});
});

it(`Test ${adapterShortName} adapter: Check predefined states`, function (done) {
this.timeout(10000);
checkValueOfState(`${adapterShortName}.0.127_0_0_1.0.r`, 10, () => {
checkValueOfState(`${adapterShortName}.0.127_0_0_1.0.g`, 20, () => {
checkValueOfState(`${adapterShortName}.0.127_0_0_1.0.b`, 30, () => {
checkValueOfState(`${adapterShortName}.0.127_0_0_1.0.w`, 50, () => {
checkValueOfState(`${adapterShortName}.0.127_0_0_1.0.progOn`, false, () => {
checkValueOfState(`${adapterShortName}.0.127_0_0_1.0.on`, true, () => {
done();
});
});
});
});
});
});
});

it(`Test ${adapterShortName} adapter: test control`, function (done) {
this.timeout(10000);
onStateChanged = (id, state) => {
if (id === `${adapterShortName}.0.127_0_0_1.0.r`) {
expect(state.val).to.be.equal(20);
done();
}
};
states.setState(`${adapterShortName}.0.127_0_0_1.0.r`, 20, true);
});
}).timeout(60000);

it(`Test ${adapterShortName} adapter: Check predefined states`, async () => {
await checkValueOfStateAsync(`${adapterShortName}.0.127_0_0_1.r`, deviceState.r);
await checkValueOfStateAsync(`${adapterShortName}.0.127_0_0_1.g`, deviceState.g);
await checkValueOfStateAsync(`${adapterShortName}.0.127_0_0_1.b`, deviceState.b);
await checkValueOfStateAsync(`${adapterShortName}.0.127_0_0_1.w`, deviceState.w);
await checkValueOfStateAsync(`${adapterShortName}.0.127_0_0_1.progOn`, deviceState.progOn);
await checkValueOfStateAsync(`${adapterShortName}.0.127_0_0_1.on`, deviceState.on);
await checkValueOfStateAsync(`${adapterShortName}.0.127_0_0_1.reachable`, true);
}).timeout(10000);

it(`Test ${adapterShortName} adapter: test control`, async () => {
deviceState.r = 80;
states.setState(`${adapterShortName}.0.127_0_0_1.r`, deviceState.r, true);
await checkValueOfStateAcknowledgedAsync(`${adapterShortName}.0.127_0_0_1.r`, deviceState.r);
await checkValueOfStateAcknowledgedAsync(
`${adapterShortName}.0.127_0_0_1.rgb`,
`#${deviceState.r.toString(16).padStart(2, '0')}${deviceState.g.toString(16).padStart(2, '0')}${deviceState.b.toString(16).padStart(2, '0')}${deviceState.w.toString(16).padStart(2, '0')}`,
);
}).timeout(10000);

it(`Test ${adapterShortName} adapter: test reachable`, async () => {
await stopServer();
checkValueOfStateAcknowledged(`${adapterShortName}.0.127_0_0_1.reachable`, false);
}).timeout(10000);

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

setup.stopController(normalTerminated => {
stopServer()
.then(() => {
console.log(`Adapter normal terminated: ${normalTerminated}`);
done();
});
console.log(`Adapter normal terminated: ${normalTerminated}`);
done();
});
});
});
34 changes: 22 additions & 12 deletions test/simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ const devices = {
MiLightRGB,
};

function string2Buffer(str) {
const arr = str.split(' ');
const buffer = Buffer.alloc(arr.length);
for (let i = 0; i < arr.length; i++) {
buffer[i] = parseInt(arr[i], 16);
}
return buffer;
}
// function string2Buffer(str) {
// const arr = str.split(' ');
// const buffer = Buffer.alloc(arr.length);
// for (let i = 0; i < arr.length; i++) {
// buffer[i] = parseInt(arr[i], 16);
// }
// return buffer;
// }

function findCommand(device, receivedData) {
return Object.keys(device).find(key => {
Expand Down Expand Up @@ -66,22 +66,27 @@ const state = {
progNo: 1,
speed: 5,
progOn: false,
}
};
const sockets = {};
let nextSocketId = 0;

// Create a TCP server
const server = net.Server();
server.on('connection', socket => {
console.log('Client connected');
const socketId = nextSocketId++;
console.log('Client connected with id', socketId);
sockets[nextSocketId] = socket;

socket.on('close', () => {
delete sockets[nextSocketId];
console.log('Client disconnected');
});

socket.on('data', data => {
console.log(`Client data: ${data.map(byte => byte.toString(16)).join(' ')}`);
// find command
let cmd;
const dev = Object.keys(devices).find(device => {
Object.keys(devices).find(device => {
cmd = findCommand(devices[device], data);
if (cmd) {
console.log(`Found command for ${device}: "${cmd}"`);
Expand Down Expand Up @@ -142,7 +147,7 @@ async function startServer(defaultState) {
Object.assign(state, defaultState);
}
server.listen(5577, '127.0.0.1', () => {
console.log('Server started');
console.log(`Server started on port 5577: ${JSON.stringify(state)}`);
resolve();
});
});
Expand All @@ -155,6 +160,11 @@ async function stopServer() {
console.log('Server closed');
resolve();
});
// Destroy all open sockets
for (const socketId in sockets) {
console.log('socket', socketId, 'destroyed');
sockets[socketId].destroy();
}
} else {
resolve();
}
Expand Down

0 comments on commit 535744f

Please sign in to comment.