Skip to content

Commit

Permalink
clean and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Jul 25, 2015
1 parent add721b commit f60ccc1
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 44 deletions.
2 changes: 1 addition & 1 deletion env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 6 additions & 12 deletions lib/plugins/maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 29 additions & 23 deletions lib/plugins/treatmentnotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -76,14 +78,18 @@ function init() {
}

function requestTreatmentNotify (lastTreatment, sbx) {
var message = buildTreatmentMessage(lastTreatment, sbx);

sbx.notifications.requestNotify({
level: levels.INFO
, title: lastTreatment.eventType
, message: message
, plugin: treatmentnotify
});
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
});
}
}

function buildTreatmentMessage(lastTreatment, sbx) {
Expand Down
4 changes: 2 additions & 2 deletions lib/pushnotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
};
Expand Down
11 changes: 10 additions & 1 deletion tests/env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

});
16 changes: 15 additions & 1 deletion tests/maker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 9 additions & 3 deletions tests/pushover.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -19,17 +23,18 @@ describe('pushover', function ( ) {

var notify = {
title: 'Warning, this is a test!'
, message: 'details details details details'
, level: levels.WARN
, pushoverSound: 'climb'
, plugin: {name: 'test'}
};

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();
};

Expand All @@ -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);
Expand Down
24 changes: 23 additions & 1 deletion tests/treatmentnotify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}];

Expand All @@ -93,12 +93,34 @@ 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);


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();
});

});

0 comments on commit f60ccc1

Please sign in to comment.