AutosensJsonExtraTests.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Autosens using real JSON from bundle", .serialized) struct AutosensJsonExtraTests {
  5. let timeZoneForTests = TimeZoneForTests()
  6. // static func from<T: Decodable>(string: String) throws -> T
  7. func loadJson<T: Decodable>(_ name: String) throws -> T {
  8. let testBundle = Bundle(for: BundleReference.self)
  9. let path = testBundle.path(forResource: name, ofType: "json")!
  10. let data = try Data(contentsOf: URL(fileURLWithPath: path))
  11. return try JSONCoding.decoder.decode(T.self, from: data)
  12. }
  13. @Test("Test with resistance") func generateJavascriptInputs() throws {
  14. let glucose: [BloodGlucose] = try loadJson("as-glucose")
  15. let pump: [PumpHistoryEvent] = try loadJson("as-pump")
  16. let basalProfile: [BasalProfileEntry] = try loadJson("as-basal")
  17. let profile: Profile = try loadJson("as-profile")
  18. let carbs: [CarbsEntry] = try loadJson("as-carbs")
  19. let tempTargets: [TempTarget] = try loadJson("as-temp-targets")
  20. let clock = Date("2025-06-08T00:14:35.481Z")!
  21. timeZoneForTests.setTimezone(identifier: "America/Los_Angeles")
  22. let autosensResult = try AutosensGenerator.generate(
  23. glucose: glucose,
  24. pumpHistory: pump,
  25. basalProfile: basalProfile,
  26. profile: profile,
  27. carbs: carbs,
  28. tempTargets: tempTargets,
  29. maxDeviations: 96,
  30. clock: clock,
  31. includeDeviationsForTesting: true
  32. )
  33. let deviationsUnsorted: [Decimal] = try loadJson("deviationsUnsorted")
  34. #expect(autosensResult.ratio == 1.2)
  35. #expect(autosensResult.newisf == 46)
  36. #expect(deviationsUnsorted.count == autosensResult.deviationsUnsorted?.count)
  37. for (ref, calc) in zip(deviationsUnsorted, autosensResult.deviationsUnsorted!) {
  38. // we can get differences due to rounding inconsistencies between
  39. // javascript and swift with negative numbers
  40. #expect(ref.isWithin(0.01, of: calc))
  41. }
  42. timeZoneForTests.resetTimezone()
  43. }
  44. }