Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing channel modes for message channel #89

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions chat-android/src/main/java/com/ably/chat/RoomOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,20 @@ internal fun RoomOptions.validateRoomOptions() {
internal fun RoomOptions.messagesChannelOptions(): ChannelOptions {
return ChatChannelOptions {
presence?.let {
val presenceModes = mutableListOf<ChannelMode>()
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(
Expand Down
22 changes: 21 additions & 1 deletion chat-android/src/test/java/com/ably/chat/SandboxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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<MessageEvent>()

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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Comment on lines +44 to +48
Copy link

@coderabbitai coderabbitai bot Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the order of channel modes to match requirements

The order of channel modes needs to be adjusted according to the specified requirements. The current implementation has publish first and subscribe second, but the requirements state that subscribe should be first, followed by publish.

Apply this diff to fix the order:

 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.subscribe, capturedChannelOptions[0].modes[0])
+Assert.assertEquals(ChannelMode.publish, capturedChannelOptions[0].modes[1])
 Assert.assertEquals(ChannelMode.presence, capturedChannelOptions[0].modes[2])
 Assert.assertEquals(ChannelMode.presence_subscribe, capturedChannelOptions[0].modes[3])

Good use of Assert.assertEquals for detailed error messages when tests fail. This follows the recommended practice for better debugging.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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])
Assert.assertEquals(4, capturedChannelOptions[0].modes.size)
Assert.assertEquals(ChannelMode.subscribe, capturedChannelOptions[0].modes[0])
Assert.assertEquals(ChannelMode.publish, capturedChannelOptions[0].modes[1])
Assert.assertEquals(ChannelMode.presence, capturedChannelOptions[0].modes[2])
Assert.assertEquals(ChannelMode.presence_subscribe, capturedChannelOptions[0].modes[3])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai order for first two modes doesn't matter

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sacOO7, thanks for the clarification. I understand that the order of the first two channel modes (ChannelMode.publish and ChannelMode.subscribe) does not matter. I'll note this for future reviews.


✏️ Learnings added
Learnt from: sacOO7
PR: ably/ably-chat-kotlin#89
File: chat-android/src/test/java/com/ably/chat/room/RoomFeatureSharedChannelTest.kt:44-48
Timestamp: 2024-12-10T14:47:31.404Z
Learning: In tests in `RoomFeatureSharedChannelTest.kt` for the Ably Chat Kotlin project, the order of `ChannelMode.publish` and `ChannelMode.subscribe` in the `modes` array does not affect functionality; assertions should not enforce a specific order for these modes.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

// Check if occupancy matrix is set
Assert.assertEquals("metrics", capturedChannelOptions[0].params["occupancy"])

Expand Down
Loading