@@ -29,20 +29,73 @@ struct DefaultRoomReactionsTests {
2929 #expect( channel. publishedMessages. last? . extras == [ " headers " : [ " someHeadersKey " : " someHeadersValue " ] , " ephemeral " : true ] )
3030 }
3131
32- // @spec CHA-ER4
32+ // @spec CHA-ER4a
33+ // @spec CHA-ER4b
3334 @Test
34- func subscribe_returnsSubscription( ) async throws {
35- // all setup values here are arbitrary
35+ func subscriptionCanBeRegisteredToReceiveReactionEvents( ) async throws {
3636 // Given
37- let channel = MockRealtimeChannel ( name: " basketball::$chat " )
37+ func generateMessage( serial: String , reactionType: String ) -> ARTMessage {
38+ let message = ARTMessage ( )
39+ message. action = . create // arbitrary
40+ message. serial = serial // arbitrary
41+ message. clientId = " " // arbitrary
42+ message. data = [
43+ " type " : reactionType,
44+ ]
45+ message. version = " 0 "
46+ return message
47+ }
3848
39- // When
49+ let channel = MockRealtimeChannel (
50+ messageToEmitOnSubscribe: generateMessage ( serial: " 1 " , reactionType: " :like: " )
51+ )
4052 let defaultRoomReactions = DefaultRoomReactions ( channel: channel, clientID: " mockClientId " , roomID: " basketball " , logger: TestLogger ( ) )
4153
4254 // When
43- let subscription : SubscriptionAsyncSequence < Reaction > ? = defaultRoomReactions. subscribe ( )
55+ let subscription = defaultRoomReactions. subscribe { reaction in
56+ // Then
57+ #expect( reaction. type == " :like: " )
58+ }
4459
45- // Then
46- #expect( subscription != nil )
60+ // CHA-ER4b
61+ subscription. unsubscribe ( )
62+
63+ // will not be received and expectations above will not fail
64+ channel. simulateIncomingMessage (
65+ generateMessage ( serial: " 2 " , reactionType: " :dislike: " ) ,
66+ for: RoomReactionEvents . reaction. rawValue
67+ )
68+ }
69+
70+ // CHA-ER4c is currently untestable due to not subscribing to those events on lower level
71+ // @spec CHA-ER4d
72+ @Test
73+ func malformedRealtimeEventsShallNotBeEmittedToSubscribers( ) async throws {
74+ // Given
75+ let channel = MockRealtimeChannel (
76+ messageToEmitOnSubscribe: {
77+ let message = ARTMessage ( )
78+ message. action = . create // arbitrary
79+ message. serial = " 123 " // arbitrary
80+ message. clientId = " " // arbitrary
81+ message. data = [
82+ " type " : " :like: " ,
83+ ]
84+ message. extras = [ : ] as any ARTJsonCompatible
85+ message. version = " 0 "
86+ return message
87+ } ( )
88+ )
89+ let defaultRoomReactions = DefaultRoomReactions ( channel: channel, clientID: " mockClientId " , roomID: " basketball " , logger: TestLogger ( ) )
90+
91+ // When
92+ defaultRoomReactions. subscribe { reaction in
93+ #expect( reaction. type == " :like: " )
94+ }
95+ // will not be received and expectations above will not fail
96+ channel. simulateIncomingMessage (
97+ ARTMessage ( ) , // malformed message
98+ for: RealtimeMessageName . chatMessage. rawValue
99+ )
47100 }
48101}
0 commit comments