ProfileJsonTests.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("Profile testing using JSON inputs", .serialized) struct ProfileJsonTests {
  5. let timeZoneForTests = TimeZoneForTests()
  6. @Test(
  7. "Profile 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 profileInputs = algorithmComparison.makeProfileInput else {
  22. print("Skipping, no profileInputs 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(profileInputs: profileInputs)
  33. print("Checked \(filePath) \(algorithmComparison.timezone)")
  34. timeZoneForTests.resetTimezone()
  35. }
  36. }
  37. func checkFixedJsAgainstSwift(profileInputs: MakeProfileInputs) async throws {
  38. let openAps = OpenAPSFixed()
  39. let profileResultSwift = OpenAPSSwift.makeProfile(
  40. preferences: profileInputs.preferences,
  41. pumpSettings: profileInputs.pumpSettings,
  42. bgTargets: profileInputs.bgTargets,
  43. basalProfile: profileInputs.basalProfile,
  44. isf: profileInputs.isf,
  45. carbRatio: profileInputs.carbRatios,
  46. tempTargets: profileInputs.tempTargets,
  47. model: profileInputs.model,
  48. trioSettings: profileInputs.trioSettings,
  49. clock: profileInputs.clock
  50. )
  51. let profileResultJavascript = await openAps.makeProfileJavascript(
  52. preferences: profileInputs.preferences,
  53. pumpSettings: profileInputs.pumpSettings,
  54. bgTargets: profileInputs.bgTargets,
  55. basalProfile: profileInputs.basalProfile,
  56. isf: profileInputs.isf,
  57. carbRatio: profileInputs.carbRatios,
  58. tempTargets: profileInputs.tempTargets,
  59. model: profileInputs.model,
  60. autotune: RawJSON.null,
  61. trioSettings: profileInputs.trioSettings,
  62. clock: profileInputs.clock
  63. )
  64. let comparison = JSONCompare.createComparison(
  65. function: .makeProfile,
  66. swift: profileResultSwift,
  67. swiftDuration: 0.1,
  68. javascript: profileResultJavascript,
  69. javascriptDuration: 0.1,
  70. iobInputs: nil,
  71. mealInputs: nil,
  72. autosensInputs: nil,
  73. determineBasalInputs: nil,
  74. makeProfileInputs: 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. }