@@ -969,6 +969,32 @@ describe('DataUtil', () => {
969969 } ) ;
970970
971971 context ( 'dosingDecision' , ( ) => {
972+ it ( 'should convert requestedBolus.amount to requestedBolus.normal and remove amount' , ( ) => {
973+ const datum = {
974+ type : 'dosingDecision' ,
975+ requestedBolus : { amount : 3.5 } ,
976+ } ;
977+
978+ dataUtil . validateDatumIn = sinon . stub ( ) . returns ( true ) ;
979+ dataUtil . normalizeDatumIn ( datum ) ;
980+ expect ( datum . requestedBolus . normal ) . to . equal ( 3.5 ) ;
981+ expect ( datum . requestedBolus ) . to . not . have . property ( 'amount' ) ;
982+ } ) ;
983+
984+ it ( 'should convert recommendedBolus.normal and recommendedBolus.extended to recommendedBolus.amount and remove normal, extended, duration' , ( ) => {
985+ const datum = {
986+ type : 'dosingDecision' ,
987+ recommendedBolus : { normal : 2 , extended : 1 , duration : 60000 } ,
988+ } ;
989+
990+ dataUtil . validateDatumIn = sinon . stub ( ) . returns ( true ) ;
991+ dataUtil . normalizeDatumIn ( datum ) ;
992+ expect ( datum . recommendedBolus . amount ) . to . equal ( 3 ) ;
993+ expect ( datum . recommendedBolus ) . to . not . have . property ( 'normal' ) ;
994+ expect ( datum . recommendedBolus ) . to . not . have . property ( 'extended' ) ;
995+ expect ( datum . recommendedBolus ) . to . not . have . property ( 'duration' ) ;
996+ } ) ;
997+
972998 it ( 'should add the datum to the `bolusDosingDecisionDatumsByIdMap`' , ( ) => {
973999 dataUtil . validateDatumIn = sinon . stub ( ) . returns ( true ) ;
9741000
@@ -1039,7 +1065,51 @@ describe('DataUtil', () => {
10391065 } ) ;
10401066
10411067 describe ( 'joinBolusAndDosingDecision' , ( ) => {
1042- it ( 'should join loop dosing decisions, and associated pump settings, to boluses that are within a minute of each other' , ( ) => {
1068+ it ( 'should join loop dosing decisions, and associated pump settings, to boluses that are definitively associated by ID' , ( ) => {
1069+ const uploadId = 'upload1' ;
1070+ const upload = { type : 'upload' , id : uploadId , dataSetType : 'continuous' , uploadId, time : Date . parse ( '2024-02-02T10:05:59.000Z' ) , client : { name : 'org.tidepool.Loop' } } ;
1071+ const bolus = { type : 'bolus' , id : 'bolus1' , uploadId, time : Date . parse ( '2024-02-02T10:05:59.000Z' ) , origin : { name : 'org.tidepool.Loop' } } ;
1072+ const bolus2 = { type : 'bolus' , id : 'bolus2' , uploadId, time : Date . parse ( '2024-02-02T11:05:59.000Z' ) , origin : { name : 'org.tidepool.Loop' } } ;
1073+ const pumpSettings = { ...loopMultirate , id : 'pumpSettings1' } ;
1074+
1075+ const dosingDecision = {
1076+ type : 'dosingDecision' ,
1077+ id : 'dosingDecision1' ,
1078+ time : Date . parse ( '2024-02-02T10:05:00.000Z' ) ,
1079+ origin : { name : 'org.tidepool.Loop' } ,
1080+ associations : [
1081+ { reason : 'bolus' , id : 'bolus2' } ,
1082+ { reason : 'pumpSettings' , id : 'pumpSettings1' } ,
1083+ ] ,
1084+ requestedBolus : { normal : 12 } ,
1085+ insulinOnBoard : { amount : 4 } ,
1086+ food : { nutrition : { carbohydrate : { net : 30 } } } ,
1087+ bgHistorical : [
1088+ { value : 100 } ,
1089+ { value : 110 } ,
1090+ ] ,
1091+ } ;
1092+
1093+ dataUtil . bolusDosingDecisionDatumsByIdMap = { dosingDecision1 : dosingDecision } ;
1094+ dataUtil . pumpSettingsDatumsByIdMap = { pumpSettings1 : pumpSettings } ;
1095+ dataUtil . loopDataSetsByIdMap = { [ uploadId ] : upload } ;
1096+
1097+ _ . each ( [ bolus , bolus2 ] , dataUtil . joinBolusAndDosingDecision ) ;
1098+ // should not attach dosing decision to bolus that is not associated
1099+ expect ( bolus . dosingDecision ) . to . be . undefined ;
1100+
1101+ // should attach associated pump settings to dosingDecisions
1102+ expect ( bolus2 . dosingDecision ) . to . eql ( dosingDecision ) ;
1103+ expect ( bolus2 . dosingDecision . pumpSettings ) . to . eql ( pumpSettings ) ;
1104+
1105+ // should translate relevant dosing decision data onto expected bolus fields
1106+ expect ( bolus2 . expectedNormal ) . to . equal ( 12 ) ;
1107+ expect ( bolus2 . carbInput ) . to . equal ( 30 ) ;
1108+ expect ( bolus2 . bgInput ) . to . equal ( 110 ) ;
1109+ expect ( bolus2 . insulinOnBoard ) . to . equal ( 4 ) ;
1110+ } ) ;
1111+
1112+ it ( 'should join loop dosing decisions, and associated pump settings, to boluses that are within a minute of each other if not definitive associations exist' , ( ) => {
10431113 const uploadId = 'upload1' ;
10441114 const upload = { type : 'upload' , id : uploadId , dataSetType : 'continuous' , uploadId, time : Date . parse ( '2024-02-02T10:05:59.000Z' ) , client : { name : 'org.tidepool.Loop' } } ;
10451115 const bolus = { type : 'bolus' , id : 'bolus1' , uploadId, time : Date . parse ( '2024-02-02T10:05:59.000Z' ) , origin : { name : 'org.tidepool.Loop' } } ;
@@ -1076,6 +1146,38 @@ describe('DataUtil', () => {
10761146 expect ( bolus . insulinOnBoard ) . to . equal ( 4 ) ;
10771147 } ) ;
10781148
1149+ it ( 'should not add expectedNormal to joined loop dosing decisions if the requested normal is equal to the bolus normal' , ( ) => {
1150+ const uploadId = 'upload1' ;
1151+ const upload = { type : 'upload' , id : uploadId , dataSetType : 'continuous' , uploadId, time : Date . parse ( '2024-02-02T10:05:59.000Z' ) , client : { name : 'org.tidepool.Loop' } } ;
1152+ const bolus = { type : 'bolus' , id : 'bolus1' , uploadId, time : Date . parse ( '2024-02-02T10:05:59.000Z' ) , origin : { name : 'org.tidepool.Loop' } , normal : 12 } ;
1153+ const pumpSettings = { ...loopMultirate , id : 'pumpSettings1' } ;
1154+
1155+ const dosingDecision = {
1156+ type : 'dosingDecision' ,
1157+ id : 'dosingDecision1' ,
1158+ time : Date . parse ( '2024-02-02T10:05:00.000Z' ) ,
1159+ origin : { name : 'org.tidepool.Loop' } ,
1160+ associations : [ { reason : 'pumpSettings' , id : 'pumpSettings1' } ] ,
1161+ requestedBolus : { normal : 12 } ,
1162+ insulinOnBoard : { amount : 4 } ,
1163+ food : { nutrition : { carbohydrate : { net : 30 } } } ,
1164+ bgHistorical : [
1165+ { value : 100 } ,
1166+ { value : 110 } ,
1167+ ] ,
1168+ } ;
1169+
1170+ dataUtil . bolusDosingDecisionDatumsByIdMap = { dosingDecision1 : dosingDecision } ;
1171+ dataUtil . pumpSettingsDatumsByIdMap = { pumpSettings1 : pumpSettings } ;
1172+ dataUtil . loopDataSetsByIdMap = { [ uploadId ] : upload } ;
1173+
1174+ dataUtil . joinBolusAndDosingDecision ( bolus ) ;
1175+ expect ( bolus . dosingDecision ) . to . eql ( dosingDecision ) ;
1176+
1177+ // should not add the bolus.expectedNormal field since the requested normal is equal to the bolus normal
1178+ expect ( bolus . expectedNormal ) . to . be . undefined ;
1179+ } ) ;
1180+
10791181 it ( 'should not join loop dosing decisions to boluses that are outside of a minute of each other' , ( ) => {
10801182 const dosingDecision = { type : 'dosingDecision' , id : 'dosingDecision1' , time : Date . parse ( '2024-02-02T10:05:00.000Z' ) , origin : { name : 'org.tidepool.Loop' } } ;
10811183 const bolus = { type : 'bolus' , id : 'bolus1' , time : Date . parse ( '2024-02-02T10:06:01.000Z' ) , origin : { name : 'org.tidepool.Loop' } } ;
0 commit comments