RadrootsKitTestingTests.swift (5064B)
1 import Foundation 2 import RadrootsKit 3 import RadrootsKitTesting 4 import Testing 5 6 @Test func deterministicLaunchConfigurationAddsStableLocaleArguments() { 7 let config = RadrootsUITestLaunchConfiguration.deterministic( 8 environment: ["RADROOTS_TEST": "true"], 9 arguments: ["--radroots-test"] 10 ) 11 12 #expect(config.environment["RADROOTS_TEST"] == "true") 13 #expect( 14 config.arguments == [ 15 "--radroots-test", 16 "-AppleLanguages", 17 "(en)", 18 "-AppleLocale", 19 "en_US_POSIX", 20 ]) 21 } 22 23 @Test func launchConfigurationMergesEnvironmentOverBaseValues() { 24 let config = RadrootsUITestLaunchConfiguration( 25 environment: ["A": "override", "B": "new"], 26 arguments: [] 27 ) 28 29 #expect( 30 config.mergedEnvironment(over: ["A": "old", "C": "keep"]) == [ 31 "A": "override", 32 "B": "new", 33 "C": "keep", 34 ]) 35 } 36 37 @Test func inMemorySecureStoreRoundTripsAndNormalizesKeys() throws { 38 let store = RadrootsInMemorySecureStore() 39 let key = RadrootsSecureStoreKey(namespace: " identity ", name: " selected ") 40 41 try store.put(Data("secret".utf8), for: key) 42 43 #expect(try store.contains(RadrootsSecureStoreKey(namespace: "identity", name: "selected"))) 44 #expect(try store.get(key) == Data("secret".utf8)) 45 #expect(store.keys() == [RadrootsSecureStoreKey(namespace: "identity", name: "selected")]) 46 } 47 48 @Test func inMemorySecureStoreRecordsPolicyWithoutReturningSecret() throws { 49 let store = RadrootsInMemorySecureStore() 50 let key = RadrootsSecureStoreKey(namespace: "identity", name: "selected") 51 52 try store.put( 53 Data("secret".utf8), 54 for: key, 55 policy: .userPresenceLocalSecret 56 ) 57 58 #expect(try store.policy(for: key) == .userPresenceLocalSecret) 59 #expect(try store.contains(key)) 60 } 61 62 @Test func inMemorySecureStoreDeletesNamespace() throws { 63 let store = RadrootsInMemorySecureStore() 64 let selected = RadrootsSecureStoreKey(namespace: " identity ", name: "selected") 65 let relay = RadrootsSecureStoreKey(namespace: "relay", name: "selected") 66 67 try store.put(Data("secret".utf8), for: selected) 68 try store.put(Data("relay".utf8), for: relay) 69 try store.deleteNamespace(" identity ") 70 71 #expect(try store.get(selected) == nil) 72 #expect(try store.get(relay) == Data("relay".utf8)) 73 } 74 75 @Test func inMemoryPermissionStatusProviderReturnsDefaultsAndOverrides() async throws { 76 let provider = RadrootsInMemoryPermissionStatusProvider( 77 statuses: [.camera: .denied], 78 defaultStatus: .notDetermined, 79 observedAt: Date(timeIntervalSince1970: 1) 80 ) 81 82 #expect(try await provider.snapshot(for: .camera).status == .denied) 83 #expect(try await provider.snapshot(for: .location).status == .notDetermined) 84 85 await provider.setStatus(.authorized, for: .location, observedAt: Date(timeIntervalSince1970: 2)) 86 87 let location = try await provider.snapshot(for: .location) 88 #expect(location.status == .authorized) 89 #expect(location.observedAt == Date(timeIntervalSince1970: 2)) 90 } 91 92 @Test func fakeLocationServicesTracksRequestsAndResults() async throws { 93 let reading = try RadrootsLocationReading( 94 coordinate: RadrootsLocationCoordinate(latitude: 49.2827, longitude: -123.1207), 95 horizontalAccuracyMeters: 6, 96 capturedAt: Date(timeIntervalSince1970: 10) 97 ) 98 let service = RadrootsFakeLocationServices( 99 availability: RadrootsLocationServicesAvailability( 100 locationServicesEnabled: true, 101 authorization: .notDetermined 102 ), 103 authorizationAfterRequest: .authorizedWhenInUse, 104 currentLocationOutcome: .success(reading) 105 ) 106 107 #expect(await service.currentAvailability().authorization == .notDetermined) 108 #expect(try await service.requestWhenInUseAuthorization() == .authorizedWhenInUse) 109 #expect(await service.currentAvailability().authorization == .authorizedWhenInUse) 110 111 let result = try await service.currentLocation(RadrootsCurrentLocationRequest(timeoutSeconds: 2)) 112 #expect(result.reading == reading) 113 #expect(result.authorization == .authorizedWhenInUse) 114 #expect(await service.requestAuthorizationCount == 1) 115 #expect(await service.currentLocationRequestCount == 1) 116 } 117 118 @Test func fakeLocationServicesCanReturnTypedFailures() async throws { 119 let reading = try RadrootsLocationReading( 120 coordinate: RadrootsLocationCoordinate(latitude: 49.2827, longitude: -123.1207), 121 horizontalAccuracyMeters: 6, 122 capturedAt: Date(timeIntervalSince1970: 10) 123 ) 124 let service = RadrootsFakeLocationServices( 125 availability: RadrootsLocationServicesAvailability( 126 locationServicesEnabled: true, 127 authorization: .authorizedWhenInUse 128 ), 129 currentLocationOutcome: .success(reading) 130 ) 131 await service.setCurrentLocationOutcome(.failure(.timeout)) 132 133 await #expect(throws: RadrootsLocationServicesError.timeout) { 134 _ = try await service.currentLocation(RadrootsCurrentLocationRequest(timeoutSeconds: 2)) 135 } 136 }