@@ -29,15 +29,9 @@ import com.wire.kalium.persistence.db.GlobalDatabaseBuilder
2929import com.wire.kalium.persistence.model.LogoutReason
3030import com.wire.kalium.persistence.model.ServerConfigEntity
3131import com.wire.kalium.persistence.model.SsoIdEntity
32- import kotlinx.coroutines.Dispatchers
32+ import kotlinx.coroutines.flow.first
3333import kotlinx.coroutines.ExperimentalCoroutinesApi
34- import kotlinx.coroutines.test.StandardTestDispatcher
35- import kotlinx.coroutines.test.TestCoroutineScheduler
36- import kotlinx.coroutines.test.TestDispatcher
37- import kotlinx.coroutines.test.resetMain
3834import kotlinx.coroutines.test.runTest
39- import kotlinx.coroutines.test.setMain
40- import kotlin.test.AfterTest
4135import kotlin.test.BeforeTest
4236import kotlin.test.Test
4337import kotlin.test.assertEquals
@@ -207,6 +201,79 @@ class AccountsDAOTest : GlobalDBBaseTest() {
207201 assertEquals(null , result)
208202 }
209203
204+ @Test
205+ fun whenUpdatingPersistentWebSocketStatus_thenStatusIsUpdated () = runTest {
206+ val account = VALID_ACCOUNT
207+ globalDatabaseBuilder.accountsDAO.insertOrReplace(account.info.userIDEntity, account.ssoId, account.managedBy, account.serverConfigId, false )
208+
209+ // initial status false
210+ val initial = globalDatabaseBuilder.accountsDAO.persistentWebSocketStatus(account.info.userIDEntity)
211+ assertEquals(false , initial)
212+
213+ // update to true
214+ globalDatabaseBuilder.accountsDAO.updatePersistentWebSocketStatus(account.info.userIDEntity, true )
215+ val updated = globalDatabaseBuilder.accountsDAO.persistentWebSocketStatus(account.info.userIDEntity)
216+ assertEquals(true , updated)
217+ }
218+
219+ @Test
220+ fun whenSettingAllAccountsPersistentWebSocketEnabled_thenAllStatusesAreUpdated () = runTest {
221+ val a1 = VALID_ACCOUNT
222+ val a2 = VALID_ACCOUNT .copy(info = AccountInfoEntity (UserIDEntity (" user2" , " domain2" ), null ))
223+ val a3 = VALID_ACCOUNT .copy(info = AccountInfoEntity (UserIDEntity (" user3" , " domain3" ), null ))
224+
225+ listOf (a1, a2, a3).forEach {
226+ globalDatabaseBuilder.accountsDAO.insertOrReplace(it.info.userIDEntity, it.ssoId, it.managedBy, it.serverConfigId, false )
227+ }
228+
229+ globalDatabaseBuilder.accountsDAO.setAllAccountsPersistentWebSocketEnabled(true )
230+
231+ listOf (a1, a2, a3).forEach {
232+ val status = globalDatabaseBuilder.accountsDAO.persistentWebSocketStatus(it.info.userIDEntity)
233+ assertEquals(true , status)
234+ }
235+ }
236+
237+ @Test
238+ fun whenGettingAllValidAccountPersistentWebSocketStatus_thenOnlyValidAccountsIncluded () = runTest {
239+ val valid1 = VALID_ACCOUNT
240+ val valid2 = VALID_ACCOUNT .copy(info = AccountInfoEntity (UserIDEntity (" userB" , " domainB" ), null ))
241+ val invalid = INVALID_ACCOUNT
242+
243+ // insert accounts with different initial statuses
244+ globalDatabaseBuilder.accountsDAO.insertOrReplace(valid1.info.userIDEntity, valid1.ssoId, valid1.managedBy, valid1.serverConfigId, true )
245+ globalDatabaseBuilder.accountsDAO.insertOrReplace(valid2.info.userIDEntity, valid2.ssoId, valid2.managedBy, valid2.serverConfigId, false )
246+ globalDatabaseBuilder.accountsDAO.insertOrReplace(invalid.info.userIDEntity, invalid.ssoId, invalid.managedBy, invalid.serverConfigId, true )
247+ globalDatabaseBuilder.accountsDAO.markAccountAsInvalid(invalid.info.userIDEntity, invalid.info.logoutReason!! )
248+
249+ val list = globalDatabaseBuilder.accountsDAO.getAllValidAccountPersistentWebSocketStatus().first()
250+ // Should contain only the two valid accounts in any order
251+ val ids = list.map { it.userIDEntity }.toSet()
252+ assertEquals(setOf (valid1.info.userIDEntity, valid2.info.userIDEntity), ids)
253+ val map = list.associateBy({ it.userIDEntity }, { it.isPersistentWebSocketEnabled })
254+ assertEquals(true , map[valid1.info.userIDEntity])
255+ assertEquals(false , map[valid2.info.userIDEntity])
256+ }
257+
258+ @Test
259+ fun whenRequestingValidAccountWithServerConfigId_thenReturnMapForValidAccounts () = runTest {
260+ val valid1 = VALID_ACCOUNT
261+ val valid2 = VALID_ACCOUNT .copy(info = AccountInfoEntity (UserIDEntity (" userC" , " domainC" ), null ))
262+ val invalid = INVALID_ACCOUNT
263+
264+ listOf (valid1, valid2, invalid).forEach {
265+ globalDatabaseBuilder.accountsDAO.insertOrReplace(it.info.userIDEntity, it.ssoId, it.managedBy, it.serverConfigId, false )
266+ }
267+ globalDatabaseBuilder.accountsDAO.markAccountAsInvalid(invalid.info.userIDEntity, invalid.info.logoutReason!! )
268+
269+ val map = globalDatabaseBuilder.accountsDAO.validAccountWithServerConfigId()
270+ // only valid1 and valid2 should be present
271+ assertEquals(setOf (valid1.info.userIDEntity, valid2.info.userIDEntity), map.keys)
272+ map.values.forEach { serverConfig ->
273+ assertEquals(SERVER_CONFIG , serverConfig)
274+ }
275+ }
276+
210277 private companion object {
211278
212279 val VALID_ACCOUNT = FullAccountEntity (
0 commit comments