Skip to content

Commit

Permalink
[ECO-5082] chore: add sandbox for presence
Browse files Browse the repository at this point in the history
  • Loading branch information
ttypic committed Nov 14, 2024
1 parent d6b38ba commit 492fba8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 5 additions & 4 deletions chat-android/src/main/java/com/ably/chat/Presence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ interface Presence : EmitsDiscontinuities {
/**
* Method to get list of the current online users and returns the latest presence messages associated to it.
* @param {Ably.RealtimePresenceParams} params - Parameters that control how the presence set is retrieved.
* @returns {List<PresenceMessage>} or upon failure, the promise will throw [[Ably.ErrorInfo]] object which explains the error.
* @throws {@link io.ably.lib.types.AblyException} object which explains the error.
* @returns {List<PresenceMessage>}
*/
suspend fun get(waitForSync: Boolean = true, clientId: String? = null, connectionId: String? = null): List<PresenceMember>

Expand All @@ -45,21 +46,21 @@ interface Presence : EmitsDiscontinuities {
/**
* Method to join room presence, will emit an enter event to all subscribers. Repeat calls will trigger more enter events.
* @param {PresenceData} data - The users data, a JSON serializable object that will be sent to all subscribers.
* @returns {Promise<void>} or upon failure, the promise will be rejected with an {@link ErrorInfo} object which explains the error.
* @throws {@link io.ably.lib.types.AblyException} object which explains the error.
*/
suspend fun enter(data: PresenceData? = null)

/**
* Method to update room presence, will emit an update event to all subscribers. If the user is not present, it will be treated as a join event.
* @param {PresenceData} data - The users data, a JSON serializable object that will be sent to all subscribers.
* @returns {Promise<void>} or upon failure, the promise will be rejected with an {@link ErrorInfo} object which explains the error.
* @throws {@link io.ably.lib.types.AblyException} object which explains the error.
*/
suspend fun update(data: PresenceData? = null)

/**
* Method to leave room presence, will emit a leave event to all subscribers. If the user is not present, it will be treated as a no-op.
* @param {PresenceData} data - The users data, a JSON serializable object that will be sent to all subscribers.
* @returns {Promise<void>} or upon failure, the promise will be rejected with an {@link ErrorInfo} object which explains the error.
* @throws {@link io.ably.lib.types.AblyException} object which explains the error.
*/
suspend fun leave(data: PresenceData? = null)

Expand Down
17 changes: 14 additions & 3 deletions chat-android/src/test/java/com/ably/chat/SandboxTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.ably.chat

import io.ably.lib.realtime.ChannelState
import java.util.UUID
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
Expand All @@ -17,10 +16,22 @@ class SandboxTest {
}

@Test
fun basicIntegrationTest() = runTest {
fun `should return empty list of presence members if nobody is entered`() = runTest {
val chatClient = sandbox.createSandboxChatClient()
val room = chatClient.rooms.get(UUID.randomUUID().toString())
room.attach()
assertEquals(ChannelState.attached, room.messages.channel.state)
val members = room.presence.get()
assertEquals(0, members.size)
}

@Test
fun `should return yourself as presence member after you entered`() = runTest {
val chatClient = sandbox.createSandboxChatClient()
val room = chatClient.rooms.get(UUID.randomUUID().toString())
room.attach()
room.presence.enter()
val members = room.presence.get()
assertEquals(1, members.size)
assertEquals("sandbox-client", members.first().clientId)
}
}

0 comments on commit 492fba8

Please sign in to comment.