-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMockRealtime.swift
More file actions
58 lines (49 loc) · 2.48 KB
/
MockRealtime.swift
File metadata and controls
58 lines (49 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Ably
@testable import AblyChat
import Foundation
/// A mock implementation of `InternalRealtimeClientProtocol`. We’ll figure out how to do mocking in tests properly in https://github.com/ably-labs/ably-chat-swift/issues/5.
final class MockRealtime: InternalRealtimeClientProtocol {
let callRecorder = MockMethodCallRecorder()
let connection: MockConnection
let channels: MockChannels
let paginatedCallback: (@Sendable () throws(ARTErrorInfo) -> ARTHTTPPaginatedResponse)?
let createWrapperSDKProxyReturnValue: MockSuppliedRealtime?
private(set) var requestArguments: [(method: String, path: String, params: [String: String]?, body: Any?, headers: [String: String]?)] = []
private(set) var createWrapperSDKProxyOptionsArgument: ARTWrapperSDKProxyOptions?
var clientId: String? {
"mockClientId"
}
init(
channels: MockChannels = .init(channels: []),
connection: MockConnection = .init(),
paginatedCallback: (@Sendable () throws(ARTErrorInfo) -> ARTHTTPPaginatedResponse)? = nil,
createWrapperSDKProxyReturnValue: MockSuppliedRealtime? = nil
) {
self.channels = channels
self.paginatedCallback = paginatedCallback
self.connection = connection
self.createWrapperSDKProxyReturnValue = createWrapperSDKProxyReturnValue
}
func request(_ method: String, path: String, params: [String: String]?, body: Any?, headers: [String: String]?) async throws(InternalError) -> ARTHTTPPaginatedResponse {
requestArguments.append((method: method, path: path, params: params, body: body, headers: headers))
guard let paginatedCallback else {
fatalError("Paginated callback not set")
}
do {
callRecorder.addRecord(
signature: "request(_:path:params:body:headers:)",
arguments: ["method": method, "path": path, "params": params, "body": body == nil ? [:] : body as? [String: Any], "headers": headers]
)
return try paginatedCallback()
} catch {
throw error.toInternalError()
}
}
func createWrapperSDKProxy(with options: ARTWrapperSDKProxyOptions) -> some RealtimeClientProtocol {
guard let createWrapperSDKProxyReturnValue else {
fatalError("createWrapperSDKProxyReturnValue must be set in order to call createWrapperSDKProxy(with:)")
}
createWrapperSDKProxyOptionsArgument = options
return createWrapperSDKProxyReturnValue
}
}