Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-5118] Added tests for occupancy, presence and typing #197

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/AblyChat/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public protocol RealtimeChannelsProtocol: ARTRealtimeChannelsProtocol, Sendable
/// Expresses the requirements of the object returned by ``RealtimeChannelsProtocol/get(_:options:)``.
public protocol RealtimeChannelProtocol: ARTRealtimeChannelProtocol, Sendable {}

public protocol RealtimePresenceProtocol: ARTRealtimePresenceProtocol, Sendable {}

public protocol ConnectionProtocol: ARTConnectionProtocol, Sendable {}

/// Like (a subset of) `ARTRealtimeChannelOptions` but with value semantics. (It’s unfortunate that `ARTRealtimeChannelOptions` doesn’t have a `-copy` method.)
Expand Down
11 changes: 11 additions & 0 deletions Sources/AblyChat/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,14 @@ internal actor DefaultRoom<LifecycleManagerFactory: RoomLifecycleManagerFactory>
}
}
}

#if DEBUG
extension DefaultRoom {
var testsOnly_lifecycleManager: DefaultRoomLifecycleManager<DefaultRoomLifecycleContributor> {
guard let lifecycleManager = lifecycleManager as? DefaultRoomLifecycleManager<DefaultRoomLifecycleContributor> else {
preconditionFailure("DefaultRoomLifecycleManager is expected here.")
}
return lifecycleManager
}
}
#endif
101 changes: 101 additions & 0 deletions Tests/AblyChatTests/DefaultRoomOccupancyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import Ably
@testable import AblyChat
import Testing

struct DefaultRoomOccupancyTests {
// @spec CHA-O1
@Test
func channelNameIsSetAsChatMessagesChannelName() async throws {
// Given
let realtime = MockRealtime.create()
let chatAPI = ChatAPI(realtime: realtime)
let channel = MockRealtimeChannel(name: "basketball::$chat::$chatMessages")
let featureChannel = MockFeatureChannel(channel: channel)

// When
let defaultOccupancy = DefaultOccupancy(featureChannel: featureChannel, chatAPI: chatAPI, roomID: "basketball", logger: TestLogger())

// Then
#expect(defaultOccupancy.channel.name == "basketball::$chat::$chatMessages")
}

// @spec CHA-O2
// @spec CHA-O3
@Test
func requestOccupancyCheck() async throws {
// Given
let realtime = MockRealtime.create {
(MockHTTPPaginatedResponse(
items: [
[
"connections": 5,
"presenceMembers": 2,
],
]
), nil)
}
let chatAPI = ChatAPI(realtime: realtime)
let channel = MockRealtimeChannel(name: "basketball::$chat::$chatMessages")
let featureChannel = MockFeatureChannel(channel: channel)
let defaultOccupancy = DefaultOccupancy(featureChannel: featureChannel, chatAPI: chatAPI, roomID: "basketball", logger: TestLogger())

// When
let occupancyInfo = try await defaultOccupancy.get()

// Then
#expect(occupancyInfo.connections == 5)
#expect(occupancyInfo.presenceMembers == 2)
}

// @spec CHA-O4
@Test
func usersCanSubscribeToRealtimeOccupancyUpdates() async throws {
// Given
let realtime = MockRealtime.create()
let chatAPI = ChatAPI(realtime: realtime)
let channel = MockRealtimeChannel(name: "basketball::$chat::$chatMessages")
let featureChannel = MockFeatureChannel(channel: channel)
let defaultOccupancy = DefaultOccupancy(featureChannel: featureChannel, chatAPI: chatAPI, roomID: "basketball", logger: TestLogger())

// CHA-O4a, CHA-O4c

// When
let subscription = await defaultOccupancy.subscribe()
subscription.emit(OccupancyEvent(connections: 5, presenceMembers: 2))

// Then
let occupancyInfo = try #require(await subscription.first { _ in true })
#expect(occupancyInfo.connections == 5)
#expect(occupancyInfo.presenceMembers == 2)

// CHA-O4b

// When
subscription.unsubscribe()
subscription.emit(OccupancyEvent(connections: 5, presenceMembers: 2))

// Then
let nilOccupancyInfo = await subscription.first { _ in true }
#expect(nilOccupancyInfo == nil)
}

// @spec CHA-O5
@Test
func onDiscontinuity() async throws {
// Given
let realtime = MockRealtime.create()
let chatAPI = ChatAPI(realtime: realtime)
let channel = MockRealtimeChannel()
let featureChannel = MockFeatureChannel(channel: channel)
let defaultOccupancy = DefaultOccupancy(featureChannel: featureChannel, chatAPI: chatAPI, roomID: "basketball", logger: TestLogger())

// When: The feature channel emits a discontinuity through `onDiscontinuity`
let featureChannelDiscontinuity = DiscontinuityEvent(error: ARTErrorInfo.createUnknownError()) // arbitrary error
let discontinuitySubscription = await defaultOccupancy.onDiscontinuity()
await featureChannel.emitDiscontinuity(featureChannelDiscontinuity)

// Then: The DefaultOccupancy instance emits this discontinuity through `onDiscontinuity`
let discontinuity = try #require(await discontinuitySubscription.first { _ in true })
#expect(discontinuity == featureChannelDiscontinuity)
}
}
Loading
Loading