forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.test.js
273 lines (233 loc) · 7.46 KB
/
loop.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'use strict';
const _ = require('lodash');
const should = require('should');
const helper = require('./inithelper')();
var ctx_top = helper.getctx();
ctx_top.language.set('en');
const language = ctx_top.language;
var env = require('../lib/server/env')();
var loop = require('../lib/plugins/loop')(ctx_top);
var sandbox = require('../lib/sandbox')(ctx_top);
var statuses = [
{
'created_at':'2016-08-13T20:09:15Z',
'device':'loop://ExamplePhone',
'loop':{
'enacted':{
'timestamp':'2016-08-13T20:09:15Z',
'rate':0.875,
'duration':30,
'received':true
},
'version':'0.9.1',
'recommendedBolus':0,
'timestamp':'2016-08-13T20:09:15Z',
'predicted':{
'startDate':'2016-08-13T20:03:47Z',
'values':[
149,
149,
148,
148,
147,
147
]
},
'iob':{
'timestamp':'2016-08-13T20:05:00Z',
'iob':0.1733152537837709
},
'name':'Loop'
}
},
{
'created_at':'2016-08-13T20:04:15Z',
'device':'loop://ExamplePhone',
'loop':{
'version':'0.9.1',
'recommendedBolus':0,
'timestamp':'2016-08-13T20:04:15Z',
'failureReason':'SomeError',
'name':'Loop'
}
},
{
'created_at':'2016-08-13T01:13:20Z',
'device':'loop://ExamplePhone',
'loop':{
'timestamp':'2016-08-13T01:18:20Z',
'version':'0.9.1',
'iob':{
'timestamp':'2016-08-13T01:15:00Z',
'iob':-0.1205140849137931
},
'name':'Loop'
}
},
{
'created_at':'2016-08-13T01:13:20Z',
'device':'loop://ExamplePhone',
'loop':{
'timestamp':'2016-08-13T01:13:20Z',
'version':'0.9.1',
'iob':{
'timestamp':'2016-08-13T01:10:00Z',
'iob':-0.1205140849137931
},
'failureReason':'StaleDataError(\"Glucose Date: 2016-08-12 23:23:49 +0000 or Pump status date: 2016-08-13 01:13:10 +0000 older than 15.0 min\")',
'name':'Loop'
}
},
{
'created_at':'2016-08-13T01:13:15Z',
'pump':{
'reservoir':90.5,
'clock':'2016-08-13T01:13:10Z',
'battery':{
'status':'normal',
'voltage':1.5
},
'pumpID':'543204'
},
'device':'loop://ExamplePhone',
'uploader':{
'timestamp':'2016-08-13T01:13:15Z',
'battery':43,
'name':'ExamplePhone'
}
}
];
var now = ctx_top.moment(statuses[0].created_at);
_.forEach(statuses, function updateMills (status) {
status.mills = ctx_top.moment(status.created_at).valueOf();
});
describe('loop', function ( ) {
it('should set the property and update the pill and add forecast points', function (done) {
var ctx = {
settings: {
units: 'mg/dl'
}
, pluginBase: {
updatePillText: function mockedUpdatePillText (plugin, options) {
options.label.should.equal('Loop ⌁');
options.value.should.equal('1m ago ↝ 147');
var first = _.first(options.info);
first.label.should.equal('1m ago');
first.value.should.equal('<b>Temp Basal Started</b> 0.88U/hour for 30m, IOB: 0.17U, Predicted Min-Max BG: 147-149, Eventual BG: 147');
}
, addForecastPoints: function mockAddForecastPoints (points) {
points.length.should.equal(6);
done();
}
}
, language: language
};
var sbx = sandbox.clientInit(ctx, now.valueOf(), {devicestatus: statuses});
var unmockedOfferProperty = sbx.offerProperty;
sbx.offerProperty = function mockedOfferProperty (name, setter) {
name.should.equal('loop');
var result = setter();
should.exist(result);
result.display.symbol.should.equal('⌁');
result.display.code.should.equal('enacted');
sbx.offerProperty = unmockedOfferProperty;
unmockedOfferProperty(name, setter);
};
loop.setProperties(sbx);
loop.updateVisualisation(sbx);
});
it('should show errors', function (done) {
var ctx = {
settings: {
units: 'mg/dl'
}
, pluginBase: {
updatePillText: function mockedUpdatePillText (plugin, options) {
options.label.should.equal('Loop x');
options.value.should.equal('1m ago');
var first = _.first(options.info);
first.label.should.equal('1m ago');
first.value.should.equal('Error: SomeError');
done();
}
, language: language
},
language: language
};
var errorTime = ctx_top.moment(statuses[1].created_at);
var sbx = sandbox.clientInit(ctx, errorTime.valueOf(), {devicestatus: statuses});
var unmockedOfferProperty = sbx.offerProperty;
sbx.offerProperty = function mockedOfferProperty (name, setter) {
name.should.equal('loop');
var result = setter();
should.exist(result);
result.display.symbol.should.equal('x');
result.display.code.should.equal('error');
sbx.offerProperty = unmockedOfferProperty;
unmockedOfferProperty(name, setter);
};
loop.setProperties(sbx);
loop.updateVisualisation(sbx);
});
it('should check the recieved flag to see if it was received', function (done) {
var ctx = {
settings: {
units: 'mg/dl'
}
, notifications: require('../lib/notifications')(env, ctx_top)
, language: language
};
ctx.notifications.initRequests();
var notStatuses = _.cloneDeep(statuses);
notStatuses[0].loop.enacted.received = false;
var sbx = require('../lib/sandbox')().clientInit(ctx, now, {devicestatus: notStatuses});
sbx.offerProperty = function mockedOfferProperty (name, setter) {
name.should.equal('loop');
var result = setter();
should.exist(result);
result.display.symbol.should.equal('x');
result.display.code.should.equal('error');
done();
};
loop.setProperties(sbx);
});
it('should generate an alert for a stuck loop', function (done) {
var ctx = {
settings: {
units: 'mg/dl'
}
, notifications: require('../lib/notifications')(env, ctx_top)
, language: language
};
ctx.notifications.initRequests();
var sbx = sandbox.clientInit(ctx, now.clone().add(2, 'hours').valueOf(), {devicestatus: statuses});
sbx.extendedSettings = { 'enableAlerts': 'TRUE' };
loop.setProperties(sbx);
loop.checkNotifications(sbx);
var highest = ctx.notifications.findHighestAlarm('Loop');
highest.level.should.equal(ctx_top.levels.URGENT);
highest.title.should.equal('Loop isn\'t looping');
done();
});
it('should handle virtAsst requests', function (done) {
var ctx = {
settings: {
units: 'mg/dl'
}
, notifications: require('../lib/notifications')(env, ctx_top)
, language: language
};
var sbx = sandbox.clientInit(ctx, now.valueOf(), {devicestatus: statuses});
loop.setProperties(sbx);
loop.virtAsst.intentHandlers.length.should.equal(2);
loop.virtAsst.intentHandlers[0].intentHandler(function next(title, response) {
title.should.equal('Loop Forecast');
response.should.equal('According to the loop forecast you are expected to be between 147 and 149 over the next in 25 minutes');
loop.virtAsst.intentHandlers[1].intentHandler(function next(title, response) {
title.should.equal('Last Loop');
response.should.equal('The last successful loop was a few seconds ago');
done();
}, [], sbx);
}, [], sbx);
});
});