Skip to content

Commit ca4e39e

Browse files
committed
Fix tests
1 parent 26ce545 commit ca4e39e

File tree

5 files changed

+45
-52
lines changed

5 files changed

+45
-52
lines changed

Sources/StreamChat/Repositories/ConnectionRepository.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ConnectionRepository: @unchecked Sendable {
2626
private let syncRepository: SyncRepository
2727
private let webSocketRequestEncoder: RequestEncoder?
2828
private let webSocketClient: WebSocketClient?
29-
private var webSocketConnectEndpoint: Endpoint<EmptyResponse>?
29+
private(set) var webSocketConnectEndpoint: Endpoint<EmptyResponse>?
3030
private let apiClient: APIClient
3131
private let timerType: TimerScheduling.Type
3232

Sources/StreamChat/WebSocketClient/Events/ConnectionEvents.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,3 @@ public final class HealthCheckEvent: ConnectionEvent, EventDTO, Sendable {
3333
)
3434
}
3535
}
36-
37-
/// Emitted when `Client` changes it's connection status. You can listen to this event and indicate the different connection
38-
/// states in the UI (banners like "Offline", "Reconnecting"", etc.).
39-
public struct ConnectionStatusUpdated: Event {
40-
/// The current connection status of `Client`
41-
public let connectionStatus: ConnectionStatus
42-
43-
// Underlying WebSocketConnectionState
44-
let webSocketConnectionState: WebSocketConnectionState
45-
46-
init(webSocketConnectionState: WebSocketConnectionState) {
47-
connectionStatus = .init(webSocketConnectionState: webSocketConnectionState)
48-
self.webSocketConnectionState = webSocketConnectionState
49-
}
50-
}

TestTools/StreamChatTestTools/Mocks/StreamChat/WebSocketClient/WebSocketClient_Mock.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Foundation
99
/// Mock implementation of `WebSocketClient`.
1010
final class WebSocketClient_Mock: WebSocketClient, @unchecked Sendable {
1111
let init_sessionConfiguration: URLSessionConfiguration
12-
let init_requestEncoder: RequestEncoder
1312
let init_eventDecoder: AnyEventDecoder
1413
let init_eventNotificationCenter: EventNotificationCenter
1514
let init_environment: WebSocketClient.Environment
@@ -29,7 +28,6 @@ final class WebSocketClient_Mock: WebSocketClient, @unchecked Sendable {
2928

3029
init(
3130
sessionConfiguration: URLSessionConfiguration = .ephemeral,
32-
requestEncoder: RequestEncoder = DefaultRequestEncoder(baseURL: .unique(), apiKey: .init(.unique)),
3331
eventDecoder: AnyEventDecoder = EventDecoder(),
3432
eventNotificationCenter: EventNotificationCenter = EventNotificationCenter_Mock(database: DatabaseContainer_Spy()),
3533
pingController: WebSocketPingController? = nil,
@@ -50,7 +48,6 @@ final class WebSocketClient_Mock: WebSocketClient, @unchecked Sendable {
5048
}
5149

5250
init_sessionConfiguration = sessionConfiguration
53-
init_requestEncoder = requestEncoder
5451
init_eventDecoder = eventDecoder
5552
init_eventNotificationCenter = eventNotificationCenter
5653
init_environment = environment

Tests/StreamChatTests/ChatClient_Tests.swift

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,14 @@ final class ChatClient_Tests: XCTestCase {
175175
let webSocket = testEnv.webSocketClient
176176
assertMandatoryHeaderFields(webSocket?.init_sessionConfiguration)
177177
XCTAssertEqual(webSocket?.init_sessionConfiguration.waitsForConnectivity, false)
178-
XCTAssert(webSocket?.init_requestEncoder is RequestEncoder_Spy)
179178
XCTAssert((webSocket?.init_eventNotificationCenter as? EventPersistentNotificationCenter)?.database === client.databaseContainer)
180179
XCTAssertNotNil(webSocket?.init_eventDecoder)
181180

182181
// EventDataProcessorMiddleware must be always first
183182
XCTAssert((webSocket?.init_eventNotificationCenter as? EventPersistentNotificationCenter)?.middlewares[0] is EventDataProcessorMiddleware)
184183

185184
// Assert Client sets itself as delegate for the request encoder
186-
XCTAssert(webSocket?.init_requestEncoder.connectionDetailsProviderDelegate === client)
185+
XCTAssert(client.webSocketRequestEncoder?.connectionDetailsProviderDelegate === client)
187186
}
188187

189188
func test_webSocketClient_hasAllMandatoryMiddlewares() throws {
@@ -475,30 +474,28 @@ final class ChatClient_Tests: XCTestCase {
475474
AssertAsync.canBeReleased(&chatClient)
476475

477476
// Take main then background queue.
478-
for queue in [DispatchQueue.main, DispatchQueue.global()] {
479-
let error: Error? = try waitFor { completion in
480-
// Dispatch creating a chat-client to specific queue.
481-
queue.async {
482-
// Create a `ChatClient` instance with the same config
483-
// to access the storage with exited current user.
484-
let chatClient = ChatClient(config: config)
485-
chatClient.connectUser(userInfo: .init(id: currentUserId), token: .unique(userId: currentUserId))
486-
487-
let expected = try? chatClient.apiClient.encoder.encodeRequest(for: .webSocketConnect(userInfo: UserInfo(id: currentUserId)))
488-
489-
// 1. Check `currentUserId` is fetched synchronously
490-
// 2. `webSocket` has correct connect endpoint
491-
if chatClient.currentUserId == currentUserId,
492-
chatClient.webSocketClient?.connectRequest == expected {
493-
completion(nil)
494-
} else {
495-
completion(TestError())
496-
}
477+
let queueAndExpectation: [(DispatchQueue, XCTestExpectation)] = [
478+
(.main, XCTestExpectation(description: "main")),
479+
(.global(), XCTestExpectation(description: "global"))
480+
]
481+
for (queue, expectation) in queueAndExpectation {
482+
// Dispatch creating a chat-client to specific queue.
483+
queue.async {
484+
// Create a `ChatClient` instance with the same config
485+
// to access the storage with exited current user.
486+
let chatClient = ChatClient(config: config)
487+
chatClient.connectUser(userInfo: .init(id: currentUserId), token: .unique(userId: currentUserId))
488+
489+
let expectedWebSocketEndpoint = AnyEndpoint(.webSocketConnect(userInfo: UserInfo(id: currentUserId)))
490+
// 1. Check `currentUserId` is fetched synchronously
491+
// 2. `webSocket` has correct connect endpoint
492+
if chatClient.currentUserId == currentUserId,
493+
chatClient.connectionRepository.webSocketConnectEndpoint.map(AnyEndpoint.init) == expectedWebSocketEndpoint {
494+
expectation.fulfill()
497495
}
498496
}
499-
500-
XCTAssertNil(error)
501497
}
498+
wait(for: queueAndExpectation.map({ $1 }), timeout: defaultTimeout)
502499
}
503500

504501
// MARK: - Connect Token Provider

Tests/StreamChatTests/Repositories/ConnectionRepository_Tests.swift

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,15 @@ final class ConnectionRepository_Tests: XCTestCase {
204204

205205
repository.updateWebSocketEndpoint(with: token, userInfo: nil)
206206

207-
let expected = try apiClient.encoder.encodeRequest(for: .webSocketConnect(userInfo: UserInfo(id: tokenUserId)))
208-
209207
// UserInfo should take priority
210-
XCTAssertEqual(webSocketClient.connectRequest, expected)
208+
XCTAssertEqual(
209+
repository.webSocketConnectEndpoint.map(AnyEndpoint.init),
210+
AnyEndpoint(
211+
.webSocketConnect(
212+
userInfo: UserInfo(id: tokenUserId)
213+
)
214+
)
215+
)
211216
}
212217

213218
func test_updateWebSocketEndpointWithTokenAndUserInfo() throws {
@@ -218,20 +223,29 @@ final class ConnectionRepository_Tests: XCTestCase {
218223

219224
repository.updateWebSocketEndpoint(with: token, userInfo: userInfo)
220225

221-
let expected = try apiClient.encoder.encodeRequest(for: .webSocketConnect(userInfo: UserInfo(id: userInfoUserId)))
222-
223226
// UserInfo should take priority
224-
XCTAssertEqual(webSocketClient.connectRequest, expected)
227+
XCTAssertEqual(
228+
repository.webSocketConnectEndpoint.map(AnyEndpoint.init),
229+
AnyEndpoint(
230+
.webSocketConnect(
231+
userInfo: UserInfo(id: userInfoUserId)
232+
)
233+
)
234+
)
225235
}
226236

227237
func test_updateWebSocketEndpointWithUserId() throws {
228238
let userId = "123-userId"
229239
repository.updateWebSocketEndpoint(with: userId)
230240

231-
let expected = try apiClient.encoder.encodeRequest(for: .webSocketConnect(userInfo: UserInfo(id: userId)))
232-
233-
// UserInfo should take priority
234-
XCTAssertEqual(webSocketClient.connectRequest, expected)
241+
XCTAssertEqual(
242+
repository.webSocketConnectEndpoint.map(AnyEndpoint.init),
243+
AnyEndpoint(
244+
.webSocketConnect(
245+
userInfo: UserInfo(id: userId)
246+
)
247+
)
248+
)
235249
}
236250

237251
// MARK: Handle connection update

0 commit comments

Comments
 (0)