Skip to content

Commit e234bf2

Browse files
authored
LOOP-3980 Avoid cancelling temp basal on launch. (#499)
* Avoid cancelling temp basal on launch * Add test for ignoring initial value of isClosedLoop
1 parent 1c85112 commit e234bf2

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Loop/Managers/LoopDataManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ final class LoopDataManager: LoopSettingsAlerterDelegate {
181181
// Cancel any active temp basal when going into closed loop off mode
182182
// The dispatch is necessary in case this is coming from a didSet already on the settings struct.
183183
self.automaticDosingStatus.$isClosedLoop
184+
.dropFirst()
184185
.removeDuplicates()
185186
.receive(on: DispatchQueue.main)
186187
.sink { if !$0 {

LoopTests/Managers/LoopDataManagerTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,62 @@ class LoopDataManagerDosingTests: XCTestCase {
498498
XCTAssertEqual(recommendedBolus!.amount, 1.52, accuracy: 0.01)
499499
}
500500

501+
func testIsClosedLoopAvoidsTriggeringTempBasalCancelOnCreation() {
502+
let settings = LoopSettings(
503+
dosingEnabled: false,
504+
glucoseTargetRangeSchedule: glucoseTargetRangeSchedule,
505+
maximumBasalRatePerHour: maxBasalRate,
506+
maximumBolus: maxBolus,
507+
suspendThreshold: suspendThreshold
508+
)
509+
510+
let doseStore = MockDoseStore()
511+
let glucoseStore = MockGlucoseStore()
512+
let carbStore = MockCarbStore()
513+
514+
let currentDate = Date()
515+
516+
dosingDecisionStore = MockDosingDecisionStore()
517+
automaticDosingStatus = AutomaticDosingStatus(isClosedLoop: false, isClosedLoopAllowed: true)
518+
let existingTempBasal = DoseEntry(
519+
type: .tempBasal,
520+
startDate: currentDate.addingTimeInterval(-.minutes(2)),
521+
endDate: currentDate.addingTimeInterval(.minutes(28)),
522+
value: 1.0,
523+
unit: .unitsPerHour,
524+
deliveredUnits: nil,
525+
description: "Mock Temp Basal",
526+
syncIdentifier: "asdf",
527+
scheduledBasalRate: nil,
528+
insulinType: .novolog,
529+
automatic: true,
530+
manuallyEntered: false,
531+
isMutable: true)
532+
loopDataManager = LoopDataManager(
533+
lastLoopCompleted: currentDate.addingTimeInterval(-.minutes(5)),
534+
basalDeliveryState: .tempBasal(existingTempBasal),
535+
settings: settings,
536+
overrideHistory: TemporaryScheduleOverrideHistory(),
537+
lastPumpEventsReconciliation: nil, // this date is only used to init the doseStore if a DoseStoreProtocol isn't passed in, so this date can be nil
538+
analyticsServicesManager: AnalyticsServicesManager(),
539+
localCacheDuration: .days(1),
540+
doseStore: doseStore,
541+
glucoseStore: glucoseStore,
542+
carbStore: carbStore,
543+
dosingDecisionStore: dosingDecisionStore,
544+
settingsStore: MockSettingsStore(),
545+
now: { currentDate },
546+
pumpInsulinType: .novolog,
547+
automaticDosingStatus: automaticDosingStatus
548+
)
549+
let mockDelegate = MockDelegate()
550+
loopDataManager.delegate = mockDelegate
551+
552+
// Dose enacting happens asynchronously, as does receiving isClosedLoop signals
553+
waitOnMain(timeout: 5)
554+
XCTAssertNil(mockDelegate.recommendation)
555+
}
556+
501557
}
502558

503559
extension LoopDataManagerDosingTests {

0 commit comments

Comments
 (0)