diff --git a/library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt b/library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt index 3d3f0853..e6041b80 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt @@ -1,20 +1,18 @@ package org.xmtp.android.library import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.platform.app.InstrumentationRegistry import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import org.junit.Assert -import org.junit.Assert.* +import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.xmtp.android.library.messages.PrivateKey import org.xmtp.android.library.messages.PrivateKeyBuilder import org.xmtp.android.library.messages.walletAddress -import java.security.SecureRandom +import java.io.File @RunWith(AndroidJUnit4::class) class HistorySyncTest { @@ -22,12 +20,15 @@ class HistorySyncTest { private lateinit var boWallet: PrivateKeyBuilder private lateinit var alix: PrivateKey private lateinit var alixClient: Client + private lateinit var alixClient2: 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 + private lateinit var alixGroup: Group + private lateinit var alix2Group: Group @Before fun setUp() { @@ -39,113 +40,108 @@ class HistorySyncTest { caroWallet = fixtures.caroAccount caro = fixtures.caro - alixClient = fixtures.alixClient boClient = fixtures.boClient caroClient = fixtures.caroClient - } - @Test - fun testSyncConsent() { - val key = SecureRandom().generateSeed(32) - val context = InstrumentationRegistry.getInstrumentation().targetContext - val alixWallet = PrivateKeyBuilder() + alixClient = fixtures.alixClient + + alixGroup = runBlocking { alixClient.conversations.newGroup(listOf(bo.walletAddress)) } - val alixClient = runBlocking { + alixClient2 = runBlocking { Client().create( account = alixWallet, options = ClientOptions( ClientOptions.Api(XMTPEnvironment.LOCAL, false), - appContext = context, - dbEncryptionKey = key + appContext = fixtures.context, + dbEncryptionKey = fixtures.key, + dbDirectory = fixtures.context.filesDir.absolutePath.toString() ) ) } - val dm = runBlocking { alixClient.conversations.findOrCreateDm(bo.walletAddress) } - runBlocking { - dm.updateConsentState(ConsentState.DENIED) - assertEquals(dm.consentState(), ConsentState.DENIED) -// boClient.conversations.sync() - } -// val boDm = runBlocking { boClient.findConversation(dm.id) } - val alixClient2 = runBlocking { - Client().create( - account = alixWallet, - options = ClientOptions( - ClientOptions.Api(XMTPEnvironment.LOCAL, false), - appContext = context, - dbEncryptionKey = key, - dbDirectory = context.filesDir.absolutePath.toString() - ) - ) + runBlocking { + alixGroup.updateConsentState(ConsentState.DENIED) + alixClient.conversations.syncAllConversations() + alixClient2.conversations.syncAllConversations() + alix2Group = alixClient2.findGroup(alixGroup.id)!! } + } + @Test + fun testSyncConsent() { + assertEquals(alixGroup.consentState(), ConsentState.DENIED) + assertEquals(alix2Group.consentState(), ConsentState.UNKNOWN) val state = runBlocking { alixClient2.inboxState(true) } assertEquals(state.installations.size, 2) runBlocking { -// boClient.conversations.sync() -// boDm?.sync() - dm.send("Hello") alixClient2.preferences.syncConsent() + Thread.sleep(1000) alixClient.conversations.syncAllConversations() Thread.sleep(2000) alixClient2.conversations.syncAllConversations() Thread.sleep(2000) - val dm2 = alixClient2.findConversation(dm.id)!! - assertEquals(ConsentState.DENIED, dm2.consentState()) + assertEquals(ConsentState.DENIED, alix2Group.consentState()) alixClient2.preferences.setConsentState( listOf( ConsentRecord( - dm2.id, + alix2Group.id, EntryType.CONVERSATION_ID, ConsentState.ALLOWED ) ) ) assertEquals( - alixClient2.preferences.conversationState(dm2.id), + alixClient2.preferences.conversationState(alix2Group.id), ConsentState.ALLOWED ) - assertEquals(dm2.consentState(), ConsentState.ALLOWED) + assertEquals(alix2Group.consentState(), ConsentState.ALLOWED) } } @Test - fun testStreamConsent() { - val key = SecureRandom().generateSeed(32) - val context = InstrumentationRegistry.getInstrumentation().targetContext - val alixWallet = PrivateKeyBuilder() - - val alixClient = runBlocking { - Client().create( - account = alixWallet, - options = ClientOptions( - ClientOptions.Api(XMTPEnvironment.LOCAL, false), - appContext = context, - dbEncryptionKey = key - ) - ) + fun testSyncMessages() { + runBlocking { + alixGroup.send("A message") + alixGroup.send("A second message") } - val alixGroup = runBlocking { alixClient.conversations.newGroup(listOf(bo.walletAddress)) } + assertEquals(runBlocking { alixGroup.messages() }.size, 3) + assertEquals(runBlocking { alix2Group.messages() }.size, 0) + val state = runBlocking { alixClient2.inboxState(true) } + assertEquals(state.installations.size, 2) - val alixClient2 = runBlocking { + val alixClient3 = runBlocking { Client().create( account = alixWallet, options = ClientOptions( ClientOptions.Api(XMTPEnvironment.LOCAL, false), - appContext = context, - dbEncryptionKey = key, - dbDirectory = context.filesDir.absolutePath.toString() + appContext = fixtures.context, + dbEncryptionKey = fixtures.key, + dbDirectory = File(fixtures.context.filesDir.absolutePath, "xmtp_db3").toPath() + .toString() ) ) } runBlocking { - alixGroup.send("Hello") + alix2Group.send("A message") + alix2Group.send("A second message") + alixClient3.requestMessageHistorySync() + Thread.sleep(1000) alixClient.conversations.syncAllConversations() + Thread.sleep(2000) alixClient2.conversations.syncAllConversations() + Thread.sleep(2000) + alixClient3.conversations.syncAllConversations() + Thread.sleep(2000) + val alix3Group = alixClient3.findGroup(alixGroup.id)!! + assertEquals(runBlocking { alixGroup.messages() }.size, 5) + assertEquals(runBlocking { alix2Group.messages() }.size, 5) + assertEquals(runBlocking { alix3Group.messages() }.size, 5) } - val alix2Group = alixClient2.findGroup(alixGroup.id)!! + } + + @Test + fun testStreamConsent() { val consent = mutableListOf() val job = CoroutineScope(Dispatchers.IO).launch { try { @@ -157,14 +153,16 @@ class HistorySyncTest { } } - Thread.sleep(1000) + Thread.sleep(2000) runBlocking { alix2Group.updateConsentState(ConsentState.DENIED) val dm3 = alixClient2.conversations.newConversation(caro.walletAddress) dm3.updateConsentState(ConsentState.DENIED) alixClient.conversations.syncAllConversations() + Thread.sleep(2000) alixClient2.conversations.syncAllConversations() + Thread.sleep(2000) } Thread.sleep(2000)