Selaa lähdekoodia

Merge pull request #547 from nightscout/fix-oref-swift-tests

Small fixes for oref-swift unit tests
Deniz Cengiz 10 kuukautta sitten
vanhempi
commit
aa5d66b1e5

+ 4 - 6
Trio.xcodeproj/project.pbxproj

@@ -219,6 +219,7 @@
 		3B1C5C482D68E269004E9273 /* IobHistoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B1C5C362D68E269004E9273 /* IobHistoryTests.swift */; };
 		3B2F77862D7E52ED005ED9FA /* TDD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B2F77852D7E52ED005ED9FA /* TDD.swift */; };
 		3B2F77882D7E5387005ED9FA /* CurrentTDDSetup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B2F77872D7E5387005ED9FA /* CurrentTDDSetup.swift */; };
+		3B31D5742E0E26C00047D32D /* ReplayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B31D5732E0E26BB0047D32D /* ReplayTests.swift */; };
 		3B3B57C92DA07B3400849D16 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3B57C82DA07B3400849D16 /* GoogleService-Info.plist */; };
 		3B4196E02D8C4BC00091DFF7 /* HomeStateModel+CGM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B4196DF2D8C4BBB0091DFF7 /* HomeStateModel+CGM.swift */; };
 		3B4550532D862C0000551B0D /* PumpHistoryEvent+Duplicates.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B4550522D862BF200551B0D /* PumpHistoryEvent+Duplicates.swift */; };
@@ -299,7 +300,6 @@
 		3BAD36CC2D7D420E00CC298D /* CoreDataInitializationCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BAD36CB2D7D420500CC298D /* CoreDataInitializationCoordinator.swift */; };
 		3BBB76AA2E01C70B0040977D /* MealCob.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BBB76A92E01C7070040977D /* MealCob.swift */; };
 		3BBC22632DF5B94100169236 /* AutosensTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BBC22622DF5B93900169236 /* AutosensTests.swift */; };
-		3BBC227C2DF6F87200169236 /* HttpFiles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BBC227B2DF6F86700169236 /* HttpFiles.swift */; };
 		3BC0AA3B2DA74C87000DF7B7 /* iob-total.js in Resources */ = {isa = PBXBuildFile; fileRef = 3BC0AA3A2DA74C87000DF7B7 /* iob-total.js */; };
 		3BC0AA3E2DA817EC000DF7B7 /* iob-calculate.js in Resources */ = {isa = PBXBuildFile; fileRef = 3BC0AA3C2DA817EC000DF7B7 /* iob-calculate.js */; };
 		3BC0AA3F2DA817EC000DF7B7 /* iob-history.js in Resources */ = {isa = PBXBuildFile; fileRef = 3BC0AA3D2DA817EC000DF7B7 /* iob-history.js */; };
@@ -1153,6 +1153,7 @@
 		3B1C5C3E2D68E269004E9273 /* IobJsonTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IobJsonTypes.swift; sourceTree = "<group>"; };
 		3B2F77852D7E52ED005ED9FA /* TDD.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TDD.swift; sourceTree = "<group>"; };
 		3B2F77872D7E5387005ED9FA /* CurrentTDDSetup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentTDDSetup.swift; sourceTree = "<group>"; };
+		3B31D5732E0E26BB0047D32D /* ReplayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplayTests.swift; sourceTree = "<group>"; };
 		3B3B57C82DA07B3400849D16 /* GoogleService-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
 		3B4196DF2D8C4BBB0091DFF7 /* HomeStateModel+CGM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HomeStateModel+CGM.swift"; sourceTree = "<group>"; };
 		3B4550522D862BF200551B0D /* PumpHistoryEvent+Duplicates.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PumpHistoryEvent+Duplicates.swift"; sourceTree = "<group>"; };
@@ -1211,7 +1212,6 @@
 		3BAD36CB2D7D420500CC298D /* CoreDataInitializationCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataInitializationCoordinator.swift; sourceTree = "<group>"; };
 		3BBB76A92E01C7070040977D /* MealCob.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MealCob.swift; sourceTree = "<group>"; };
 		3BBC22622DF5B93900169236 /* AutosensTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutosensTests.swift; sourceTree = "<group>"; };
-		3BBC227B2DF6F86700169236 /* HttpFiles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpFiles.swift; sourceTree = "<group>"; };
 		3BC0AA3A2DA74C87000DF7B7 /* iob-total.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "iob-total.js"; sourceTree = "<group>"; };
 		3BC0AA3C2DA817EC000DF7B7 /* iob-calculate.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "iob-calculate.js"; sourceTree = "<group>"; };
 		3BC0AA3D2DA817EC000DF7B7 /* iob-history.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "iob-history.js"; sourceTree = "<group>"; };
@@ -2815,11 +2815,11 @@
 		3B1C5C3F2D68E269004E9273 /* utils */ = {
 			isa = PBXGroup;
 			children = (
-				3BBC227B2DF6F86700169236 /* HttpFiles.swift */,
 				3B1C5C3D2D68E269004E9273 /* Extensions.swift */,
 				3B4821812E080CAE00F0DD17 /* HttpFiles.swift */,
 				3B1C5C3E2D68E269004E9273 /* IobJsonTypes.swift */,
 				3BF92F372D86E106006B545A /* OpenAPSFixed.swift */,
+				3B31D5732E0E26BB0047D32D /* ReplayTests.swift */,
 				3B8B5D3D2DF5240600365ED3 /* TimeZoneForTests.swift */,
 			);
 			path = utils;
@@ -2871,8 +2871,6 @@
 				DD30BA172E078F8100DA677C /* ComputedInsulinSensitivities+Getter.swift */,
 				DD30BA112E07763E00DA677C /* Profile+TherapySettingGetter.swift */,
 				DD30BA0D2E076FFC00DA677C /* Profile+Autosens.swift */,
-				3B4550522D862BF200551B0D /* PumpHistoryEvent+Duplicates.swift */,
-				3B5F45B52D6A239000F70982 /* DoubleApproximateMatching.swift */,
 				3B5CD2A32D4AEA5D00CE213C /* Date+MinutesFromMidnight.swift */,
 				3BCE75B42D4B3917009E9453 /* Decimal+rounding.swift */,
 				3B5F45B52D6A239000F70982 /* DoubleApproximateMatching.swift */,
@@ -5159,7 +5157,6 @@
 				3B8B5D3E2DF5240C00365ED3 /* TimeZoneForTests.swift in Sources */,
 				3B5CD2CA2D4AECD500CE213C /* ProfileJavascriptTests.swift in Sources */,
 				3B5CD2CB2D4AECD500CE213C /* ProfileTargetsTests.swift in Sources */,
-				3BBC227C2DF6F87200169236 /* HttpFiles.swift in Sources */,
 				3B5CD2CD2D4AECD500CE213C /* ProfileIsfTests.swift in Sources */,
 				3B5CD2CE2D4AECD500CE213C /* ProfileBasalTests.swift in Sources */,
 				3BFA5BF92D989F510072B082 /* MockTDDStorage.swift in Sources */,
@@ -5179,6 +5176,7 @@
 				BD8FC0592D66189700B95AED /* TestAssembly.swift in Sources */,
 				DDC6CA6D2DD90A2A0060EE25 /* LocalizationTests.swift in Sources */,
 				3B997DCF2DC00A3A006B6BB2 /* JSONImporterTests.swift in Sources */,
+				3B31D5742E0E26C00047D32D /* ReplayTests.swift in Sources */,
 				3B8B5D3C2DF523C000365ED3 /* AutosensJsonTests.swift in Sources */,
 				BD8FC0662D661A0000B95AED /* GlucoseStorageTests.swift in Sources */,
 				BD8FC05B2D6618AF00B95AED /* DeterminationStorageTests.swift in Sources */,

+ 41 - 40
TrioTests/CoreDataTests/GlucoseStorageTests.swift

@@ -186,44 +186,45 @@ import Testing
         #expect(storage.alarm == nil, "Should not trigger any alarm")
     }
 
-    @Test("getGlucoseStatus returns correct deltas for 0/5/15/30m readings") func testGetGlucoseStatusFourPoints() async throws {
-        let now = Date()
-        // Prepare 4 readings: at 0, 5, 15, and 30 minutes ago
-        let specs: [(offset: TimeInterval, value: Int)] = [
-            (0, 100), // now
-            (5 * 60, 110), // 5m ago
-            (15 * 60, 120), // 15m ago
-            (30 * 60, 130) // 30m ago
-        ]
-
-        // Insert them into CoreData so that our fetch predicate picks them up
-        for (offset, value) in specs {
-            await testContext.perform {
-                let glucoseToStore = GlucoseStored(context: testContext)
-                glucoseToStore.id = UUID()
-                glucoseToStore.date = now.addingTimeInterval(-offset)
-                glucoseToStore.glucose = Int16(value)
-            }
-        }
-        try testContext.save()
-
-        // Call the method under test
-        let status = try await storage.getGlucoseStatus()
-        #expect(status != nil, "Expected non‐nil status")
-
-        // “Now” glucose is the 0m reading
-        #expect(status!.glucose == 100)
-
-        // lastDelta: only the 5m point: (100–110)/5*5 = –10
-        #expect(status!.delta == -10)
-
-        // shortAvgDelta: average of 5m and 15m windows:
-        //   5m window:   (100–110)/5*5   = –10
-        //   15m window: (100–120)/15*5 ≈ –6.6667 → –6.67
-        //   avg ≈ (–10 + –6.67)/2 = –8.333… → rounded to –8.33
-        #expect(status!.shortAvgDelta == -8.33)
-
-        // longAvgDelta: only the 30m window: (100–130)/30*5 = –5
-        #expect(status!.longAvgDelta == -5)
-    }
+    /* Commenting out while we don't have getGlucoseStatus defined
+     @Test("getGlucoseStatus returns correct deltas for 0/5/15/30m readings") func testGetGlucoseStatusFourPoints() async throws {
+         let now = Date()
+         // Prepare 4 readings: at 0, 5, 15, and 30 minutes ago
+         let specs: [(offset: TimeInterval, value: Int)] = [
+             (0, 100), // now
+             (5 * 60, 110), // 5m ago
+             (15 * 60, 120), // 15m ago
+             (30 * 60, 130) // 30m ago
+         ]
+
+         // Insert them into CoreData so that our fetch predicate picks them up
+         for (offset, value) in specs {
+             await testContext.perform {
+                 let glucoseToStore = GlucoseStored(context: testContext)
+                 glucoseToStore.id = UUID()
+                 glucoseToStore.date = now.addingTimeInterval(-offset)
+                 glucoseToStore.glucose = Int16(value)
+             }
+         }
+         try testContext.save()
+
+         // Call the method under test
+         let status = try await storage.getGlucoseStatus()
+         #expect(status != nil, "Expected non‐nil status")
+
+         // “Now” glucose is the 0m reading
+         #expect(status!.glucose == 100)
+
+         // lastDelta: only the 5m point: (100–110)/5*5 = –10
+         #expect(status!.delta == -10)
+
+         // shortAvgDelta: average of 5m and 15m windows:
+         //   5m window:   (100–110)/5*5   = –10
+         //   15m window: (100–120)/15*5 ≈ –6.6667 → –6.67
+         //   avg ≈ (–10 + –6.67)/2 = –8.333… → rounded to –8.33
+         #expect(status!.shortAvgDelta == -8.33)
+
+         // longAvgDelta: only the 30m window: (100–130)/30*5 = –5
+         #expect(status!.longAvgDelta == -5)
+     }*/
 }

+ 2 - 0
TrioTests/Info.plist

@@ -14,5 +14,7 @@
 	<string>$(PRODUCT_NAME)</string>
 	<key>CFBundlePackageType</key>
 	<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
+	<key>EnableReplayTests</key>
+	<string>$(ENABLE_REPLAY_TESTS)</string>
 </dict>
 </plist>

+ 5 - 4
TrioTests/OpenAPSSwiftTests/AutosensJsonTests.swift

@@ -83,7 +83,8 @@ import Testing
             javascriptDuration: 0.1,
             iobInputs: nil,
             mealInputs: nil,
-            autosensInputs: nil
+            autosensInputs: nil,
+            determineBasalInputs: nil
         )
 
         if comparison.resultType == .valueDifference {
@@ -157,9 +158,9 @@ import Testing
 
     @Test(
         "should produce same results for autosens for fixed JS",
-        .enabled(if: false)
+        .enabled(if: ReplayTests.enabled)
     ) func replayErrorInputs() async throws {
-        let timezone = "America/Los_Angeles"
+        let timezone = "Europe/Berlin"
         var skippedTimezones = Set<String>()
         let files = try await HttpFiles.listFiles()
         for filePath in files {
@@ -194,7 +195,7 @@ import Testing
         }
     }
 
-    @Test("Format autosens inputs for running in JS", .enabled(if: false)) func formatInputs() async throws {
+    @Test("Format autosens inputs for running in JS", .enabled(if: ReplayTests.enabled)) func formatInputs() async throws {
         // this test is meant for one-off analysis so it's ok to hard code
         // a file, just make sure to _not_ check in updates to this to
         // avoid polluting our change logs

+ 3 - 3
TrioTests/OpenAPSSwiftTests/IobJsonTests.swift

@@ -41,7 +41,7 @@ import Testing
     // to about 250 files at a time
     @Test(
         "IoB should produce same results for fixed JS and different for bundle JS",
-        .enabled(if: false)
+        .enabled(if: ReplayTests.enabled)
     ) func replayErrorInputs() async throws {
         let files = try await HttpFiles.listFiles()
         for filePath in files {
@@ -178,7 +178,7 @@ import Testing
         }
     }
 
-    @Test("Debug utility for checking iob-history", .enabled(if: false)) func debugIobHistory() async throws {
+    @Test("Debug utility for checking iob-history", .enabled(if: ReplayTests.enabled)) func debugIobHistory() async throws {
         let testBundle = Bundle(for: BundleReference.self)
         let path = testBundle.path(forResource: "iob-error-log", ofType: "json")!
         let data = try Data(contentsOf: URL(fileURLWithPath: path))
@@ -227,7 +227,7 @@ import Testing
     }
 
     /// simple utility for creating inputs for Javascript for use in testing
-    @Test("format inputs for Javascript", .enabled(if: false)) func generateJavascriptInputs() throws {
+    @Test("format inputs for Javascript", .enabled(if: ReplayTests.enabled)) func generateJavascriptInputs() throws {
         let testBundle = Bundle(for: BundleReference.self)
         let path = testBundle.path(forResource: "iob-error-log", ofType: "json")!
         let data = try Data(contentsOf: URL(fileURLWithPath: path))

+ 2 - 2
TrioTests/OpenAPSSwiftTests/MealJsonTests.swift

@@ -7,7 +7,7 @@ import Testing
 
     @Test(
         "Meal should produce same results for fixed JS",
-        .enabled(if: false)
+        .enabled(if: ReplayTests.enabled)
     ) func replayErrorInputs() async throws {
         // Note: This test case can only test one timezone per invocation
         // so you need to manually change this to try out errors from
@@ -94,7 +94,7 @@ import Testing
         #expect(comparison.resultType == .matching)
     }
 
-    @Test("Format meal inputs for running in JS", .enabled(if: false)) func formatInputs() async throws {
+    @Test("Format meal inputs for running in JS", .enabled(if: ReplayTests.enabled)) func formatInputs() async throws {
         let openAps = OpenAPSFixed()
 
         // this test is meant for one-off analysis so it's ok to hard code

+ 16 - 0
TrioTests/OpenAPSSwiftTests/utils/ReplayTests.swift

@@ -0,0 +1,16 @@
+import Foundation
+
+/// Flag to enable replay tests.
+///
+/// These test are only used for debugging so normally they should be disabled. But
+/// if you're debugging the oref-swift functions they are extremely useful. To enable them
+/// add these lines to your ConfigOverride.xcconfig file:
+/// ```
+/// ENABLE_REPLAY_TESTS = YES
+/// ```
+enum ReplayTests {
+    static var enabled: Bool {
+        let bundle = Bundle(for: BundleReference.self)
+        return bundle.object(forInfoDictionaryKey: "EnableReplayTests") as? String == "YES"
+    }
+}