Commit e758df663b

e758df663b3c3c8328ae3df420737922f95fcb65

parent: d7927c6b4b

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-24 07:09 UTC

Separate the monitoring schedule decisions, and cover them

Two decisions were tangled into the alerting path: whether a domain is due, and
how the interval moves after a check. A mistake in either is silent in both
directions — an interval that grows without bound stops monitoring with no
error, and one that never backs off hammers every domain on every wake.

isDue takes an injected clock instead of calling Date() inline. adapted returns
the next state and whether the change is new, leaving the alerting to its
caller. It reproduces one behaviour that was previously an implicit gap between
two ifs: an alert whose hash is unchanged moves neither the interval nor the
stable counter, so a persistent condition does not keep resetting the backoff.
That is now an explicit guard with a comment.

nextScheduleInterval, monitoredDomains and sanitizeSettings were already pure
and already static; they were simply never called by a test.

18 tests. One corrected my own assumption rather than the code: the clamp is
max(base, min(max, current * multiplier)), so the interval multiplies upward
from the base and a value below it is raised rather than multiplied — the
backoff can never schedule a domain more often than its configured base.

Layout: unified · split

DomainDig.xcodeproj/project.pbxproj +4
@@ -30,6 +30,7 @@
30 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000601 /* FeatureAccessTests.swift */; }; 30 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000601 /* FeatureAccessTests.swift */; };
31 A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000701 /* SMTPMessageTests.swift */; }; 31 A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000701 /* SMTPMessageTests.swift */; };
32 A1B2C3D40000000000000802 /* LocalAPIRoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */; }; 32 A1B2C3D40000000000000802 /* LocalAPIRoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */; };
33 A1B2C3D40000000000000902 /* MonitoringSchedulingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */; };
33 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; }; 34 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; };
34 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */; }; 35 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */; };
35 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000501 /* DomainNameTests.swift */; }; 36 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000501 /* DomainNameTests.swift */; };
@@ -107,6 +108,7 @@
107 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = FeatureAccessTests.swift; sourceTree = "<group>"; }; 108 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = FeatureAccessTests.swift; sourceTree = "<group>"; };
108 A1B2C3D40000000000000701 /* SMTPMessageTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = SMTPMessageTests.swift; sourceTree = "<group>"; }; 109 A1B2C3D40000000000000701 /* SMTPMessageTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = SMTPMessageTests.swift; sourceTree = "<group>"; };
109 A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = LocalAPIRoutingTests.swift; sourceTree = "<group>"; }; 110 A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = LocalAPIRoutingTests.swift; sourceTree = "<group>"; };
111 A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = MonitoringSchedulingTests.swift; sourceTree = "<group>"; };
110 A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; }; 112 A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; };
111 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CloudSyncMergeTests.swift; sourceTree = "<group>"; }; 113 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CloudSyncMergeTests.swift; sourceTree = "<group>"; };
112 A1B2C3D40000000000000501 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.swift; sourceTree = "<group>"; }; 114 A1B2C3D40000000000000501 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.swift; sourceTree = "<group>"; };
@@ -249,6 +251,7 @@
249 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */, 251 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */,
250 A1B2C3D40000000000000701 /* SMTPMessageTests.swift */, 252 A1B2C3D40000000000000701 /* SMTPMessageTests.swift */,
251 A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */, 253 A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */,
254 A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */,
252 A1B2C3D40000000000000301 /* AvailabilityTests.swift */, 255 A1B2C3D40000000000000301 /* AvailabilityTests.swift */,
253 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */, 256 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */,
254 A1B2C3D40000000000000501 /* DomainNameTests.swift */, 257 A1B2C3D40000000000000501 /* DomainNameTests.swift */,
@@ -516,6 +519,7 @@
516 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */, 519 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */,
517 A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */, 520 A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */,
518 A1B2C3D40000000000000802 /* LocalAPIRoutingTests.swift in Sources */, 521 A1B2C3D40000000000000802 /* LocalAPIRoutingTests.swift in Sources */,
522 A1B2C3D40000000000000902 /* MonitoringSchedulingTests.swift in Sources */,
519 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */, 523 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */,
520 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */, 524 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */,
521 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */, 525 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */,
DomainDig/DomainMonitoringService.swift +66 −36
@@ -151,7 +151,7 @@ final class DomainMonitoringScheduler {
151 #endif 151 #endif
152 } 152 }
153 153
154 private static func nextScheduleInterval( 154 static func nextScheduleInterval(
155 settings: MonitoringSettings, 155 settings: MonitoringSettings,
156 trackedDomains: [TrackedDomain], 156 trackedDomains: [TrackedDomain],
157 now: Date = Date() 157 now: Date = Date()
@@ -172,6 +172,51 @@ final class DomainMonitoringScheduler {
172 .min() ?? settings.config.sanitizedBaseInterval 172 .min() ?? settings.config.sanitizedBaseInterval
173 } 173 }
174 174
175 /// Whether a domain is due for a background check.
176 static func isDue(
177 trackedDomain: TrackedDomain,
178 settings: MonitoringSettings,
179 now: Date
180 ) -> Bool {
181 guard let lastCheck = trackedDomain.monitoringState.lastCheck else { return true }
182 let state = normalizedMonitoringState(for: trackedDomain, settings: settings)
183 return lastCheck.addingTimeInterval(state.currentInterval) <= now
184 }
185
186 /// How the interval and the stable-check counter move after one check, and
187 /// whether this is a change not seen before.
188 ///
189 /// Separated from the alerting it used to sit inside, because a mistake
190 /// here is silent in both directions: an interval that grows without bound
191 /// stops monitoring, and one that never backs off hammers every domain on
192 /// every wake. Note the middle case — an alert whose hash is unchanged
193 /// moves neither the interval nor the counter.
194 static func adapted(
195 _ state: MonitoringState,
196 changeHash: String?,
197 settings: MonitoringSettings,
198 now: Date
199 ) -> (state: MonitoringState, isNewChange: Bool) {
200 var state = state
201 let baseInterval = settings.config.sanitizedBaseInterval
202
203 guard let changeHash else {
204 state.consecutiveStableChecks += 1
205 state.currentInterval = settings.adaptiveEnabled
206 ? max(baseInterval, min(settings.config.maxInterval, state.currentInterval * settings.sensitivity.intervalMultiplier))
207 : baseInterval
208 return (state, false)
209 }
210
211 guard state.lastChangeHash != changeHash else { return (state, false) }
212
213 state.lastChangeHash = changeHash
214 state.lastChangeDate = now
215 state.consecutiveStableChecks = 0
216 state.currentInterval = settings.adaptiveEnabled ? settings.sensitivity.minimumInterval : baseInterval
217 return (state, true)
218 }
219
175 static func normalizedMonitoringState( 220 static func normalizedMonitoringState(
176 for trackedDomain: TrackedDomain, 221 for trackedDomain: TrackedDomain,
177 settings: MonitoringSettings 222 settings: MonitoringSettings
@@ -566,9 +611,7 @@ final class DomainMonitoringService {
566 settings: MonitoringSettings 611 settings: MonitoringSettings
567 ) -> Bool { 612 ) -> Bool {
568 guard trigger == .background else { return true } 613 guard trigger == .background else { return true }
569 guard let lastCheck = trackedDomain.monitoringState.lastCheck else { return true } 614 return DomainMonitoringScheduler.isDue(trackedDomain: trackedDomain, settings: settings, now: Date())
570 let state = DomainMonitoringScheduler.normalizedMonitoringState(for: trackedDomain, settings: settings)
571 return lastCheck.addingTimeInterval(state.currentInterval) <= Date()
572 } 615 }
573 616
574 private func processAdaptiveMonitoringState( 617 private func processAdaptiveMonitoringState(
@@ -581,41 +624,28 @@ final class DomainMonitoringService {
581 now: Date 624 now: Date
582 ) async -> (trackedDomain: TrackedDomain, alertsTriggered: Int) { 625 ) async -> (trackedDomain: TrackedDomain, alertsTriggered: Int) {
583 var updated = trackedDomain 626 var updated = trackedDomain
584 var state = DomainMonitoringScheduler.normalizedMonitoringState(for: trackedDomain, settings: settings)
585 let baseInterval = settings.config.sanitizedBaseInterval
586 let minInterval = settings.sensitivity.minimumInterval
587 let maxInterval = settings.config.maxInterval
588 let isQuiet = settings.quietHours?.contains(now, calendar: calendar) == true 627 let isQuiet = settings.quietHours?.contains(now, calendar: calendar) == true
589 var alertsTriggered = 0 628 var alertsTriggered = 0
590 629
591 if let alertDescriptor { 630 let (adaptedState, isNewChange) = DomainMonitoringScheduler.adapted(
592 if state.lastChangeHash != alertDescriptor.changeHash { 631 DomainMonitoringScheduler.normalizedMonitoringState(for: trackedDomain, settings: settings),
593 state.lastChangeHash = alertDescriptor.changeHash 632 changeHash: alertDescriptor?.changeHash,
594 state.lastChangeDate = now 633 settings: settings,
595 state.consecutiveStableChecks = 0 634 now: now
596 state.currentInterval = settings.adaptiveEnabled ? minInterval : baseInterval 635 )
597 636 var state = adaptedState
598 if settings.alertsEnabled, 637
599 alertDescriptor.severity >= settings.alertFilter.minimumSeverity { 638 if isNewChange,
600 alertsTriggered += await raiseAlert( 639 let alertDescriptor,
601 alertDescriptor, 640 settings.alertsEnabled,
602 for: &updated, 641 alertDescriptor.severity >= settings.alertFilter.minimumSeverity {
603 state: &state, 642 alertsTriggered += await raiseAlert(
604 deferred: isQuiet || !notificationsAuthorized, 643 alertDescriptor,
605 now: now 644 for: &updated,
606 ) 645 state: &state,
607 } 646 deferred: isQuiet || !notificationsAuthorized,
608 } 647 now: now
609 } else { 648 )
610 state.consecutiveStableChecks += 1
611 if settings.adaptiveEnabled {
612 state.currentInterval = max(
613 baseInterval,
614 min(maxInterval, state.currentInterval * settings.sensitivity.intervalMultiplier)
615 )
616 } else {
617 state.currentInterval = baseInterval
618 }
619 } 649 }
620 650
621 if !isQuiet, 651 if !isQuiet,
DomainDigTests/MonitoringSchedulingTests.swift added +246
@@ -0,0 +1,246 @@
1import XCTest
2@testable import DomainDig
3
4/// Monitoring decides when it runs, and a mistake there is silent in both
5/// directions: an interval that grows without bound stops monitoring with no
6/// error, and one that never backs off hammers every tracked domain on every
7/// background wake. None of it had coverage.
8final class MonitoringSchedulingTests: XCTestCase {
9
10 private let now = Date(timeIntervalSinceReferenceDate: 1_000_000)
11
12 // MARK: Backing off when nothing changes
13
14 /// The interval multiplies upward from the base, never from below it: the
15 /// clamp is `max(base, min(max, current * multiplier))`, so a current value
16 /// under the base is raised to the base rather than multiplied.
17 func testAStableCheckMultipliesTheIntervalAndCountsUp() {
18 let settings = settings(adaptive: true, sensitivity: .medium)
19 let base = settings.config.sanitizedBaseInterval
20 let start = MonitoringState(consecutiveStableChecks: 2, currentInterval: base)
21
22 let (state, isNewChange) = DomainMonitoringScheduler.adapted(
23 start, changeHash: nil, settings: settings, now: now
24 )
25
26 XCTAssertFalse(isNewChange)
27 XCTAssertEqual(state.consecutiveStableChecks, 3)
28 XCTAssertEqual(state.currentInterval, base * settings.sensitivity.intervalMultiplier)
29 XCTAssertGreaterThan(settings.sensitivity.intervalMultiplier, 1)
30 }
31
32 func testTheBackoffIsCappedAtTheMaximum() {
33 let settings = settings(adaptive: true, sensitivity: .medium)
34 let start = MonitoringState(currentInterval: settings.config.maxInterval)
35
36 let (state, _) = DomainMonitoringScheduler.adapted(
37 start, changeHash: nil, settings: settings, now: now
38 )
39
40 XCTAssertEqual(state.currentInterval, settings.config.maxInterval)
41 }
42
43 func testTheBackoffNeverDropsBelowTheBaseInterval() {
44 let settings = settings(adaptive: true, sensitivity: .medium)
45 let start = MonitoringState(currentInterval: 1)
46
47 let (state, _) = DomainMonitoringScheduler.adapted(
48 start, changeHash: nil, settings: settings, now: now
49 )
50
51 XCTAssertEqual(state.currentInterval, settings.config.sanitizedBaseInterval)
52 }
53
54 func testWithoutAdaptiveModeAStableCheckReturnsToBase() {
55 let settings = settings(adaptive: false, sensitivity: .medium)
56 let start = MonitoringState(currentInterval: 99_999)
57
58 let (state, _) = DomainMonitoringScheduler.adapted(
59 start, changeHash: nil, settings: settings, now: now
60 )
61
62 XCTAssertEqual(state.currentInterval, settings.config.sanitizedBaseInterval)
63 }
64
65 // MARK: Reacting to a change
66
67 func testANewChangeResetsToTheSensitivityMinimum() {
68 let settings = settings(adaptive: true, sensitivity: .medium)
69 let start = MonitoringState(lastChangeHash: "old", consecutiveStableChecks: 7, currentInterval: 50_000)
70
71 let (state, isNewChange) = DomainMonitoringScheduler.adapted(
72 start, changeHash: "new", settings: settings, now: now
73 )
74
75 XCTAssertTrue(isNewChange)
76 XCTAssertEqual(state.currentInterval, settings.sensitivity.minimumInterval)
77 XCTAssertEqual(state.consecutiveStableChecks, 0)
78 XCTAssertEqual(state.lastChangeHash, "new")
79 XCTAssertEqual(state.lastChangeDate, now)
80 }
81
82 func testANewChangeWithoutAdaptiveModeResetsToBase() {
83 let settings = settings(adaptive: false, sensitivity: .medium)
84 let start = MonitoringState(lastChangeHash: "old", currentInterval: 50_000)
85
86 let (state, isNewChange) = DomainMonitoringScheduler.adapted(
87 start, changeHash: "new", settings: settings, now: now
88 )
89
90 XCTAssertTrue(isNewChange)
91 XCTAssertEqual(state.currentInterval, settings.config.sanitizedBaseInterval)
92 }
93
94 /// The subtle case, deliberate and easy to "tidy" away: a condition that is
95 /// still alerting but has not changed moves neither the interval nor the
96 /// counter, so a persistent alert does not keep resetting the backoff.
97 func testAnUnchangedAlertMovesNothing() {
98 let settings = settings(adaptive: true, sensitivity: .medium)
99 let start = MonitoringState(lastChangeHash: "same", consecutiveStableChecks: 4, currentInterval: 7200)
100
101 let (state, isNewChange) = DomainMonitoringScheduler.adapted(
102 start, changeHash: "same", settings: settings, now: now
103 )
104
105 XCTAssertFalse(isNewChange)
106 XCTAssertEqual(state.currentInterval, 7200)
107 XCTAssertEqual(state.consecutiveStableChecks, 4)
108 }
109
110 // MARK: Whether a domain is due
111
112 func testADomainNeverCheckedIsDue() {
113 let domain = trackedDomain(lastCheck: nil, interval: 3600)
114
115 XCTAssertTrue(DomainMonitoringScheduler.isDue(
116 trackedDomain: domain, settings: settings(), now: now
117 ))
118 }
119
120 func testADomainCheckedWithinItsIntervalIsNotDue() {
121 let domain = trackedDomain(lastCheck: now.addingTimeInterval(-1800), interval: 3600)
122
123 XCTAssertFalse(DomainMonitoringScheduler.isDue(
124 trackedDomain: domain, settings: settings(), now: now
125 ))
126 }
127
128 func testADomainPastItsIntervalIsDue() {
129 let domain = trackedDomain(lastCheck: now.addingTimeInterval(-3601), interval: 3600)
130
131 XCTAssertTrue(DomainMonitoringScheduler.isDue(
132 trackedDomain: domain, settings: settings(), now: now
133 ))
134 }
135
136 /// A zero interval means "never configured", and must not read as "due
137 /// never" or "due always" — it falls back to the base interval.
138 func testAZeroIntervalFallsBackToTheBaseInterval() {
139 let settings = settings()
140 let domain = trackedDomain(lastCheck: now.addingTimeInterval(-60), interval: 0)
141
142 XCTAssertFalse(DomainMonitoringScheduler.isDue(trackedDomain: domain, settings: settings, now: now))
143 XCTAssertEqual(
144 DomainMonitoringScheduler.normalizedMonitoringState(for: domain, settings: settings).currentInterval,
145 settings.config.sanitizedBaseInterval
146 )
147 }
148
149 // MARK: When to ask the system for the next wake
150
151 func testWithNothingMonitoredTheNextWakeIsTheBaseInterval() {
152 let settings = settings()
153
154 XCTAssertEqual(
155 DomainMonitoringScheduler.nextScheduleInterval(settings: settings, trackedDomains: [], now: now),
156 settings.config.sanitizedBaseInterval
157 )
158 }
159
160 func testTheSoonestDueDomainSetsTheNextWake() {
161 let soon = trackedDomain(lastCheck: now.addingTimeInterval(-3000), interval: 3600)
162 let later = trackedDomain(lastCheck: now.addingTimeInterval(-100), interval: 3600)
163
164 let interval = DomainMonitoringScheduler.nextScheduleInterval(
165 settings: settings(), trackedDomains: [later, soon], now: now
166 )
167
168 XCTAssertEqual(interval, 600, accuracy: 1)
169 }
170
171 func testAnOverdueDomainAsksForAnImmediateWake() {
172 let overdue = trackedDomain(lastCheck: now.addingTimeInterval(-99_999), interval: 3600)
173
174 XCTAssertEqual(
175 DomainMonitoringScheduler.nextScheduleInterval(
176 settings: settings(), trackedDomains: [overdue], now: now
177 ),
178 0
179 )
180 }
181
182 // MARK: Which domains are monitored
183
184 func testAllTrackedScopeStillRespectsThePerDomainToggle() {
185 let on = trackedDomain(monitoringEnabled: true)
186 let off = trackedDomain(monitoringEnabled: false)
187
188 let monitored = MonitoringStorage.monitoredDomains(
189 settings: settings(scope: .allTracked), trackedDomains: [on, off]
190 )
191
192 XCTAssertEqual(monitored.map(\.id), [on.id])
193 }
194
195 func testSelectedScopeNeedsBothSelectionAndToggle() {
196 let selectedOn = trackedDomain(monitoringEnabled: true)
197 let selectedOff = trackedDomain(monitoringEnabled: false)
198 let unselectedOn = trackedDomain(monitoringEnabled: true)
199 var settings = settings(scope: .selectedOnly)
200 settings.selectedDomainIDs = [selectedOn.id, selectedOff.id]
201
202 let monitored = MonitoringStorage.monitoredDomains(
203 settings: settings, trackedDomains: [selectedOn, selectedOff, unselectedOn]
204 )
205
206 XCTAssertEqual(monitored.map(\.id), [selectedOn.id])
207 }
208
209 /// A selection that outlives the domain it named must not keep it alive.
210 func testSanitizingDropsSelectionsForDomainsThatNoLongerExist() {
211 let surviving = trackedDomain()
212 var settings = settings(scope: .selectedOnly)
213 settings.selectedDomainIDs = [surviving.id, UUID()]
214
215 let sanitized = MonitoringStorage.sanitizeSettings(settings, trackedDomains: [surviving])
216
217 XCTAssertEqual(sanitized.selectedDomainIDs, [surviving.id])
218 }
219
220 // MARK: Helpers
221
222 private func settings(
223 adaptive: Bool = true,
224 sensitivity: MonitoringSensitivity = .medium,
225 scope: MonitoringScope = .allTracked
226 ) -> MonitoringSettings {
227 MonitoringSettings(
228 config: MonitoringConfig(isEnabled: true, adaptiveEnabled: adaptive, sensitivity: sensitivity),
229 scope: scope
230 )
231 }
232
233 private func trackedDomain(
234 lastCheck: Date? = nil,
235 interval: TimeInterval = 3600,
236 monitoringEnabled: Bool = true
237 ) -> TrackedDomain {
238 TrackedDomain(
239 domain: "example.com",
240 createdAt: now,
241 updatedAt: now,
242 monitoringEnabled: monitoringEnabled,
243 monitoringState: MonitoringState(lastCheck: lastCheck, currentInterval: interval)
244 )
245 }
246}