clean and more tests

This commit is contained in:
Jason Calabrese
2015-07-25 10:03:43 -07:00
parent add721b5fe
commit f60ccc1c32
8 changed files with 94 additions and 43 deletions
+1 -1
View File
@@ -257,7 +257,7 @@ function readENV(varName, defaultValue) {
function hasExtendedSetting(prefix, envs) {
return _.find(envs, function (value, key) {
return key.indexOf(prefix.toUpperCase() + '_') >= 0 || key.indexOf(prefix.toLowerCase() + '_') >= 0
return key.indexOf(prefix.toUpperCase() + '_') >= 0 || key.indexOf(prefix.toLowerCase() + '_') >= 0;
}) !== undefined;
}
+6 -12
View File
@@ -44,18 +44,16 @@ function init (env) {
maker.sendEvent = function sendEvent (event, callback) {
if (!event || !event.name) {
if (callback) {
callback('No event name found');
}
callback('No event name found');
} else if (!event.level) {
callback('No event level found');
} else {
maker.makeRequests(event, function sendCallback (err, response) {
if (err) {
if (callback) { callback(err); }
callback(err);
} else {
lastAllClear = 0;
if (callback) { callback(null, response); }
callback(null, response);
}
});
}
@@ -113,16 +111,12 @@ function init (env) {
.get(url)
.on('response', function (response) {
console.info('sent maker request: ', url);
if (callback) {
callback(null, response);
}
callback(null, response);
})
.on('error', function (err) {
if (callback) {
callback(err);
}
callback(err);
});
}
};
if (keys && keys.length > 0) {
return maker;
+28 -22
View File
@@ -25,30 +25,32 @@ function init() {
var mbgMessage = mbgCurrent ? 'Meter BG ' + sbx.scaleEntry(lastMBG) + ' ' + sbx.unitsLabel : '';
var treatmentMessage = treatmentCurrent ? 'Treatment: ' + lastTreatment.eventType : '';
var isAnnouncement = lastTreatment.eventType === 'Announcement';
if (!isAnnouncement) {
autoSnoozeAlarms(mbgMessage, treatmentMessage, sbx);
}
autoSnoozeAlarms(mbgMessage, treatmentMessage, lastTreatment, sbx);
//and add some info notifications
//the notification providers (push, websockets, etc) are responsible to not sending the same notifications repeatedly
if (mbgCurrent) { requestMBGNotify(lastMBG, sbx); }
if (treatmentCurrent && isAnnouncement) {
requestAnnouncementNotify(lastTreatment, sbx);
} else if (treatmentCurrent) {
if (treatmentCurrent) {
requestTreatmentNotify(lastTreatment, sbx);
}
}
};
function autoSnoozeAlarms(mbgMessage, treatmentMessage, sbx) {
var snoozeLength = (sbx.extendedSettings.snoozeMins && Number(sbx.extendedSettings.snoozeMins) * 60 * 1000) || TIME_10_MINS_MS;
sbx.notifications.requestSnooze({
level: levels.URGENT
, title: 'Snoozing alarms since there was a recent treatment'
, message: _.trim([mbgMessage, treatmentMessage].join('\n'))
, lengthMills: snoozeLength
});
function isAnnouncement (lastTreatment) {
return lastTreatment.eventType === 'Announcement';
}
function autoSnoozeAlarms(mbgMessage, treatmentMessage, lastTreatment, sbx) {
//announcements don't snooze alarms
if (!isAnnouncement(lastTreatment)) {
var snoozeLength = (sbx.extendedSettings.snoozeMins && Number(sbx.extendedSettings.snoozeMins) * 60 * 1000) || TIME_10_MINS_MS;
sbx.notifications.requestSnooze({
level: levels.URGENT
, title: 'Snoozing alarms since there was a recent treatment'
, message: _.trim([mbgMessage, treatmentMessage].join('\n'))
, lengthMills: snoozeLength
});
}
}
function requestMBGNotify (lastMBG, sbx) {
@@ -76,14 +78,18 @@ function init() {
}
function requestTreatmentNotify (lastTreatment, sbx) {
var message = buildTreatmentMessage(lastTreatment, sbx);
if (isAnnouncement(lastTreatment)) {
requestAnnouncementNotify(lastTreatment, sbx);
} else {
var message = buildTreatmentMessage(lastTreatment, sbx);
sbx.notifications.requestNotify({
level: levels.INFO
, title: lastTreatment.eventType
, message: message
, plugin: treatmentnotify
});
sbx.notifications.requestNotify({
level: levels.INFO
, title: lastTreatment.eventType
, message: message
, plugin: treatmentnotify
});
}
}
function buildTreatmentMessage(lastTreatment, sbx) {
+2 -2
View File
@@ -10,7 +10,7 @@ function init(env, ctx) {
// declare local constants for time differences
var TIME_15_MINS_S = 15 * 60
, TIME_30_MINS_S = 30 * 60
, TIME_30_MINS_MS = 30 * 60 * 1000
;
function pushnotify() {
@@ -61,7 +61,7 @@ function init(env, ctx) {
var notify = receipts.get(response.receipt);
console.info('push ack, response: ', response, ', notify: ', notify);
if (notify) {
ctx.notifications.ack(notify.level, TIME_30_MINS_S, true);
ctx.notifications.ack(notify.level, TIME_30_MINS_MS, true);
}
return !!notify;
};
+10 -1
View File
@@ -77,9 +77,18 @@ describe('env', function ( ) {
delete process.env.SCARYPLUGIN_DO_THING;
});
it('look for extended settings', function () {
it('check if there are extended settings', function () {
var env = require('../env')();
env.hasExtendedSetting('PUSHOVER', {'PUSHOVER_API_TOKEN': '12345'}).should.equal(true);
});
it('add pushover to enable if one of the env vars is set', function () {
process.env.PUSHOVER_API_TOKEN = '12345';
var env = require('../env')();
env.enable.should.containEql('pushover');
delete process.env.PUSHOVER_API_TOKEN;
});
});
+15 -1
View File
@@ -20,12 +20,26 @@ describe('maker', function ( ) {
});
it('send a request', function (done) {
maker.sendEvent({name: 'test', level: levels.toLowerCase(levels.WARN)}, function sendCallback (err) {
maker.sendEvent({name: 'test', message: 'This is the message', level: levels.toLowerCase(levels.WARN)}, function sendCallback (err) {
should.not.exist(err);
done();
});
});
it('not send a request without a name', function (done) {
maker.sendEvent({level: levels.toLowerCase(levels.WARN)}, function sendCallback (err) {
should.exist(err);
done();
});
});
it('not send a request without a level', function (done) {
maker.sendEvent({name: 'test'}, function sendCallback (err) {
should.exist(err);
done();
});
});
it('send a allclear, but only once', function (done) {
function mockedToTestSingleDone (key, event, eventName, callback) {
callback(); done();
+9 -3
View File
@@ -1,11 +1,15 @@
'use strict';
require('should');
var should = require('should');
var levels = require('../lib/levels');
describe('pushover', function ( ) {
var baseurl = 'https://nightscout.test';
var env = {
extendedSettings: {
baseUrl: baseurl
, extendedSettings: {
pushover: {
userKey: '12345'
, apiToken: '6789'
@@ -19,7 +23,6 @@ describe('pushover', function ( ) {
var notify = {
title: 'Warning, this is a test!'
, message: 'details details details details'
, level: levels.WARN
, pushoverSound: 'climb'
, plugin: {name: 'test'}
@@ -27,9 +30,11 @@ describe('pushover', function ( ) {
pushover.sendAPIRequest = function mockedSendAPIRequest (msg) {
msg.title.should.equal(notify.title);
should.not.exist(msg.message);
msg.priority.should.equal(2);
msg.retry.should.equal(15 * 60);
msg.sound.should.equal(notify.pushoverSound);
msg.callback.indexOf(baseurl).should.equal(0);
done();
};
@@ -48,6 +53,7 @@ describe('pushover', function ( ) {
pushover.sendAPIRequest = function mockedSendAPIRequest (msg) {
msg.title.should.equal(notify.title);
msg.message.should.equal(notify.message);
msg.priority.should.equal(2);
msg.retry.should.equal(2 * 60);
msg.sound.should.equal(notify.pushoverSound);
+23 -1
View File
@@ -73,7 +73,7 @@ describe('treatmentnotify', function ( ) {
done();
});
it('Request an Un-Snoozeable notification for an announcement even there is an active snooze', function (done) {
it('Request a notification for an announcement even there is an active snooze', function (done) {
ctx.notifications.initRequests();
ctx.data.treatments = [{mills: now, mgdl: 40, eventType: 'Announcement', notes: 'This not an alarm'}];
@@ -93,7 +93,9 @@ describe('treatmentnotify', function ( ) {
var announcement = _.first(ctx.notifications.findUnSnoozeable());
should.exist(announcement);
announcement.title.should.equal('Urgent Announcement');
announcement.level.should.equal(levels.URGENT);
announcement.pushoverSound.should.equal('persistent');
should.deepEqual(ctx.notifications.findHighestAlarm(), announcement);
ctx.notifications.snoozedBy(announcement).should.equal(false);
@@ -101,4 +103,24 @@ describe('treatmentnotify', function ( ) {
done();
});
it('Request a notification for a non-error announcement', function (done) {
ctx.notifications.initRequests();
ctx.data.treatments = [{mills: now, mgdl: 100, eventType: 'Announcement', notes: 'This not an alarm'}];
var sbx = require('../lib/sandbox')().serverInit(env, ctx);
treatmentnotify.checkNotifications(sbx);
var announcement = _.first(ctx.notifications.findUnSnoozeable());
should.exist(announcement);
announcement.title.should.equal('Announcement');
announcement.level.should.equal(levels.INFO);
should.not.exist(announcement.pushoverSound);
should.not.exist(ctx.notifications.findHighestAlarm());
ctx.notifications.snoozedBy(announcement).should.equal(false);
done();
});
});