DetermineBasalJsonTests.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("DetermineBasal testing using JSON inputs", .serialized) struct DetermineBasalJsonTests {
  5. let timeZoneForTests = TimeZoneForTests()
  6. @Test(
  7. "DetermineBasal should produce same results for fixed JS",
  8. .enabled(if: ReplayTests.enabled)
  9. ) func replayErrorInputs() async throws {
  10. // Note: This test case can only test one timezone per invocation
  11. // so you need to manually change this to try out errors from
  12. // different timezones
  13. let testingTimezone = ReplayTests.timezone
  14. let files = try await HttpFiles.listFiles()
  15. for filePath in files {
  16. let algorithmComparison = try await HttpFiles.downloadFile(at: filePath)
  17. print("Checking \(filePath) @ \(algorithmComparison.createdAt)")
  18. guard algorithmComparison.timezone == testingTimezone else {
  19. continue
  20. }
  21. guard let determineBasalInput = algorithmComparison.determineBasalInput else {
  22. print("Skipping, no determineBasalInput found")
  23. if let str = algorithmComparison.comparisonError {
  24. print(str)
  25. }
  26. if let str = algorithmComparison.swiftException {
  27. print(str)
  28. }
  29. continue
  30. }
  31. timeZoneForTests.setTimezone(identifier: algorithmComparison.timezone)
  32. try await checkFixedJsAgainstSwift(determineBasalInput: determineBasalInput)
  33. print("Checked \(filePath) \(algorithmComparison.timezone)")
  34. timeZoneForTests.resetTimezone()
  35. }
  36. }
  37. func checkFixedJsAgainstSwift(determineBasalInput: DetermineBasalInputs) async throws {
  38. let openAps = OpenAPSFixed()
  39. let (determineBasalResultSwift, _) = OpenAPSSwift.determineBasal(
  40. glucose: determineBasalInput.glucose,
  41. currentTemp: determineBasalInput.currentTemp,
  42. iob: try JSONBridge.to(determineBasalInput.iob),
  43. profile: try JSONBridge.to(determineBasalInput.profile),
  44. autosens: try JSONBridge.to(determineBasalInput.autosens),
  45. meal: try JSONBridge.to(determineBasalInput.meal),
  46. microBolusAllowed: determineBasalInput.microBolusAllowed,
  47. reservoir: determineBasalInput.reservoir ?? 0,
  48. pumpHistory: determineBasalInput.pumpHistory,
  49. preferences: determineBasalInput.preferences,
  50. basalProfile: determineBasalInput.basalProfile,
  51. trioCustomOrefVariables: determineBasalInput.trioCustomOrefVariables,
  52. clock: determineBasalInput.clock
  53. )
  54. let determineBasalResultJavascript = try await openAps.determineBasalJavascript(
  55. glucose: determineBasalInput.glucose,
  56. currentTemp: determineBasalInput.currentTemp,
  57. iob: try JSONBridge.to(determineBasalInput.iob),
  58. profile: try JSONBridge.to(determineBasalInput.profile),
  59. autosens: try JSONBridge.to(determineBasalInput.autosens),
  60. meal: try JSONBridge.to(determineBasalInput.meal),
  61. microBolusAllowed: determineBasalInput.microBolusAllowed,
  62. reservoir: determineBasalInput.reservoir ?? 0,
  63. pumpHistory: determineBasalInput.pumpHistory,
  64. preferences: determineBasalInput.preferences,
  65. basalProfile: determineBasalInput.basalProfile,
  66. trioCustomOrefVariables: determineBasalInput.trioCustomOrefVariables,
  67. clock: determineBasalInput.clock
  68. )
  69. let comparison = JSONCompare.createComparison(
  70. function: .determineBasal,
  71. swift: determineBasalResultSwift,
  72. swiftDuration: 0.1,
  73. javascript: determineBasalResultJavascript,
  74. javascriptDuration: 0.1,
  75. iobInputs: nil,
  76. mealInputs: nil,
  77. autosensInputs: nil,
  78. determineBasalInputs: nil,
  79. makeProfileInputs: nil
  80. )
  81. if comparison.resultType == .valueDifference {
  82. print(comparison.differences!.prettyPrintedJSON!)
  83. }
  84. if comparison.resultType != .matching {
  85. print("REPLAY ERROR: Fixed JS didn't match")
  86. }
  87. #expect(comparison.resultType == .matching)
  88. }
  89. @Test("Format determineBasal inputs for running in JS", .enabled(if: false)) func formatInputs() async throws {
  90. let openAps = OpenAPSFixed()
  91. // this test is meant for one-off analysis so it's ok to hard code
  92. // a file, just make sure to _not_ check in updates to this to
  93. // avoid polluting our change logs
  94. let algorithmComparison = try await HttpFiles.downloadFile(at: "/files/f1d04efa-c39b-4f0a-9955-65ab663ff9fb.0.json")
  95. let determineBasalInput = algorithmComparison.determineBasalInput!
  96. let encoder = JSONCoding.encoder
  97. let output = try encoder.encode(determineBasalInput)
  98. let sharedDir = FileManager.default.temporaryDirectory
  99. let outputURL = sharedDir.appendingPathComponent("determine_basal_error_inputs.json")
  100. // Print the path so you can find it
  101. print("Writing to: \(outputURL.path)")
  102. try output.write(to: outputURL)
  103. timeZoneForTests.setTimezone(identifier: algorithmComparison.timezone)
  104. let (determineBasalResultSwift, _) = OpenAPSSwift.determineBasal(
  105. glucose: determineBasalInput.glucose,
  106. currentTemp: determineBasalInput.currentTemp,
  107. iob: try JSONBridge.to(determineBasalInput.iob),
  108. profile: try JSONBridge.to(determineBasalInput.profile),
  109. autosens: try JSONBridge.to(determineBasalInput.autosens),
  110. meal: try JSONBridge.to(determineBasalInput.meal),
  111. microBolusAllowed: determineBasalInput.microBolusAllowed,
  112. reservoir: determineBasalInput.reservoir ?? 0,
  113. pumpHistory: determineBasalInput.pumpHistory,
  114. preferences: determineBasalInput.preferences,
  115. basalProfile: determineBasalInput.basalProfile,
  116. trioCustomOrefVariables: determineBasalInput.trioCustomOrefVariables,
  117. clock: determineBasalInput.clock
  118. )
  119. print("Swift result")
  120. switch determineBasalResultSwift {
  121. case let .success(rawJson):
  122. print(rawJson)
  123. case let .failure(error):
  124. print(error.localizedDescription)
  125. }
  126. let determineBasalResultJavascript = try await openAps.determineBasalJavascript(
  127. glucose: determineBasalInput.glucose,
  128. currentTemp: determineBasalInput.currentTemp,
  129. iob: try JSONBridge.to(determineBasalInput.iob),
  130. profile: try JSONBridge.to(determineBasalInput.profile),
  131. autosens: try JSONBridge.to(determineBasalInput.autosens),
  132. meal: try JSONBridge.to(determineBasalInput.meal),
  133. microBolusAllowed: determineBasalInput.microBolusAllowed,
  134. reservoir: determineBasalInput.reservoir ?? 0,
  135. pumpHistory: determineBasalInput.pumpHistory,
  136. preferences: determineBasalInput.preferences,
  137. basalProfile: determineBasalInput.basalProfile,
  138. trioCustomOrefVariables: determineBasalInput.trioCustomOrefVariables,
  139. clock: determineBasalInput.clock
  140. )
  141. print("Fixed JS result")
  142. switch determineBasalResultJavascript {
  143. case let .success(rawJson):
  144. print(rawJson)
  145. case let .failure(error):
  146. print(error.localizedDescription)
  147. }
  148. let comparison = JSONCompare.createComparison(
  149. function: .determineBasal,
  150. swift: determineBasalResultSwift,
  151. swiftDuration: 0.1,
  152. javascript: determineBasalResultJavascript,
  153. javascriptDuration: 0.1,
  154. iobInputs: nil,
  155. mealInputs: nil,
  156. autosensInputs: nil,
  157. determineBasalInputs: nil,
  158. makeProfileInputs: nil
  159. )
  160. if comparison.resultType == .valueDifference {
  161. print(comparison.differences!.prettyPrintedJSON!)
  162. printForecasts(comparison.differences)
  163. }
  164. #expect(comparison.resultType == .matching)
  165. timeZoneForTests.resetTimezone()
  166. }
  167. func printForecasts(_ values: [String: Any]?) {
  168. guard let values = values else { return }
  169. guard let forecasts = values["predBGs"] as? Trio.ValueDifference else { return }
  170. let js = forecasts.js.toDictionary()
  171. let swift = forecasts.swift.toDictionary()
  172. for forecastType in ["IOB", "ZT", "UAM", "COB"] {
  173. print("")
  174. guard let swiftForecast = swift[forecastType]?.toIntArray(),
  175. let jsForecast = js[forecastType]?.toIntArray()
  176. else {
  177. print("missing \(forecastType) forecast, skipping")
  178. continue
  179. }
  180. if swiftForecast.count == jsForecast.count {
  181. print(forecastType)
  182. } else {
  183. print("\(forecastType) has length mismatch ❌")
  184. }
  185. print("Row\tSft\tJS\tMatch")
  186. print("--------------")
  187. for (row, values) in zip(swiftForecast, jsForecast).enumerated() {
  188. let pass: String
  189. if abs(values.0 - values.1) <= 1 {
  190. pass = "✅"
  191. } else {
  192. pass = "❌"
  193. }
  194. print("\(row)\t\(values.0)\t\(values.1)\t\(pass)")
  195. }
  196. }
  197. }
  198. }
  199. extension JSONValue {
  200. func toDictionary() -> [String: Trio.JSONValue] {
  201. switch self {
  202. case let .object(dict):
  203. return dict
  204. default:
  205. fatalError()
  206. }
  207. }
  208. func toIntArray() -> [Int] {
  209. switch self {
  210. case let .array(array):
  211. return array.map { $0.toInt() }
  212. default:
  213. fatalError()
  214. }
  215. }
  216. func toInt() -> Int {
  217. switch self {
  218. case let .number(number):
  219. return Int(number)
  220. default:
  221. fatalError()
  222. }
  223. }
  224. }