-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECO-4944] feat: add room level reaction implementation
- Loading branch information
Showing
4 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
chat-android/src/test/java/com/ably/chat/RoomReactionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.ably.chat | ||
|
||
import com.google.gson.JsonObject | ||
import io.ably.lib.realtime.AblyRealtime.Channels | ||
import io.ably.lib.realtime.Channel | ||
import io.ably.lib.realtime.buildRealtimeChannel | ||
import io.ably.lib.types.MessageExtras | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.slot | ||
import io.mockk.spyk | ||
import io.mockk.verify | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class RoomReactionsTest { | ||
private val realtimeChannels = mockk<Channels>(relaxed = true) | ||
private val realtimeChannel = spyk<Channel>(buildRealtimeChannel("room1::\$chat::\$reactions")) | ||
private lateinit var roomReactions: DefaultRoomReactions | ||
|
||
@Before | ||
fun setUp() { | ||
every { realtimeChannels.get(any(), any()) } answers { | ||
val channelName = firstArg<String>() | ||
if (channelName == "room1::\$chat::\$reactions") { | ||
realtimeChannel | ||
} else { | ||
buildRealtimeChannel(channelName) | ||
} | ||
} | ||
|
||
roomReactions = DefaultRoomReactions( | ||
roomId = "room1", | ||
clientId = "client1", | ||
realtimeChannels = realtimeChannels, | ||
) | ||
} | ||
|
||
/** | ||
* @spec CHA-ER1 | ||
*/ | ||
@Test | ||
fun `channel name is set according to the spec`() = runTest { | ||
val roomReactions = DefaultRoomReactions( | ||
roomId = "foo", | ||
clientId = "client1", | ||
realtimeChannels = realtimeChannels, | ||
) | ||
|
||
assertEquals( | ||
"foo::\$chat::\$reactions", | ||
roomReactions.channel.name, | ||
) | ||
} | ||
|
||
/** | ||
* @spec CHA-ER3a | ||
*/ | ||
@Test | ||
fun `should be able to subscribe to incoming reactions`() = runTest { | ||
val pubSubMessageListenerSlot = slot<PubSubMessageListener>() | ||
|
||
every { realtimeChannel.subscribe("roomReaction", capture(pubSubMessageListenerSlot)) } returns Unit | ||
|
||
val deferredValue = DeferredValue<Reaction>() | ||
|
||
roomReactions.subscribe { | ||
deferredValue.completeWith(it) | ||
} | ||
|
||
verify { realtimeChannel.subscribe("roomReaction", any()) } | ||
|
||
pubSubMessageListenerSlot.captured.onMessage( | ||
PubSubMessage().apply { | ||
data = JsonObject().apply { | ||
addProperty("type", "like") | ||
} | ||
clientId = "clientId" | ||
timestamp = 1000L | ||
extras = MessageExtras( | ||
JsonObject().apply { | ||
add( | ||
"headers", | ||
JsonObject().apply { | ||
addProperty("foo", "bar") | ||
}, | ||
) | ||
}, | ||
) | ||
}, | ||
) | ||
|
||
val reaction = deferredValue.await() | ||
|
||
assertEquals( | ||
Reaction( | ||
type = "like", | ||
createdAt = 1000L, | ||
clientId = "clientId", | ||
metadata = mapOf(), | ||
headers = mapOf("foo" to "bar"), | ||
isSelf = false, | ||
), | ||
reaction, | ||
) | ||
} | ||
} |