forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeago.test.js
131 lines (102 loc) · 4.13 KB
/
timeago.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
var should = require('should');
var levels = require('../lib/levels');
var times = require('../lib/times');
describe('timeago', function() {
var ctx = {};
ctx.levels = levels;
ctx.ddata = require('../lib/data/ddata')();
ctx.notifications = require('../lib/notifications')(env, ctx);
ctx.language = require('../lib/language')();
ctx.settings = require('../lib/settings')();
ctx.settings.heartbeat = 0.5; // short heartbeat to speedup tests
var timeago = require('../lib/plugins/timeago')(ctx);
var env = require('../lib/server/env')();
function freshSBX () {
//set extendedSettings right before calling withExtendedSettings, there's some strange test interference here
env.extendedSettings = { timeago: { enableAlerts: true } };
var sbx = require('../lib/sandbox')().serverInit(env, ctx).withExtendedSettings(timeago);
return sbx;
}
it('Not trigger an alarm when data is current', function(done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{ mills: Date.now(), mgdl: 100, type: 'sgv' }];
var sbx = freshSBX();
timeago.checkNotifications(sbx);
should.not.exist(ctx.notifications.findHighestAlarm('Time Ago'));
done();
});
it('Not trigger an alarm with future data', function(done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{ mills: Date.now() + times.mins(15).msecs, mgdl: 100, type: 'sgv' }];
var sbx = freshSBX();
timeago.checkNotifications(sbx);
should.not.exist(ctx.notifications.findHighestAlarm('Time Ago'));
done();
});
it('should trigger a warning when data older than 15m', function(done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{ mills: Date.now() - times.mins(16).msecs, mgdl: 100, type: 'sgv' }];
var sbx = freshSBX();
timeago.checkNotifications(sbx);
var currentTime = new Date().getTime();
var highest = ctx.notifications.findHighestAlarm('Time Ago');
highest.level.should.equal(levels.WARN);
highest.message.should.equal('Last received: 16 mins ago\nBG Now: 100 mg/dl');
done();
});
it('should trigger an urgent alarm when data older than 30m', function(done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{ mills: Date.now() - times.mins(31).msecs, mgdl: 100, type: 'sgv' }];
var sbx = freshSBX();
timeago.checkNotifications(sbx);
var highest = ctx.notifications.findHighestAlarm('Time Ago');
highest.level.should.equal(levels.URGENT);
highest.message.should.equal('Last received: 31 mins ago\nBG Now: 100 mg/dl');
done();
});
it('calc timeago displays', function() {
var now = Date.now();
should.deepEqual(
timeago.calcDisplay({ mills: now + times.mins(15).msecs }, now)
, { label: 'in the future', shortLabel: 'future' }
);
//TODO: current behavior, we can do better
//just a little in the future, pretend it's ok
should.deepEqual(
timeago.calcDisplay({ mills: now + times.mins(4).msecs }, now)
, { value: 1, label: 'min ago', shortLabel: 'm' }
);
should.deepEqual(
timeago.calcDisplay(null, now)
, { label: 'time ago', shortLabel: 'ago' }
);
should.deepEqual(
timeago.calcDisplay({ mills: now }, now)
, { value: 1, label: 'min ago', shortLabel: 'm' }
);
should.deepEqual(
timeago.calcDisplay({ mills: now - 1 }, now)
, { value: 1, label: 'min ago', shortLabel: 'm' }
);
should.deepEqual(
timeago.calcDisplay({ mills: now - times.sec(30).msecs }, now)
, { value: 1, label: 'min ago', shortLabel: 'm' }
);
should.deepEqual(
timeago.calcDisplay({ mills: now - times.mins(30).msecs }, now)
, { value: 30, label: 'mins ago', shortLabel: 'm' }
);
should.deepEqual(
timeago.calcDisplay({ mills: now - times.hours(5).msecs }, now)
, { value: 5, label: 'hours ago', shortLabel: 'h' }
);
should.deepEqual(
timeago.calcDisplay({ mills: now - times.days(5).msecs }, now)
, { value: 5, label: 'days ago', shortLabel: 'd' }
);
should.deepEqual(
timeago.calcDisplay({ mills: now - times.days(10).msecs }, now)
, { label: 'long ago', shortLabel: 'ago' }
);
});
});