Skip to content

Commit

Permalink
fix tests and make them more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfonso committed May 23, 2024
1 parent b6534ad commit 792d13a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
12 changes: 12 additions & 0 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -2679,14 +2680,16 @@ 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';
entityData.lang = this.lang;
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 {
Expand Down Expand Up @@ -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.');
}
Expand Down
4 changes: 2 additions & 2 deletions test/integrationTests/objects_change_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions test/integrationTests/testTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<IOBObject>} objects to add.
* @param {boolean} [startingUp] prevent setting entitiesUpdated to false on startup
* @returns {Promise<Array<Entity>>}
*/
exports.waitForEntitiesUpdate = async function (harness, objects) {
Expand Down Expand Up @@ -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<Entity>} entities
* @returns {Promise<Array<Entity>>}
*/
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) {
Expand All @@ -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;
Expand Down

0 comments on commit 792d13a

Please sign in to comment.