mirror of
https://github.com/node-red/node-red.git
synced 2025-12-27 07:31:07 +01:00
Add unit tests
This commit is contained in:
@@ -106,16 +106,16 @@ function isTelemetryEnabled () {
|
||||
return undefined
|
||||
}
|
||||
|
||||
// If there are telemetry settings, use what it says
|
||||
if (telemetrySettings && telemetrySettings.enabled !== undefined) {
|
||||
return telemetrySettings.enabled
|
||||
}
|
||||
|
||||
// User has made a choice; defer to that
|
||||
if (runtimeTelemetryEnabled !== undefined) {
|
||||
return runtimeTelemetryEnabled
|
||||
}
|
||||
|
||||
// If there are telemetry settings, use what it says
|
||||
if (telemetrySettings && telemetrySettings.enabled !== undefined) {
|
||||
return telemetrySettings.enabled
|
||||
}
|
||||
|
||||
// At this point, we have no sign the user has consented to telemetry, so
|
||||
// keep disabled - but return undefined as a false-like value to distinguish
|
||||
// it from the explicit disable above
|
||||
@@ -157,6 +157,7 @@ function stopTelemetry () {
|
||||
module.exports = {
|
||||
init: (_runtime) => {
|
||||
runtime = _runtime
|
||||
|
||||
if (isTelemetryEnabled()) {
|
||||
startTelemetry()
|
||||
}
|
||||
@@ -185,5 +186,12 @@ module.exports = {
|
||||
* Get telemetry enabled status
|
||||
* @returns {boolean} true if telemetry is enabled, false if disabled, undefined if not set
|
||||
*/
|
||||
isEnabled: isTelemetryEnabled
|
||||
isEnabled: isTelemetryEnabled,
|
||||
|
||||
stop: () => {
|
||||
if (scheduleTask) {
|
||||
scheduleTask.stop()
|
||||
scheduleTask = null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
const process = require('process')
|
||||
|
||||
module.exports = async (runtime) => {
|
||||
module.exports = (runtime) => {
|
||||
return {
|
||||
'env.nodejs': process.version.replace(/^v/, ''),
|
||||
'env.node-red': runtime.settings.version
|
||||
|
||||
96
test/unit/@node-red/runtime/lib/telemetry/index_spec.js
Normal file
96
test/unit/@node-red/runtime/lib/telemetry/index_spec.js
Normal file
@@ -0,0 +1,96 @@
|
||||
const should = require("should");
|
||||
const NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
const telemetryApi = NR_TEST_UTILS.require("@node-red/runtime/lib/telemetry/index");
|
||||
|
||||
describe("telemetry", function() {
|
||||
|
||||
afterEach(function () {
|
||||
telemetryApi.stop()
|
||||
messages = []
|
||||
})
|
||||
|
||||
let messages = []
|
||||
|
||||
function getMockRuntime(settings) {
|
||||
return {
|
||||
settings: {
|
||||
get: key => { return settings[key] },
|
||||
set: (key, value) => { settings[key] = value },
|
||||
available: () => true,
|
||||
},
|
||||
log: {
|
||||
debug: (msg) => { messages.push(msg)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Principles to test:
|
||||
// - No settings at all; disable telemetry
|
||||
// - Runtime settings only; do what it says
|
||||
// - User settings take precedence over runtime settings
|
||||
|
||||
it('Disables telemetry with no settings present', function () {
|
||||
telemetryApi.init(getMockRuntime({}))
|
||||
messages.should.have.length(0)
|
||||
// Returns undefined as we don't know either way
|
||||
;(telemetryApi.isEnabled() === undefined).should.be.true()
|
||||
})
|
||||
it('Runtime settings - enable', function () {
|
||||
// Enabled in runtime settings
|
||||
telemetryApi.init(getMockRuntime({
|
||||
telemetry: { enabled: true }
|
||||
}))
|
||||
telemetryApi.isEnabled().should.be.true()
|
||||
messages.should.have.length(1)
|
||||
;/Telemetry enabled/.test(messages[0]).should.be.true()
|
||||
})
|
||||
it('Runtime settings - disable', function () {
|
||||
telemetryApi.init(getMockRuntime({
|
||||
telemetry: { enabled: false },
|
||||
}))
|
||||
// Returns false, not undefined
|
||||
telemetryApi.isEnabled().should.be.false()
|
||||
messages.should.have.length(0)
|
||||
})
|
||||
|
||||
it('User settings - enable overrides runtime settings', function () {
|
||||
telemetryApi.init(getMockRuntime({
|
||||
telemetry: { enabled: false },
|
||||
telemetryEnabled: true
|
||||
}))
|
||||
telemetryApi.isEnabled().should.be.true()
|
||||
messages.should.have.length(1)
|
||||
;/Telemetry enabled/.test(messages[0]).should.be.true()
|
||||
})
|
||||
|
||||
it('User settings - disable overrides runtime settings', function () {
|
||||
telemetryApi.init(getMockRuntime({
|
||||
telemetry: { enabled: true },
|
||||
telemetryEnabled: false
|
||||
}))
|
||||
telemetryApi.isEnabled().should.be.false()
|
||||
messages.should.have.length(0)
|
||||
})
|
||||
|
||||
it('Can enable/disable telemetry', function () {
|
||||
const settings = {}
|
||||
telemetryApi.init(getMockRuntime(settings))
|
||||
;(telemetryApi.isEnabled() === undefined).should.be.true()
|
||||
|
||||
telemetryApi.enable()
|
||||
|
||||
telemetryApi.isEnabled().should.be.true()
|
||||
messages.should.have.length(1)
|
||||
;/Telemetry enabled/.test(messages[0]).should.be.true()
|
||||
settings.should.have.property('telemetryEnabled', true)
|
||||
|
||||
telemetryApi.disable()
|
||||
|
||||
telemetryApi.isEnabled().should.be.false()
|
||||
messages.should.have.length(2)
|
||||
;/Telemetry disabled/.test(messages[1]).should.be.true()
|
||||
settings.should.have.property('telemetryEnabled', false)
|
||||
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
const should = require("should");
|
||||
const NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
const api = NR_TEST_UTILS.require("@node-red/runtime/lib/telemetry/metrics/01-core");
|
||||
|
||||
describe("telemetry metrics/01-core", function() {
|
||||
|
||||
it('reports core metrics', function () {
|
||||
const result = api({
|
||||
settings: {
|
||||
get: key => { return {instanceId: "1234"}[key]}
|
||||
}
|
||||
})
|
||||
result.should.have.property("instanceId", "1234")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,14 @@
|
||||
const should = require("should");
|
||||
const NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
const api = NR_TEST_UTILS.require("@node-red/runtime/lib/telemetry/metrics/02-os");
|
||||
|
||||
describe("telemetry metrics/02-os", function() {
|
||||
|
||||
it('reports os metrics', function () {
|
||||
const result = api()
|
||||
result.should.have.property("os.type")
|
||||
result.should.have.property("os.release")
|
||||
result.should.have.property("os.arch")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
const should = require("should");
|
||||
const NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
const api = NR_TEST_UTILS.require("@node-red/runtime/lib/telemetry/metrics/03-env");
|
||||
|
||||
describe("telemetry metrics/03-env", function() {
|
||||
|
||||
it('reports env metrics', function () {
|
||||
const result = api({
|
||||
settings: {
|
||||
version: '1.2.3'
|
||||
}
|
||||
})
|
||||
result.should.have.property("env.nodejs", process.version.replace(/^v/, ''))
|
||||
result.should.have.property("env.node-red", '1.2.3')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user