apple_kit

Apple-native services for Radroots iOS and macOS apps
git clone https://radroots.dev/git/apple_kit.git
Log | Files | Refs | README

RadrootsPermissionLocationTests.swift (5176B)


      1 import Foundation
      2 import Testing
      3 @testable import RadrootsKit
      4 
      5 @Test func permissionSnapshotsPreserveKindStatusAndObservationTime() {
      6     let observedAt = Date(timeIntervalSince1970: 20)
      7     let snapshot = RadrootsPermissionSnapshot(
      8         kind: .location,
      9         status: .authorized,
     10         observedAt: observedAt
     11     )
     12 
     13     #expect(snapshot.kind == .location)
     14     #expect(snapshot.status == .authorized)
     15     #expect(snapshot.observedAt == observedAt)
     16 }
     17 
     18 @Test func locationAvailabilityMapsUsableAuthorizationStates() {
     19     #expect(RadrootsLocationServicesAvailability(
     20         locationServicesEnabled: true,
     21         authorization: .notDetermined
     22     ).canRequestWhenInUseAuthorization)
     23     #expect(RadrootsLocationServicesAvailability(
     24         locationServicesEnabled: true,
     25         authorization: .authorizedWhenInUse
     26     ).canRequestCurrentLocation)
     27     #expect(RadrootsLocationServicesAvailability(
     28         locationServicesEnabled: true,
     29         authorization: .authorizedAlways
     30     ).canRequestCurrentLocation)
     31     #expect(!RadrootsLocationServicesAvailability(
     32         locationServicesEnabled: false,
     33         authorization: .authorizedWhenInUse
     34     ).canRequestCurrentLocation)
     35     #expect(RadrootsLocationAuthorization.authorizedWhenInUse.permissionStatus == .authorized)
     36     #expect(RadrootsLocationAuthorization.restricted.permissionStatus == .restricted)
     37 }
     38 
     39 @Test func locationCoordinateValidatesLatitudeAndLongitude() throws {
     40     let coordinate = try RadrootsLocationCoordinate(latitude: 49.2827, longitude: -123.1207)
     41 
     42     #expect(coordinate.latitude == 49.2827)
     43     #expect(coordinate.longitude == -123.1207)
     44 
     45     #expect(throws: RadrootsLocationServicesError.self) {
     46         _ = try RadrootsLocationCoordinate(latitude: 91, longitude: 0)
     47     }
     48     #expect(throws: RadrootsLocationServicesError.self) {
     49         _ = try RadrootsLocationCoordinate(latitude: 0, longitude: -181)
     50     }
     51     #expect(throws: RadrootsLocationServicesError.self) {
     52         _ = try RadrootsLocationCoordinate(latitude: .nan, longitude: 0)
     53     }
     54 }
     55 
     56 @Test func locationReadingValidatesAccuracyCourseSpeedAndFreshness() throws {
     57     let capturedAt = Date(timeIntervalSince1970: 100)
     58     let reading = try RadrootsLocationReading(
     59         coordinate: RadrootsLocationCoordinate(latitude: 10, longitude: 20),
     60         horizontalAccuracyMeters: 5,
     61         altitudeMeters: 120,
     62         verticalAccuracyMeters: 8,
     63         speedMetersPerSecond: 2,
     64         courseDegrees: 359.9,
     65         capturedAt: capturedAt
     66     )
     67 
     68     #expect(reading.horizontalAccuracyMeters == 5)
     69     #expect(try reading.age(relativeTo: Date(timeIntervalSince1970: 106)) == 6)
     70     #expect(try reading.isFresh(relativeTo: Date(timeIntervalSince1970: 106), maximumAgeSeconds: 10))
     71     #expect(!(try reading.isFresh(relativeTo: Date(timeIntervalSince1970: 120), maximumAgeSeconds: 10)))
     72 
     73     #expect(throws: RadrootsLocationServicesError.self) {
     74         _ = try RadrootsLocationReading(
     75             coordinate: RadrootsLocationCoordinate(latitude: 10, longitude: 20),
     76             horizontalAccuracyMeters: -1,
     77             capturedAt: capturedAt
     78         )
     79     }
     80     #expect(throws: RadrootsLocationServicesError.self) {
     81         _ = try RadrootsLocationReading(
     82             coordinate: RadrootsLocationCoordinate(latitude: 10, longitude: 20),
     83             horizontalAccuracyMeters: 1,
     84             speedMetersPerSecond: -1,
     85             capturedAt: capturedAt
     86         )
     87     }
     88     #expect(throws: RadrootsLocationServicesError.self) {
     89         _ = try RadrootsLocationReading(
     90             coordinate: RadrootsLocationCoordinate(latitude: 10, longitude: 20),
     91             horizontalAccuracyMeters: 1,
     92             courseDegrees: 360,
     93             capturedAt: capturedAt
     94         )
     95     }
     96 }
     97 
     98 @Test func currentLocationRequestAndResultValidateOperationalBounds() throws {
     99     let request = try RadrootsCurrentLocationRequest(
    100         timeoutSeconds: 4,
    101         desiredAccuracyMeters: 10,
    102         maximumCachedReadingAgeSeconds: 15
    103     )
    104     let reading = try RadrootsLocationReading(
    105         coordinate: RadrootsLocationCoordinate(latitude: 10, longitude: 20),
    106         horizontalAccuracyMeters: 5,
    107         capturedAt: Date(timeIntervalSince1970: 100)
    108     )
    109     let result = try RadrootsCurrentLocationResult(
    110         reading: reading,
    111         authorization: .authorizedWhenInUse,
    112         servedFromCache: true
    113     )
    114 
    115     #expect(request.timeoutSeconds == 4)
    116     #expect(request.desiredAccuracyMeters == 10)
    117     #expect(request.maximumCachedReadingAgeSeconds == 15)
    118     #expect(result.reading == reading)
    119     #expect(result.servedFromCache)
    120 
    121     #expect(throws: RadrootsLocationServicesError.self) {
    122         _ = try RadrootsCurrentLocationRequest(timeoutSeconds: 0)
    123     }
    124     #expect(throws: RadrootsLocationServicesError.self) {
    125         _ = try RadrootsCurrentLocationRequest(timeoutSeconds: .infinity)
    126     }
    127     #expect(throws: RadrootsLocationServicesError.self) {
    128         _ = try RadrootsCurrentLocationRequest(timeoutSeconds: 1, desiredAccuracyMeters: -.leastNonzeroMagnitude)
    129     }
    130     #expect(throws: RadrootsLocationServicesError.self) {
    131         _ = try RadrootsCurrentLocationResult(reading: reading, authorization: .denied)
    132     }
    133 }