diff --git a/Sources/XXMessengerClient/Messenger/Functions/MessengerListenForMessages.swift b/Sources/XXMessengerClient/Messenger/Functions/MessengerListenForMessages.swift new file mode 100644 index 0000000000000000000000000000000000000000..90fa02dab3cf84d85e50e3d696fe0a5a226d4884 --- /dev/null +++ b/Sources/XXMessengerClient/Messenger/Functions/MessengerListenForMessages.swift @@ -0,0 +1,35 @@ +import XCTestDynamicOverlay +import XXClient + +public struct MessengerListenForMessages { + public enum Error: Swift.Error { + case notConnected + } + + public var run: () throws -> Void + + public func callAsFunction() throws -> Void { + try run() + } +} + +extension MessengerListenForMessages { + public static func live(_ env: MessengerEnvironment) -> MessengerListenForMessages { + MessengerListenForMessages { + guard let e2e = env.e2e() else { + throw Error.notConnected + } + try e2e.registerListener( + senderId: nil, + messageType: 2, + callback: env.messageListeners.registered() + ) + } + } +} + +extension MessengerListenForMessages { + public static let unimplemented = MessengerListenForMessages( + run: XCTUnimplemented("\(Self.self)") + ) +} diff --git a/Tests/XXMessengerClientTests/Messenger/Functions/MessengerListenForMessagesTests.swift b/Tests/XXMessengerClientTests/Messenger/Functions/MessengerListenForMessagesTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..948ee33e3d668fdb0eff4a4cfad72f82b908c395 --- /dev/null +++ b/Tests/XXMessengerClientTests/Messenger/Functions/MessengerListenForMessagesTests.swift @@ -0,0 +1,69 @@ +import CustomDump +import XCTest +import XXClient +@testable import XXMessengerClient + +final class MessengerListenForMessagesTests: XCTestCase { + func testListen() throws { + struct RegisterListenerParams: Equatable { + var senderId: Data? + var messageType: Int + } + var didRegisterListenerWithParams: [RegisterListenerParams] = [] + var didRegisterListenerWithCallback: [Listener] = [] + var didHandleMessage: [Message] = [] + + var env: MessengerEnvironment = .unimplemented + env.e2e.get = { + var e2e: E2E = .unimplemented + e2e.registerListener.run = { senderId, messageType, callback in + didRegisterListenerWithParams.append(.init(senderId: senderId, messageType: messageType)) + didRegisterListenerWithCallback.append(callback) + } + return e2e + } + env.messageListeners.registered = { + Listener { message in didHandleMessage.append(message) } + } + let listen: MessengerListenForMessages = .live(env) + + try listen() + + XCTAssertNoDifference(didRegisterListenerWithParams, [ + .init(senderId: nil, messageType: 2) + ]) + + let message = Message.stub(123) + didRegisterListenerWithCallback.first?.handle(message) + + XCTAssertNoDifference(didHandleMessage, [message]) + } + + func testListenWhenNotLoggedIn() { + var env: MessengerEnvironment = .unimplemented + env.e2e.get = { nil } + let listen: MessengerListenForMessages = .live(env) + + XCTAssertThrowsError(try listen()) { error in + XCTAssertNoDifference(error as? MessengerListenForMessages.Error, .notConnected) + } + } + + func testListenFailure() { + struct Failure: Error, Equatable {} + let error = Failure() + + var env: MessengerEnvironment = .unimplemented + env.e2e.get = { + var e2e: E2E = .unimplemented + e2e.registerListener.run = { _, _, _ in throw error } + return e2e + } + env.messageListeners.registered = { Listener.unimplemented } + let listen: MessengerListenForMessages = .live(env) + + XCTAssertThrowsError(try listen()) { err in + XCTAssertNoDifference(err as? Failure, error) + } + } +}