|
| 1 | +// LoopFollow |
| 2 | +// NightscoutTreatmentDeduplicationTests.swift |
| 3 | + |
| 4 | +import Foundation |
| 5 | +@testable import LoopFollow |
| 6 | +import Testing |
| 7 | + |
| 8 | +struct NightscoutTreatmentDeduplicationTests { |
| 9 | + private typealias Entry = [String: AnyObject] |
| 10 | + |
| 11 | + @Test("keeps Trio FPU siblings with one id and distinct times") |
| 12 | + func keepsFPUOccurrences() { |
| 13 | + let entries = [ |
| 14 | + entry("newer", createdAt: "2026-08-16T02:06:00Z"), |
| 15 | + entry("older", createdAt: "2026-08-16T01:06:00Z"), |
| 16 | + ] |
| 17 | + #expect(deduplicatedIDs(entries) == ["newer", "older"]) |
| 18 | + } |
| 19 | + |
| 20 | + @Test("collapses duplicate Nightscout documents and keeps the first") |
| 21 | + func collapsesDuplicates() { |
| 22 | + let entries = [ |
| 23 | + entry("first", createdAt: "2026-08-16T01:06:00Z"), |
| 24 | + entry("duplicate", createdAt: "2026-08-16T01:06:00Z"), |
| 25 | + ] |
| 26 | + #expect(deduplicatedIDs(entries) == ["first"]) |
| 27 | + } |
| 28 | + |
| 29 | + @Test("uses normalized effective time and event type") |
| 30 | + func usesLogicalOccurrence() { |
| 31 | + let first = entry("first", timestamp: "2026-08-16T01:06:00Z", createdAt: "2026-08-16T01:05:58Z") |
| 32 | + let equivalent = entry("equivalent", timestamp: "2026-08-16T01:06:00.000Z", createdAt: "2026-08-16T01:06:02Z") |
| 33 | + let createdAtOnly = entry("created-at", timestamp: nil, createdAt: "2026-08-16T01:06:00Z") |
| 34 | + let bolus = entry("bolus", eventType: "Correction Bolus", timestamp: "2026-08-16T01:06:00Z") |
| 35 | + #expect(deduplicatedIDs([first, equivalent, createdAtOnly, bolus]) == ["first", "bolus"]) |
| 36 | + } |
| 37 | + |
| 38 | + @Test("preserves missing identifiers and fallback behavior") |
| 39 | + func preservesFallbacks() { |
| 40 | + let noID = entry("no-id", id: nil) |
| 41 | + let blankID = entry("blank-id", id: "") |
| 42 | + let noTime = [entry("no-time"), entry("no-time-duplicate")] |
| 43 | + let sensor = entry("sensor", eventType: "Sensor Start", timestamp: "2026-08-16T02:00:00Z", createdAt: "2026-08-16T01:00:00Z") |
| 44 | + let sensorDuplicate = entry("sensor-duplicate", eventType: "Sensor Start", timestamp: "2026-08-16T03:00:00Z", createdAt: "2026-08-16T01:00:00Z") |
| 45 | + #expect(deduplicatedIDs([noID, noID, blankID, blankID]) == ["no-id", "no-id", "blank-id", "blank-id"]) |
| 46 | + #expect(deduplicatedIDs(noTime) == ["no-time"]) |
| 47 | + #expect(deduplicatedIDs([sensor, sensorDuplicate]) == ["sensor"]) |
| 48 | + } |
| 49 | + |
| 50 | + private func deduplicatedIDs(_ entries: [Entry]) -> [String] { |
| 51 | + MainViewController.deduplicatedTreatmentEntries(entries).compactMap { $0["_id"] as? String } |
| 52 | + } |
| 53 | + |
| 54 | + private func entry(_ mongoID: String, id: String? = "shared-id", eventType: String = "Carb Correction", timestamp: String? = nil, createdAt: String? = nil) -> Entry { |
| 55 | + var result: Entry = [ |
| 56 | + "_id": mongoID as AnyObject, |
| 57 | + "eventType": eventType as AnyObject, |
| 58 | + ] |
| 59 | + if let id { result["id"] = id as AnyObject } |
| 60 | + if let timestamp { result["timestamp"] = timestamp as AnyObject } |
| 61 | + if let createdAt { result["created_at"] = createdAt as AnyObject } |
| 62 | + return result |
| 63 | + } |
| 64 | +} |
0 commit comments