1
1
package org.xmtp.android.library
2
2
3
+ import androidx.test.ext.junit.runners.AndroidJUnit4
3
4
import app.cash.turbine.test
5
+ import com.google.protobuf.kotlin.toByteString
4
6
import kotlinx.coroutines.ExperimentalCoroutinesApi
5
- import kotlinx.coroutines.test.runTest
7
+ import org.junit.Assert
6
8
import org.junit.Assert.assertEquals
7
9
import org.junit.Assert.assertThrows
8
10
import org.junit.Before
9
11
import org.junit.Test
12
+ import org.junit.runner.RunWith
10
13
import org.web3j.crypto.Hash
11
14
import org.xmtp.android.library.codecs.TextCodec
12
15
import org.xmtp.android.library.messages.EnvelopeBuilder
@@ -37,6 +40,7 @@ import java.nio.charset.StandardCharsets
37
40
import java.util.Date
38
41
39
42
@OptIn(ExperimentalCoroutinesApi ::class )
43
+ @RunWith(AndroidJUnit4 ::class )
40
44
class ConversationTest {
41
45
lateinit var fakeApiClient: FakeApiClient
42
46
lateinit var aliceWallet: PrivateKeyBuilder
@@ -439,7 +443,7 @@ class ConversationTest {
439
443
}
440
444
441
445
@Test
442
- fun testCanStreamConversationsV2 () = runTest {
446
+ fun testCanStreamConversationsV2 () = kotlinx.coroutines.test. runTest {
443
447
bobClient.conversations.stream().test {
444
448
val conversation = bobClient.conversations.newConversation(alice.walletAddress)
445
449
conversation.send(content = " hi" )
@@ -449,7 +453,7 @@ class ConversationTest {
449
453
}
450
454
451
455
@Test
452
- fun testStreamingMessagesFromV1Conversation () = runTest {
456
+ fun testStreamingMessagesFromV1Conversation () = kotlinx.coroutines.test. runTest {
453
457
// Overwrite contact as legacy
454
458
fixtures.publishLegacyContact(client = bobClient)
455
459
fixtures.publishLegacyContact(client = aliceClient)
@@ -478,7 +482,7 @@ class ConversationTest {
478
482
}
479
483
480
484
@Test
481
- fun testStreamingMessagesFromV2Conversations () = runTest {
485
+ fun testStreamingMessagesFromV2Conversations () = kotlinx.coroutines.test. runTest {
482
486
val conversation = aliceClient.conversations.newConversation(bob.walletAddress)
483
487
conversation.streamMessages().test {
484
488
val encoder = TextCodec ()
@@ -504,7 +508,7 @@ class ConversationTest {
504
508
}
505
509
506
510
@Test
507
- fun testStreamAllMessagesGetsMessageFromKnownConversation () = runTest {
511
+ fun testStreamAllMessagesGetsMessageFromKnownConversation () = kotlinx.coroutines.test. runTest {
508
512
val fixtures = fixtures()
509
513
val client = fixtures.aliceClient
510
514
val bobConversation = fixtures.bobClient.conversations.newConversation(client.address)
@@ -568,4 +572,40 @@ class ConversationTest {
568
572
assertEquals(" hi" , message.body)
569
573
assertEquals(message.id, messageID)
570
574
}
575
+
576
+ @Test
577
+ fun testFetchConversation () {
578
+ // Generated from JS script
579
+ val ints = arrayOf(
580
+ 31 , 116 , 198 , 193 , 189 , 122 , 19 , 254 , 191 , 189 , 211 , 215 , 255 , 131 ,
581
+ 171 , 239 , 243 , 33 , 4 , 62 , 143 , 86 , 18 , 195 , 251 , 61 , 128 , 90 , 34 , 126 , 219 , 236
582
+ )
583
+ val bytes =
584
+ ints.foldIndexed(ByteArray (ints.size)) { i, a, v -> a.apply { set(i, v.toByte()) } }
585
+
586
+ val key = PrivateKey .newBuilder().also {
587
+ it.secp256K1 = it.secp256K1.toBuilder().also { builder ->
588
+ builder.bytes = bytes.toByteString()
589
+ }.build()
590
+ it.publicKey = it.publicKey.toBuilder().also { builder ->
591
+ builder.secp256K1Uncompressed =
592
+ builder.secp256K1Uncompressed.toBuilder().also { keyBuilder ->
593
+ keyBuilder.bytes =
594
+ KeyUtil .addUncompressedByte(KeyUtil .getPublicKey(bytes)).toByteString()
595
+ }.build()
596
+ }.build()
597
+ }.build()
598
+
599
+ val client = Client ().create(account = PrivateKeyBuilder (key))
600
+ Assert .assertEquals(client.apiClient.environment, XMTPEnvironment .DEV )
601
+ val conversations = client.conversations.list()
602
+ Assert .assertEquals(1 , conversations.size)
603
+ val topic = conversations[0 ].topic
604
+ val conversation = client.fetchConversation(topic)
605
+ Assert .assertEquals(conversations[0 ].topic, conversation?.topic)
606
+ Assert .assertEquals(conversations[0 ].peerAddress, conversation?.peerAddress)
607
+
608
+ val noConversation = client.fetchConversation(" invalid_topic" )
609
+ Assert .assertEquals(null , noConversation)
610
+ }
571
611
}
0 commit comments