-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move history sync tests into their own file
- Loading branch information
1 parent
fbfa0ca
commit e6cd769
Showing
2 changed files
with
173 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
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.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 | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class HistorySyncTest { | ||
private lateinit var alixWallet: PrivateKeyBuilder | ||
private lateinit var boWallet: 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() { | ||
fixtures = fixtures() | ||
alixWallet = fixtures.alixAccount | ||
alix = fixtures.alix | ||
boWallet = fixtures.boAccount | ||
bo = fixtures.bo | ||
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() | ||
|
||
val alixClient = runBlocking { | ||
Client().create( | ||
account = alixWallet, | ||
options = ClientOptions( | ||
ClientOptions.Api(XMTPEnvironment.LOCAL, false), | ||
appContext = context, | ||
dbEncryptionKey = key | ||
) | ||
) | ||
} | ||
val dm = runBlocking { alixClient.conversations.findOrCreateDm(bo.walletAddress) } | ||
runBlocking { | ||
dm.updateConsentState(ConsentState.DENIED) | ||
Assert.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() | ||
) | ||
) | ||
} | ||
|
||
val state = runBlocking { alixClient2.inboxState(true) } | ||
Assert.assertEquals(state.installations.size, 2) | ||
|
||
runBlocking { | ||
boClient.conversations.sync() | ||
boDm?.sync() | ||
alixClient2.preferences.syncConsent() | ||
alixClient.conversations.syncAllConversations() | ||
Thread.sleep(2000) | ||
alixClient2.conversations.syncAllConversations() | ||
Thread.sleep(2000) | ||
val dm2 = alixClient2.findConversation(dm.id)!! | ||
Assert.assertEquals(ConsentState.DENIED, dm2.consentState()) | ||
alixClient2.preferences.setConsentState( | ||
listOf( | ||
ConsentRecord( | ||
dm2.id, | ||
EntryType.CONVERSATION_ID, | ||
ConsentState.ALLOWED | ||
) | ||
) | ||
) | ||
Assert.assertEquals( | ||
alixClient2.preferences.conversationState(dm2.id), | ||
ConsentState.ALLOWED | ||
) | ||
Assert.assertEquals(dm2.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 | ||
) | ||
) | ||
} | ||
val alixGroup = runBlocking { alixClient.conversations.newGroup(listOf(bo.walletAddress)) } | ||
|
||
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.send("Hello") | ||
alixClient.conversations.syncAllConversations() | ||
alixClient2.conversations.syncAllConversations() | ||
} | ||
val alix2Group = alixClient2.findGroup(alixGroup.id)!! | ||
val consent = mutableListOf<ConsentRecord>() | ||
val job = CoroutineScope(Dispatchers.IO).launch { | ||
try { | ||
alixClient.preferences.streamConsent() | ||
.collect { entry -> | ||
consent.add(entry) | ||
} | ||
} catch (e: Exception) { | ||
} | ||
} | ||
|
||
Thread.sleep(1000) | ||
|
||
runBlocking { | ||
alix2Group.updateConsentState(ConsentState.DENIED) | ||
val dm3 = alixClient2.conversations.newConversation(caro.walletAddress) | ||
dm3.updateConsentState(ConsentState.DENIED) | ||
alixClient.conversations.syncAllConversations() | ||
alixClient2.conversations.syncAllConversations() | ||
} | ||
|
||
Thread.sleep(2000) | ||
Assert.assertEquals(3, consent.size) | ||
Assert.assertEquals(alixGroup.consentState(), ConsentState.DENIED) | ||
job.cancel() | ||
} | ||
} |