Skip to content

Commit 3fafdf5

Browse files
committed
Added room reactions tests.
1 parent 19b6901 commit 3fafdf5

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

Sources/AblyChat/DefaultRoomReactions.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ internal final class DefaultRoomReactions: RoomReactions, EmitsDiscontinuities {
7878
subscription.emit(reaction)
7979
} catch {
8080
logger.log(message: "Error processing incoming reaction message: \(error)", level: .error)
81+
#if DEBUG
82+
for subscription in self.malformedMessageSubscriptions {
83+
subscription.emit(message)
84+
}
85+
#endif
8186
}
8287
}
8388
}
@@ -97,4 +102,16 @@ internal final class DefaultRoomReactions: RoomReactions, EmitsDiscontinuities {
97102
private enum RoomReactionsError: Error {
98103
case noReferenceToSelf
99104
}
105+
106+
#if DEBUG
107+
/// Subscription of malformed message events for testing purposes.
108+
private var malformedMessageSubscriptions: [Subscription<ARTMessage>] = []
109+
110+
/// Returns a subscription which emits malformed message events for testing purposes.
111+
internal func testsOnly_subscribeToMalformedMessageEvents() -> Subscription<ARTMessage> {
112+
let subscription = Subscription<ARTMessage>(bufferingPolicy: .unbounded)
113+
malformedMessageSubscriptions.append(subscription)
114+
return subscription
115+
}
116+
#endif
100117
}

Tests/AblyChatTests/DefaultRoomReactionsTests.swift

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,56 @@ struct DefaultRoomReactionsTests {
4343
#expect(channel.lastMessagePublishedExtras as? Dictionary == ["headers": ["someHeadersKey": "someHeadersValue"]])
4444
}
4545

46-
// @spec CHA-ER4
46+
// @spec CHA-ER4a
4747
@Test
48-
func subscribe_returnsSubscription() async throws {
49-
// all setup values here are arbitrary
48+
func subscriptionCanBeRegisteredToReceiveReactionEvents() async throws {
5049
// Given
51-
let channel = MockRealtimeChannel(name: "basketball::$chat::$reactions")
50+
let channel = MockRealtimeChannel(
51+
messageJSONToEmitOnSubscribe: [
52+
"name": "roomReaction",
53+
"clientId": "who-sent-the-message",
54+
"data": [
55+
"type": ":like:",
56+
"metadata": [
57+
"foo": "bar",
58+
],
59+
],
60+
"timestamp": "1726232498871",
61+
"extras": [
62+
"headers": [
63+
"baz": "qux",
64+
],
65+
],
66+
] as? [String: any Sendable]
67+
)
5268
let featureChannel = MockFeatureChannel(channel: channel)
69+
let defaultRoomReactions = await DefaultRoomReactions(featureChannel: featureChannel, clientID: "mockClientId", roomID: "basketball", logger: TestLogger())
5370

5471
// When
72+
let reactionSubscription = await defaultRoomReactions.subscribe()
73+
74+
// Then
75+
let reaction = try #require(await reactionSubscription.first { _ in true })
76+
#expect(reaction.type == ":like:")
77+
}
78+
79+
// CHA-ER4c is currently untestable due to not subscribing to those events on lower level
80+
// @spec CHA-ER4d
81+
@Test
82+
func malformedRealtimeEventsShallNotBeEmittedToSubscribers() async throws {
83+
// Given
84+
let channel = MockRealtimeChannel(
85+
messageJSONToEmitOnSubscribe: ["foo": "bar"] // malformed realtime message
86+
)
87+
let featureChannel = MockFeatureChannel(channel: channel)
5588
let defaultRoomReactions = await DefaultRoomReactions(featureChannel: featureChannel, clientID: "mockClientId", roomID: "basketball", logger: TestLogger())
5689

5790
// When
58-
let subscription: Subscription<Reaction>? = await defaultRoomReactions.subscribe()
91+
let malformedMessagesSubscription = await defaultRoomReactions.testsOnly_subscribeToMalformedMessageEvents()
92+
_ = await defaultRoomReactions.subscribe()
5993

6094
// Then
61-
#expect(subscription != nil)
95+
_ = try #require(await malformedMessagesSubscription.first { _ in true })
6296
}
6397

6498
// @spec CHA-ER5

0 commit comments

Comments
 (0)