apple_kit

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

RadrootsExternalActionsTesting.swift (2693B)


      1 import Foundation
      2 import RadrootsKit
      3 
      4 public actor RadrootsFakeExternalActions: RadrootsExternalActions {
      5     private var capabilityOverrides: [RadrootsExternalActionDestination: Bool]
      6     private var defaultCanOpen: Bool
      7     private var openOutcome: Result<Void, RadrootsExternalActionError>
      8     private var capabilityRequestCountValue: Int
      9     private var openRequestCountValue: Int
     10     private var lastCapabilityDestinationValue: RadrootsExternalActionDestination?
     11     private var openedDestinationsValue: [RadrootsExternalActionDestination]
     12 
     13     public init(
     14         capabilityOverrides: [RadrootsExternalActionDestination: Bool] = [:],
     15         defaultCanOpen: Bool = true,
     16         openOutcome: Result<Void, RadrootsExternalActionError> = .success(())
     17     ) {
     18         self.capabilityOverrides = capabilityOverrides
     19         self.defaultCanOpen = defaultCanOpen
     20         self.openOutcome = openOutcome
     21         self.capabilityRequestCountValue = 0
     22         self.openRequestCountValue = 0
     23         self.lastCapabilityDestinationValue = nil
     24         self.openedDestinationsValue = []
     25     }
     26 
     27     public func setCapability(_ canOpen: Bool, for destination: RadrootsExternalActionDestination) {
     28         capabilityOverrides[destination] = canOpen
     29     }
     30 
     31     public func setDefaultCapability(_ canOpen: Bool) {
     32         defaultCanOpen = canOpen
     33     }
     34 
     35     public func setOpenOutcome(_ outcome: Result<Void, RadrootsExternalActionError>) {
     36         openOutcome = outcome
     37     }
     38 
     39     public func canOpen(_ destination: RadrootsExternalActionDestination) async -> RadrootsExternalActionCapability {
     40         capabilityRequestCountValue += 1
     41         lastCapabilityDestinationValue = destination
     42         return RadrootsExternalActionCapability(
     43             destination: destination,
     44             canOpen: capabilityOverrides[destination] ?? defaultCanOpen
     45         )
     46     }
     47 
     48     public func open(_ request: RadrootsExternalActionRequest) async throws {
     49         openRequestCountValue += 1
     50         openedDestinationsValue.append(request.destination)
     51         switch openOutcome {
     52         case .success:
     53             return
     54         case .failure(let error):
     55             throw error
     56         }
     57     }
     58 
     59     public var capabilityRequestCount: Int {
     60         capabilityRequestCountValue
     61     }
     62 
     63     public var openRequestCount: Int {
     64         openRequestCountValue
     65     }
     66 
     67     public var lastCapabilityDestination: RadrootsExternalActionDestination? {
     68         lastCapabilityDestinationValue
     69     }
     70 
     71     public var openedDestinations: [RadrootsExternalActionDestination] {
     72         openedDestinationsValue
     73     }
     74 
     75     public var lastOpenedDestination: RadrootsExternalActionDestination? {
     76         openedDestinationsValue.last
     77     }
     78 }