Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WSS tests #11

Merged
merged 6 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/registry-build-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ jobs:
- name: Execute tests
run: |
export MQTTS_TEST_URL=${{ secrets.MQTTS_URL }}
export WSS_TEST_URL=${{ secrets.WSS_URL }}
yarn test

build-and-push-images:
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/registry-pr-purge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Purge Pull Request Image
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows#registry_package
# Run cleanup job if a new package was published or updated
on:
workflow_dispatch:
pull_request:
types: [closed]

Expand All @@ -17,5 +16,4 @@ jobs:
token: ${{ secrets.GHCR_TOKEN}}
organization: ${{ github.repository_owner}}
container: ${{ github.event.repository.name }}
tag-regex: /pr-${{github.event.pull_request.number}}$/
dry-run: true
tag-regex: pr-${{github.event.pull_request.number}}$
1 change: 0 additions & 1 deletion .github/workflows/registry-purge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Docker Registry Cleanup
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows#registry_package
# Run cleanup job if a new package was published or updated
on:
workflow_dispatch:
registry_package:

jobs:
Expand Down
3 changes: 3 additions & 0 deletions config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
},
"mqtts_test_client": {
"url": "MQTTS_TEST_URL"
},
"wss_test_client": {
"url": "WSS_TEST_URL"
}
}
3 changes: 3 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
},
"mqtts_test_client:": {
"url": ""
},
"wss_test_client:": {
"url": ""
}
}
29 changes: 29 additions & 0 deletions test/helpers/wss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const config = require('config').get('wss_test_client');

module.exports = function wssBox (
// parameter destructuring
{ enabled, url, topic, connectionOptions, decodeOptions, messageFormat } = {
// defaults
enabled: true,
url: config.get('url'),
topic: 'generalTestTopic',
messageFormat: 'csv',
}
) {
return {
name: 'wssTestSenseBox',
location: [7.62, 51.96],
exposure: 'outdoor',
model: 'homeEthernet',
mqtt: {
enabled,
url,
topic,
connectionOptions,
decodeOptions,
messageFormat,
},
};
};
39 changes: 38 additions & 1 deletion test/test-ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const expect = require('chai').expect,
Box,
} = require('@sensebox/opensensemap-api-models'),
wsBox = require('./helpers/ws'),
wssBox = require('./helpers/wss'),
mqttClient = require('../src/client'),
mqtt = require('mqtt');

Expand All @@ -30,7 +31,7 @@ describe('ws client', function () {
);
setTimeout(() => {
resolve();
}, 100);
}, 1000);
}, 100);
});
});
Expand All @@ -46,3 +47,39 @@ describe('ws client', function () {
}
});
});

describe('wss client', function () {
let testBox;
before(async function () {
await connect(dbConnectionString({ db: 'mqttTest' }));

testBox = await Box.initNew(wssBox());
mqttClient.connect(testBox);
const mqclient = mqtt.connect(testBox.integrations.mqtt.url);

return new Promise((resolve) => {
mqclient.on('connect', () => {
setTimeout(() => {
mqclient.subscribe(testBox.integrations.mqtt.topic);
mqclient.publish(
testBox.integrations.mqtt.topic,
testBox.sensors.map((s) => `${s._id},12`).join('\n')
);
setTimeout(() => {
resolve();
}, 1000);
}, 100);
});
});
});

it('should accept measurements via wss message', async function () {
const box = await Box.findBoxById(testBox._id, {
onlyLastMeasurements: true,
});

for (const sensor of box.sensors) {
expect(sensor.lastMeasurement.value).equal('12');
}
});
});