forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathar2.test.js
163 lines (124 loc) · 5.28 KB
/
ar2.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
'use strict';
const should = require('should');
const helper = require('./inithelper')();
const FIVE_MINS = 300000;
const SIX_MINS = 360000;
describe('ar2', function ( ) {
var ctx = helper.getctx();
ctx.ddata = require('../lib/data/ddata')();
ctx.notifications = require('../lib/notifications')(env, ctx);
var ar2 = require('../lib/plugins/ar2')(ctx);
var bgnow = require('../lib/plugins/bgnow')(ctx);
var env = require('../lib/server/env')();
var now = Date.now();
var before = now - FIVE_MINS;
function prepareSandbox(base) {
var sbx = base || require('../lib/sandbox')().serverInit(env, ctx);
bgnow.setProperties(sbx);
ar2.setProperties(sbx);
return sbx;
}
it('should plot a cone', function () {
ctx.ddata.sgvs = [{mgdl: 100, mills: before}, {mgdl: 105, mills: now}];
var sbx = prepareSandbox();
var cone = ar2.forecastCone(sbx);
cone.length.should.equal(26);
});
it('should plot a line if coneFactor is 0', function () {
ctx.ddata.sgvs = [{mgdl: 100, mills: before}, {mgdl: 105, mills: now}];
var env0 = require('../lib/server/env')();
env0.extendedSettings = { ar2: { coneFactor: 0 } };
var sbx = require('../lib/sandbox')().serverInit(env0, ctx).withExtendedSettings(ar2);
bgnow.setProperties(sbx);
var cone = ar2.forecastCone(sbx);
cone.length.should.equal(13);
});
it('Not trigger an alarm when in range', function (done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{mgdl: 100, mills: before}, {mgdl: 105, mills: now}];
var sbx = prepareSandbox();
ar2.checkNotifications(sbx);
should.not.exist(ctx.notifications.findHighestAlarm());
done();
});
it('should trigger a warning when going above target', function (done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{mgdl: 150, mills: before}, {mgdl: 170, mills: now}];
var sbx = prepareSandbox();
sbx.offerProperty('iob', function setFakeIOB() {
return {displayLine: 'IOB: 1.25U'};
});
sbx.offerProperty('direction', function setFakeDirection() {
return {value: 'FortyFiveUp', label: '↗', entity: '↗'};
});
ar2.checkNotifications(sbx);
var highest = ctx.notifications.findHighestAlarm();
highest.level.should.equal(helper.ctx.levels.WARN);
highest.title.should.equal('Warning, HIGH predicted');
highest.message.should.equal('BG Now: 170 +20 ↗ mg/dl\nBG 15m: 206 mg/dl\nIOB: 1.25U');
done();
});
it('should trigger a urgent alarm when going high fast', function (done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{mgdl: 140, mills: before}, {mgdl: 200, mills: now}];
var sbx = prepareSandbox();
ar2.checkNotifications(sbx);
var highest = ctx.notifications.findHighestAlarm();
highest.level.should.equal(helper.ctx.levels.URGENT);
highest.title.should.equal('Urgent, HIGH');
done();
});
it('should trigger a warning when below target', function (done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{mgdl: 90, mills: before}, {mgdl: 80, mills: now}];
var sbx = prepareSandbox();
ar2.checkNotifications(sbx);
var highest = ctx.notifications.findHighestAlarm();
highest.level.should.equal(helper.ctx.levels.WARN);
highest.title.should.equal('Warning, LOW');
done();
});
it('should trigger a warning when almost below target', function (done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{mgdl: 90, mills: before}, {mgdl: 83, mills: now}];
var sbx = prepareSandbox();
ar2.checkNotifications(sbx);
var highest = ctx.notifications.findHighestAlarm();
highest.level.should.equal(helper.ctx.levels.WARN);
highest.title.should.equal('Warning, LOW predicted');
done();
});
it('should trigger a urgent alarm when falling fast', function (done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{mgdl: 120, mills: before}, {mgdl: 85, mills: now}];
var sbx = prepareSandbox();
ar2.checkNotifications(sbx);
var highest = ctx.notifications.findHighestAlarm();
highest.level.should.equal(helper.ctx.levels.URGENT);
highest.title.should.equal('Urgent, LOW predicted');
done();
});
it('should trigger a warning alarm by interpolating when more than 5mins apart', function (done) {
ctx.notifications.initRequests();
//same as previous test but prev is 10 mins ago, so delta isn't enough to trigger an urgent alarm
ctx.ddata.sgvs = [{mgdl: 120, mills: before - SIX_MINS}, {mgdl: 85, mills: now}];
var sbx = prepareSandbox();
ar2.checkNotifications(sbx);
var highest = ctx.notifications.findHighestAlarm();
highest.level.should.equal(helper.ctx.levels.WARN);
highest.title.should.equal('Warning, LOW predicted');
done();
});
it('should handle virtAsst requests', function (done) {
var now = Date.now();
var before = now - FIVE_MINS;
ctx.ddata.sgvs = [{mgdl: 100, mills: before}, {mgdl: 105, mills: now}];
var sbx = prepareSandbox();
ar2.virtAsst.intentHandlers.length.should.equal(1);
ar2.virtAsst.intentHandlers[0].intentHandler(function next(title, response) {
title.should.equal('AR2 Forecast');
response.should.equal('According to the AR2 forecast you are expected to be between 109 and 120 over the next in 30 minutes');
done();
}, [], sbx);
});
});