From cabed6a17257508db58eebb6b03a124a3b83697b Mon Sep 17 00:00:00 2001 From: evgeny Date: Tue, 10 Dec 2024 13:59:36 +0000 Subject: [PATCH] fix: add missing channel modes for message channel --- .../main/java/com/ably/chat/RoomOptions.kt | 20 +++++++++++------ .../test/java/com/ably/chat/SandboxTest.kt | 22 ++++++++++++++++++- .../chat/room/RoomFeatureSharedChannelTest.kt | 8 ++++--- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/chat-android/src/main/java/com/ably/chat/RoomOptions.kt b/chat-android/src/main/java/com/ably/chat/RoomOptions.kt index 03f20304..efb65e2b 100644 --- a/chat-android/src/main/java/com/ably/chat/RoomOptions.kt +++ b/chat-android/src/main/java/com/ably/chat/RoomOptions.kt @@ -121,14 +121,20 @@ internal fun RoomOptions.validateRoomOptions() { internal fun RoomOptions.messagesChannelOptions(): ChannelOptions { return ChatChannelOptions { presence?.let { - val presenceModes = mutableListOf() - if (presence.enter) { - presenceModes.add(ChannelMode.presence) - } - if (presence.subscribe) { - presenceModes.add(ChannelMode.presence_subscribe) + val channelModes = buildList { + // We should have this modes for regular messages + add(ChannelMode.publish) + add(ChannelMode.subscribe) + + if (presence.enter) { + add(ChannelMode.presence) + } + if (presence.subscribe) { + add(ChannelMode.presence_subscribe) + } } - modes = presenceModes.toTypedArray() + + modes = channelModes.toTypedArray() } occupancy?.let { params = mapOf( diff --git a/chat-android/src/test/java/com/ably/chat/SandboxTest.kt b/chat-android/src/test/java/com/ably/chat/SandboxTest.kt index 76e96488..8a19df34 100644 --- a/chat-android/src/test/java/com/ably/chat/SandboxTest.kt +++ b/chat-android/src/test/java/com/ably/chat/SandboxTest.kt @@ -109,7 +109,7 @@ class SandboxTest { } @Test - fun `should be able to send and retrieve messages`() = runTest { + fun `should be able to send and retrieve messages without room features`() = runTest { val chatClient = sandbox.createSandboxChatClient() val roomId = UUID.randomUUID().toString() @@ -128,6 +128,26 @@ class SandboxTest { ) } + @Test + fun `should be able to send and retrieve messages with all room features enabled`() = runTest { + val chatClient = sandbox.createSandboxChatClient() + val roomId = UUID.randomUUID().toString() + + val room = chatClient.rooms.get(roomId, RoomOptions.default) + + room.attach() + + val messageEvent = CompletableDeferred() + + room.messages.subscribe { messageEvent.complete(it) } + room.messages.send("hello") + + assertEquals( + "hello", + messageEvent.await().message.text, + ) + } + @Test fun `should be able to send and retrieve messages from history`() = runTest { val chatClient = sandbox.createSandboxChatClient() diff --git a/chat-android/src/test/java/com/ably/chat/room/RoomFeatureSharedChannelTest.kt b/chat-android/src/test/java/com/ably/chat/room/RoomFeatureSharedChannelTest.kt index 7fd500cb..bbca86fd 100644 --- a/chat-android/src/test/java/com/ably/chat/room/RoomFeatureSharedChannelTest.kt +++ b/chat-android/src/test/java/com/ably/chat/room/RoomFeatureSharedChannelTest.kt @@ -41,9 +41,11 @@ class RoomFeatureSharedChannelTest { Assert.assertEquals(1, capturedChannelOptions.size) // Check for set presence modes - Assert.assertEquals(2, capturedChannelOptions[0].modes.size) - Assert.assertEquals(ChannelMode.presence, capturedChannelOptions[0].modes[0]) - Assert.assertEquals(ChannelMode.presence_subscribe, capturedChannelOptions[0].modes[1]) + Assert.assertEquals(4, capturedChannelOptions[0].modes.size) + Assert.assertEquals(ChannelMode.publish, capturedChannelOptions[0].modes[0]) + Assert.assertEquals(ChannelMode.subscribe, capturedChannelOptions[0].modes[1]) + Assert.assertEquals(ChannelMode.presence, capturedChannelOptions[0].modes[2]) + Assert.assertEquals(ChannelMode.presence_subscribe, capturedChannelOptions[0].modes[3]) // Check if occupancy matrix is set Assert.assertEquals("metrics", capturedChannelOptions[0].params["occupancy"])