ProfileJsNativeCompareTests.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import Foundation
  2. @testable import FreeAPS
  3. import Testing
  4. @Suite("Profile js vs native comparison") struct ProfileJsNativeCompareTests {
  5. // Base test inputs that match the JavaScript test setup
  6. private func createBaseInputs() -> (
  7. Preferences,
  8. PumpSettings,
  9. BGTargets,
  10. [BasalProfileEntry],
  11. InsulinSensitivities,
  12. CarbRatios,
  13. [TempTarget],
  14. String,
  15. FreeAPSSettings
  16. ) {
  17. let pumpSettings = PumpSettings(
  18. insulinActionCurve: 10,
  19. maxBolus: 10,
  20. maxBasal: 2
  21. )
  22. let bgTargets = BGTargets(
  23. units: .mgdL,
  24. userPreferredUnits: .mgdL,
  25. targets: [
  26. BGTargetEntry(low: 100, high: 120, start: "00:00", offset: 0)
  27. ]
  28. )
  29. let basalProfile = [
  30. BasalProfileEntry(start: "00:00", minutes: 0, rate: 1.0)
  31. ]
  32. let isf = InsulinSensitivities(
  33. units: .mgdL,
  34. userPreferredUnits: .mgdL,
  35. sensitivities: [
  36. InsulinSensitivityEntry(sensitivity: 100, offset: 0, start: "00:00")
  37. ]
  38. )
  39. let preferences = Preferences()
  40. let carbRatios = CarbRatios(
  41. units: .grams,
  42. schedule: [
  43. CarbRatioEntry(start: "00:00", offset: 0, ratio: 20)
  44. ]
  45. )
  46. let tempTargets: [TempTarget] = []
  47. let model = "\"250\""
  48. let freeaps = FreeAPSSettings()
  49. return (preferences, pumpSettings, bgTargets, basalProfile, isf, carbRatios, tempTargets, model, freeaps)
  50. }
  51. @Test("should compare Profile for js and native with base inputs") func withBasicInputs() async throws {
  52. let inputs = createBaseInputs()
  53. let openAps = OpenAPS(storage: BaseFileStorage())
  54. let profileJs = try! await openAps.makeProfileJavascript(
  55. preferences: inputs.0,
  56. pumpSettings: inputs.1,
  57. bgTargets: inputs.2,
  58. basalProfile: inputs.3,
  59. isf: inputs.4,
  60. carbRatio: inputs.5,
  61. tempTargets: inputs.6,
  62. model: inputs.7,
  63. autotune: RawJSON.null,
  64. freeaps: inputs.8
  65. )
  66. let profileNative = OpenAPSSwift.makeProfile(
  67. preferences: inputs.0,
  68. pumpSettings: inputs.1,
  69. bgTargets: inputs.2,
  70. basalProfile: inputs.3,
  71. isf: inputs.4,
  72. carbRatio: inputs.5,
  73. tempTargets: inputs.6,
  74. model: inputs.7,
  75. freeaps: inputs.8
  76. )
  77. let differences = try! JSONCompare.differences(function: .makeProfile, native: profileNative, javascript: profileJs)
  78. if !differences.isEmpty {
  79. JSONCompare.prettyPrint(differences)
  80. }
  81. #expect(differences.isEmpty)
  82. }
  83. }