Skip to content

Commit

Permalink
add involved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 16, 2024
1 parent c3b66b3 commit 6d9f4e7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class V3ClientTest {

@Test
fun testsCanCreateGroup() {
val group = runBlocking { boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress)) }
val group =
runBlocking { boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress)) }
assertEquals(
runBlocking { group.members().map { it.inboxId }.sorted() },
listOf(caroV2V3Client.inboxId, boV3Client.inboxId).sorted()
Expand All @@ -93,8 +94,29 @@ class V3ClientTest {
}

@Test
fun testsCanSendMessages() {
val group = runBlocking { boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress)) }
fun testsCanCreateDm() {
val dm = runBlocking { boV3Client.conversations.findOrCreateDm(caroV2V3.walletAddress) }
assertEquals(
runBlocking { dm.members().map { it.inboxId }.sorted() },
listOf(caroV2V3Client.inboxId, boV3Client.inboxId).sorted()
)

val sameDm = runBlocking { boV3Client.findDm(caroV2V3.walletAddress) }
assertEquals(sameDm?.id, dm.id)

runBlocking { caroV2V3Client.conversations.syncConversations() }
val caroDm = runBlocking { caroV2V3Client.findDm(boV3Client.address) }
assertEquals(caroDm?.id, dm.id)

Assert.assertThrows("Recipient not on network", XMTPException::class.java) {
runBlocking { boV3Client.conversations.findOrCreateDm(alixV2.walletAddress) }
}
}

@Test
fun testsCanSendMessagesToGroup() {
val group =
runBlocking { boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress)) }
runBlocking { group.send("howdy") }
val messageId = runBlocking { group.send("gm") }
runBlocking { group.sync() }
Expand All @@ -103,13 +125,51 @@ class V3ClientTest {
assertEquals(group.messages().first().deliveryStatus, MessageDeliveryStatus.PUBLISHED)
assertEquals(group.messages().size, 3)

runBlocking { caroV2V3Client.conversations.syncGroups() }
runBlocking { caroV2V3Client.conversations.syncConversations() }
val sameGroup = runBlocking { caroV2V3Client.conversations.listGroups().last() }
runBlocking { sameGroup.sync() }
assertEquals(sameGroup.messages().size, 2)
assertEquals(sameGroup.messages().first().body, "gm")
}

@Test
fun testsCanSendMessagesToDm() {
val boDm =
runBlocking { boV3Client.conversations.newConversation(caroV2V3.walletAddress) }
runBlocking { boDm.send("howdy") }
var messageId = runBlocking { boDm.send("gm") }
var boDmMessage = runBlocking { boDm.messages() }
assertEquals(boDmMessage.first().body, "gm")
assertEquals(boDmMessage.first().id, messageId)
assertEquals(boDmMessage.first().deliveryStatus, MessageDeliveryStatus.PUBLISHED)
assertEquals(boDmMessage.size, 3)

runBlocking { caroV2V3Client.conversations.syncConversations() }
var sameDm = runBlocking { caroV2V3Client.conversations.list().last() }
runBlocking { sameDm.sync() }
var caroDmMessage = runBlocking { sameDm.messages() }
assertEquals(caroDmMessage.size, 2)
assertEquals(caroDmMessage.first().body, "gm")

// Do the inverse
val caroDm =
runBlocking { caroV2V3Client.conversations.newConversation(boV3.walletAddress) }
runBlocking { caroDm.send("howdy") }
messageId = runBlocking { caroDm.send("gm") }
caroDmMessage = runBlocking { caroDm.messages() }
assertEquals(caroDmMessage.first().body, "gm")
assertEquals(caroDmMessage.first().id, messageId)
assertEquals(caroDmMessage.first().deliveryStatus, MessageDeliveryStatus.PUBLISHED)
assertEquals(caroDmMessage.size, 4)

runBlocking { boV3Client.conversations.syncConversations() }
sameDm = runBlocking { boV3Client.conversations.list().last() }
runBlocking { sameDm.sync() }
boDmMessage = runBlocking { sameDm.messages() }
assertEquals(boDmMessage.size, 5)
assertEquals(boDmMessage.first().body, "gm")
}

@Test
fun testGroupConsent() {
runBlocking {
Expand Down Expand Up @@ -159,7 +219,8 @@ class V3ClientTest {

@Test
fun testCanStreamAllMessagesFromV2andV3Users() {
val group = runBlocking { boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress)) }
val group =
runBlocking { boV3Client.conversations.newGroup(listOf(caroV2V3.walletAddress)) }
val conversation =
runBlocking { alixV2Client.conversations.newConversation(caroV2V3.walletAddress) }
runBlocking { caroV2V3Client.conversations.syncGroups() }
Expand Down
8 changes: 8 additions & 0 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,14 @@ class Client() {
}
}

suspend fun addressesFromInboxIds(
refreshFromNetwork: Boolean,
inboxIds: List<String>,
): List<InboxState> {
val client = v3Client ?: throw XMTPException("Error no V3 client initialized")
return client.addressesFromInboxId(refreshFromNetwork, inboxIds).map { InboxState(it) }
}

suspend fun inboxState(refreshFromNetwork: Boolean): InboxState {
val client = v3Client ?: throw XMTPException("Error no V3 client initialized")
return InboxState(client.inboxState(refreshFromNetwork))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ sealed class Conversation {
}
}

suspend fun sync() {
return when (this) {
is V1 -> throw XMTPException("Only supported for V3")
is V2 -> throw XMTPException("Only supported for V3")
is Group -> group.sync()
is Dm -> dm.sync()
}
}

/**
* This lists messages sent to the [Conversation].
* @param before initial date to filter
Expand Down
20 changes: 16 additions & 4 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ data class Conversations(
val dm = findOrCreateDm(peerAddress)
val conversation = Conversation.Dm(dm)
conversationsByTopic[conversation.topic] = conversation
if (!client.hasV2Client) {
if (!client.hasV2Client || !client.canMessage(peerAddress)) {
return conversation
}
}
Expand Down Expand Up @@ -330,7 +330,6 @@ data class Conversations(
limit: Int? = null,
order: ConversationOrder = ConversationOrder.CREATED_AT,
): List<Dm> {
if (client.hasV2Client) throw XMTPException("Only supported for V3 only clients.")
val ffiDms = libXMTPConversations?.listDms(
opts = FfiListConversationsOptions(
after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
Expand Down Expand Up @@ -428,10 +427,23 @@ data class Conversations(
conversationsByTopic += newConversations.filter {
it.peerAddress != client.address && Topic.isValidTopic(it.topic)
}.map { Pair(it.topic, it) }
} else if (client.v3Client != null) {
}
if (client.v3Client != null) {
syncConversations()
val dms = listDms()
conversationsByTopic += dms.map { Pair(it.topic, Conversation.Dm(it)) }
if (!client.hasV2Client) {
conversationsByTopic += dms.map { it.topic to Conversation.Dm(it) }
} else {
conversationsByTopic.putAll(
dms.filter { dm ->
conversationsByTopic.values.none { existing ->
client.inboxIdFromAddress(existing.peerAddress)?.lowercase() == dm.peerInboxId().lowercase()
}
}.associate { dm ->
dm.topic to Conversation.Dm(dm)
}
)
}
}

if (includeGroups) {
Expand Down

0 comments on commit 6d9f4e7

Please sign in to comment.