forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.test.js
206 lines (164 loc) · 5.5 KB
/
notifications.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var should = require('should');
var Stream = require('stream');
var levels = require('../lib/levels');
describe('notifications', function ( ) {
var env = {testMode: true};
var ctx = {
bus: new Stream
, ddata: {
lastUpdated: Date.now()
}
};
var notifications = require('../lib/notifications')(env, ctx);
function examplePlugin () {}
var exampleInfo = {
title: 'test'
, message: 'testing'
, level: levels.INFO
, plugin: examplePlugin
};
var exampleWarn = {
title: 'test'
, message: 'testing'
, level: levels.WARN
, plugin: examplePlugin
};
var exampleUrgent = {
title: 'test'
, message: 'testing'
, level: levels.URGENT
, plugin: examplePlugin
};
var exampleSnooze = {
level: levels.WARN
, title: 'exampleSnooze'
, message: 'exampleSnooze message'
, lengthMills: 10000
};
var exampleSnoozeNone = {
level: levels.WARN
, title: 'exampleSnoozeNone'
, message: 'exampleSnoozeNone message'
, lengthMills: 1
};
var exampleSnoozeUrgent = {
level: levels.URGENT
, title: 'exampleSnoozeUrgent'
, message: 'exampleSnoozeUrgent message'
, 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);
notifications.findHighestAlarm().should.equal(exampleWarn);
notifications.initRequests();
should.not.exist(notifications.findHighestAlarm());
done();
});
it('emitAWarning', 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 ( ) {
done();
});
notifications.resetStateForTests();
notifications.initRequests();
notifications.requestNotify(exampleWarn);
notifications.findHighestAlarm().should.equal(exampleWarn);
notifications.process();
});
it('emitAnInfo', function (done) {
notifyToDone(done);
notifications.resetStateForTests();
notifications.initRequests();
notifications.requestNotify(exampleInfo);
should.not.exist(notifications.findHighestAlarm());
notifications.process();
});
it('emitAllClear 1 time after alarm is auto acked', function (done) {
clearToDone(done);
notifications.resetStateForTests();
notifications.initRequests();
notifications.requestNotify(exampleWarn);
notifications.findHighestAlarm().should.equal(exampleWarn);
notifications.process();
notifications.initRequests();
//don't request a notify this time, and an auto ack should be sent
should.not.exist(notifications.findHighestAlarm());
notifications.process();
var alarm = notifications.getAlarmForTests(levels.WARN);
alarm.level.should.equal(levels.WARN);
alarm.silenceTime.should.equal(1);
alarm.lastAckTime.should.be.approximately(Date.now(), 2000);
should.not.exist(alarm.lastEmitTime);
//clear last emit time, even with that all clear shouldn't be sent again since there was no alarm cleared
delete alarm.lastEmitTime;
//process 1 more time to make sure all clear is only sent once
notifications.initRequests();
//don't request a notify this time, and an auto ack should be sent
should.not.exist(notifications.findHighestAlarm());
notifications.process();
});
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();
});
});