TrioAlertClassifierTests.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Trio Alerts: TrioAlertClassifier — alertIdentifier round-trip") struct TrioAlertClassifierIdentifierTests {
  5. /// Ground-truth slugs emitted by bundled pump managers. The classifier is
  6. /// substring-matched and case-insensitive, so the test inputs reproduce the
  7. /// real source-of-truth casing.
  8. // MARK: - OmnipodKit (covers Eros + DASH)
  9. @Test("Omnipod: lowReservoir → reservoirLow") func omnipodLowReservoir() {
  10. #expect(TrioAlertClassifier.categorize(alertIdentifier: "lowReservoir") == .reservoirLow)
  11. }
  12. @Test("Omnipod: podExpiring is a reminder, not an expiration") func omnipodPodExpiring() {
  13. #expect(TrioAlertClassifier.categorize(alertIdentifier: "podExpiring") == .deviceExpirationReminder)
  14. }
  15. @Test("Omnipod: userPodExpiration → reminder") func omnipodUserPodExpiration() {
  16. #expect(TrioAlertClassifier.categorize(alertIdentifier: "userPodExpiration") == .deviceExpirationReminder)
  17. }
  18. @Test("Omnipod: podExpireImminent → shutdown imminent") func omnipodExpireImminent() {
  19. #expect(TrioAlertClassifier.categorize(alertIdentifier: "podExpireImminent") == .podShutdownImminent)
  20. }
  21. @Test("Omnipod: suspendEnded → suspendTimeExpired") func omnipodSuspendEnded() {
  22. #expect(TrioAlertClassifier.categorize(alertIdentifier: "suspendEnded") == .suspendTimeExpired)
  23. }
  24. @Test("Omnipod: suspendEnded-repeating → suspendTimeExpired") func omnipodSuspendEndedRepeating() {
  25. #expect(TrioAlertClassifier.categorize(alertIdentifier: "suspendEnded-repeating") == .suspendTimeExpired)
  26. }
  27. @Test("Omnipod: unexpectedAlert → hardwareFault") func omnipodUnexpectedAlert() {
  28. #expect(TrioAlertClassifier.categorize(alertIdentifier: "unexpectedAlert") == .hardwareFault)
  29. }
  30. @Test("Omnipod: timeOffsetChangeDetected → other") func omnipodTimeOffset() {
  31. // Not a clinically actionable category — falls through to .other.
  32. if case .other = TrioAlertClassifier.categorize(alertIdentifier: "timeOffsetChangeDetected") {
  33. // ok
  34. } else {
  35. Issue.record("Expected .other for timeOffsetChangeDetected")
  36. }
  37. }
  38. @Test("Omnipod: lowRLBattery → batteryLow") func omnipodLowRLBattery() {
  39. #expect(TrioAlertClassifier.categorize(alertIdentifier: "lowRLBattery") == .batteryLow)
  40. }
  41. @Test("Omnipod: finishSetupReminder → other") func omnipodFinishSetup() {
  42. if case .other = TrioAlertClassifier.categorize(alertIdentifier: "finishSetupReminder") {
  43. // ok
  44. } else {
  45. Issue.record("Expected .other for finishSetupReminder")
  46. }
  47. }
  48. @Test("Omnipod: suspendInProgress → other") func omnipodSuspendInProgress() {
  49. if case .other = TrioAlertClassifier.categorize(alertIdentifier: "suspendInProgress") {
  50. // ok
  51. } else {
  52. Issue.record("Expected .other for suspendInProgress")
  53. }
  54. }
  55. // MARK: - DanaKit
  56. @Test("Dana: batteryZeroPercent → batteryEmpty") func danaBatteryZero() {
  57. #expect(TrioAlertClassifier.categorize(alertIdentifier: "batteryZeroPercent") == .batteryEmpty)
  58. }
  59. @Test("Dana: pumpError → hardwareFault") func danaPumpError() {
  60. #expect(TrioAlertClassifier.categorize(alertIdentifier: "pumpError") == .hardwareFault)
  61. }
  62. @Test("Dana: occlusion") func danaOcclusion() {
  63. #expect(TrioAlertClassifier.categorize(alertIdentifier: "occlusion") == .occlusion)
  64. }
  65. @Test("Dana: lowBattery → batteryLow") func danaLowBattery() {
  66. #expect(TrioAlertClassifier.categorize(alertIdentifier: "lowBattery") == .batteryLow)
  67. }
  68. @Test("Dana: shutdown → hardwareFault") func danaShutdown() {
  69. // The classifier matches the exact lowercased "shutdown" to hardwareFault.
  70. #expect(TrioAlertClassifier.categorize(alertIdentifier: "shutdown") == .hardwareFault)
  71. }
  72. @Test("Dana: emptyReservoir → reservoirEmpty") func danaEmptyReservoir() {
  73. #expect(TrioAlertClassifier.categorize(alertIdentifier: "emptyReservoir") == .reservoirEmpty)
  74. }
  75. @Test("Dana: remainingInsulinLevel → reservoirLow") func danaRemainingInsulin() {
  76. #expect(TrioAlertClassifier.categorize(alertIdentifier: "remainingInsulinLevel") == .reservoirLow)
  77. }
  78. @Test("Dana: checkShaft → hardwareFault") func danaCheckShaft() {
  79. #expect(TrioAlertClassifier.categorize(alertIdentifier: "checkShaft") == .hardwareFault)
  80. }
  81. @Test("Dana: ble5InvalidKeys → other") func danaInvalidKeys() {
  82. if case .other = TrioAlertClassifier.categorize(alertIdentifier: "ble5InvalidKeys") {
  83. // ok
  84. } else {
  85. Issue.record("Expected .other for ble5InvalidKeys")
  86. }
  87. }
  88. // MARK: - MinimedKit
  89. @Test("Minimed: PumpBatteryLow → batteryLow") func minimedBatteryLow() {
  90. #expect(TrioAlertClassifier.categorize(alertIdentifier: "PumpBatteryLow") == .batteryLow)
  91. }
  92. @Test("Minimed: PumpReservoirEmpty → reservoirEmpty") func minimedReservoirEmpty() {
  93. #expect(TrioAlertClassifier.categorize(alertIdentifier: "PumpReservoirEmpty") == .reservoirEmpty)
  94. }
  95. @Test("Minimed: PumpReservoirLow → reservoirLow") func minimedReservoirLow() {
  96. #expect(TrioAlertClassifier.categorize(alertIdentifier: "PumpReservoirLow") == .reservoirLow)
  97. }
  98. // MARK: - Trio internal slugs (APSManager + NotLoopingMonitor + GlucoseAlertCoordinator)
  99. @Test("Trio: occlusion") func trioOcclusion() {
  100. #expect(TrioAlertClassifier.categorize(alertIdentifier: "occlusion") == .occlusion)
  101. }
  102. @Test("Trio: reservoirLow") func trioReservoirLow() {
  103. #expect(TrioAlertClassifier.categorize(alertIdentifier: "reservoirLow") == .reservoirLow)
  104. }
  105. @Test("Trio: reservoirEmpty") func trioReservoirEmpty() {
  106. #expect(TrioAlertClassifier.categorize(alertIdentifier: "reservoirEmpty") == .reservoirEmpty)
  107. }
  108. @Test("Trio: batteryLow") func trioBatteryLow() {
  109. #expect(TrioAlertClassifier.categorize(alertIdentifier: "batteryLow") == .batteryLow)
  110. }
  111. @Test("Trio: batteryEmpty") func trioBatteryEmpty() {
  112. #expect(TrioAlertClassifier.categorize(alertIdentifier: "batteryEmpty") == .batteryEmpty)
  113. }
  114. @Test("Trio: hardwareFault") func trioHardwareFault() {
  115. #expect(TrioAlertClassifier.categorize(alertIdentifier: "hardwareFault") == .hardwareFault)
  116. }
  117. @Test("Trio: deliveryUncertain") func trioDeliveryUncertain() {
  118. #expect(TrioAlertClassifier.categorize(alertIdentifier: "deliveryUncertain") == .deliveryUncertain)
  119. }
  120. @Test("Trio: deviceExpirationReminder") func trioReminder() {
  121. #expect(TrioAlertClassifier.categorize(alertIdentifier: "deviceExpirationReminder") == .deviceExpirationReminder)
  122. }
  123. @Test("Trio: deviceExpired") func trioExpired() {
  124. #expect(TrioAlertClassifier.categorize(alertIdentifier: "deviceExpired") == .deviceExpired)
  125. }
  126. @Test("Trio: podShutdownImminent") func trioShutdownImminent() {
  127. #expect(TrioAlertClassifier.categorize(alertIdentifier: "podShutdownImminent") == .podShutdownImminent)
  128. }
  129. @Test("Trio: suspendTimeExpired") func trioSuspendTimeExpired() {
  130. #expect(TrioAlertClassifier.categorize(alertIdentifier: "suspendTimeExpired") == .suspendTimeExpired)
  131. }
  132. @Test("Trio: bolusFailed") func trioBolusFailed() {
  133. #expect(TrioAlertClassifier.categorize(alertIdentifier: "bolusFailed") == .bolusFailed)
  134. }
  135. @Test("Trio: manualTempBasalActive") func trioManualTempBasal() {
  136. #expect(TrioAlertClassifier.categorize(alertIdentifier: "manualTempBasalActive") == .manualTempBasalActive)
  137. }
  138. @Test("Trio: notLooping") func trioNotLooping() {
  139. #expect(TrioAlertClassifier.categorize(alertIdentifier: "notLooping") == .notLooping)
  140. }
  141. @Test("Trio: loop.notActive (NotLoopingMonitor slug)") func trioLoopNotActive() {
  142. #expect(TrioAlertClassifier.categorize(alertIdentifier: "loop.notActive") == .notLooping)
  143. }
  144. @Test("Trio: sensorFailure") func trioSensorFailure() {
  145. #expect(TrioAlertClassifier.categorize(alertIdentifier: "sensorFailure") == .sensorFailure)
  146. }
  147. @Test("Trio: algorithmError") func trioAlgorithmError() {
  148. #expect(TrioAlertClassifier.categorize(alertIdentifier: "algorithmError") == .algorithmError)
  149. }
  150. @Test("Trio: glucose.urgentLow.<uuid>") func trioGlucoseUrgentLow() {
  151. #expect(
  152. TrioAlertClassifier
  153. .categorize(alertIdentifier: "glucose.urgentLow.5C9C3D2A-1234-4321-9876-ABCDEF012345") == .glucoseUrgentLow
  154. )
  155. }
  156. @Test("Trio: glucose.low.<uuid>") func trioGlucoseLow() {
  157. #expect(TrioAlertClassifier.categorize(alertIdentifier: "glucose.low.uuid-1") == .glucoseLow)
  158. }
  159. @Test("Trio: glucose.forecastedLow.<uuid>") func trioGlucoseForecastedLow() {
  160. #expect(TrioAlertClassifier.categorize(alertIdentifier: "glucose.forecastedLow.uuid-2") == .glucoseForecastedLow)
  161. }
  162. @Test("Trio: glucose.high.<uuid>") func trioGlucoseHigh() {
  163. #expect(TrioAlertClassifier.categorize(alertIdentifier: "glucose.high.uuid-3") == .glucoseHigh)
  164. }
  165. // MARK: - CGM driver slugs the classifier should recognize when bridges land
  166. @Test("CGM: sensorFailed → sensorFailure") func cgmSensorFailed() {
  167. #expect(TrioAlertClassifier.categorize(alertIdentifier: "sensorFailed") == .sensorFailure)
  168. }
  169. @Test("CGM: sensorStopped → sensorFailure") func cgmSensorStopped() {
  170. #expect(TrioAlertClassifier.categorize(alertIdentifier: "sensorStopped") == .sensorFailure)
  171. }
  172. @Test("CGM: transmitterDisconnected → sensorFailure") func cgmTransmitterDisconnected() {
  173. #expect(TrioAlertClassifier.categorize(alertIdentifier: "transmitterDisconnected") == .sensorFailure)
  174. }
  175. @Test("CGM: sensorExpired → deviceExpired") func cgmSensorExpired() {
  176. #expect(TrioAlertClassifier.categorize(alertIdentifier: "sensorExpired") == .deviceExpired)
  177. }
  178. @Test("CGM: transmitterEoL → deviceExpired") func cgmTransmitterEoL() {
  179. #expect(TrioAlertClassifier.categorize(alertIdentifier: "transmitterEoL") == .deviceExpired)
  180. }
  181. @Test("CGM: sensorGrace → reminder, not expired") func cgmSensorGrace() {
  182. #expect(TrioAlertClassifier.categorize(alertIdentifier: "sensorGrace") == .deviceExpirationReminder)
  183. }
  184. @Test("CGM: gracePeriod → reminder") func cgmGracePeriod() {
  185. #expect(TrioAlertClassifier.categorize(alertIdentifier: "gracePeriod") == .deviceExpirationReminder)
  186. }
  187. @Test("CGM: transmitterError → hardwareFault") func cgmTransmitterError() {
  188. #expect(TrioAlertClassifier.categorize(alertIdentifier: "transmitterError") == .hardwareFault)
  189. }
  190. @Test("CGM: criticalFault → hardwareFault") func cgmCriticalFault() {
  191. #expect(TrioAlertClassifier.categorize(alertIdentifier: "criticalFault") == .hardwareFault)
  192. }
  193. // MARK: - Glucose family wins over substring overlap
  194. @Test("Glucose stale beats the generic 'glucose' fallthrough") func glucoseDataStale() {
  195. #expect(TrioAlertClassifier.categorize(alertIdentifier: "glucoseDataStale") == .glucoseDataStale)
  196. }
  197. // MARK: - Unrecognized slug
  198. @Test("Unrecognized slug → .other with original identifier") func unrecognizedFallsThrough() {
  199. let category = TrioAlertClassifier.categorize(alertIdentifier: "totallyMadeUpSlug")
  200. if case let .other(id) = category {
  201. #expect(id == "totallyMadeUpSlug")
  202. } else {
  203. Issue.record("Expected .other(\"totallyMadeUpSlug\")")
  204. }
  205. }
  206. }
  207. @Suite("Trio Alerts: TrioAlertClassifier — error round-trip") struct TrioAlertClassifierErrorTests {
  208. @Test("APSError.invalidPumpState → hardwareFault") func invalidPumpState() {
  209. let err = APSError.invalidPumpState(message: "x")
  210. #expect(TrioAlertClassifier.categorize(error: err) == .hardwareFault)
  211. }
  212. @Test("APSError.glucoseError → glucoseDataStale") func glucoseError() {
  213. let err = APSError.glucoseError(message: "x")
  214. #expect(TrioAlertClassifier.categorize(error: err) == .glucoseDataStale)
  215. }
  216. @Test("APSError.apsError → algorithmError") func apsErrorCase() {
  217. let err = APSError.apsError(message: "x")
  218. #expect(TrioAlertClassifier.categorize(error: err) == .algorithmError)
  219. }
  220. @Test("APSError.manualBasalTemp → manualTempBasalActive") func manualBasalTemp() {
  221. let err = APSError.manualBasalTemp(message: "x")
  222. #expect(TrioAlertClassifier.categorize(error: err) == .manualTempBasalActive)
  223. }
  224. /// `categorize(pumpError:)` keys on `String(describing:)`, not
  225. /// `localizedDescription`. Match that contract with `CustomStringConvertible`
  226. /// so the fake errors actually expose the substring the classifier looks for.
  227. private struct PumpFake: Error, CustomStringConvertible {
  228. let description: String
  229. }
  230. @Test("APSError.pumpError(occlusion) descends into pumpError categorize") func pumpErrorOcclusion() {
  231. let err = APSError.pumpError(PumpFake(description: "Pump occlusion detected"))
  232. #expect(TrioAlertClassifier.categorize(error: err) == .occlusion)
  233. }
  234. @Test("APSError.pumpError(uncertain) → deliveryUncertain") func pumpErrorUncertain() {
  235. let err = APSError.pumpError(PumpFake(description: "UncertainDelivery: bolus may have failed"))
  236. #expect(TrioAlertClassifier.categorize(error: err) == .deliveryUncertain)
  237. }
  238. @Test("APSError.pumpError(timeout) → commsTransient") func pumpErrorComms() {
  239. let err = APSError.pumpError(PumpFake(description: "Communication timeout, no response"))
  240. #expect(TrioAlertClassifier.categorize(error: err) == .commsTransient)
  241. }
  242. @Test("Generic error → .other") func genericError() {
  243. struct OddError: Error {}
  244. let category = TrioAlertClassifier.categorize(error: OddError())
  245. if case .other = category {
  246. // ok
  247. } else {
  248. Issue.record("Expected .other for unrecognized error")
  249. }
  250. }
  251. }
  252. @Suite("Trio Alerts: TrioAlertClassifier — surfacing rules") struct TrioAlertCategorySurfacingTests {
  253. @Test("commsTransient is the only category dwell-gated at both boundaries") func commsTransientNotImmediate() {
  254. #expect(!TrioAlertCategory.commsTransient.shouldFireImmediately)
  255. // Spot-check that everything else surfaces immediately.
  256. let immediate: [TrioAlertCategory] = [
  257. .occlusion, .reservoirLow, .reservoirEmpty, .batteryLow, .batteryEmpty,
  258. .hardwareFault, .deliveryUncertain, .deviceExpirationReminder, .deviceExpired,
  259. .podShutdownImminent, .suspendTimeExpired, .bolusFailed, .manualTempBasalActive,
  260. .notLooping, .sensorFailure, .glucoseUrgentLow, .glucoseLow, .glucoseForecastedLow,
  261. .glucoseHigh, .glucoseDataStale, .algorithmError, .other("any")
  262. ]
  263. for category in immediate {
  264. #expect(category.shouldFireImmediately, "Expected \(category) to surface immediately")
  265. }
  266. }
  267. @Test("sensorFailure default tier is Critical") func sensorFailureCritical() {
  268. #expect(PumpAlertCategory.sensorFailure.defaultSeverity == .critical)
  269. }
  270. @Test("algorithmError defaults to Normal") func algorithmErrorNormal() {
  271. #expect(PumpAlertCategory.algorithmError.defaultSeverity == .normal)
  272. }
  273. @Test("Identifier slug round-trips through the classifier for every category") func slugRoundTrip() {
  274. for category in PumpAlertCategory.allCases {
  275. // Build the canonical slug from the matching TrioAlertCategory if available.
  276. let trioCategory = TrioAlertCategory.allCasesForRoundTripTest
  277. .first(where: { PumpAlertCategory(trioCategory: $0) == category })
  278. guard let trioCategory else { continue }
  279. let slug = trioCategory.alertIdentifier
  280. let reclassified = TrioAlertClassifier.categorize(alertIdentifier: slug)
  281. #expect(
  282. reclassified == trioCategory,
  283. "Slug \"\(slug)\" should reclassify as \(trioCategory) but got \(reclassified)"
  284. )
  285. }
  286. }
  287. }
  288. // Helper: TrioAlertCategory is not CaseIterable because of `.other(String)`,
  289. // so we enumerate the round-trippable cases explicitly for the round-trip test.
  290. extension TrioAlertCategory {
  291. static var allCasesForRoundTripTest: [TrioAlertCategory] {
  292. [
  293. .occlusion, .reservoirLow, .reservoirEmpty, .batteryLow, .batteryEmpty,
  294. .hardwareFault, .deliveryUncertain, .deviceExpirationReminder, .deviceExpired,
  295. .podShutdownImminent, .suspendTimeExpired, .bolusFailed, .manualTempBasalActive,
  296. .notLooping, .sensorFailure, .glucoseUrgentLow, .glucoseLow, .glucoseForecastedLow,
  297. .glucoseHigh, .glucoseDataStale, .algorithmError, .commsTransient
  298. ]
  299. }
  300. }