Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 16, 2024
1 parent 6d9f4e7 commit acff4b9
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 51 deletions.
92 changes: 44 additions & 48 deletions library/src/androidTest/java/org/xmtp/android/library/DmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,79 +31,95 @@ import java.security.SecureRandom
class DmTest {
private lateinit var alixWallet: PrivateKeyBuilder
private lateinit var boWallet: PrivateKeyBuilder
private lateinit var caroWallet: PrivateKeyBuilder
private lateinit var alix: PrivateKey
private lateinit var alixClient: Client
private lateinit var bo: PrivateKey
private lateinit var boClient: Client
private lateinit var caroWallet: PrivateKeyBuilder
private lateinit var caro: PrivateKey
private lateinit var caroClient: Client
private lateinit var fixtures: Fixtures

@Before
fun setUp() {
val key = SecureRandom().generateSeed(32)
val context = InstrumentationRegistry.getInstrumentation().targetContext
fixtures =
fixtures(
clientOptions = ClientOptions(
alixWallet = PrivateKeyBuilder()
alix = alixWallet.getPrivateKey()
alixClient = runBlocking {
Client().createOrBuild(
account = alixWallet,
address = alixWallet.address,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableV3 = true,
appContext = context,
dbEncryptionKey = key
)
)
}
boWallet = PrivateKeyBuilder()
bo = boWallet.getPrivateKey()
boClient = runBlocking {
Client().createOrBuild(
account = boWallet,
address = boWallet.address,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableV3 = true,
appContext = context,
dbEncryptionKey = key
)
)
}

caroWallet = PrivateKeyBuilder()
caro = caroWallet.getPrivateKey()
caroClient = runBlocking {
Client().createOrBuild(
account = caroWallet,
address = caroWallet.address,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableV3 = true,
appContext = context,
dbEncryptionKey = key
)
)
alixWallet = fixtures.aliceAccount
alix = fixtures.alice
boWallet = fixtures.bobAccount
bo = fixtures.bob
caroWallet = fixtures.caroAccount
caro = fixtures.caro

alixClient = fixtures.aliceClient
boClient = fixtures.bobClient
caroClient = fixtures.caroClient
}

}

@Test
fun testCanCreateADm() {
val dm = runBlocking {
boClient.conversations.newConversation(alix.walletAddress)
boClient.conversations.findOrCreateDm(alix.walletAddress)
}
}
@Test
fun testCanListDmMembers() {
val group = runBlocking {
boClient.conversations.newGroup(
listOf(
boClient.conversations.findOrCreateDm(
alix.walletAddress,
caro.walletAddress
)
)
}
assertEquals(
runBlocking { group.members().map { it.inboxId }.sorted() },
listOf(
caroClient.inboxId,
alixClient.inboxId,
boClient.inboxId
).sorted()
)

assertEquals(
Conversation.Group(group).peerAddresses.sorted(),
Conversation.Dm(group).peerAddresses.sorted(),
listOf(
caroClient.inboxId,
alixClient.inboxId,
).sorted()
)

assertEquals(
runBlocking { group.peerInboxIds().sorted() },
listOf(
caroClient.inboxId,
runBlocking { group.peerInboxId() },
alixClient.inboxId,
).sorted()
)
}

Expand Down Expand Up @@ -132,30 +148,10 @@ class DmTest {
assertEquals("thisisanewurl.com", alixGroup.imageUrlSquare)
}

@Test
fun testCanRemoveGroupMembers() {
val group = runBlocking {
boClient.conversations.newGroup(
listOf(
alixClient.address,
caroClient.address
)
)
}
runBlocking { group.removeMembers(listOf(caro.walletAddress)) }
assertEquals(
runBlocking { group.members().map { it.inboxId }.sorted() },
listOf(
alixClient.inboxId,
boClient.inboxId
).sorted()
)
}

@Test
fun testCanListDms() {
runBlocking {
boClient.conversations.newGroup(listOf(alix.walletAddress))
boClient.conversations.findOrCreateDm(alix.walletAddress)
boClient.conversations.newGroup(listOf(caro.walletAddress))
}
val groups = runBlocking { boClient.conversations.listGroups() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ class V3ClientTest {
}
}

@Test
fun testsCanListConversations() {
val dm = runBlocking { boV3Client.conversations.findOrCreateDm(caroV2V3.walletAddress) }
val group =
runBlocking { boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress)) }
assertEquals(runBlocking { boV3Client.conversations.listConversations().size }, 2)
assertEquals(runBlocking { boV3Client.conversations.list(includeGroups = true).size }, 2)
assertEquals(runBlocking { boV3Client.conversations.listDms().size }, 1)
assertEquals(runBlocking { boV3Client.conversations.listGroups().size }, 1)

runBlocking { caroV2V3Client.conversations.syncConversations() }
assertEquals(
runBlocking { caroV2V3Client.conversations.list(includeGroups = true).size },
2
)
assertEquals(runBlocking { caroV2V3Client.conversations.listDms().size }, 1)
assertEquals(runBlocking { caroV2V3Client.conversations.listGroups().size }, 1)
}

@Test
fun testsCanSendMessagesToGroup() {
val group =
Expand Down Expand Up @@ -217,13 +236,70 @@ class V3ClientTest {
}
}

@Test
fun testCanStreamAllMessagesFromV3Users() {
val group =
runBlocking { caroV2V3Client.conversations.newGroup(listOf(boV3.walletAddress)) }
val conversation =
runBlocking { caroV2V3Client.conversations.newConversation(boV3.walletAddress) }
runBlocking { boV3Client.conversations.syncConversations() }

val allMessages = mutableListOf<DecodedMessage>()

val job = CoroutineScope(Dispatchers.IO).launch {
try {
boV3Client.conversations.streamAllMessages(includeGroups = true)
.collect { message ->
allMessages.add(message)
}
} catch (e: Exception) {
}
}
Thread.sleep(1000)
runBlocking {
group.send("hi")
conversation.send("hi")
}
Thread.sleep(1000)
assertEquals(2, allMessages.size)
job.cancel()
}

@Test
fun testCanStreamGroupsAndConversationsFromV3Users() {
val allMessages = mutableListOf<String>()

val job = CoroutineScope(Dispatchers.IO).launch {
try {
boV3Client.conversations.streamAll()
.collect { message ->
allMessages.add(message.topic)
}
} catch (e: Exception) {
}
}
Thread.sleep(1000)

runBlocking {
caroV2V3Client.conversations.newConversation(boV3.walletAddress)
Thread.sleep(1000)
caroV2V3Client.conversations.newGroup(listOf(boV3.walletAddress))
}

Thread.sleep(2000)
assertEquals(2, allMessages.size)
job.cancel()
}

@Test
fun testCanStreamAllMessagesFromV2andV3Users() {
val group =
runBlocking { boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress)) }
val conversation =
runBlocking { alixV2Client.conversations.newConversation(caroV2V3.walletAddress) }
runBlocking { caroV2V3Client.conversations.syncGroups() }
val dm =
runBlocking { boV3Client.conversations.newConversation(caroV2V3.walletAddress) }
runBlocking { caroV2V3Client.conversations.syncConversations() }

val allMessages = mutableListOf<DecodedMessage>()

Expand All @@ -240,9 +316,10 @@ class V3ClientTest {
runBlocking {
group.send("hi")
conversation.send("hi")
dm.send("hi")
}
Thread.sleep(1000)
assertEquals(2, allMessages.size)
assertEquals(3, allMessages.size)
job.cancel()
}

Expand All @@ -265,10 +342,11 @@ class V3ClientTest {
alixV2Client.conversations.newConversation(caroV2V3.walletAddress)
Thread.sleep(1000)
boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress))
boV3Client.conversations.newConversation(caroV2V3.walletAddress)
}

Thread.sleep(2000)
assertEquals(2, allMessages.size)
assertEquals(3, allMessages.size)
job.cancel()
}
}

0 comments on commit acff4b9

Please sign in to comment.