From ba0f387d1c4aecb1ed60ab88b9ef4f8e46db3a0b Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Sun, 1 Jan 2023 19:14:59 +0200 Subject: [PATCH] Refactor moment to be loaded from ctx (#7331) * Experimental branch that replaces momentjs with dayjs in the client * Revert unintentional change * feat * Turns out dayjs is a no-go, but this has some good restructuring so submitting that --- bundle/bundle.source.js | 10 ++++++++-- lib/admin_plugins/cleanentriesdb.js | 5 +++-- lib/admin_plugins/cleanstatusdb.js | 5 +++-- lib/admin_plugins/cleantreatmentsdb.js | 5 +++-- lib/admin_plugins/index.js | 14 +++++++------- lib/client/boluscalc.js | 12 +++++------- lib/client/careportal.js | 7 +++---- lib/client/index.js | 9 ++++++++- lib/data/dataloader.js | 1 - lib/plugins/ar2.js | 2 +- lib/plugins/basalprofile.js | 2 +- lib/plugins/batteryage.js | 2 +- lib/plugins/bgnow.js | 2 +- lib/plugins/cannulaage.js | 2 +- lib/plugins/cob.js | 2 +- lib/plugins/insulinage.js | 2 +- lib/plugins/iob.js | 6 +++--- lib/plugins/loop.js | 3 ++- lib/plugins/openaps.js | 2 +- lib/plugins/pump.js | 2 +- lib/plugins/sensorage.js | 2 +- lib/plugins/virtAsstBase.js | 3 ++- lib/plugins/xdripjs.js | 2 +- lib/profile/profileeditor.js | 11 +++++------ lib/profilefunctions.js | 5 +++-- lib/report/reportclient.js | 3 ++- lib/sandbox.js | 2 +- lib/server/bootevent.js | 4 +++- lib/utils.js | 3 +-- tests/ar2.test.js | 23 +++++++++-------------- tests/basalprofileplugin.test.js | 11 +++++++---- tests/bgnow.test.js | 13 +++++++------ tests/boluswizardpreview.test.js | 23 +++++++++++------------ tests/cannulaage.test.js | 10 +++++----- tests/cob.test.js | 9 +++------ tests/inithelper.js | 24 ++++++++++++++++++++++++ tests/insulinage.test.js | 7 +++---- tests/iob.test.js | 14 +++++++------- tests/loop.test.js | 23 +++++++++-------------- tests/openaps.test.js | 18 +++++++----------- tests/profile.test.js | 14 ++++++++------ tests/pump.test.js | 20 +++++++++----------- tests/sensorage.test.js | 8 +++----- tests/utils.test.js | 18 +++++++++++------- webpack/webpack.config.js | 4 +++- 45 files changed, 199 insertions(+), 170 deletions(-) create mode 100644 tests/inithelper.js diff --git a/bundle/bundle.source.js b/bundle/bundle.source.js index 85d68097387..2504c9bfed5 100644 --- a/bundle/bundle.source.js +++ b/bundle/bundle.source.js @@ -18,14 +18,20 @@ require('../node_modules/flot/jquery.flot.time'); require('../node_modules/flot/jquery.flot.pie'); require('../node_modules/flot/jquery.flot.fillbetween'); -window.moment = require('moment-timezone'); +const moment = require('moment-timezone'); + +window.moment = moment; window.Nightscout = window.Nightscout || {}; +var ctx = { + moment: moment +}; + window.Nightscout = { client: require('../lib/client'), units: require('../lib/units')(), - admin_plugins: require('../lib/admin_plugins/')() + admin_plugins: require('../lib/admin_plugins/')(ctx) }; window.Nightscout.report_plugins_preinit = require('../lib/report_plugins/'); diff --git a/lib/admin_plugins/cleanentriesdb.js b/lib/admin_plugins/cleanentriesdb.js index 3793eef8ffd..c13bfc48109 100644 --- a/lib/admin_plugins/cleanentriesdb.js +++ b/lib/admin_plugins/cleanentriesdb.js @@ -1,6 +1,6 @@ 'use strict'; -var moment = require('moment'); +var moment; var cleanentriesdb = { name: 'cleanentriesdb' @@ -8,7 +8,8 @@ var cleanentriesdb = { , pluginType: 'admin' }; -function init() { +function init(ctx) { + moment = ctx.moment; return cleanentriesdb; } diff --git a/lib/admin_plugins/cleanstatusdb.js b/lib/admin_plugins/cleanstatusdb.js index 29fb99bae16..44772f0de47 100644 --- a/lib/admin_plugins/cleanstatusdb.js +++ b/lib/admin_plugins/cleanstatusdb.js @@ -1,6 +1,6 @@ 'use strict'; -var moment = require('moment'); +var moment; var cleanstatusdb = { name: 'cleanstatusdb' @@ -8,7 +8,8 @@ var cleanstatusdb = { , pluginType: 'admin' }; -function init () { +function init (ctx) { + moment = ctx.moment; return cleanstatusdb; } diff --git a/lib/admin_plugins/cleantreatmentsdb.js b/lib/admin_plugins/cleantreatmentsdb.js index a8cc725fd82..bd5d3c52087 100644 --- a/lib/admin_plugins/cleantreatmentsdb.js +++ b/lib/admin_plugins/cleantreatmentsdb.js @@ -1,6 +1,6 @@ 'use strict'; -var moment = require('moment'); +var moment; var cleantreatmentsdb = { name: 'cleantreatmentsdb' @@ -8,7 +8,8 @@ var cleantreatmentsdb = { , pluginType: 'admin' }; -function init() { +function init(ctx) { + moment = ctx.moment; return cleantreatmentsdb; } diff --git a/lib/admin_plugins/index.js b/lib/admin_plugins/index.js index 224054538d1..8bc3f1ae0ba 100644 --- a/lib/admin_plugins/index.js +++ b/lib/admin_plugins/index.js @@ -3,14 +3,14 @@ var _find = require('lodash/find'); var _each = require('lodash/each'); -function init() { +function init(ctx) { var allPlugins = [ - require('./subjects')() - , require('./roles')() - , require('./cleanstatusdb')() - , require('./cleantreatmentsdb')() - , require('./cleanentriesdb')() - , require('./futureitems')() + require('./subjects')(ctx) + , require('./roles')(ctx) + , require('./cleanstatusdb')(ctx) + , require('./cleantreatmentsdb')(ctx) + , require('./cleanentriesdb')(ctx) + , require('./futureitems')(ctx) ]; function plugins(name) { diff --git a/lib/client/boluscalc.js b/lib/client/boluscalc.js index da61e983616..67637ad4d30 100644 --- a/lib/client/boluscalc.js +++ b/lib/client/boluscalc.js @@ -1,7 +1,6 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment-timezone'); var times = require('../times'); var Storages = require('js-storage'); @@ -46,9 +45,9 @@ function init (client, $) { } function setDateAndTime (time) { - time = time || moment(); - eventTime.val(time.format('HH:mm')); - eventDate.val(time.format('YYYY-MM-DD')); + time = time || new Date(); + eventTime.val(time.getHours() + ":" + time.getMinutes()); + eventDate.val(time.toISOString().split('T')[0]); } function mergeDateAndTime () { @@ -125,16 +124,15 @@ function init (client, $) { boluscalc.calculateInsulin(); maybePrevent(event); - // Nightscout.utils.updateBrushToTime(moment.toDate()); }; boluscalc.eventTimeTypeChange = function eventTimeTypeChange (event) { if ($('#bc_othertime').is(':checked')) { $('#bc_eventTimeValue').focus(); $('#bc_retro').css('display', ''); - if (mergeDateAndTime() < moment()) { + if (mergeDateAndTime() < Date.now()) { $('#bc_retro').css('background-color', 'red').text(translate('RETRO MODE')); - } else if (mergeDateAndTime() > moment()) { + } else if (mergeDateAndTime() > Date.now()) { $('#bc_retro').css('background-color', 'blue').text(translate('IN THE FUTURE')); } else { $('#bc_retro').css('display', 'none'); diff --git a/lib/client/careportal.js b/lib/client/careportal.js index 9c18609ce1f..4b3ccc2d757 100644 --- a/lib/client/careportal.js +++ b/lib/client/careportal.js @@ -1,6 +1,5 @@ 'use strict'; -var moment = require('moment-timezone'); var _ = require('lodash'); var parse_duration = require('parse-duration'); // https://www.npmjs.com/package/parse-duration var times = require('../times'); @@ -18,9 +17,9 @@ function init (client, $) { var eventDate = $('#eventDateValue'); function setDateAndTime (time) { - time = time || moment(); - eventTime.val(time.format('HH:mm')); - eventDate.val(time.format('YYYY-MM-DD')); + time = time || client.ctx.moment(); + eventTime.val(time.hours() + ":" + time.minutes()); + eventDate.val(time.toISOString().split('T')[0]); } function mergeDateAndTime () { diff --git a/lib/client/index.js b/lib/client/index.js index 06062a61291..fb1d17d1430 100644 --- a/lib/client/index.js +++ b/lib/client/index.js @@ -9,7 +9,6 @@ var Storages = require('js-storage'); var language = require('../language')(); var sandbox = require('../sandbox')(); -var profile = require('../profilefunctions')(); var units = require('../units')(); var levels = require('../levels'); var times = require('../times'); @@ -18,6 +17,8 @@ var receiveDData = require('./receiveddata'); var brushing = false; var browserSettings; +var moment = window.moment; +var timezones = moment.tz.names(); var client = {}; @@ -203,6 +204,7 @@ client.load = function load (serverSettings, callback) { , extendedSettings: client.settings.extendedSettings , language: language , levels: levels + , moment: moment }).registerClientDefaults(); browserSettings.loadPluginSettings(client); @@ -210,6 +212,7 @@ client.load = function load (serverSettings, callback) { client.utils = require('../utils')({ settings: client.settings , language: language + , moment: moment }); client.rawbg = client.plugins('rawbg'); @@ -223,6 +226,8 @@ client.load = function load (serverSettings, callback) { , bus: require('../bus')(client.settings, client.ctx) , settings: client.settings , pluginBase: client.plugins.base(majorPills, minorPills, statusPills, bgStatus, client.tooltip, Storages.localStorage) + , moment: moment + , timezones: timezones }; client.ctx.language = language; @@ -298,6 +303,8 @@ client.load = function load (serverSettings, callback) { client.careportal = require('./careportal')(client, $); client.boluscalc = require('./boluscalc')(client, $); + var profile = require('../profilefunctions')(null, client.ctx); + client.profilefunctions = profile; client.editMode = false; diff --git a/lib/data/dataloader.js b/lib/data/dataloader.js index dfb78eafde9..6361b37b333 100644 --- a/lib/data/dataloader.js +++ b/lib/data/dataloader.js @@ -139,7 +139,6 @@ function init(env, ctx) { }); console.info('Load Complete:\n\t', counts.join(', ')); - done(err, result); } diff --git a/lib/plugins/ar2.js b/lib/plugins/ar2.js index 5232feb0245..31ab4766f50 100644 --- a/lib/plugins/ar2.js +++ b/lib/plugins/ar2.js @@ -2,7 +2,6 @@ var _ = require('lodash'); var times = require('../times'); -var moment = require('moment'); var BG_REF = 140; //Central tendency var BG_MIN = 36; //Not 39, but why? @@ -17,6 +16,7 @@ var AR2_COLOR = 'cyan'; function init (ctx) { var translate = ctx.language.translate; + var moment = ctx.moment; var ar2 = { name: 'ar2' diff --git a/lib/plugins/basalprofile.js b/lib/plugins/basalprofile.js index 806dde5859d..69da48c98d6 100644 --- a/lib/plugins/basalprofile.js +++ b/lib/plugins/basalprofile.js @@ -1,10 +1,10 @@ 'use strict'; var times = require('../times'); -var moment = require('moment'); var consts = require('../constants'); var _ = require('lodash'); function init (ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; diff --git a/lib/plugins/batteryage.js b/lib/plugins/batteryage.js index 466c4faa77a..941cb3199d7 100644 --- a/lib/plugins/batteryage.js +++ b/lib/plugins/batteryage.js @@ -1,9 +1,9 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); function init(ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; var levels = ctx.levels; diff --git a/lib/plugins/bgnow.js b/lib/plugins/bgnow.js index 35b96c024b4..43120eeb1e1 100644 --- a/lib/plugins/bgnow.js +++ b/lib/plugins/bgnow.js @@ -1,7 +1,6 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); var times = require('../times'); var offset = times.mins(2.5).msecs; @@ -9,6 +8,7 @@ var bucketFields = ['index', 'fromMills', 'toMills']; function init (ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; var utils = require('../utils')(ctx); diff --git a/lib/plugins/cannulaage.js b/lib/plugins/cannulaage.js index 841848b9549..3b3f52eca49 100644 --- a/lib/plugins/cannulaage.js +++ b/lib/plugins/cannulaage.js @@ -1,9 +1,9 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); function init(ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; var levels = ctx.levels; diff --git a/lib/plugins/cob.js b/lib/plugins/cob.js index 250614ecfd6..a75de32f9f8 100644 --- a/lib/plugins/cob.js +++ b/lib/plugins/cob.js @@ -1,10 +1,10 @@ 'use strict'; var _ = require('lodash') - , moment = require('moment') , times = require('../times'); function init (ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; var iob = require('./iob')(ctx); diff --git a/lib/plugins/insulinage.js b/lib/plugins/insulinage.js index 0346ab24cc2..53da82c1ee8 100644 --- a/lib/plugins/insulinage.js +++ b/lib/plugins/insulinage.js @@ -1,9 +1,9 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); function init(ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; var levels = ctx.levels; diff --git a/lib/plugins/iob.js b/lib/plugins/iob.js index cdcc15706e3..7ea38c168ae 100644 --- a/lib/plugins/iob.js +++ b/lib/plugins/iob.js @@ -1,10 +1,10 @@ 'use strict'; -var _ = require('lodash') - , moment = require('moment') - , times = require('../times'); +const _ = require('lodash') +const times = require('../times'); function init(ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; var utils = require('../utils')(ctx); diff --git a/lib/plugins/loop.js b/lib/plugins/loop.js index c6217e87d4b..a08edb0a479 100644 --- a/lib/plugins/loop.js +++ b/lib/plugins/loop.js @@ -1,12 +1,13 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); var times = require('../times'); // var ALL_STATUS_FIELDS = ['status-symbol', 'status-label', 'iob', 'freq', 'rssi']; Unused variable function init (ctx) { + var moment = ctx.moment; + var utils = require('../utils')(ctx); var translate = ctx.language.translate; var levels = ctx.levels; diff --git a/lib/plugins/openaps.js b/lib/plugins/openaps.js index fc9b7d04603..81e7bc25a05 100644 --- a/lib/plugins/openaps.js +++ b/lib/plugins/openaps.js @@ -1,13 +1,13 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); var times = require('../times'); var consts = require('../constants'); // var ALL_STATUS_FIELDS = ['status-symbol', 'status-label', 'iob', 'meal-assist', 'freq', 'rssi']; Unused variable function init (ctx) { + var moment = ctx.moment; var utils = require('../utils')(ctx); var openaps = { name: 'openaps' diff --git a/lib/plugins/pump.js b/lib/plugins/pump.js index dc087ba04f7..f055a4b1299 100644 --- a/lib/plugins/pump.js +++ b/lib/plugins/pump.js @@ -1,12 +1,12 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); var times = require('../times'); var ALL_STATUS_FIELDS = ['reservoir', 'battery', 'clock', 'status', 'device']; function init (ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; var timeago = require('./timeago')(ctx); var openaps = require('./openaps')(ctx); diff --git a/lib/plugins/sensorage.js b/lib/plugins/sensorage.js index a28db2109df..a14b4f7c213 100644 --- a/lib/plugins/sensorage.js +++ b/lib/plugins/sensorage.js @@ -1,10 +1,10 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); var times = require('../times'); function init(ctx) { + var moment = ctx.moment; var translate = ctx.language.translate; var levels = ctx.levels; diff --git a/lib/plugins/virtAsstBase.js b/lib/plugins/virtAsstBase.js index 591bc5a24b9..e7cf2c731a3 100644 --- a/lib/plugins/virtAsstBase.js +++ b/lib/plugins/virtAsstBase.js @@ -1,9 +1,10 @@ 'use strict'; -var moment = require('moment'); var _each = require('lodash/each'); function init(env, ctx) { + var moment = ctx.moment; + function virtAsstBase() { return virtAsstBase; } diff --git a/lib/plugins/xdripjs.js b/lib/plugins/xdripjs.js index ffecc78c336..e1e64b89528 100644 --- a/lib/plugins/xdripjs.js +++ b/lib/plugins/xdripjs.js @@ -1,10 +1,10 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment'); var times = require('../times'); function init(ctx) { + var moment = ctx.moment; var levels = ctx.levels; var utils = require('../utils')(ctx); var firstPrefs = true; diff --git a/lib/profile/profileeditor.js b/lib/profile/profileeditor.js index d05d4ba1161..23c74e9bece 100644 --- a/lib/profile/profileeditor.js +++ b/lib/profile/profileeditor.js @@ -4,7 +4,6 @@ var init = function init () { //for the tests window isn't the global object var $ = window.$; var _ = window._; - var moment = window.moment; var Nightscout = window.Nightscout; var client = Nightscout.client; @@ -157,7 +156,7 @@ var init = function init () { // Load timezones timezoneInput.empty(); - moment.tz.names().forEach(function addTz(tz) { + client.ctx.timezones.forEach(function addTz(tz) { timezoneInput.append(''); }); @@ -198,8 +197,8 @@ var init = function init () { } databaseRecords.val(currentrecord); - timeInput.val(moment(mongorecords[currentrecord].startDate).format('HH:mm')); - dateInput.val(moment(mongorecords[currentrecord].startDate).format('YYYY-MM-DD')); + timeInput.val(client.ctx.moment(mongorecords[currentrecord].startDate).format('HH:mm')); + dateInput.val(client.ctx.moment(mongorecords[currentrecord].startDate).format('YYYY-MM-DD')); initProfile(); } @@ -635,11 +634,11 @@ var init = function init () { } function toTimeString(minfrommidnight) { - return moment.utc().startOf('day').add(minfrommidnight,'minutes').format('HH:mm'); // using utc to avoid daylight saving offset + return client.ctx.moment.utc().startOf('day').add(minfrommidnight,'minutes').format('HH:mm'); // using utc to avoid daylight saving offset } function toDisplayTime (minfrommidnight) { - var time = moment.utc().startOf('day').add(minfrommidnight,'minutes'); // using utc to avoid daylight saving offset + var time = client.ctx.moment.utc().startOf('day').add(minfrommidnight,'minutes'); // using utc to avoid daylight saving offset return client.settings.timeFormat === 24 ? time.format('HH:mm') : time.format('h:mm A'); } diff --git a/lib/profilefunctions.js b/lib/profilefunctions.js index 167e64ca5e7..bf5d9b09d3b 100644 --- a/lib/profilefunctions.js +++ b/lib/profilefunctions.js @@ -1,14 +1,15 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment-timezone'); var c = require('memory-cache'); var times = require('./times'); var cacheTTL = 5000; var prevBasalTreatment = null; -function init (profileData) { +function init (profileData, ctx) { + +var moment = ctx.moment; var cache = new c.Cache(); var profile = {}; diff --git a/lib/report/reportclient.js b/lib/report/reportclient.js index b62a5467b64..cb294640fe9 100644 --- a/lib/report/reportclient.js +++ b/lib/report/reportclient.js @@ -4,7 +4,6 @@ var init = function init () { //for the tests window isn't the global object var $ = window.$; var _ = window._; - var moment = window.moment; var Nightscout = window.Nightscout; var client = Nightscout.client; var report_plugins_preinit = Nightscout.report_plugins_preinit; @@ -12,6 +11,8 @@ var init = function init () { client.init(function loaded () { + var moment = client.ctx.moment; + report_plugins = report_plugins_preinit(client.ctx); Nightscout.report_plugins = report_plugins; diff --git a/lib/sandbox.js b/lib/sandbox.js index 85e0e86dc1d..6b338e7b1cc 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -55,7 +55,7 @@ function init () { sbx.language = ctx.language; sbx.translate = ctx.language.translate; - var profile = require('./profilefunctions')(); + var profile = require('./profilefunctions')(null, ctx); //Plugins will expect the right profile based on time profile.loadData(_.cloneDeep(ctx.ddata.profiles)); profile.updateTreatments(ctx.ddata.profileTreatments, ctx.ddata.tempbasalTreatments, ctx.ddata.combobolusTreatments); diff --git a/lib/server/bootevent.js b/lib/server/bootevent.js index 39bc0569ebb..edcd35c11cd 100644 --- a/lib/server/bootevent.js +++ b/lib/server/bootevent.js @@ -9,6 +9,7 @@ function boot (env, language) { console.log('Executing startBoot'); + ctx.moment = require('moment-timezone'); ctx.runtimeState = 'booting'; ctx.settings = env.settings; ctx.bus = require('../bus')(env.settings, ctx); @@ -211,6 +212,7 @@ function boot (env, language) { settings: env.settings , language: ctx.language , levels: ctx.levels + , moment: ctx.moment }).registerServerDefaults(); ctx.wares = require('../middleware/')(env); @@ -271,7 +273,7 @@ function boot (env, language) { function setupListeners (ctx, next) { console.log('Executing setupListeners'); - + if (hasBootErrors(ctx)) { return next(); } diff --git a/lib/utils.js b/lib/utils.js index 7d6d434472e..8cd32bba529 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,12 +1,11 @@ 'use strict'; var _ = require('lodash'); -var moment = require('moment-timezone'); var units = require('./units')(); function init(ctx) { - + var moment = ctx.moment; var settings = ctx.settings; var translate = ctx.language.translate; var timeago = require('./plugins/timeago')(ctx); diff --git a/tests/ar2.test.js b/tests/ar2.test.js index 91ae4353841..3b0d344a66f 100644 --- a/tests/ar2.test.js +++ b/tests/ar2.test.js @@ -1,21 +1,16 @@ 'use strict'; const should = require('should'); -const levels = require('../lib/levels'); -const fs = require('fs'); +const helper = require('./inithelper')(); const FIVE_MINS = 300000; const SIX_MINS = 360000; describe('ar2', function ( ) { - var ctx = { - settings: {} - , language: require('../lib/language')(fs) - , levels: levels - }; + var ctx = helper.getctx(); + ctx.ddata = require('../lib/data/ddata')(); ctx.notifications = require('../lib/notifications')(env, ctx); - ctx.levels = levels; var ar2 = require('../lib/plugins/ar2')(ctx); var bgnow = require('../lib/plugins/bgnow')(ctx); @@ -75,7 +70,7 @@ describe('ar2', function ( ) { }); ar2.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm(); - highest.level.should.equal(levels.WARN); + 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'); @@ -89,7 +84,7 @@ describe('ar2', function ( ) { var sbx = prepareSandbox(); ar2.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm(); - highest.level.should.equal(levels.URGENT); + highest.level.should.equal(helper.ctx.levels.URGENT); highest.title.should.equal('Urgent, HIGH'); done(); @@ -102,7 +97,7 @@ describe('ar2', function ( ) { var sbx = prepareSandbox(); ar2.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm(); - highest.level.should.equal(levels.WARN); + highest.level.should.equal(helper.ctx.levels.WARN); highest.title.should.equal('Warning, LOW'); done(); @@ -115,7 +110,7 @@ describe('ar2', function ( ) { var sbx = prepareSandbox(); ar2.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm(); - highest.level.should.equal(levels.WARN); + highest.level.should.equal(helper.ctx.levels.WARN); highest.title.should.equal('Warning, LOW predicted'); done(); @@ -128,7 +123,7 @@ describe('ar2', function ( ) { var sbx = prepareSandbox(); ar2.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm(); - highest.level.should.equal(levels.URGENT); + highest.level.should.equal(helper.ctx.levels.URGENT); highest.title.should.equal('Urgent, LOW predicted'); done(); @@ -143,7 +138,7 @@ describe('ar2', function ( ) { var sbx = prepareSandbox(); ar2.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm(); - highest.level.should.equal(levels.WARN); + highest.level.should.equal(helper.ctx.levels.WARN); highest.title.should.equal('Warning, LOW predicted'); done(); diff --git a/tests/basalprofileplugin.test.js b/tests/basalprofileplugin.test.js index 59b14463e04..c1ed3903089 100644 --- a/tests/basalprofileplugin.test.js +++ b/tests/basalprofileplugin.test.js @@ -2,6 +2,8 @@ const should = require('should'); const fs = require('fs'); const language = require('../lib/language')(fs); +const helper = require('./inithelper')(); + describe('basalprofile', function ( ) { var sandbox = require('../lib/sandbox')(); @@ -51,8 +53,7 @@ describe('basalprofile', function ( ) { ] }; - - var profile = require('../lib/profilefunctions')([profileData]); + var profile = require('../lib/profilefunctions')([profileData], helper.ctx); it('update basal profile pill', function (done) { var data = {}; @@ -68,7 +69,9 @@ describe('basalprofile', function ( ) { , language: language }; - var time = new Date('2015-06-21T00:00:00+00:00').getTime(); + var time = new Date('2015-06-21T00:00:00+00:00'); + + console.log('TIME1', time); var sbx = sandbox.clientInit(ctx, time, data); sbx.data.profile = profile; @@ -86,7 +89,7 @@ describe('basalprofile', function ( ) { , language: language }; - var time = new Date('2015-06-21T00:00:00+00:00').getTime(); + var time = new Date('2015-06-21T00:00:00+00:00'); var sbx = sandbox.clientInit(ctx, time, data); sbx.data.profile = profile; diff --git a/tests/bgnow.test.js b/tests/bgnow.test.js index b9c97e0a304..98be53a48eb 100644 --- a/tests/bgnow.test.js +++ b/tests/bgnow.test.js @@ -2,17 +2,14 @@ var should = require('should'); var _ = require('lodash'); +const helper = require('./inithelper')(); var FIVE_MINS = 300000; var SIX_MINS = 360000; describe('BG Now', function ( ) { - var ctx = { - language: require('../lib/language')() - , settings: require('../lib/settings')() - }; - - ctx.levels = require('../lib/levels'); + + const ctx = helper.ctx; var bgnow = require('../lib/plugins/bgnow')(ctx); var sandbox = require('../lib/sandbox')(ctx); @@ -70,6 +67,7 @@ describe('BG Now', function ( ) { } } , language: require('../lib/language')() + , moment: helper.ctx.moment }; var sbx = sandbox.clientInit(ctx, now, data); @@ -92,6 +90,7 @@ describe('BG Now', function ( ) { } , pluginBase: {} , language: require('../lib/language')() + , moment: helper.ctx.moment }; var data = {sgvs: [{mills: before, mgdl: 100}, {mills: now, mgdl: 105}]}; @@ -137,6 +136,7 @@ describe('BG Now', function ( ) { } , pluginBase: {} , language: require('../lib/language')() + , moment: helper.ctx.moment }; var data = {sgvs: [{mills: before, mgdl: 85}, {mills: now, mgdl: 85}]}; @@ -183,6 +183,7 @@ describe('BG Now', function ( ) { } , pluginBase: {} , language: require('../lib/language')() + , moment: helper.ctx.moment }; var data = {sgvs: [{mills: before - SIX_MINS, mgdl: 100}, {mills: now, mgdl: 105}]}; diff --git a/tests/boluswizardpreview.test.js b/tests/boluswizardpreview.test.js index 6cc4127e19b..7590e8700a9 100644 --- a/tests/boluswizardpreview.test.js +++ b/tests/boluswizardpreview.test.js @@ -1,16 +1,13 @@ var should = require('should'); var Stream = require('stream'); -var levels = require('../lib/levels'); +const helper = require('./inithelper')(); describe('boluswizardpreview', function ( ) { var env = require('../lib/server/env')(); env.testMode = true; - var ctx = { - settings: {} - , language: require('../lib/language')() - , levels: levels - }; + var ctx = helper.getctx(); + ctx.ddata = require('../lib/data/ddata')(); ctx.notifications = require('../lib/notifications')(env, ctx); @@ -138,6 +135,7 @@ describe('boluswizardpreview', function ( ) { units: 'mmol' } , pluginBase: {} + , moment: helper.ctx.moment }; ctx.language = require('../lib/language')(); @@ -145,7 +143,7 @@ describe('boluswizardpreview', function ( ) { var data = {sgvs: [{mills: before, mgdl: 100}, {mills: now, mgdl: 100}]}; data.treatments = [{mills: now, insulin: '1.0'}]; data.devicestatus = []; - data.profile = require('../lib/profilefunctions')([profileData]); + data.profile = require('../lib/profilefunctions')([profileData], ctx); var sbx = sandbox.clientInit(ctx, Date.now(), data); sbx.properties.iob = iob.calcTotal(data.treatments, data.devicestatus, data.profile, now); @@ -181,6 +179,7 @@ describe('boluswizardpreview', function ( ) { units: 'mmol' } , pluginBase: {} + , moment: helper.ctx.moment }; ctx.language = require('../lib/language')(); @@ -188,7 +187,7 @@ describe('boluswizardpreview', function ( ) { var data = {sgvs: [{mills: before, mgdl: 175}, {mills: now, mgdl: 153}]}; data.treatments = [{mills: now, insulin: '0.45'}]; data.devicestatus = []; - data.profile = require('../lib/profilefunctions')([profileData]); + data.profile = require('../lib/profilefunctions')([profileData], ctx); var sbx = sandbox.clientInit(ctx, Date.now(), data); sbx.properties.iob = iob.calcTotal(data.treatments, data.devicestatus, data.profile, now); @@ -229,7 +228,7 @@ describe('boluswizardpreview', function ( ) { boluswizardpreview.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm(); - highest.level.should.equal(levels.WARN); + highest.level.should.equal(ctx.levels.WARN); highest.title.should.equal('Warning, Check BG, time to bolus?'); highest.message.should.equal('BG Now: 180 +5 ↗ mg/dl\nBG 15m: 187 mg/dl\nBWP: 0.66U'); done(); @@ -240,11 +239,10 @@ describe('boluswizardpreview', function ( ) { ctx.ddata.sgvs = [{mills: before, mgdl: 295}, {mills: now, mgdl: 300}]; ctx.ddata.treatments = []; ctx.ddata.profiles = [profile]; - ctx.levels = require('../lib/levels'); var sbx = prepareSandbox(); boluswizardpreview.checkNotifications(sbx); - ctx.notifications.findHighestAlarm().level.should.equal(levels.URGENT); + ctx.notifications.findHighestAlarm().level.should.equal(ctx.levels.URGENT); done(); }); @@ -285,10 +283,11 @@ describe('boluswizardpreview', function ( ) { done(); } } + , moment: helper.ctx.moment }; ctx.language = require('../lib/language')(); - var loadedProfile = require('../lib/profilefunctions')(); + var loadedProfile = require('../lib/profilefunctions')(null, ctx); loadedProfile.loadData([profile]); var data = { diff --git a/tests/cannulaage.test.js b/tests/cannulaage.test.js index b9108b044c7..5b001248020 100644 --- a/tests/cannulaage.test.js +++ b/tests/cannulaage.test.js @@ -1,16 +1,16 @@ 'use strict'; require('should'); -var levels = require('../lib/levels'); +const helper = require('./inithelper')(); +const levels = helper.ctx.levels; describe('cage', function ( ) { var env = require('../lib/server/env')(); - var ctx = {}; + var ctx = helper.getctx(); + ctx.ddata = require('../lib/data/ddata')(); ctx.notifications = require('../lib/notifications')(env, ctx); - ctx.language = require('../lib/language')(); - ctx.levels = levels; - + var cage = require('../lib/plugins/cannulaage')(ctx); var sandbox = require('../lib/sandbox')(ctx); function prepareSandbox ( ) { diff --git a/tests/cob.test.js b/tests/cob.test.js index 2da45861550..4b54cddb699 100644 --- a/tests/cob.test.js +++ b/tests/cob.test.js @@ -1,15 +1,12 @@ 'use strict'; const _ = require('lodash'); -const fs = require('fs'); -const language = require('../lib/language')(fs); +const helper = require('./inithelper')(); require('should'); describe('COB', function ( ) { - var ctx = {}; - ctx.settings = {}; - ctx.language = language; + var ctx = helper.ctx; var cob = require('../lib/plugins/cob')(ctx); @@ -20,7 +17,7 @@ describe('COB', function ( ) { , carbs_hr: 30 }; - var profile = require('../lib/profilefunctions')([profileData]); + var profile = require('../lib/profilefunctions')([profileData], ctx); it('should calculate IOB, multiple treatments', function() { diff --git a/tests/inithelper.js b/tests/inithelper.js new file mode 100644 index 00000000000..c4de997c5fc --- /dev/null +++ b/tests/inithelper.js @@ -0,0 +1,24 @@ + +const fs = require('fs'); +const moment = require('moment-timezone'); +const language = require('../lib/language')(fs); +const settings = require('../lib/settings')(); +const levels = require('../lib/levels'); + +function helper() { + + helper.ctx = { + language: language + , settings: settings + , levels: levels + , moment: moment + }; + + helper.getctx = function getctx () { + return helper.ctx; + } + + return helper; +} + +module.exports = helper; diff --git a/tests/insulinage.test.js b/tests/insulinage.test.js index b21b54c011d..8fcb7e484b6 100644 --- a/tests/insulinage.test.js +++ b/tests/insulinage.test.js @@ -1,15 +1,14 @@ 'use strict'; require('should'); -var levels = require('../lib/levels'); +const helper = require('./inithelper')(); +const levels = helper.ctx.levels; describe('insulinage', function ( ) { var env = require('../lib/server/env')(); - var ctx = {}; - ctx.levels = levels; + var ctx = helper.getctx(); ctx.ddata = require('../lib/data/ddata')(); ctx.notifications = require('../lib/notifications')(env, ctx); - ctx.language = require('../lib/language')(); var iage = require('../lib/plugins/insulinage')(ctx); var sandbox = require('../lib/sandbox')(ctx); diff --git a/tests/iob.test.js b/tests/iob.test.js index b44099488c4..705c9efc992 100644 --- a/tests/iob.test.js +++ b/tests/iob.test.js @@ -2,11 +2,12 @@ const _ = require('lodash'); const should = require('should'); -const fs = require('fs'); +const helper = require('./inithelper')(); describe('IOB', function() { - var ctx = {}; - ctx.language = require('../lib/language')(fs); + + let ctx = helper.ctx; + ctx.settings = require('../lib/settings')(); var iob = require('../lib/plugins/iob')(ctx); @@ -55,7 +56,7 @@ describe('IOB', function() { dia: 3, sens: 0}; - var profile = require('../lib/profilefunctions')([profileData]); + var profile = require('../lib/profilefunctions')([profileData], ctx); var rightAfterBolus = iob.calcTotal(treatments, [], profile, time); @@ -113,8 +114,7 @@ describe('IOB', function() { dia: 4, sens: 0}; - var profile = require('../lib/profilefunctions')([profileData]); - + var profile = require('../lib/profilefunctions')([profileData], ctx); var rightAfterBolus = iob.calcTotal(treatments, [], profile, time); @@ -140,7 +140,7 @@ describe('IOB', function() { describe('from devicestatus', function () { var time = Date.now(); - var profile = require('../lib/profilefunctions')([{ dia: 3, sens: 0 }]); + var profile = require('../lib/profilefunctions')([{ dia: 3, sens: 0 }], ctx); var treatments = [{ mills: time - 1, insulin: '3.00' diff --git a/tests/loop.test.js b/tests/loop.test.js index 64f5bc81148..51d1c39338d 100644 --- a/tests/loop.test.js +++ b/tests/loop.test.js @@ -2,17 +2,12 @@ const _ = require('lodash'); const should = require('should'); -const moment = require('moment'); -const fs = require('fs'); -const language = require('../lib/language')(fs); -const levels = require('../lib/levels'); - -var ctx_top = { - language: language - , settings: require('../lib/settings')() - , levels: levels -}; +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); @@ -107,10 +102,10 @@ var statuses = [ } ]; -var now = moment(statuses[0].created_at); +var now = ctx_top.moment(statuses[0].created_at); _.forEach(statuses, function updateMills (status) { - status.mills = moment(status.created_at).valueOf(); + status.mills = ctx_top.moment(status.created_at).valueOf(); }); describe('loop', function ( ) { @@ -174,7 +169,7 @@ describe('loop', function ( ) { language: language }; - var errorTime = moment(statuses[1].created_at); + var errorTime = ctx_top.moment(statuses[1].created_at); var sbx = sandbox.clientInit(ctx, errorTime.valueOf(), {devicestatus: statuses}); @@ -242,7 +237,7 @@ describe('loop', function ( ) { loop.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm('Loop'); - highest.level.should.equal(levels.URGENT); + highest.level.should.equal(ctx_top.levels.URGENT); highest.title.should.equal('Loop isn\'t looping'); done(); }); diff --git a/tests/openaps.test.js b/tests/openaps.test.js index 1e31e934016..ce5d5306fe7 100644 --- a/tests/openaps.test.js +++ b/tests/openaps.test.js @@ -2,18 +2,14 @@ const _ = require('lodash'); const should = require('should'); -const moment = require('moment'); -const fs = require('fs'); -const language = require('../lib/language')(fs); +const helper = require('./inithelper')(); -var top_ctx = { - language: language - , settings: require('../lib/settings')() -}; +var top_ctx = helper.getctx(); top_ctx.language.set('en'); -var levels = require('../lib/levels'); -top_ctx.levels = levels; +const language = top_ctx.language; +const levels = top_ctx.levels; + var env = require('../lib/server/env')(); var openaps = require('../lib/plugins/openaps')(top_ctx); var sandbox = require('../lib/sandbox')(top_ctx); @@ -247,10 +243,10 @@ var statuses = [{ "created_at": "2017-09-05T19:19:39.899Z" }]; -var now = moment(statuses[0].created_at); +var now = top_ctx.moment(statuses[0].created_at); _.forEach(statuses, function updateMills (status) { - status.mills = moment(status.created_at).valueOf(); + status.mills = top_ctx.moment(status.created_at).valueOf(); }); describe('openaps', function ( ) { diff --git a/tests/profile.test.js b/tests/profile.test.js index 5928ccd2618..58de12cea2c 100644 --- a/tests/profile.test.js +++ b/tests/profile.test.js @@ -1,9 +1,11 @@ var should = require('should'); -var moment = require('moment-timezone'); +const helper = require('./inithelper')(); +const moment = helper.ctx.moment; describe('Profile', function ( ) { - var profile_empty = require('../lib/profilefunctions')(); + + var profile_empty = require('../lib/profilefunctions')(null, helper.ctx); beforeEach(function() { profile_empty.clear(); @@ -33,7 +35,7 @@ describe('Profile', function ( ) { , 'target_high': 120 }; - var profile = require('../lib/profilefunctions')([profileData]); + var profile = require('../lib/profilefunctions')([profileData],helper.ctx); var now = Date.now(); it('should know what the DIA is with old style profiles', function() { @@ -73,7 +75,7 @@ describe('Profile', function ( ) { it('should know how to reload data and still know what the low target is with old style profiles', function() { - var profile2 = require('../lib/profilefunctions')([profileData]); + var profile2 = require('../lib/profilefunctions')([profileData], helper.ctx); var profileData2 = { 'dia': 3, 'carbs_hr': 30, @@ -157,7 +159,7 @@ describe('Profile', function ( ) { 'units': 'mmol' }; - var complexProfile = require('../lib/profilefunctions')([complexProfileData]); + var complexProfile = require('../lib/profilefunctions')([complexProfileData], helper.ctx); var noon = new Date('2015-06-22 12:00:00').getTime(); var threepm = new Date('2015-06-22 15:00:00').getTime(); @@ -336,7 +338,7 @@ describe('Profile', function ( ) { } ]; - var multiProfile = require('../lib/profilefunctions')(multiProfileData); + var multiProfile = require('../lib/profilefunctions')(multiProfileData, helper.ctx); var noon = new Date('2015-06-22 12:00:00').getTime(); var threepm = new Date('2015-06-26 15:00:00').getTime(); diff --git a/tests/pump.test.js b/tests/pump.test.js index c2289fddd17..efe6e847dc3 100644 --- a/tests/pump.test.js +++ b/tests/pump.test.js @@ -2,19 +2,18 @@ var _ = require('lodash'); var should = require('should'); -var moment = require('moment'); -const fs = require('fs'); -const language = require('../lib/language')(fs); +const helper = require('./inithelper')(); +const moment = helper.ctx.moment; -var top_ctx = { - language: language - , settings: require('../lib/settings')() -}; +var top_ctx = helper.getctx(); +top_ctx.settings = require('../lib/settings')(); top_ctx.language.set('en'); + var env = require('../lib/server/env')(); -var levels = require('../lib/levels'); -var profile = require('../lib/profilefunctions')(); -top_ctx.levels = levels; +const levels = top_ctx.levels; +const language = top_ctx.language; + +var profile = require('../lib/profilefunctions')(null, top_ctx); var pump = require('../lib/plugins/pump')(top_ctx); var sandbox = require('../lib/sandbox')(top_ctx); @@ -93,7 +92,6 @@ var statuses2 = [{ } }]; - var now = moment(statuses[1].created_at); _.forEach(statuses, function updateMills (status) { diff --git a/tests/sensorage.test.js b/tests/sensorage.test.js index 11a6953cb37..7b102827e3e 100644 --- a/tests/sensorage.test.js +++ b/tests/sensorage.test.js @@ -2,15 +2,13 @@ var should = require('should'); var times = require('../lib/times'); -var levels = require('../lib/levels'); +const helper = require('./inithelper')(); describe('sage', function ( ) { var env = require('../lib/server/env')(); - var ctx = {}; - ctx.levels = levels; + var ctx = helper.getctx(); ctx.ddata = require('../lib/data/ddata')(); ctx.notifications = require('../lib/notifications')(env, ctx); - ctx.language = require('../lib/language')(); var sage = require('../lib/plugins/sensorage')(ctx); var sandbox = require('../lib/sandbox')(); @@ -152,7 +150,7 @@ describe('sage', function ( ) { sage.checkNotifications(sbx); var highest = ctx.notifications.findHighestAlarm('SAGE'); - highest.level.should.equal(levels.URGENT); + highest.level.should.equal(ctx.levels.URGENT); highest.title.should.equal('Sensor age 6 days 22 hours'); done(); }); diff --git a/tests/utils.test.js b/tests/utils.test.js index c6138f47491..068e1ab2a45 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -2,14 +2,18 @@ require('should'); +const helper = require('./inithelper')(); + describe('utils', function ( ) { - var utils = require('../lib/utils')({ - language: require('../lib/language')() - , settings: { - alarmTimeagoUrgentMins: 30 - , alarmTimeagoWarnMins: 15 - } - }); + + const ctx = helper.getctx(); + + ctx.settings = { + alarmTimeagoUrgentMins: 30 + , alarmTimeagoWarnMins: 15 + }; + + var utils = require('../lib/utils')(ctx); it('format numbers', function () { utils.toFixed(5.499999999).should.equal('5.50'); diff --git a/webpack/webpack.config.js b/webpack/webpack.config.js index 5fb24daabdc..9d8cb268711 100644 --- a/webpack/webpack.config.js +++ b/webpack/webpack.config.js @@ -2,7 +2,7 @@ const path = require('path'); const webpack = require('webpack'); const pluginArray = []; const sourceMapType = 'source-map'; -const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin'); +//const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin'); const projectRoot = path.resolve(__dirname, '..'); /* @@ -56,12 +56,14 @@ pluginArray.push(new webpack.ProvidePlugin({ process: 'process/browser', })); +/* // limit Timezone data from Moment pluginArray.push(new MomentTimezoneDataPlugin({ startYear: 2015, endYear: 2035, })); +*/ if (process.env.NODE_ENV === 'development') { const ESLintPlugin = require('eslint-webpack-plugin');