Skip to content

Commit

Permalink
Add test for room channel fetching / lifecycle contributor creation
Browse files Browse the repository at this point in the history
This tests the current behaviour, upon which we will improve in #105.
  • Loading branch information
lawrence-forooghian committed Nov 21, 2024
1 parent 1fe51a9 commit eb32620
Showing 3 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/AblyChat/DefaultRoomLifecycleContributor.swift
Original file line number Diff line number Diff line change
@@ -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<ARTErrorInfo>] = []

internal init(channel: DefaultRoomLifecycleContributorChannel, feature: RoomFeature) {
31 changes: 31 additions & 0 deletions Tests/AblyChatTests/DefaultRoomTests.swift
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit eb32620

Please sign in to comment.