MealJsonTests.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Meal testing using JSON inputs", .serialized) struct MealJsonTests {
  5. let timeZoneForTests = TimeZoneForTests()
  6. @Test(
  7. "Meal 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 mealInputs = algorithmComparison.mealInput else {
  22. print("Skipping, no mealInputs 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. // The JS implementation of IoB when the pump is suspend is so fundamentally
  32. // broken that I wasn't able to fix it in JS. So we'll just skip these, but I
  33. // verified them by hand and the Swift implementation appears to be correct
  34. if let mostRecentPumpEvent = mealInputs.pumpHistory.filter({ $0.isExternal != true }).first {
  35. if mostRecentPumpEvent.type == .pumpSuspend
  36. {
  37. print("Skipping, known issue with JS and currently suspended pumps")
  38. continue
  39. }
  40. }
  41. timeZoneForTests.setTimezone(identifier: algorithmComparison.timezone)
  42. try await checkFixedJsAgainstSwift(mealInputs: mealInputs)
  43. print("Checked \(filePath) \(algorithmComparison.timezone)")
  44. timeZoneForTests.resetTimezone()
  45. }
  46. }
  47. func checkFixedJsAgainstSwift(mealInputs: MealInputs) async throws {
  48. let openAps = OpenAPSFixed()
  49. let (mealResultSwift, _) = OpenAPSSwift.meal(
  50. pumphistory: mealInputs.pumpHistory,
  51. profile: try JSONBridge.to(mealInputs.profile),
  52. basalProfile: mealInputs.basalProfile,
  53. clock: mealInputs.clock,
  54. carbs: mealInputs.carbs,
  55. glucose: mealInputs.glucose
  56. )
  57. let mealResultJavascript = await openAps.mealJavascript(
  58. pumphistory: mealInputs.pumpHistory,
  59. profile: try JSONBridge.to(mealInputs.profile),
  60. basalProfile: mealInputs.basalProfile,
  61. clock: mealInputs.clock,
  62. carbs: mealInputs.carbs,
  63. glucose: mealInputs.glucose
  64. )
  65. let comparison = JSONCompare.createComparison(
  66. function: .meal,
  67. swift: mealResultSwift,
  68. swiftDuration: 0.1,
  69. javascript: mealResultJavascript,
  70. javascriptDuration: 0.1,
  71. iobInputs: nil,
  72. mealInputs: nil,
  73. autosensInputs: nil,
  74. determineBasalInputs: nil
  75. )
  76. if comparison.resultType == .valueDifference {
  77. print(comparison.differences!.prettyPrintedJSON!)
  78. }
  79. if comparison.resultType != .matching {
  80. print("REPLAY ERROR: Fixed JS didn't match")
  81. }
  82. #expect(comparison.resultType == .matching)
  83. }
  84. @Test("Format meal inputs for running in JS", .enabled(if: false)) func formatInputs() async throws {
  85. let openAps = OpenAPSFixed()
  86. // this test is meant for one-off analysis so it's ok to hard code
  87. // a file, just make sure to _not_ check in updates to this to
  88. // avoid polluting our change logs
  89. let algorithmComparison = try await HttpFiles.downloadFile(at: "/files/7a8a377e-f483-46a5-adbb-290baa04801b.3.json")
  90. let mealInputs = algorithmComparison.mealInput!
  91. let encoder = JSONCoding.encoder
  92. let output = try encoder.encode(mealInputs)
  93. let sharedDir = FileManager.default.temporaryDirectory
  94. let outputURL = sharedDir.appendingPathComponent("meal_error_inputs.json")
  95. // Print the path so you can find it
  96. print("Writing to: \(outputURL.path)")
  97. try output.write(to: outputURL)
  98. timeZoneForTests.setTimezone(identifier: algorithmComparison.timezone)
  99. let (mealResultSwift, _) = OpenAPSSwift.meal(
  100. pumphistory: mealInputs.pumpHistory,
  101. profile: try JSONBridge.to(mealInputs.profile),
  102. basalProfile: mealInputs.basalProfile,
  103. clock: mealInputs.clock,
  104. carbs: mealInputs.carbs,
  105. glucose: mealInputs.glucose
  106. )
  107. print("Swift result")
  108. switch mealResultSwift {
  109. case let .success(rawJson):
  110. print(rawJson)
  111. case let .failure(error):
  112. print(error.localizedDescription)
  113. }
  114. let mealResultJavascript = await openAps.mealJavascript(
  115. pumphistory: mealInputs.pumpHistory,
  116. profile: try JSONBridge.to(mealInputs.profile),
  117. basalProfile: mealInputs.basalProfile,
  118. clock: mealInputs.clock,
  119. carbs: mealInputs.carbs,
  120. glucose: mealInputs.glucose
  121. )
  122. print("Fixed JS result")
  123. switch mealResultJavascript {
  124. case let .success(rawJson):
  125. print(rawJson)
  126. case let .failure(error):
  127. print(error.localizedDescription)
  128. }
  129. timeZoneForTests.resetTimezone()
  130. }
  131. }