From 535744f2acf4993002d6a58bed58c3a489178e69 Mon Sep 17 00:00:00 2001 From: GermanBluefox Date: Fri, 6 Sep 2024 12:34:40 +0800 Subject: [PATCH] added legacy tests --- package-lock.json | 21 +++++++++ package.json | 3 +- test/legacy.js | 113 ++++++++++++++++++++++++++-------------------- test/simulate.js | 34 +++++++++----- 4 files changed, 108 insertions(+), 63 deletions(-) diff --git a/package-lock.json b/package-lock.json index 89c4b7b..6482339 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@alcalzone/release-script-plugin-license": "^3.7.0", "@alcalzone/release-script-plugin-manual-review": "^3.7.0", "@iobroker/adapter-dev": "^1.3.0", + "@iobroker/legacy-testing": "^1.0.13", "@iobroker/testing": "^4.1.3", "@tsconfig/node14": "^14.1.2", "@types/chai": "^4.3.19", @@ -1136,6 +1137,16 @@ "node": ">=12" } }, + "node_modules/@iobroker/legacy-testing": { + "version": "1.0.13", + "resolved": "https://registry.npmjs.org/@iobroker/legacy-testing/-/legacy-testing-1.0.13.tgz", + "integrity": "sha512-1ksISxpGyG8lKRRx6poe17otu0Ql5YculMNYXaE4mM4cF53eKv8y9JktDN2GIQan5+ieF/cREvwgQApOLWBUGw==", + "dev": true, + "dependencies": { + "chai": "^4.5.0", + "mocha": "^10.7.3" + } + }, "node_modules/@iobroker/testing": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/@iobroker/testing/-/testing-4.1.3.tgz", @@ -5546,6 +5557,16 @@ } } }, + "@iobroker/legacy-testing": { + "version": "1.0.13", + "resolved": "https://registry.npmjs.org/@iobroker/legacy-testing/-/legacy-testing-1.0.13.tgz", + "integrity": "sha512-1ksISxpGyG8lKRRx6poe17otu0Ql5YculMNYXaE4mM4cF53eKv8y9JktDN2GIQan5+ieF/cREvwgQApOLWBUGw==", + "dev": true, + "requires": { + "chai": "^4.5.0", + "mocha": "^10.7.3" + } + }, "@iobroker/testing": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/@iobroker/testing/-/testing-4.1.3.tgz", diff --git a/package.json b/package.json index ddd64c3..63e54f3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/legacy.js b/test/legacy.js index dc7596d..391f739 100644 --- a/test/legacy.js +++ b/test/legacy.js @@ -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'); @@ -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 @@ -58,6 +87,7 @@ describe(`Test ${adapterShortName} adapter`, function () { config.native.devices = [ { + type: 'LD382A', ip: '127.0.0.1', port: 5577, name: 'LD382A', @@ -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, @@ -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(); }); }); }); diff --git a/test/simulate.js b/test/simulate.js index 8a74995..5e936e8 100644 --- a/test/simulate.js +++ b/test/simulate.js @@ -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 => { @@ -66,14 +66,19 @@ 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'); }); @@ -81,7 +86,7 @@ server.on('connection', socket => { 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}"`); @@ -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(); }); }); @@ -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(); }