Explorar o código

Fix meal total unit tests; add 25% tolerance to JS

Deniz Cengiz hai 11 meses
pai
achega
f9b930d1e1

+ 7 - 7
Trio/Sources/APS/OpenAPSSwift/Meal/MealCob.swift

@@ -152,12 +152,12 @@ struct MealCob {
 
         // Process bucketed data (excluding last 3 entries to avoid incomplete deltas)
         // If bucketed data < 4, skips loop and just returns default values, matching JS behavior
-        for i in 0 ..< max(0, bucketedData.count - 3) {
-            let bgTime = bucketedData[i].date
-            let bg = bucketedData[i].glucose
+        for bucketCount in 0 ..< max(0, bucketedData.count - 3) {
+            let bgTime = bucketedData[bucketCount].date
+            let bg = bucketedData[bucketCount].glucose
 
             // Skip invalid glucose readings
-            guard bg >= 39, bucketedData[i + 3].glucose >= 39 else {
+            guard bg >= 39, bucketedData[bucketCount + 3].glucose >= 39 else {
                 continue
             }
 
@@ -169,8 +169,8 @@ struct MealCob {
                 throw CobError.isfLookupError
             }
 
-            let avgDelta = (bg - bucketedData[i + 3].glucose) / 3
-            let delta = bg - bucketedData[i + 1].glucose
+            let avgDelta = (bg - bucketedData[bucketCount + 3].glucose) / 3
+            let delta = bg - bucketedData[bucketCount + 1].glucose
 
             var simulationProfile = profile
             simulationProfile.currentBasal = try Basal.basalLookup(basalProfile, now: bgTime)
@@ -182,7 +182,7 @@ struct MealCob {
             let deviation = delta - bgi
 
             // Calculate the deviation right now, for use in min_5m
-            if i == 0 {
+            if bucketCount == 0 {
                 currentDeviation = ((avgDelta - bgi) * 1000).rounded() / 1000
                 if let ciDate = ciDate, ciDate > bgTime {
                     allDeviations.append(currentDeviation.rounded())

+ 15 - 6
TrioTests/OpenAPSSwiftTests/MealTotalTests.swift

@@ -98,8 +98,11 @@ import Testing
 
         // After 1 hour, we should see partial carb absorption
         #expect(result != nil)
-        #expect(result?.mealCOB == 12)
-        #expect(result?.currentDeviation.isWithin(0.1, of: 3) == true)
+        #expect(result!.mealCOB.isWithin(12 * 0.25, of: 12) == true, "mealCOB: \(result!.mealCOB.description)")
+        #expect(
+            result!.currentDeviation.isWithin(3 * 0.25, of: 0) == true,
+            "currentDeviation: \(result!.currentDeviation.description)"
+        )
     }
 
     @Test("should return nil when no treatments provided") func emptyObjectWhenNoTreatments() async throws {
@@ -171,9 +174,12 @@ import Testing
         )
 
         #expect(result != nil)
-        #expect(result?.carbs == 20)
-        #expect(result?.currentDeviation.isWithin(0.1, of: 0.67) == true)
-        #expect(result?.mealCOB == 14)
+        #expect(result!.carbs == 20)
+        #expect(
+            result!.currentDeviation.isWithin(0.67 * 0.25, of: 0.67) == true,
+            "currentDeviation: \(result!.currentDeviation.description)"
+        )
+        #expect(result!.mealCOB.isWithin(14 * 0.25, of: 14) == true, "mealCOB: \(result!.mealCOB.description)")
     }
 
     @Test("should ignore treatments outside the meal window") func ignoreTreatmentsOutsideMealWindow() async throws {
@@ -223,7 +229,10 @@ import Testing
         #expect(result != nil)
         #expect(result?.carbs == 0)
         #expect(result?.mealCOB == 0)
-        #expect(result?.currentDeviation.isWithin(0.1, of: 0.67) == true)
+        #expect(
+            result?.currentDeviation.isWithin(0.67 * 0.25, of: 0.67) == true,
+            "currentDeviation: \(result!.currentDeviation.description)"
+        )
     }
 
     @Test("should respect maxMealAbsorptionTime from profile") func respectMaxMealAbsorptionTime() async throws {