RadrootsBackgroundTaskTests.swift (3997B)
1 import Foundation 2 import Testing 3 4 @testable import RadrootsKit 5 6 @Test func backgroundTaskIdentifierNormalizesAndRejectsUnsafeValues() throws { 7 let identifier = try RadrootsBackgroundTaskIdentifier(" ORG.RADROOTS.FIELD-IOS.refresh ") 8 9 #expect(identifier.rawValue == "org.radroots.field-ios.refresh") 10 11 #expect(throws: RadrootsBackgroundTaskError.invalidRequest) { 12 _ = try RadrootsBackgroundTaskIdentifier(" ") 13 } 14 #expect(throws: RadrootsBackgroundTaskError.invalidRequest) { 15 _ = try RadrootsBackgroundTaskIdentifier(".org.radroots") 16 } 17 #expect(throws: RadrootsBackgroundTaskError.invalidRequest) { 18 _ = try RadrootsBackgroundTaskIdentifier("org.radroots..refresh") 19 } 20 } 21 22 @Test func backgroundTaskRequestValidatesKindSpecificOptions() throws { 23 let refresh = try RadrootsBackgroundTaskRequest( 24 identifier: "org.radroots.field-ios.background.refresh", 25 kind: .appRefresh, 26 earliestBeginDate: Date(timeIntervalSince1970: 10) 27 ) 28 29 #expect(refresh.kind == .appRefresh) 30 #expect(refresh.earliestBeginDate == Date(timeIntervalSince1970: 10)) 31 #expect(!refresh.requiresNetworkConnectivity) 32 33 let processing = try RadrootsBackgroundTaskRequest( 34 identifier: "org.radroots.field-ios.background.processing", 35 kind: .processing, 36 requiresNetworkConnectivity: true, 37 requiresExternalPower: true 38 ) 39 40 #expect(processing.kind == .processing) 41 #expect(processing.requiresNetworkConnectivity) 42 #expect(processing.requiresExternalPower) 43 44 #expect(throws: RadrootsBackgroundTaskError.invalidRequest) { 45 _ = try RadrootsBackgroundTaskRequest( 46 identifier: "org.radroots.field-ios.background.refresh", 47 kind: .appRefresh, 48 requiresNetworkConnectivity: true 49 ) 50 } 51 #expect(throws: RadrootsBackgroundTaskError.invalidRequest) { 52 _ = try RadrootsBackgroundTaskRequest( 53 identifier: "org.radroots.field-ios.background.refresh", 54 kind: .appRefresh, 55 earliestBeginDate: Date(timeIntervalSinceReferenceDate: .infinity) 56 ) 57 } 58 } 59 60 @Test func backgroundTaskSnapshotPreservesRequestValues() throws { 61 let request = try RadrootsBackgroundTaskRequest( 62 identifier: "org.radroots.field-ios.background.processing", 63 kind: .processing, 64 earliestBeginDate: Date(timeIntervalSince1970: 5), 65 requiresNetworkConnectivity: true 66 ) 67 let snapshot = try RadrootsBackgroundTaskSnapshot( 68 request: request, 69 submittedAt: Date(timeIntervalSince1970: 7) 70 ) 71 72 #expect(snapshot.identifier == request.identifier) 73 #expect(snapshot.kind == .processing) 74 #expect(snapshot.earliestBeginDate == Date(timeIntervalSince1970: 5)) 75 #expect(snapshot.submittedAt == Date(timeIntervalSince1970: 7)) 76 #expect(snapshot.requiresNetworkConnectivity) 77 #expect(!snapshot.requiresExternalPower) 78 79 #expect(throws: RadrootsBackgroundTaskError.invalidRequest) { 80 _ = try RadrootsBackgroundTaskSnapshot( 81 request: request, 82 submittedAt: Date(timeIntervalSinceReferenceDate: .infinity) 83 ) 84 } 85 } 86 87 @Test func unavailableBackgroundTaskSchedulerThrowsTypedErrors() async throws { 88 let scheduler = RadrootsUnavailableBackgroundTaskScheduler() 89 let identifier = try RadrootsBackgroundTaskIdentifier("org.radroots.field-ios.background.refresh") 90 let request = try RadrootsBackgroundTaskRequest(identifier: identifier, kind: .appRefresh) 91 92 await #expect(throws: RadrootsBackgroundTaskError.unavailable) { 93 _ = try await scheduler.submit(request) 94 } 95 await #expect(throws: RadrootsBackgroundTaskError.unavailable) { 96 try await scheduler.cancel(identifier) 97 } 98 await #expect(throws: RadrootsBackgroundTaskError.unavailable) { 99 try await scheduler.cancelAll() 100 } 101 await #expect(throws: RadrootsBackgroundTaskError.unavailable) { 102 _ = try await scheduler.pendingTasks() 103 } 104 }