diff --git a/package.json b/package.json index f800470..f6659b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minimed-connect-to-nightscout", - "version": "0.2.2", + "version": "0.2.3", "description": "Send Medtronic pump and CGM data to Nightscout.", "author": "Mark Wilson ", "repository": { @@ -12,13 +12,21 @@ }, "main": "index.js", "scripts": { - "start": "node run.js" + "start": "node run.js", + "test": "mocha" }, "license": "MIT", + "engines": { + "node": "0.10.x" + }, "dependencies": { - "common": "0.2.5", - "extend": "3.0.0", - "request": "2.64.0" + "common": "0.2.x", + "extend": "3.0.x", + "request": "2.64.x" + }, + "devDependencies": { + "expect.js": "0.3.x", + "mocha": "2.3.x" }, "keywords": [ "medtronic", diff --git a/test/fixtures.js b/test/fixtures.js new file mode 100644 index 0000000..de3dc4b --- /dev/null +++ b/test/fixtures.js @@ -0,0 +1,93 @@ +var extend = require('extend'); + +var makeSG = module.exports.makeSG = function(sg, time) { + time = time || "Oct 20, 2015 11:09:00"; + return { + "sg" : sg, + "datetime" : time, + "version" : 1, + "timeChange" : false, + "kind" : "SG" + }; +}; + +var makeSGs = module.exports.makeSGs = function(count) { + var out = []; + for(var i = 0; i < count; i++) { + out.push(makeSG(100 + i)); + } + return out; +}; + +var data = module.exports.data = function(overrides) { + overrides = overrides || {}; + return extend(true, { + "sgs" : makeSGs(300), + "conduitSerialNumber" : "0", + "conduitMedicalDeviceInRange" : true, + "version" : 1, + "calibStatus" : "LESS_THAN_TWELVE_HRS", + "medicalDeviceFamily" : "PARADIGM", + "kind" : "Main", + "medicalDeviceTime" : 0, + "lastAlarm" : { + "type" : "ALARM", + "version" : 1, + "flash" : false, + "datetime" : "Oct 12, 2015 03:12:10", + "kind" : "Alarm", + "code" : 102 + }, + "currentServerTime" : 1445091119507, + "sMedicalDeviceTime" : "Oct 17, 2015 09:09:14", + "conduitInRange" : true, + "limits" : [ + { + "index" : 0, + "lowLimit" : 80, + "kind" : "Limits", + "highLimit" : 300, + "version" : 1 + } + ], + "reservoirAmount" : 52, + "timeFormat" : "HR_24", + "sensorState" : "NORMAL", + "lastConduitTime" : 0, + "medicalDeviceBatteryLevelPercent" : 100, + "reservoirLevelPercent" : 50, + // We don't look at this; otherwise it should match "sgs" + "lastSG" : { + "kind" : "SG", + "datetime" : "Oct 17, 2015 09:08:00", + "version" : 1, + "timeChange" : false, + "sg" : 180 + }, + "lastSensorTSAsString" : "Oct 17, 2015 09:08:00", + "lastName" : "", + "activeInsulin" : { + "datetime" : "Oct 17, 2015 09:09:14", + "version" : 1, + "amount" : 1.275, + "kind" : "Insulin" + }, + "conduitBatteryLevel" : 100, + "conduitBatteryStatus" : "FULL", + "conduitSensorInRange" : true, + "medicalDeviceSerialNumber" : "", + "timeToNextCalibHours" : 11, + "sensorDurationHours" : 91, + "firstName" : "", + "lastConduitUpdateServerTime" : 1445091101422, + "lastMedicalDeviceDataUpdateServerTime" : 1445091101422, + "medicalDeviceSuspended" : false, + "bgunits" : "MGDL", + "bgUnits" : "MGDL", + "sLastSensorTime" : "Oct 17, 2015 09:08:00", + "lastSensorTS" : 0, + "medicalDeviceTimeAsString" : "Oct 17, 2015 09:09:14", + "lastSGTrend" : "UP_DOUBLE", + "lastSensorTime" : 0 + }, overrides); +}; diff --git a/test/transform.js b/test/transform.js new file mode 100644 index 0000000..cec38f4 --- /dev/null +++ b/test/transform.js @@ -0,0 +1,51 @@ +/* jshint node: true */ +/* globals describe, it */ +"use strict"; + +var expect = require('expect.js'); + +var f = require('./fixtures.js'), + transform = require('../transform.js'); + +describe('transform()', function() { + it('should obey sgvLimit', function() { + var data = f.data(); + + expect( + transform(data) + .filter(function(e) { return e['type'] === 'sgv'; }) + .length + ).to.eql(data['sgs'].length); + + expect( + transform(data, 4) + .filter(function(e) { return e['type'] === 'sgv'; }) + .length + ).to.be(4); + }); + + it('should include pump device family', function() { + expect( + transform( + f.data({'medicalDeviceFamily': 'foo'}) + )[0]['device'] + ).to.be('connect://foo'); + }); + + it('should discard data more than 20 minutes old', function() { + var now = Date.now(); + var THRESHOLD = 20; + var boundary = now - THRESHOLD * 60 * 1000; + expect( + transform( + f.data({'currentServerTime': now, 'lastMedicalDeviceDataUpdateServerTime': boundary}) + ).length + ).to.be.greaterThan(0); + + expect( + transform( + f.data({'currentServerTime': now, 'lastMedicalDeviceDataUpdateServerTime': boundary - 1}) + ).length + ).to.be(0); + }); +});