Skip to content

Commit 3fdad4d

Browse files
committed
Added occupancy tests.
1 parent bf5b09a commit 3fdad4d

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import Ably
2+
@testable import AblyChat
3+
import Testing
4+
5+
struct DefaultRoomOccupancyTests {
6+
// @spec CHA-O3
7+
@Test
8+
func requestOccupancyCheck() async throws {
9+
// Given
10+
let realtime = MockRealtime.create {
11+
(MockHTTPPaginatedResponse(
12+
items: [
13+
[
14+
"connections": 5,
15+
"presenceMembers": 2,
16+
],
17+
]
18+
), nil)
19+
}
20+
let chatAPI = ChatAPI(realtime: realtime)
21+
let channel = MockRealtimeChannel(name: "basketball::$chat::$chatMessages")
22+
let featureChannel = MockFeatureChannel(channel: channel)
23+
let defaultOccupancy = DefaultOccupancy(featureChannel: featureChannel, chatAPI: chatAPI, roomID: "basketball", logger: TestLogger())
24+
25+
// When
26+
let occupancyInfo = try await defaultOccupancy.get()
27+
28+
// Then
29+
#expect(occupancyInfo.connections == 5)
30+
#expect(occupancyInfo.presenceMembers == 2)
31+
}
32+
33+
// @spec CHA-O4a
34+
// @spec CHA-O4c
35+
@Test
36+
func usersCanSubscribeToRealtimeOccupancyUpdates() async throws {
37+
// Given
38+
let realtime = MockRealtime.create()
39+
let chatAPI = ChatAPI(realtime: realtime)
40+
let channel = MockRealtimeChannel(name: "basketball::$chat::$chatMessages")
41+
let featureChannel = MockFeatureChannel(channel: channel)
42+
let defaultOccupancy = DefaultOccupancy(featureChannel: featureChannel, chatAPI: chatAPI, roomID: "basketball", logger: TestLogger())
43+
44+
// CHA-O4a, CHA-O4c
45+
46+
// When
47+
let subscription = await defaultOccupancy.subscribe()
48+
subscription.emit(OccupancyEvent(connections: 5, presenceMembers: 2))
49+
50+
// Then
51+
let occupancyInfo = try #require(await subscription.first { _ in true })
52+
#expect(occupancyInfo.connections == 5)
53+
#expect(occupancyInfo.presenceMembers == 2)
54+
}
55+
56+
// @spec CHA-O5
57+
@Test
58+
func onDiscontinuity() async throws {
59+
// Given
60+
let realtime = MockRealtime.create()
61+
let chatAPI = ChatAPI(realtime: realtime)
62+
let channel = MockRealtimeChannel()
63+
let featureChannel = MockFeatureChannel(channel: channel)
64+
let defaultOccupancy = DefaultOccupancy(featureChannel: featureChannel, chatAPI: chatAPI, roomID: "basketball", logger: TestLogger())
65+
66+
// When: The feature channel emits a discontinuity through `onDiscontinuity`
67+
let featureChannelDiscontinuity = DiscontinuityEvent(error: ARTErrorInfo.createUnknownError()) // arbitrary error
68+
let discontinuitySubscription = await defaultOccupancy.onDiscontinuity()
69+
await featureChannel.emitDiscontinuity(featureChannelDiscontinuity)
70+
71+
// Then: The DefaultOccupancy instance emits this discontinuity through `onDiscontinuity`
72+
let discontinuity = try #require(await discontinuitySubscription.first { _ in true })
73+
#expect(discontinuity == featureChannelDiscontinuity)
74+
}
75+
}

0 commit comments

Comments
 (0)