Skip to content

Commit

Permalink
don't emit an notifications if it's snoozed; some tests and logging t…
Browse files Browse the repository at this point in the history
…o make sure it stays fixed
  • Loading branch information
jasoncalabrese committed Jul 7, 2015
1 parent f6ef1c6 commit bc2529f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 40 deletions.
61 changes: 39 additions & 22 deletions lib/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ function init (env, ctx) {
}

if (sendClear) {
ctx.bus.emit('notification', {clear: true, title: 'All Clear', message: 'Auto ack\'d alarm(s)'});
console.info('emitted notification clear');
var notify = {clear: true, title: 'All Clear', message: 'Auto ack\'d alarm(s)'};
ctx.bus.emit('notification', notify);
logEmitEvent(notify);
}
}

Expand All @@ -87,7 +88,7 @@ function init (env, ctx) {
if (ctx.data.lastUpdated > alarm.lastAckTime + alarm.silenceTime) {
ctx.bus.emit('notification', notify);
alarm.lastEmitTime = ctx.data.lastUpdated;
console.info('emitted notification:', notify);
logEmitEvent(notify);
} else {
console.log(alarm.label + ' alarm is silenced for ' + Math.floor((alarm.silenceTime - (ctx.data.lastUpdated - alarm.lastAckTime)) / 60000) + ' minutes more');
}
Expand Down Expand Up @@ -157,9 +158,9 @@ function init (env, ctx) {
if (snoozedBy) {
logSnoozingEvent(highestAlarm, snoozedBy);
notifications.ack(snoozedBy.level, snoozedBy.lengthMills, true);
} else {
emitNotification(highestAlarm);
}

emitNotification(highestAlarm);
} else {
autoAckAlarms();
}
Expand All @@ -184,11 +185,11 @@ function init (env, ctx) {
}

if (sendClear) {
ctx.bus.emit('notification', {
clear: true
, title: 'All Clear'
, message: notifications.levels.toString(level) + ' was ack\'d'
});
var notify = {
clear: true, title: 'All Clear', message: notifications.levels.toString(level) + ' was ack\'d'
};
ctx.bus.emit('notification', notify);
logEmitEvent(notify);
}

};
Expand Down Expand Up @@ -216,22 +217,38 @@ function init (env, ctx) {
});
};

function notifyToView (notify) {
return {
level: notifications.levels.toString(notify.level)
, title: notify.title
, message: notify.message
, plugin: notify.plugin ? notify.plugin.name : '<none>'
, debug: notify.debug
}
}

function snoozeToView (snooze) {
return {
level: notifications.levels.toString(snooze.level)
, title: snooze.title
, message: snooze.message
}
}

function logEmitEvent(notify) {
var type = notify.level >= notifications.levels.WARN ? 'ALARM' : (notify.clear ? 'ALL CLEAR' : 'NOTIFICATION');
console.info([
'EMITTING ' + type + ':'
, ' ' + JSON.stringify(notifyToView(notify))
].join('\n'));
}

function logSnoozingEvent(highestAlarm, snoozedBy) {
console.info([
'SNOOZING ALARM:'
, ' ' + JSON.stringify({
level: notifications.levels.toString(highestAlarm.level)
, title: highestAlarm.title
, message: highestAlarm.message
, plugin: highestAlarm.plugin.name
, debug: highestAlarm.debug
})
, ' ' + JSON.stringify(notifyToView(highestAlarm))
, ' BECAUSE:'
, ' ' + JSON.stringify({
level: notifications.levels.toString(snoozedBy.level)
, title: snoozedBy.title
, message: snoozedBy.message
})
, ' ' + JSON.stringify(snoozeToView(snoozedBy))
].join('\n'));
}

Expand Down
62 changes: 44 additions & 18 deletions tests/notifications.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('notifications', function ( ) {
level: notifications.levels.WARN
, title: 'exampleSnooze'
, message: 'exampleSnooze message'
, lengthMills: 1000
, lengthMills: 10000
};

var exampleSnoozeNone = {
Expand All @@ -55,9 +55,33 @@ describe('notifications', function ( ) {
level: notifications.levels.URGENT
, title: 'exampleSnoozeUrgent'
, message: 'exampleSnoozeUrgent message'
, lengthMills: 1000
, lengthMills: 10000
};


function expectNotification (check, done) {
//start fresh to we don't pick up other notifications
ctx.bus = new Stream;
//if notification doesn't get called test will time out
ctx.bus.on('notification', function callback (notify) {
if (check(notify)) {
done();
}
});
}

function clearToDone (done) {
expectNotification(function expectClear (notify) {
return notify.clear;
}, done);
}

function notifyToDone (done) {
expectNotification(function expectNotClear (notify) {
return ! notify.clear;
}, done);
}

it('initAndReInit', function (done) {
notifications.initRequests();
notifications.requestNotify(exampleWarn);
Expand All @@ -84,14 +108,7 @@ describe('notifications', function ( ) {
});

it('emitAnInfo', function (done) {
//start fresh to we don't pick up other notifications
ctx.bus = new Stream;
//if notification doesn't get called test will time out
ctx.bus.on('notification', function callback (notify) {
if (!notify.clear) {
done();
}
});
notifyToDone(done);

notifications.resetStateForTests();
notifications.initRequests();
Expand All @@ -102,14 +119,7 @@ describe('notifications', function ( ) {
});

it('emitAllClear 1 time after alarm is auto acked', function (done) {
//start fresh to we don't pick up other notifications
ctx.bus = new Stream;
//if notification doesn't get called test will time out
ctx.bus.on('notification', function callback (notify) {
if (notify.clear) {
done();
}
});
clearToDone(done);

notifications.resetStateForTests();
notifications.initRequests();
Expand Down Expand Up @@ -139,38 +149,54 @@ describe('notifications', function ( ) {
});

it('Can be snoozed', function (done) {
notifyToDone(done); //shouldn't get called

notifications.resetStateForTests();
notifications.initRequests();
notifications.requestNotify(exampleWarn);
notifications.requestSnooze(exampleSnooze);
notifications.snoozedBy(exampleWarn).should.equal(exampleSnooze);
notifications.process();

done();
});

it('Can be snoozed by last snooze', function (done) {
notifyToDone(done); //shouldn't get called

notifications.resetStateForTests();
notifications.initRequests();
notifications.requestNotify(exampleWarn);
notifications.requestSnooze(exampleSnoozeNone);
notifications.requestSnooze(exampleSnooze);
notifications.snoozedBy(exampleWarn).should.equal(exampleSnooze);
notifications.process();

done();
});

it('Urgent alarms can\'t be snoozed by warn', function (done) {
clearToDone(done); //shouldn't get called

notifications.resetStateForTests();
notifications.initRequests();
notifications.requestNotify(exampleUrgent);
notifications.requestSnooze(exampleSnooze);
should.not.exist(notifications.snoozedBy(exampleUrgent));
notifications.process();

done();
});

it('Warnings can be snoozed by urgent', function (done) {
notifyToDone(done); //shouldn't get called

notifications.resetStateForTests();
notifications.initRequests();
notifications.requestNotify(exampleWarn);
notifications.requestSnooze(exampleSnoozeUrgent);
notifications.snoozedBy(exampleWarn).should.equal(exampleSnoozeUrgent);
notifications.process();

done();
});
Expand Down

0 comments on commit bc2529f

Please sign in to comment.