@@ -110,7 +110,7 @@ import Testing
110110 #expect( locationService. stopMonitoringCallCount == 1 )
111111 }
112112
113- @MainActor @Test func `always mode keeps significant location monitoring eligible when always is granted`() async {
113+ @MainActor @Test func `always mode starts significant location monitoring when always is granted`() async {
114114 let locationService = MockLocationService ( authorizationStatus: . authorizedAlways)
115115 let appModel = NodeAppModel ( locationService: locationService)
116116
@@ -119,6 +119,7 @@ import Testing
119119 #expect( granted)
120120 #expect( locationService. backgroundUpdatesEnabled == true )
121121 #expect( locationService. stopMonitoringCallCount == 0 )
122+ #expect( locationService. startMonitoringCallCount == 1 )
122123 }
123124
124125 @MainActor @Test func `always mode remains selected when ios only grants while using`() async {
@@ -132,29 +133,38 @@ import Testing
132133 #expect( locationService. stopMonitoringCallCount == 1 )
133134 }
134135
135- @MainActor @Test func `authorization do wngrade stops significant location monitoring`() {
136+ @MainActor @Test func `external do wngrade and always restoration reconcile significant monitoring`() {
137+ let defaultsKey = " location.enabledMode "
138+ let previous = UserDefaults . standard. object ( forKey: defaultsKey)
139+ defer {
140+ if let previous {
141+ UserDefaults . standard. set ( previous, forKey: defaultsKey)
142+ } else {
143+ UserDefaults . standard. removeObject ( forKey: defaultsKey)
144+ }
145+ }
146+ UserDefaults . standard. set ( OpenClawLocationMode . always. rawValue, forKey: defaultsKey)
136147 let locationService = MockLocationService ( authorizationStatus: . authorizedAlways)
148+ let appModel = NodeAppModel ( locationService: locationService)
137149
138- locationService. reconcileBackgroundMonitoringAuthorization ( . authorizedWhenInUse)
150+ withExtendedLifetime ( appModel) {
151+ locationService. simulateAuthorizationChange ( . authorizedAlways)
152+ locationService. simulateAuthorizationChange ( . authorizedWhenInUse)
153+ locationService. simulateAuthorizationChange ( . authorizedAlways)
154+ }
139155
140- #expect( locationService. backgroundUpdatesEnabled == false )
156+ #expect( locationService. backgroundUpdatesEnabled == true )
157+ #expect( locationService. startMonitoringCallCount == 2 )
141158 #expect( locationService. stopMonitoringCallCount == 1 )
142159 }
143-
144- @MainActor @Test func `authorized always keeps significant location monitoring active`() {
145- let locationService = MockLocationService ( authorizationStatus: . authorizedAlways)
146-
147- locationService. reconcileBackgroundMonitoringAuthorization ( . authorizedAlways)
148-
149- #expect( locationService. backgroundUpdatesEnabled == nil )
150- #expect( locationService. stopMonitoringCallCount == 0 )
151- }
152160}
153161
154162@MainActor
155163private final class MockLocationService : LocationServicing , @unchecked Sendable {
156- private let status : CLAuthorizationStatus
164+ private var status : CLAuthorizationStatus
165+ private var authorizationChangeHandler : ( @MainActor @Sendable ( CLAuthorizationStatus ) -> Void ) ?
157166 var backgroundUpdatesEnabled : Bool ?
167+ var startMonitoringCallCount = 0
158168 var stopMonitoringCallCount = 0
159169
160170 init ( authorizationStatus: CLAuthorizationStatus ) {
@@ -191,11 +201,23 @@ private final class MockLocationService: LocationServicing, @unchecked Sendable
191201 self . backgroundUpdatesEnabled = enabled
192202 }
193203
204+ func setAuthorizationChangeHandler(
205+ _ handler: @escaping @MainActor @Sendable ( CLAuthorizationStatus ) -> Void )
206+ {
207+ self . authorizationChangeHandler = handler
208+ }
209+
194210 func startMonitoringSignificantLocationChanges( onUpdate: @escaping @Sendable ( CLLocation ) -> Void ) {
195211 _ = onUpdate
212+ self . startMonitoringCallCount += 1
196213 }
197214
198215 func stopMonitoringSignificantLocationChanges( ) {
199216 self . stopMonitoringCallCount += 1
200217 }
218+
219+ func simulateAuthorizationChange( _ status: CLAuthorizationStatus ) {
220+ self . status = status
221+ self . authorizationChangeHandler ? ( status)
222+ }
201223}
0 commit comments