Skip to content

Commit e1b4c0a

Browse files
committed
add test on can message
1 parent c56ac33 commit e1b4c0a

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

library/src/androidTest/java/org/xmtp/android/library/GroupTest.kt

+27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
44
import androidx.test.platform.app.InstrumentationRegistry
55
import kotlinx.coroutines.runBlocking
66
import org.junit.Assert.assertEquals
7+
import org.junit.Assert.assertThrows
78
import org.junit.Before
89
import org.junit.Test
910
import org.junit.runner.RunWith
@@ -127,6 +128,32 @@ class GroupTest {
127128
assertEquals(convos.size, 3)
128129
}
129130

131+
@Test
132+
fun testCannotSendMessageToGroupMemberNotOnV3() {
133+
var fakeApiClient = FakeApiClient()
134+
val chuxAccount = PrivateKeyBuilder()
135+
val chux: PrivateKey = chuxAccount.getPrivateKey()
136+
val chuxClient: Client = Client().create(account = chuxAccount, apiClient = fakeApiClient)
137+
138+
assertThrows("Recipient not on network", XMTPException::class.java) {
139+
boClient.conversations.newGroup(listOf(chux.walletAddress))
140+
}
141+
}
142+
143+
@Test
144+
fun testCannotStartGroupWithSelf() {
145+
assertThrows("Recipient is sender", XMTPException::class.java) {
146+
boClient.conversations.newGroup(listOf(bo.walletAddress))
147+
}
148+
}
149+
150+
@Test
151+
fun testCannotStartEmptyGroupChat() {
152+
assertThrows("Cannot start an empty group chat.", XMTPException::class.java) {
153+
boClient.conversations.newGroup(listOf())
154+
}
155+
}
156+
130157
@Test
131158
fun testCanSendMessageToGroup() {
132159
val group = boClient.conversations.newGroup(listOf(alix.walletAddress))

library/src/main/java/org/xmtp/android/library/Client.kt

+6
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,12 @@ class Client() {
559559
return runBlocking { query(Topic.contact(peerAddress)).envelopesList.size > 0 }
560560
}
561561

562+
fun canMessage(addresses: List<String>): Boolean {
563+
return runBlocking {
564+
libXMTPClient != null && !libXMTPClient!!.canMessage(addresses).contains(false)
565+
}
566+
}
567+
562568
val privateKeyBundle: PrivateKeyBundle
563569
get() = PrivateKeyBundleBuilder.buildFromV1Key(privateKeyBundleV1)
564570

library/src/main/java/org/xmtp/android/library/Conversations.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ data class Conversations(
9595
) {
9696
throw XMTPException("Recipient is sender")
9797
}
98-
val contacts = accountAddresses.map { client.contacts.find(it) }
99-
if (contacts.size != accountAddresses.size) {
98+
if (!client.canMessage(accountAddresses)) {
10099
throw XMTPException("Recipient not on network")
101100
}
102101

0 commit comments

Comments
 (0)