Skip to content

Commit

Permalink
Added tests for occupancy. Ignored CHA-04d since it's impossible to e…
Browse files Browse the repository at this point in the history
…mit invalid JSON on a subscription currently.
  • Loading branch information
maratal committed Dec 15, 2024
1 parent 1cf3a74 commit 5a47cd5
Showing 1 changed file with 101 additions and 0 deletions.
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 init_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(name: "basketball::$chat::$chatMessages")
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)
}
}

0 comments on commit 5a47cd5

Please sign in to comment.