Repository navigation
Expand file tree
/
Copy pathSetupComparisonTests.swift
More file actions
114 lines (97 loc) · 5.47 KB
/
Copy pathSetupComparisonTests.swift
File metadata and controls
114 lines (97 loc) · 5.47 KB
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
//
// SetupComparisonTests.swift
// LoopTests
//
// Copyright © 2026 LoopKit Authors. All rights reserved.
//
import XCTest
import LoopAlgorithm
import LoopCore
import LoopKit
@testable import Loop
final class SetupComparisonTests: XCTestCase {
private let changeDate = Date(timeIntervalSinceReferenceDate: 800_000_000)
/// Readings every 5 minutes; `value` gives mg/dL for each reading's date.
private func samples(from start: Date, to end: Date, value: (Date) -> Double) -> [StoredGlucoseSample] {
stride(from: start.timeIntervalSinceReferenceDate, to: end.timeIntervalSinceReferenceDate, by: .minutes(5)).map {
let date = Date(timeIntervalSinceReferenceDate: $0)
return StoredGlucoseSample(startDate: date, quantity: LoopQuantity(unit: .milligramsPerDeciliter, doubleValue: value(date)))
}
}
private func change(previousDate: Date? = nil) -> SetupChange {
SetupChange(date: changeDate, isLowerBound: false, items: [.therapySetting(.carbRatio)], previousDate: previousDate)
}
func testBeforeAndAfter() throws {
let now = changeDate.addingTimeInterval(.days(5))
// Before: all in range. After: one reading in ten is low.
var index = 0
let glucose = samples(from: changeDate - .days(25), to: now) { date in
index += 1
return date < self.changeDate ? 120 : (index % 10 == 0 ? 60 : 120)
}
let comparison = try XCTUnwrap(SetupComparison(change: change(), samples: glucose, insulinHistory: nil, now: now))
let before = try XCTUnwrap(comparison.before)
XCTAssertEqual(before.start, changeDate - .days(21))
XCTAssertEqual(before.end, changeDate)
XCTAssertEqual(before.timeInRange ?? 0, 1, accuracy: 0.001)
XCTAssertEqual(before.timeBelowRange ?? 1, 0, accuracy: 0.001)
XCTAssertEqual(comparison.after.start, changeDate)
XCTAssertEqual(comparison.after.timeBelowRange ?? 0, 0.1, accuracy: 0.01)
XCTAssertTrue(comparison.isSettling)
}
func testBeforeIgnoresThePreviousChange() throws {
let now = changeDate.addingTimeInterval(.days(15))
let glucose = samples(from: changeDate - .days(25), to: now) { _ in 120 }
let comparison = try XCTUnwrap(SetupComparison(change: change(previousDate: changeDate - .hours(12)), samples: glucose, insulinHistory: nil, now: now))
XCTAssertEqual(comparison.before?.start, changeDate - .days(21))
XCTAssertEqual(comparison.before?.days ?? 0, 21, accuracy: 0.001)
XCTAssertFalse(comparison.isSettling)
}
func testNoBeforeWithoutEnoughCGMData() throws {
let now = changeDate.addingTimeInterval(.days(3))
let glucose = samples(from: changeDate - .days(2), to: now) { _ in 120 }
let comparison = try XCTUnwrap(SetupComparison(change: change(), samples: glucose, insulinHistory: nil, now: now))
XCTAssertNil(comparison.before)
}
func testAfterRunsUntilNow() throws {
let now = changeDate.addingTimeInterval(.days(30))
let glucose = samples(from: changeDate - .days(25), to: now) { _ in 120 }
let comparison = try XCTUnwrap(SetupComparison(change: change(), samples: glucose, insulinHistory: nil, now: now))
XCTAssertEqual(comparison.after.start, changeDate)
XCTAssertEqual(comparison.after.end, now)
XCTAssertFalse(comparison.isSettling)
}
func testNothingUntilADayHasPassed() {
let now = changeDate.addingTimeInterval(.hours(20))
let glucose = samples(from: changeDate - .days(14), to: now) { _ in 120 }
XCTAssertNil(SetupComparison(change: change(), samples: glucose, insulinHistory: nil, now: now))
}
func testNothingForALowerBound() {
let now = changeDate.addingTimeInterval(.days(20))
let bound = SetupChange(date: changeDate, isLowerBound: true, items: [])
XCTAssertNil(SetupComparison(change: bound, samples: [], insulinHistory: nil, now: now))
}
func testInsulinPerDay() throws {
let now = changeDate.addingTimeInterval(.days(2))
let start = changeDate - .days(21)
let glucose = samples(from: start, to: now) { _ in 120 }
let bolus = SimpleInsulinDose(deliveryType: .bolus, automatic: false, startDate: changeDate + .hours(1), endDate: changeDate + .hours(1), volume: 4, insulinModel: LoopAlgorithm.ExponentialInsulinModelPreset.rapidActingAdult)
let history = InsulinDeliveryHistory(
doses: [bolus],
scheduledBasal: [AbsoluteScheduleValue(startDate: start, endDate: now, value: 1.0)],
coverageStart: start,
end: now)
let comparison = try XCTUnwrap(SetupComparison(change: change(), samples: glucose, insulinHistory: history, now: now))
XCTAssertEqual(comparison.before?.insulinPerDay ?? 0, 24, accuracy: 0.001)
XCTAssertEqual(comparison.after.insulinPerDay ?? 0, 26, accuracy: 0.001)
}
func testNoInsulinWhenDosesDontCoverThePeriod() throws {
let now = changeDate.addingTimeInterval(.days(2))
let glucose = samples(from: changeDate - .days(21), to: now) { _ in 120 }
let history = InsulinDeliveryHistory(doses: [], scheduledBasal: [], coverageStart: changeDate - .days(1), end: now)
let comparison = try XCTUnwrap(SetupComparison(change: change(), samples: glucose, insulinHistory: history, now: now))
XCTAssertNotNil(comparison.before)
XCTAssertNil(comparison.before?.insulinPerDay)
XCTAssertNotNil(comparison.after.insulinPerDay)
}
}