From 2c8d26bf18ac49b4249fe8838f3b7ff8ca543a64 Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Tue, 28 Jan 2025 16:30:35 +0100 Subject: [PATCH] SwiftFormat + SwiftLint --- .swiftlint.yml | 2 + .../ChatAsyncIntegrationTests.swift | 210 +++++++++--------- .../MembershipAsyncIntegrationTests.swift | 4 +- .../MessageAsyncIntegrationTests.swift | 2 +- .../MessageDraftAsyncIntegrationTests.swift | 10 +- .../ThreadChannelAsyncIntegrationTests.swift | 14 +- .../ThreadMessageAsyncIntegrationTests.swift | 2 +- .../UserAsyncIntegrationTests.swift | 46 ++-- Tests/BaseClosureIntegrationTestCase.swift | 2 +- Tests/ChatIntegrationTests.swift | 14 +- Tests/UserIntegrationTests.swift | 4 +- 11 files changed, 156 insertions(+), 154 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 22771a5..97bafe4 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -8,6 +8,8 @@ disabled_rules: - function_parameter_count included: - Sources/ +excluded: + - Tests/ opt_in_rules: - force_unwrapping - overridden_super_call diff --git a/Tests/AsyncAwait/ChatAsyncIntegrationTests.swift b/Tests/AsyncAwait/ChatAsyncIntegrationTests.swift index 052011d..3e1e516 100644 --- a/Tests/AsyncAwait/ChatAsyncIntegrationTests.swift +++ b/Tests/AsyncAwait/ChatAsyncIntegrationTests.swift @@ -22,27 +22,27 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { status: "active", type: "type" ) - + XCTAssertEqual(user.name, "randomUser") XCTAssertEqual(user.status, "active") XCTAssertEqual(user.type, "type") - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteUser(id: user.id) } } - + func testChatAsync_GetUser() async throws { let user = try await chat.getUser(userId: chat.currentUser.id) - + XCTAssertEqual(user?.id, chat.currentUser.id) XCTAssertEqual(user?.name, chat.currentUser.name) } - + func testChatAsync_GetUsers() async throws { let user = try await chat.createUser(id: randomString()) let users = try await chat.getUsers(limit: 10) - + XCTAssertFalse( users.users.isEmpty ) @@ -50,11 +50,11 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { _ = try? await chat.deleteUser(id: user.id) } } - + func testChatAsync_UpdateUser() async throws { let userId = randomString() - let user = try await chat.createUser(id: userId) - + _ = try await chat.createUser(id: userId) + let newCustom: [String: JSONCodableScalar] = [ "someValue": 17_253_575_019_298_112, "someStr": "str" @@ -69,7 +69,7 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { status: "offline", type: "regular" ) - + XCTAssertEqual(updatedUser.id, userId) XCTAssertEqual(updatedUser.name, "NewName") XCTAssertEqual(updatedUser.externalId, "NewExternalId") @@ -78,17 +78,17 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { XCTAssertEqual(updatedUser.custom?.mapValues { $0.scalarValue }, newCustom.mapValues { $0.scalarValue }) XCTAssertEqual(updatedUser.status, "offline") XCTAssertEqual(updatedUser.type, "regular") - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteUser(id: userId) } } - + func testChatAsync_Delete() async throws { let user = try await chat.createUser(id: randomString()) try await chat.deleteUser(id: user.id) let retrievedUser = try await chat.getUser(userId: user.id) - + XCTAssertNil( retrievedUser ) @@ -96,51 +96,51 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { _ = try? await chat.deleteUser(id: user.id) } } - + func testChatAsync_WherePresent() async throws { let channelId = randomString() let channel = try await chat.createChannel(id: channelId, name: channelId) - + // Keeping a strong reference to this object for test purposes to simulate that someone is already present on the given channel. // If this object is not retained, it will be deallocated, resulting in no subscription to the channel, // which would cause the behavior being tested to fail. let connectResult = channel.connect() debugPrint(connectResult) - + try await Task.sleep(nanoseconds: 5_000_000_000) let channelIdentifiers = try await chat.wherePresent(userId: chat.currentUser.id) let expectedChannelIdentifiers = [channelId] - + XCTAssertEqual(expectedChannelIdentifiers, channelIdentifiers) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channelId) } } - + func testChatAsync_IsPresent() async throws { let channelId = randomString() let channel = try await chat.createChannel(id: channelId, name: channelId) - + // Keeping a strong reference to these objects for test purposes to simulate that someone is already present on the given channel. // If this object is not retained, it will be deallocated, resulting in no subscription to the channel, // which would cause the behavior being tested to fail. let connectResult = channel.connect() let joinResult = try await channel.join() - + debugPrint(connectResult) debugPrint(joinResult) - + try await Task.sleep(nanoseconds: 3_000_000_000) - + let isPresent = try await chat.isPresent(userId: chat.currentUser.id, channelId: channelId) XCTAssertTrue(isPresent) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channelId) } } - + func testChatAsync_CreateChannel() async throws { let customField: [String: JSONCodableScalar] = [ "someValue": 17_253_575_019_298_112, @@ -154,29 +154,29 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { type: .unknown, status: "status" ) - + XCTAssertEqual(channel.name, "ChannelName") XCTAssertEqual(channel.description, "ChannelDescription") XCTAssertEqual(channel.type, .unknown) XCTAssertEqual(channel.status, "status") XCTAssertEqual(channel.custom?.mapValues { $0.scalarValue }, customField.mapValues { $0.scalarValue }) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_GetChannel() async throws { let channel = try await chat.createChannel(id: randomString(), name: "ChannelName") let retrievedChannel = try await chat.getChannel(channelId: channel.id) - + XCTAssertEqual(retrievedChannel?.id, channel.id) } - + func testChatAsync_GetChannels() async throws { let channel = try await chat.createChannel(id: randomString()) let retrievedChannels = try await chat.getChannels() - + XCTAssertFalse( retrievedChannels.channels.isEmpty ) @@ -184,11 +184,11 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_UpdateChannel() async throws { let channel = try await chat.createChannel(id: randomString()) let customField: [String: JSONCodableScalar] = ["someValue": 17_253_575_019_298_112, "someStr": "str"] - + let updatedChannel = try await chat.updateChannel( id: channel.id, name: "NewName", @@ -197,24 +197,24 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { status: "status", type: .unknown ) - + XCTAssertEqual(updatedChannel.id, channel.id) XCTAssertEqual(updatedChannel.name, "NewName") XCTAssertEqual(updatedChannel.custom?.mapValues { $0.scalarValue }, customField.mapValues { $0.scalarValue }) XCTAssertEqual(updatedChannel.description, "NewDescription") XCTAssertEqual(updatedChannel.status, "status") XCTAssertEqual(updatedChannel.type, .unknown) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_DeleteChannel() async throws { let channel = try await chat.createChannel(id: randomString()) try await chat.deleteChannel(id: channel.id) let retrievedChannel = try await chat.getChannel(channelId: channel.id) - + XCTAssertNil( retrievedChannel ) @@ -222,58 +222,58 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_WhoIsPresent() async throws { let channel = try await chat.createChannel( id: randomString(), name: "ChannelName" ) - + // Keeping a strong reference to these objects for test purposes to simulate that someone is already present on the given channel. // If this object is not retained, it will be deallocated, resulting in no subscription to the channel, // which would cause the behavior being tested to fail. let joinValue = try await channel.join() debugPrint(joinValue) - + try await Task.sleep(nanoseconds: 4_000_000_000) - + let whoIsPresentValue = try await chat.whoIsPresent(channelId: channel.id) XCTAssertEqual(whoIsPresentValue.count, 1) XCTAssertEqual(whoIsPresentValue.first, chat.currentUser.id) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_EmitEvent() async throws { let expectation = expectation(description: "Emit Event") expectation.assertForOverFulfill = true expectation.expectedFulfillmentCount = 1 - + let channel = try await chat.createChannel( id: randomString(), name: "ChannelName" ) - + let task = Task { for await typpingUsers in channel.getTyping() { XCTAssertEqual(typpingUsers, [chat.currentUser.id]) expectation.fulfill() } } - + try await Task.sleep(nanoseconds: 3_000_000_000) try await chat.emitEvent(channelId: channel.id, payload: EventContent.Typing(value: true)) - + await fulfillment(of: [expectation], timeout: 6) - + addTeardownBlock { [unowned self] in task.cancel() _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_CreatePublicConversation() async throws { let customField: [String: JSONCodableScalar] = [ "someValue": 17_253_575_019_298_112, @@ -285,18 +285,18 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { channelCustom: customField, channelStatus: "status" ) - + XCTAssertEqual(channel.name, "ChannelName") XCTAssertEqual(channel.description, "ChannelDescription") XCTAssertEqual(channel.custom?.mapValues { $0.scalarValue }, customField.mapValues { $0.scalarValue }) XCTAssertEqual(channel.status, "status") XCTAssertEqual(channel.type, .public) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_CreateDirectConversation() async throws { let customField: [String: JSONCodableScalar] = [ "someValue": 17_253_575_019_298_112, @@ -306,12 +306,12 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { "val1": 123, "val2": "lorem ipsum" ] - + let anotherUser = try await chat.createUser( id: randomString(), name: "AnotherUser" ) - + let resultValue = try await chat.createDirectConversation( invitedUser: anotherUser, channelName: "ChannelName", @@ -320,26 +320,26 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { channelStatus: "status", membershipCustom: membershipCustom ) - + XCTAssertEqual(resultValue.channel.name, "ChannelName") XCTAssertEqual(resultValue.channel.description, "ChannelDescription") XCTAssertEqual(resultValue.channel.custom?.mapValues { $0.scalarValue }, customField.mapValues { $0.scalarValue }) XCTAssertEqual(resultValue.channel.status, "status") XCTAssertEqual(resultValue.channel.type, .direct) - + let inviteeMembership = try XCTUnwrap(resultValue.inviteeMembership) let hostMembership = try XCTUnwrap(resultValue.hostMembership) - + XCTAssertEqual(inviteeMembership.user.id, anotherUser.id) XCTAssertEqual(hostMembership.user.id, chat.currentUser.id) XCTAssertEqual(hostMembership.custom?.mapValues { $0.scalarValue }, membershipCustom.mapValues { $0.scalarValue }) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: resultValue.channel.id) _ = try? await chat.deleteUser(id: anotherUser.id) } } - + func testChatAsync_CreateGroupConversation() async throws { let anotherUser = try await chat.createUser( id: randomString(), @@ -353,7 +353,7 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { "val1": 123, "val2": "lorem ipsum" ] - + let resultValue = try await chat.createGroupConversation( invitedUsers: [anotherUser], channelName: "ChannelName", @@ -362,33 +362,33 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { channelStatus: "status", membershipCustom: membershipCustom ) - + XCTAssertEqual(resultValue.channel.name, "ChannelName") XCTAssertEqual(resultValue.channel.description, "ChannelDescription") XCTAssertEqual(resultValue.channel.custom?.mapValues { $0.scalarValue }, customField.mapValues { $0.scalarValue }) XCTAssertEqual(resultValue.channel.status, "status") XCTAssertEqual(resultValue.channel.type, .group) - + let inviteeMembership = try XCTUnwrap(resultValue.inviteeMemberships.first) let hostMembership = try XCTUnwrap(resultValue.hostMembership) - + XCTAssertEqual(inviteeMembership.user.id, anotherUser.id) XCTAssertEqual(hostMembership.user.id, chat.currentUser.id) XCTAssertEqual(hostMembership.custom?.mapValues { $0.scalarValue }, membershipCustom.mapValues { $0.scalarValue }) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: resultValue.channel.id) _ = try? await chat.deleteUser(id: anotherUser.id) } } - + func testChatAsync_ListenForEvents() async throws { let expectation = expectation(description: "Listen For Events") expectation.assertForOverFulfill = true expectation.expectedFulfillmentCount = 1 - + let channel = try await chat.createChannel(id: randomString()) - + let task = Task { for await event in chat.listenForEvents(type: EventContent.Typing.self, channelId: channel.id) { XCTAssertTrue(event.event.payload.value) @@ -397,17 +397,17 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { expectation.fulfill() } } - + try await Task.sleep(nanoseconds: 3_000_000_000) try await channel.startTyping() await fulfillment(of: [expectation], timeout: 6) - + addTeardownBlock { [unowned self] in task.cancel() _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_RegisterUnregisterPushChannels() async throws { let pushNotificationsConfig = PushNotificationsConfig( sendPushes: false, @@ -419,16 +419,16 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { pubNub: chat.pubNub, configuration: ChatConfiguration(pushNotificationsConfig: pushNotificationsConfig) ) - + let anotherChannel = try await anotherChat.createChannel(id: randomString()) try await anotherChat.registerPushChannels(channels: [anotherChannel.id]) try await anotherChat.unregisterPushChannels(channels: [anotherChannel.id]) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: anotherChannel.id) } } - + func testChatAsync_UnregisterAllPushChannels() async throws { let pushNotificationsConfig = PushNotificationsConfig( sendPushes: false, @@ -440,68 +440,68 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { pubNub: chat.pubNub, configuration: ChatConfiguration(pushNotificationsConfig: pushNotificationsConfig) ) - + let anotherChannel = try await anotherChat.createChannel(id: randomString()) try await anotherChat.registerPushChannels(channels: [anotherChannel.id]) try await anotherChat.unregisterAllPushChannels() - + let pushChannels = try await anotherChat.getPushChannels() XCTAssertTrue(pushChannels.isEmpty) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: anotherChannel.id) } } - + func testChatAsync_GetUnreadMessagesCount() async throws { let channel = try await chat.createChannel(id: randomString()) - + try await channel.invite(user: chat.currentUser) try await channel.sendText(text: "Some new text") try await channel.sendText(text: "Some new text") try await channel.sendText(text: "Some new text") - + let getUnreadMessagesCount = try await chat.getUnreadMessagesCount().first - + XCTAssertEqual(getUnreadMessagesCount?.count, 3) XCTAssertEqual(getUnreadMessagesCount?.channel.id, channel.id) XCTAssertEqual(getUnreadMessagesCount?.membership.user.id, chat.currentUser.id) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_MarkAllMessagesAsRead() async throws { let channel = try await chat.createChannel(id: randomString()) - + try await channel.invite(user: chat.currentUser) try await channel.sendText(text: "Some new text") try await channel.sendText(text: "Some new text") try await channel.sendText(text: "Some new text") - + try await Task.sleep(nanoseconds: 4_000_000_000) try await chat.markAllMessagesAsRead() - + try await Task.sleep(nanoseconds: 4_000_000_000) let getUnreadMessagesCount = try await chat.getUnreadMessagesCount() - + XCTAssertTrue(getUnreadMessagesCount.isEmpty) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_GetEventsHistory() async throws { let channel = try await chat.createChannel( id: randomString(), name: "Channel name" ) - + try await channel.invite(user: chat.currentUser) try await Task.sleep(nanoseconds: 4_000_000_000) - + let history = try await chat.getEventsHistory(channelId: chat.currentUser.id) let inviteEvent = try XCTUnwrap(history.events.compactMap { $0.event.payload as? EventContent.Invite }.first) @@ -512,35 +512,35 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_GetCurrentUserMentions() async throws { let channel = try await chat.createChannel( id: randomString(), name: "Channel name" ) - + try await channel.invite(user: chat.currentUser) let tt = try await channel.sendText(text: "Some text") try await Task.sleep(nanoseconds: 2_000_000_000) - + try await chat.emitEvent( channelId: chat.currentUser.id, payload: EventContent.Mention(messageTimetoken: tt, channel: channel.id) ) - + try await Task.sleep(nanoseconds: 3_000_000_000) let userMentionData = try await chat.getCurrentUserMentions().mentions.first - + XCTAssertEqual(userMentionData?.userMentionData.userId, chat.currentUser.id) XCTAssertEqual(userMentionData?.userMentionData.event.channel, channel.id) XCTAssertEqual(userMentionData?.userMentionData.message?.timetoken, tt) XCTAssertEqual(userMentionData?.userMentionData.message?.text, "Some text") - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChatAsync_CustomPayload() async throws { let getMessagePublishBody: GetMessagePublishBody? = { txtContent, _, _ in ["payload": txtContent.text] @@ -552,7 +552,7 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { nil } } - + let customPayloads = CustomPayloads( getMessagePublishBody: getMessagePublishBody, getMessageResponseBody: getMessageResponseBody @@ -561,28 +561,28 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase { pubNub: chat.pubNub, configuration: ChatConfiguration(customPayloads: customPayloads) ) - + let channel = try await anotherChat.createChannel(id: randomString()) - + try await channel.sendText(text: "Some text") try await Task.sleep(nanoseconds: 2_000_000_000) - + let message = try await channel.getHistory(count: 1).messages.first - + XCTAssertEqual(message?.text, "Some text") XCTAssertTrue(message?.files.isEmpty ?? false) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } } - + func testChat_MutedUsers() async throws { let userToMute = randomString() try await chat.mutedUsersManager.muteUser(userId: userToMute) XCTAssertEqual(chat.mutedUsersManager.mutedUsers, [userToMute]) - + try await chat.mutedUsersManager.unmuteUser(userId: userToMute) XCTAssertTrue(chat.mutedUsersManager.mutedUsers.isEmpty) } diff --git a/Tests/AsyncAwait/MembershipAsyncIntegrationTests.swift b/Tests/AsyncAwait/MembershipAsyncIntegrationTests.swift index 731aebb..c09056d 100644 --- a/Tests/AsyncAwait/MembershipAsyncIntegrationTests.swift +++ b/Tests/AsyncAwait/MembershipAsyncIntegrationTests.swift @@ -69,14 +69,14 @@ class MembershipAsyncIntegrationTests: BaseAsyncIntegrationTestCase { let unreadMessagesCount = try await membership.getUnreadMessagesCount() XCTAssertEqual(unreadMessagesCount, 3) } - + func testMembershipAsync_GetUnreadMessagesCountForEmptyChannel() async throws { let someMembership = MembershipImpl( chat: chat, channel: channel, user: UserImpl(chat: chat, id: randomString()) ) - + let unreadMessagesCount = try await someMembership.getUnreadMessagesCount() XCTAssertNil(unreadMessagesCount) } diff --git a/Tests/AsyncAwait/MessageAsyncIntegrationTests.swift b/Tests/AsyncAwait/MessageAsyncIntegrationTests.swift index 085e7f8..534978e 100644 --- a/Tests/AsyncAwait/MessageAsyncIntegrationTests.swift +++ b/Tests/AsyncAwait/MessageAsyncIntegrationTests.swift @@ -32,7 +32,7 @@ class MessageAsyncIntegrationTests: BaseAsyncIntegrationTestCase { testMessage = nil channel = nil } - + func testMessageAsync_HasNoUserReaction() async throws { let someMessage = MessageImpl( chat: chat, diff --git a/Tests/AsyncAwait/MessageDraftAsyncIntegrationTests.swift b/Tests/AsyncAwait/MessageDraftAsyncIntegrationTests.swift index 49340d8..1a9d685 100644 --- a/Tests/AsyncAwait/MessageDraftAsyncIntegrationTests.swift +++ b/Tests/AsyncAwait/MessageDraftAsyncIntegrationTests.swift @@ -20,13 +20,13 @@ class MessageDraftIntegrationTests: BaseAsyncIntegrationTestCase { override func customSetup() async throws { let channelId = "cchnl\(randomString())" let userId = "uuser\(randomString())" - + channel = try await chat.createChannel(id: channelId, name: channelId) user = try await chat.createUser(user: UserImpl(chat: chat, id: userId, name: userId)) - + try await channel.invite(user: user) } - + override func customTearDown() async throws { _ = try? await chat.deleteUser(id: user.id) _ = try? await chat.deleteChannel(id: channel.id) @@ -60,7 +60,7 @@ class MessageDraftIntegrationTests: BaseAsyncIntegrationTestCase { messageDraft.update(text: "This is a @uuser") await fulfillment(of: [expectation], timeout: 6) - + let timetoken = try await messageDraft.send() try await Task.sleep(nanoseconds: 3_000_000_000) let message = try await channel.getMessage(timetoken: timetoken) @@ -104,7 +104,7 @@ class MessageDraftIntegrationTests: BaseAsyncIntegrationTestCase { messageDraft.update(text: "This is a #cchnl") await fulfillment(of: [expectation], timeout: 6) - + let timetoken = try await messageDraft.send() try await Task.sleep(nanoseconds: 3_000_000_000) let message = try await channel.getMessage(timetoken: timetoken) diff --git a/Tests/AsyncAwait/ThreadChannelAsyncIntegrationTests.swift b/Tests/AsyncAwait/ThreadChannelAsyncIntegrationTests.swift index 31cf6a0..f291045 100644 --- a/Tests/AsyncAwait/ThreadChannelAsyncIntegrationTests.swift +++ b/Tests/AsyncAwait/ThreadChannelAsyncIntegrationTests.swift @@ -20,18 +20,18 @@ class ThreadChannelAsyncIntegrationTests: BaseAsyncIntegrationTestCase { override func customSetup() async throws { parentChannel = try await chat.createChannel(id: randomString()) - + let timetoken = try await parentChannel.sendText(text: "Message") try await Task.sleep(nanoseconds: 3_000_000_000) - + let testMessage = try await parentChannel.getMessage(timetoken: timetoken) let unwrappedTestMessage = try XCTUnwrap(testMessage) - + threadChannel = try await unwrappedTestMessage.createThread() - + try await threadChannel.sendText(text: "Reply in a thread") } - + override func customTearDown() async throws { _ = try? await parentChannel.delete() _ = try? await threadChannel.delete() @@ -42,10 +42,10 @@ class ThreadChannelAsyncIntegrationTests: BaseAsyncIntegrationTestCase { let message = try await threadChannel.getHistory().messages.first let unwrappedMessage = try XCTUnwrap(message) - + let updatedChannel = try await threadChannel.pinMessageToParentChannel(message: unwrappedMessage) let pinnedMessage = try await updatedChannel.getPinnedMessage() - + XCTAssertNotNil(pinnedMessage) } } diff --git a/Tests/AsyncAwait/ThreadMessageAsyncIntegrationTests.swift b/Tests/AsyncAwait/ThreadMessageAsyncIntegrationTests.swift index 5968b47..b076195 100644 --- a/Tests/AsyncAwait/ThreadMessageAsyncIntegrationTests.swift +++ b/Tests/AsyncAwait/ThreadMessageAsyncIntegrationTests.swift @@ -47,7 +47,7 @@ class ThreadMessageaAsyncIntegrationTests: BaseAsyncIntegrationTestCase { func testThreadMessageAsync_HasNoUserReactions() throws { XCTAssertFalse(threadMessage.hasUserReaction(reaction: "someReaction")) } - + func testThreadMessageAsync_EditText() async throws { let currentMessageText = threadMessage.text let newText = "NewTextValue" diff --git a/Tests/AsyncAwait/UserAsyncIntegrationTests.swift b/Tests/AsyncAwait/UserAsyncIntegrationTests.swift index d1f1dfa..47bbd88 100644 --- a/Tests/AsyncAwait/UserAsyncIntegrationTests.swift +++ b/Tests/AsyncAwait/UserAsyncIntegrationTests.swift @@ -28,7 +28,7 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { type: "admin" ) } - + func testUserAsync_CreateUser() async throws { let user = testableUser() let createdUser = try await chat.createUser(user: user) @@ -70,7 +70,7 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { XCTAssertEqual(updatedUser.status, "inactive") XCTAssertEqual(updatedUser.type, "regular") } - + func testUserAsync_UpdateUserCallback() async throws { try await chat.currentUser.update( name: "Markus Koller", @@ -83,7 +83,7 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { // Simulates updating an outdated version of the User object. // We expect the fresh object from the server to be returned first, and then subsequent updates to be applied on top of it - let updateResult = try await chat.currentUser.update() { + let updateResult = try await chat.currentUser.update { [ .stringOptional(\.name, $0.name?.uppercased()), .stringOptional(\.status, $0.status?.uppercased()) @@ -98,23 +98,23 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { let errorExpectation = XCTestExpectation(description: "ErrorExpectation") errorExpectation.assertForOverFulfill = true errorExpectation.expectedFulfillmentCount = 1 - + let someUser = testableUser() - + do { try await someUser.update(name: "NewName", externalId: "NewExternalId") } catch { XCTAssertEqual((error as? ChatError)?.message, "User does not exist") errorExpectation.fulfill() } - + await fulfillment(of: [errorExpectation], timeout: 4) } func testUserAsync_IsPresentOn() async throws { let channelId = randomString() let createdChannel = try await chat.createChannel(id: channelId, name: channelId) - + // Keeping a strong reference to this object for test purposes to simulate that someone is already present on the given channel. // If this object is not retained, it will be deallocated, resulting in no subscription to the channel, // which would cause the behavior being tested to fail. @@ -124,7 +124,7 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { try await Task.sleep(nanoseconds: 4_000_000_000) let isPresent = try await chat.currentUser.isPresentOn(channelId: createdChannel.id) XCTAssertTrue(isPresent) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channelId) } @@ -140,7 +140,7 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { func testUserAsync_SoftDelete() async throws { let createdUser = try await chat.createUser(user: testableUser()) let deletedUser = try await createdUser.delete(soft: true) - + XCTAssertFalse( deletedUser?.active ?? true ) @@ -154,14 +154,14 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { func testUserAsync_DeleteNotExistingUser() async throws { let someUser = testableUser() let resultValue = try await someUser.delete(soft: false) - + XCTAssertNil(resultValue) } func testUserAsync_WherePresent() async throws { let channelId = randomString() let channel = try await chat.createChannel(id: channelId, name: channelId) - + // Keeping a strong reference to this object for test purposes to simulate that someone is already present on the given channel. // If this object is not retained, it will be deallocated, resulting in no subscription to the channel, // which would cause the behavior being tested to fail. @@ -169,15 +169,15 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { debugPrint(connectResult) try await Task.sleep(nanoseconds: 5_000_000_000) - + let channelIdentifiers = try await chat.currentUser.wherePresent() let expectedChannelIdentifiers = [channelId] - + XCTAssertEqual( expectedChannelIdentifiers, channelIdentifiers ) - + addTeardownBlock { [unowned self] in _ = try? await chat.deleteChannel(id: channel.id) } @@ -185,10 +185,10 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { func testUserAsync_GetMemberships() async throws { let channel = try await chat.createChannel(id: randomString()) - + try await channel.invite(user: chat.currentUser) let getMembershipsValue = try await chat.currentUser.getMemberships() - + XCTAssertEqual(try (XCTUnwrap(getMembershipsValue.memberships.first)).user.id, chat.currentUser.id) XCTAssertEqual(try (XCTUnwrap(getMembershipsValue.memberships.first)).channel.id, channel.id) @@ -201,9 +201,9 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { let expectation = expectation(description: "StreamUpdates") expectation.assertForOverFulfill = true expectation.expectedFulfillmentCount = 1 - + let createdUser = try await chat.createUser(user: testableUser()) - + let task = Task { for await updatedUser in createdUser.streamUpdates() { XCTAssertEqual(updatedUser?.name, "NewName") @@ -216,7 +216,7 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { expectation.fulfill() } } - + try await Task.sleep(nanoseconds: 3_000_000_000) try await createdUser.update( name: "NewName", @@ -227,9 +227,9 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { status: "NewStatus", type: "NewType" ) - + await fulfillment(of: [expectation], timeout: 6) - + addTeardownBlock { [unowned self] in task.cancel() _ = try? await chat.deleteUser(id: createdUser.id) @@ -255,7 +255,7 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { } } } - + for user in [firstUser, secondUser] { try await Task.sleep(nanoseconds: 3_000_000_000) try await user.update( @@ -273,7 +273,7 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase { of: [expectation], timeout: 6 ) - + addTeardownBlock { [unowned self] in task.cancel() _ = try? await chat.deleteUser(id: firstUser.id) diff --git a/Tests/BaseClosureIntegrationTestCase.swift b/Tests/BaseClosureIntegrationTestCase.swift index d3a36ef..65d0ebd 100644 --- a/Tests/BaseClosureIntegrationTestCase.swift +++ b/Tests/BaseClosureIntegrationTestCase.swift @@ -44,7 +44,7 @@ extension BaseClosureIntegrationTestCase { // This extension uses `XCTestExpectation` to flatten the structure of closure-based tests, // reducing the need for nested closures. It allows tests to appear sequential and easier // to follow without changing the underlying closure-based behavior. -/// +// // This is not a replacement for Swift's native `async-await` but rather a way to improve // the readability of tests that involve multiple asynchronous calls with completion handlers. extension BaseClosureIntegrationTestCase { diff --git a/Tests/ChatIntegrationTests.swift b/Tests/ChatIntegrationTests.swift index 4f4199e..4b41961 100644 --- a/Tests/ChatIntegrationTests.swift +++ b/Tests/ChatIntegrationTests.swift @@ -78,22 +78,22 @@ class ChatIntegrationTests: BaseClosureIntegrationTestCase { } } } - + func testChat_UpdateUser() throws { let newCustom: [String: JSONCodableScalar] = [ "someValue": 17_253_575_019_298_112, "someStr": "str" ] - + let userId = randomString() - + try awaitResultValue { chat.createUser( id: userId, completion: $0 ) } - + let updatedUser = try awaitResultValue { chat.updateUser( id: userId, @@ -116,7 +116,7 @@ class ChatIntegrationTests: BaseClosureIntegrationTestCase { XCTAssertEqual(updatedUser.custom?.mapValues { $0.scalarValue }, newCustom.mapValues { $0.scalarValue }) XCTAssertEqual(updatedUser.status, "offline") XCTAssertEqual(updatedUser.type, "regular") - + addTeardownBlock { [unowned self] in try awaitResult { chat.deleteUser( @@ -868,14 +868,14 @@ class ChatIntegrationTests: BaseClosureIntegrationTestCase { completion: $0 ) } - + let tt = try awaitResultValue { channel.sendText( text: "Some text", completion: $0 ) } - + try awaitResultValue(delay: 2) { chat.emitEvent( channelId: chat.currentUser.id, diff --git a/Tests/UserIntegrationTests.swift b/Tests/UserIntegrationTests.swift index 6af9b53..3c0fd4e 100644 --- a/Tests/UserIntegrationTests.swift +++ b/Tests/UserIntegrationTests.swift @@ -46,7 +46,7 @@ final class UserIntegrationTests: BaseClosureIntegrationTestCase { try awaitResult { chat.deleteUser( id: user.id, completion: $0 - )} + ) } } } @@ -170,7 +170,7 @@ final class UserIntegrationTests: BaseClosureIntegrationTestCase { completion: $0 ) } - + XCTAssertFalse( deletedUser?.active ?? true )