From 792d13ab9c6779788041b1cedb1974cbd19f3703 Mon Sep 17 00:00:00 2001 From: Garfonso Date: Thu, 23 May 2024 12:16:21 +0200 Subject: [PATCH] fix tests and make them more robust --- io-package.json | 12 ++++++++++++ lib/server.js | 6 +++++- test/integrationTests/objects_change_tests.js | 4 ++-- test/integrationTests/testTools.js | 13 +++++++------ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/io-package.json b/io-package.json index 1f591f2a5..046d95f29 100644 --- a/io-package.json +++ b/io-package.json @@ -308,6 +308,18 @@ "def": false } }, + { + "_id": "info.configUpdateProcessed", + "type": "state", + "common": { + "name": "If a config update was processed", + "type": "boolean", + "read": true, + "write": false, + "role": "state", + "def": false + } + }, { "_id": "info.readyForClients", "type": "state", diff --git a/lib/server.js b/lib/server.js index c8ade67a4..7c06befaa 100644 --- a/lib/server.js +++ b/lib/server.js @@ -305,6 +305,7 @@ class WebServer { } await this._getAllStates(); await this._manageSubscribesFromConfig(); + this.log.debug('entitiesUpdated for startup.'); await this.adapter.setStateAsync('info.entitiesUpdated', true, true); } @@ -2679,7 +2680,8 @@ class WebServer { if (id === `${this.adapter.namespace}.configuration`) { this._lovelaceConfig = obj.native; await this._manageSubscribesFromConfig(); - await this.adapter.setStateAsync('info.entitiesUpdated', true, true); + this.log.debug(`configUpdateProcessed for config.`); + await this.adapter.setStateAsync('info.configUpdateProcessed', true, true); } else if (id === 'system.config') { if (obj && obj.common) { this.lang = obj.common.language || this.lang || 'en'; @@ -2687,6 +2689,7 @@ class WebServer { this.systemConfig = obj.common; this._updateConstantEntities(); this.log.debug(`${id} -> config updated, constant entities updated.`); + this.log.debug('entitiesUpdated for system.config.'); await this.adapter.setStateAsync('info.entitiesUpdated', true, true); } } else { @@ -2763,6 +2766,7 @@ class WebServer { for (const id of idsWithUpdate) { await this.onStateChange(id, null, true); } + this.log.debug('entitiesUpdated for object changes.'); await this.adapter.setStateAsync('info.entitiesUpdated', true, true); this.log.debug('Had changes, updated states and notified entitiesUpdated state.'); } diff --git a/test/integrationTests/objects_change_tests.js b/test/integrationTests/objects_change_tests.js index f76705ada..5e1b50251 100644 --- a/test/integrationTests/objects_change_tests.js +++ b/test/integrationTests/objects_change_tests.js @@ -18,7 +18,7 @@ exports.runTests = function (suite) { tools.clearClient(); //get harness && entities here. harness = getHarness(); - objects = await tools.loadMultipleObjects(jsonFiles); + objects = tools.loadMultipleObjects(jsonFiles); entities = await tools.startAndGetEntities(harness, objects, idsWithEnums, initialStates); }); @@ -32,7 +32,7 @@ exports.runTests = function (suite) { expect(onOffLamp).to.be.ok; expect(onOffLamp).has.nested.property('attributes.friendly_name', deviceObj.common.smartName); - deviceObj.common.smartName = 'Name changed'; + deviceObj.common.smartName = 'Smartname changed'; const newEntities = await tools.waitForEntitiesUpdate(harness, [deviceObj]); const newOnOffLamp = newEntities.find(e => e.context.id === deviceId); expect(newOnOffLamp).to.be.ok; diff --git a/test/integrationTests/testTools.js b/test/integrationTests/testTools.js index fd4abd0f5..c00f9fa56 100644 --- a/test/integrationTests/testTools.js +++ b/test/integrationTests/testTools.js @@ -91,9 +91,11 @@ async function waitForStateChange(targetId, harness) { console.log('Entities updated! -> resolve'); harness.removeListener('stateChange', stateChangedListener); resolve(); + } else { + console.log(`Ignore change for ${id} with state ${state?.val}`); } } else { - console.log('Ignore change for', id); + console.log(`Ignore change for ${id} with state ${state?.val}`); } } harness.on('stateChange', stateChangedListener); @@ -104,7 +106,6 @@ async function waitForStateChange(targetId, harness) { * Waits until lovelace.0.info.entitiesUpdated is set to true by lovelace. * @param harness * @param {Array} objects to add. - * @param {boolean} [startingUp] prevent setting entitiesUpdated to false on startup * @returns {Promise>} */ exports.waitForEntitiesUpdate = async function (harness, objects) { @@ -187,10 +188,9 @@ exports.expectEntity = function (entity, entityType, ioBrokerDeviceId, name, val * Adds entity to configuration, which should make adapter subscribe to state changes * @param harness * @param {Array} entities - * @returns {Promise>} */ exports.addEntitiesToConfiguration = async function (harness, entities) { - console.log('Updating UI config -> ignore update messages. :-)'); + console.log('Updating UI config'); const configObj = await harness.objects.getObjectAsync('lovelace.0.configuration'); let currentConfig = configObj.native; if (!currentConfig.views) { @@ -204,9 +204,10 @@ exports.addEntitiesToConfiguration = async function (harness, entities) { 'entity': entity.entity_id }); } - const newEntities = await exports.waitForEntitiesUpdate(harness, [configObj]); + const promise = waitForStateChange('lovelace.0.info.configUpdateProcessed', harness); + await harness.objects.setObjectAsync('lovelace.0.configuration', configObj); + await promise; console.log('Updated lovelace UI config.'); - return newEntities; }; let currentClient;