@@ -22,10 +22,10 @@ class PresenceEndpointIntegrationTests: XCTestCase {
2222 let performHereNow = {
2323 client. hereNow ( on: [ testChannel] , includeState: true ) { result in
2424 switch result {
25- case let . success( response ) :
26- XCTAssertNotNil ( response . presenceByChannel [ testChannel] )
27- XCTAssertGreaterThan ( response . presenceByChannel. totalChannels, 0 )
28- XCTAssertGreaterThan ( response . presenceByChannel. totalOccupancy, 0 )
25+ case let . success( presenceByChannel ) :
26+ XCTAssertNotNil ( presenceByChannel [ testChannel] )
27+ XCTAssertGreaterThan ( presenceByChannel. totalChannels, 0 )
28+ XCTAssertGreaterThan ( presenceByChannel. totalOccupancy, 0 )
2929 case let . failure( error) :
3030 XCTFail ( " Failed due to error: \( error) " )
3131 }
@@ -56,9 +56,9 @@ class PresenceEndpointIntegrationTests: XCTestCase {
5656 let performHereNow = { [ unowned client] in
5757 client. hereNow ( on: [ testChannel] , includeState: true ) { result in
5858 switch result {
59- case let . success( response ) :
60- XCTAssertNotNil ( response . presenceByChannel [ testChannel] )
61- XCTAssertEqual ( response . presenceByChannel [ testChannel] ? . occupantsState [ client. configuration. userId] ? . codableValue, [ " StateKey " : " StateValue " ] )
59+ case let . success( presenceByChannel ) :
60+ XCTAssertNotNil ( presenceByChannel [ testChannel] )
61+ XCTAssertEqual ( presenceByChannel [ testChannel] ? . occupantsState [ client. configuration. userId] ? . codableValue, [ " StateKey " : " StateValue " ] )
6262 case let . failure( error) :
6363 XCTFail ( " Failed due to error: \( error) " )
6464 }
@@ -99,9 +99,9 @@ class PresenceEndpointIntegrationTests: XCTestCase {
9999
100100 client. hereNow ( on: [ testChannel] , includeState: true ) { result in
101101 switch result {
102- case let . success( response ) :
103- XCTAssertNotNil ( response . presenceByChannel [ testChannel] )
104- XCTAssertEqual ( response . presenceByChannel. totalOccupancy, 0 )
102+ case let . success( presenceByChannel ) :
103+ XCTAssertNotNil ( presenceByChannel [ testChannel] )
104+ XCTAssertEqual ( presenceByChannel. totalOccupancy, 0 )
105105 case let . failure( error) :
106106 XCTFail ( " Failed due to error: \( error) " )
107107 }
@@ -120,8 +120,8 @@ class PresenceEndpointIntegrationTests: XCTestCase {
120120 let performHereNow = {
121121 client. hereNow ( on: [ testChannel, otherChannel] ) { result in
122122 switch result {
123- case let . success( response ) :
124- XCTAssertNotNil ( response . presenceByChannel [ testChannel] )
123+ case let . success( presenceByChannel ) :
124+ XCTAssertNotNil ( presenceByChannel [ testChannel] )
125125 case let . failure( error) :
126126 XCTFail ( " Failed due to error: \( error) " )
127127 }
@@ -153,10 +153,10 @@ class PresenceEndpointIntegrationTests: XCTestCase {
153153 let performHereNow = {
154154 client. hereNow ( on: [ testChannel, otherChannel] , includeState: true ) { result in
155155 switch result {
156- case let . success( response ) :
157- XCTAssertNotNil ( response . presenceByChannel [ testChannel] )
158- XCTAssertEqual ( response . presenceByChannel [ testChannel] ? . occupantsState [ client. configuration. userId] ? . codableValue, [ " StateKey " : " StateValue " ] )
159- XCTAssertEqual ( response . presenceByChannel [ otherChannel] ? . occupantsState [ client. configuration. userId] ? . codableValue, [ " StateKey " : " StateValue " ] )
156+ case let . success( presenceByChannel ) :
157+ XCTAssertNotNil ( presenceByChannel [ testChannel] )
158+ XCTAssertEqual ( presenceByChannel [ testChannel] ? . occupantsState [ client. configuration. userId] ? . codableValue, [ " StateKey " : " StateValue " ] )
159+ XCTAssertEqual ( presenceByChannel [ otherChannel] ? . occupantsState [ client. configuration. userId] ? . codableValue, [ " StateKey " : " StateValue " ] )
160160 case let . failure( error) :
161161 XCTFail ( " Failed due to error: \( error) " )
162162 }
@@ -199,8 +199,8 @@ class PresenceEndpointIntegrationTests: XCTestCase {
199199
200200 client. hereNow ( on: [ testChannel, otherChannel] , includeState: true ) { result in
201201 switch result {
202- case let . success( response ) :
203- XCTAssertTrue ( response . presenceByChannel. isEmpty)
202+ case let . success( presenceByChannel ) :
203+ XCTAssertTrue ( presenceByChannel. isEmpty)
204204 case let . failure( error) :
205205 XCTFail ( " Failed due to error: \( error) " )
206206 }
@@ -210,89 +210,47 @@ class PresenceEndpointIntegrationTests: XCTestCase {
210210 wait ( for: [ hereNowExpect] , timeout: 10.0 )
211211 }
212212
213- func testHereNow_MultiChannel_OffsetAndLimit( ) {
214- let hereNowExpect = expectation ( description: " Here Now Pagination Response " )
215- let lobbyChannel = " lobby "
216- let generalChannel = " general "
217- let vipChannel = " vip "
213+ func testHereNow_MultiChannel_Limit( ) {
214+ let hereNowExpect = expectation ( description: " Here Now Limit Response " )
215+ let testChannel = " testHereNow_Limit "
218216
219- // Create 5 clients
217+ // Create 4 clients
220218 let clientA = PubNub ( configuration: presenceConfiguration ( ) )
221219 let clientB = PubNub ( configuration: presenceConfiguration ( ) )
222220 let clientC = PubNub ( configuration: presenceConfiguration ( ) )
223221 let clientD = PubNub ( configuration: presenceConfiguration ( ) )
224222
225- // Expected users per channel
226- let lobbyUsers = Set ( [ clientA. configuration. userId, clientB. configuration. userId, clientC. configuration. userId, clientD. configuration. userId] )
227- let generalUsers = Set ( [ clientA. configuration. userId, clientB. configuration. userId, clientC. configuration. userId] )
228- let vipUsers = Set ( [ clientA. configuration. userId, clientB. configuration. userId] )
223+ // Expected users to join the channel
224+ let expectedUsers = Set ( [
225+ clientA. configuration. userId,
226+ clientB. configuration. userId,
227+ clientC. configuration. userId,
228+ clientD. configuration. userId
229+ ] )
229230
230231 let performHereNow = {
231- clientA. hereNow ( on: [ lobbyChannel , generalChannel , vipChannel ] , limit: 2 ) { [ unowned clientA ] result in
232+ clientA. hereNow ( on: [ testChannel ] , limit: 2 ) { result in
232233 switch result {
233- case let . success( firstPage) :
234- // Verify total channels matches the number of channels and total occupancy matches the number of users
235- XCTAssertEqual ( firstPage. presenceByChannel. totalChannels, 3 )
236- XCTAssertEqual ( firstPage. presenceByChannel. totalOccupancy, 9 )
237-
238- // Verify occupancy for each provided channel
239- XCTAssertEqual ( firstPage. presenceByChannel [ lobbyChannel] ? . occupancy, 4 )
240- XCTAssertEqual ( firstPage. presenceByChannel [ generalChannel] ? . occupancy, 3 )
241- XCTAssertEqual ( firstPage. presenceByChannel [ vipChannel] ? . occupancy, 2 )
242-
243- let firstPageLobby = firstPage. presenceByChannel [ lobbyChannel] ? . occupants ?? [ ]
244- let firstPageGeneral = firstPage. presenceByChannel [ generalChannel] ? . occupants ?? [ ]
245- let firstPageVip = firstPage. presenceByChannel [ vipChannel] ? . occupants ?? [ ]
246-
247- // Verify channel occupants don't exceed the limit
248- XCTAssertEqual ( firstPageLobby. count, 2 )
249- XCTAssertEqual ( firstPageGeneral. count, 2 )
250- XCTAssertEqual ( firstPageVip. count, 2 )
251-
252- // Fetch second page using offset
253- clientA. hereNow ( on: [ lobbyChannel, generalChannel, vipChannel] , offset: firstPage. nextOffset) { secondResult in
254- switch secondResult {
255- case let . success( secondPage) :
256- let secondPageLobby = secondPage. presenceByChannel [ lobbyChannel] ? . occupants ?? [ ]
257- let secondPageGeneral = secondPage. presenceByChannel [ generalChannel] ? . occupants ?? [ ]
258- let secondPageVip = secondPage. presenceByChannel [ vipChannel] ? . occupants ?? [ ]
259-
260- // Verify pagination - lobby channel should have 2 more users (4 total)
261- let allLobbyUsers = Set ( firstPageLobby + secondPageLobby)
262- XCTAssertEqual ( secondPageLobby. count, 2 )
263- XCTAssertEqual ( allLobbyUsers, lobbyUsers)
264-
265- // Verify pagination - general channel should have 1 more user (3 total)
266- let allGeneralUsers = Set ( firstPageGeneral + secondPageGeneral)
267- XCTAssertEqual ( secondPageGeneral. count, 1 )
268- XCTAssertEqual ( allGeneralUsers, generalUsers)
269-
270- // Verify pagination - vip channel should have 0 more users (2 total from first page)
271- let allVipUsers = Set ( firstPageVip + secondPageVip)
272- XCTAssertEqual ( secondPageVip. count, 0 )
273- XCTAssertEqual ( allVipUsers, vipUsers)
274-
275- case let . failure( error) :
276- XCTFail ( " Failed due to error: \( error) " )
277- }
278- hereNowExpect. fulfill ( )
279- }
234+ case let . success( response) :
235+ XCTAssertEqual ( ( response [ testChannel] ? . occupants ?? [ ] ) . count, 2 )
280236 case let . failure( error) :
281237 XCTFail ( " Failed due to error: \( error) " )
282238 }
239+ hereNowExpect. fulfill ( )
283240 }
284241 }
285242
286- let listener = waitOnMultiChannelPresence (
243+ let listener = waitOnPresence (
287244 client: clientA,
288- channelUsers: [ lobbyChannel: lobbyUsers, generalChannel: generalUsers, vipChannel: vipUsers] ,
245+ channel: testChannel,
246+ userIds: expectedUsers,
289247 completion: performHereNow
290248 )
291249
292- clientA. subscribe ( to: [ lobbyChannel , generalChannel , vipChannel ] , withPresence: true )
293- clientB. subscribe ( to: [ lobbyChannel , generalChannel , vipChannel ] , withPresence: true )
294- clientC. subscribe ( to: [ lobbyChannel , generalChannel ] , withPresence: true )
295- clientD. subscribe ( to: [ lobbyChannel ] , withPresence: true )
250+ clientA. subscribe ( to: [ testChannel ] , withPresence: true )
251+ clientB. subscribe ( to: [ testChannel ] , withPresence: true )
252+ clientC. subscribe ( to: [ testChannel ] , withPresence: true )
253+ clientD. subscribe ( to: [ testChannel ] , withPresence: true )
296254
297255 defer { listener. cancel ( ) }
298256 wait ( for: [ hereNowExpect] , timeout: 30.0 )
@@ -347,32 +305,25 @@ private extension PresenceEndpointIntegrationTests {
347305}
348306
349307private extension PresenceEndpointIntegrationTests {
350- func waitOnMultiChannelPresence(
351- client: PubNub ,
352- channelUsers: [ String : Set < String > ] ,
353- completion: @escaping ( ) -> Void
354- ) -> SubscriptionListener {
308+ func waitOnPresence( client: PubNub , channel: String , userIds: Set < String > ? = nil , completion: @escaping ( ) -> Void ) -> SubscriptionListener {
355309 let listener = SubscriptionListener ( )
356- var joinedUsersByChannel : [ String : Set < String > ] = channelUsers. mapValues { _ in Set < String > ( ) }
310+ let expectedUsers = userIds ?? Set ( [ client. configuration. userId] )
311+ var joinedUsers : Set < String > = Set < String > ( )
357312 var hasCompleted = false
358313
359314 listener. didReceivePresence = { event in
360- guard !hasCompleted, let expectedUsers = channelUsers [ event . channel] else {
315+ guard !hasCompleted, event . channel == channel else {
361316 return
362317 }
363318
364319 for action in event. actions {
365320 if case let . join( uuids) = action {
366- joinedUsersByChannel [ event . channel ] ? . formUnion ( uuids. filter { expectedUsers. contains ( $0) } )
321+ joinedUsers . formUnion ( uuids. filter { expectedUsers. contains ( $0) } )
367322 }
368323 }
369324
370- // Check if all expected users have joined all channels
371- let allJoined = channelUsers. allSatisfy { channel, expectedUsers in
372- joinedUsersByChannel [ channel] == expectedUsers
373- }
374-
375- if allJoined {
325+ // Check if all expected users have joined the channel
326+ if joinedUsers == expectedUsers {
376327 hasCompleted = true
377328 completion ( )
378329 }
@@ -384,12 +335,4 @@ private extension PresenceEndpointIntegrationTests {
384335 // Return listener to be able to cancel it when the test is done
385336 return listener
386337 }
387-
388- func waitOnPresence( client: PubNub , channel: String , userIds: Set < String > ? = nil , completion: @escaping ( ) -> Void ) -> SubscriptionListener {
389- waitOnMultiChannelPresence (
390- client: client,
391- channelUsers: [ channel: userIds ?? Set ( [ client. configuration. userId] ) ] ,
392- completion: completion
393- )
394- }
395338}
0 commit comments