From eb32620f4c7ae7f853abeb826412f4665709c0e2 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Thu, 21 Nov 2024 09:16:23 -0300 Subject: [PATCH] Add test for room channel fetching / lifecycle contributor creation This tests the current behaviour, upon which we will improve in #105. --- .../DefaultRoomLifecycleContributor.swift | 4 +-- Tests/AblyChatTests/DefaultRoomTests.swift | 31 +++++++++++++++++++ .../MockRoomLifecycleManagerFactory.swift | 6 ++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Sources/AblyChat/DefaultRoomLifecycleContributor.swift b/Sources/AblyChat/DefaultRoomLifecycleContributor.swift index 601850f7..5254a739 100644 --- a/Sources/AblyChat/DefaultRoomLifecycleContributor.swift +++ b/Sources/AblyChat/DefaultRoomLifecycleContributor.swift @@ -1,8 +1,8 @@ import Ably internal actor DefaultRoomLifecycleContributor: RoomLifecycleContributor, EmitsDiscontinuities { - internal let channel: DefaultRoomLifecycleContributorChannel - internal let feature: RoomFeature + internal nonisolated let channel: DefaultRoomLifecycleContributorChannel + internal nonisolated let feature: RoomFeature private var discontinuitySubscriptions: [Subscription] = [] internal init(channel: DefaultRoomLifecycleContributorChannel, feature: RoomFeature) { diff --git a/Tests/AblyChatTests/DefaultRoomTests.swift b/Tests/AblyChatTests/DefaultRoomTests.swift index c1a4ac2c..e69494a0 100644 --- a/Tests/AblyChatTests/DefaultRoomTests.swift +++ b/Tests/AblyChatTests/DefaultRoomTests.swift @@ -64,6 +64,37 @@ struct DefaultRoomTests { #expect(room.messages.channel.name == "basketball::$chat::$chatMessages") } + // TODO: Only create contributors for features user has enabled (https://github.com/ably-labs/ably-chat-swift/issues/105) + // TODO: Only fetch channel for features user has enabled (https://github.com/ably-labs/ably-chat-swift/issues/105) + @Test + func fetchesChannelAndCreatesLifecycleContributorForEachFeature() async throws { + // Given: a DefaultRoom instance + let channelsList = [ + MockRealtimeChannel(name: "basketball::$chat::$chatMessages"), + MockRealtimeChannel(name: "basketball::$chat::$reactions"), + ] + let channels = MockChannels(channels: channelsList) + let realtime = MockRealtime.create(channels: channels) + let lifecycleManagerFactory = MockRoomLifecycleManagerFactory() + _ = try await DefaultRoom(realtime: realtime, chatAPI: ChatAPI(realtime: realtime), roomID: "basketball", options: .init(), logger: TestLogger(), lifecycleManagerFactory: lifecycleManagerFactory) + + // Then: It: + // - fetches the channel that corresponds to each feature (well, each one that we’ve currently implemented) + // - initializes the RoomLifecycleManager with a contributor for each feature (well, each one that we’ve currently implemented) + let lifecycleManagerCreationArguments = try #require(await lifecycleManagerFactory.createManagerArguments.first) + let expectedFeatures: [RoomFeature] = [.messages, .occupancy, .reactions, .presence] + #expect(lifecycleManagerCreationArguments.contributors.count == expectedFeatures.count) + #expect(Set(lifecycleManagerCreationArguments.contributors.map(\.feature)) == Set(expectedFeatures)) + + let channelsGetArguments = channels.getArguments + let expectedFetchedChannelNames = [ + "basketball::$chat::$chatMessages", + "basketball::$chat::$reactions", + ] + #expect(channelsGetArguments.count == expectedFetchedChannelNames.count) + #expect(Set(channelsGetArguments.map(\.name)) == Set(expectedFetchedChannelNames)) + } + // @specUntested CHA-RC2b - We chose to implement this failure with an idiomatic fatalError instead of throwing, but we can’t test this. // This is just a basic sense check to make sure the room getters are working as expected, since we don’t have unit tests for some of the features at the moment. diff --git a/Tests/AblyChatTests/Mocks/MockRoomLifecycleManagerFactory.swift b/Tests/AblyChatTests/Mocks/MockRoomLifecycleManagerFactory.swift index b93e9bf2..45ee3725 100644 --- a/Tests/AblyChatTests/Mocks/MockRoomLifecycleManagerFactory.swift +++ b/Tests/AblyChatTests/Mocks/MockRoomLifecycleManagerFactory.swift @@ -2,12 +2,14 @@ actor MockRoomLifecycleManagerFactory: RoomLifecycleManagerFactory { private let manager: MockRoomLifecycleManager + private(set) var createManagerArguments: [(contributors: [DefaultRoomLifecycleContributor], logger: any InternalLogger)] = [] init(manager: MockRoomLifecycleManager = .init()) { self.manager = manager } - func createManager(contributors _: [DefaultRoomLifecycleContributor], logger _: any InternalLogger) async -> MockRoomLifecycleManager { - manager + func createManager(contributors: [DefaultRoomLifecycleContributor], logger: any InternalLogger) async -> MockRoomLifecycleManager { + createManagerArguments.append((contributors: contributors, logger: logger)) + return manager } }