From a00623e490a6cdfdf346c761440a08582729027e Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 1 Sep 2025 15:03:41 +0200 Subject: [PATCH] Cleanup tests. --- .../impl/migrations/AppMigration02Test.kt | 8 +++---- .../impl/migrations/AppMigration05Test.kt | 12 +++++----- .../impl/migrations/AppMigration06Test.kt | 12 +++++----- .../impl/reporter/DefaultBugReporterTest.kt | 24 +++++++++---------- .../signedout/impl/SignedOutPresenterTest.kt | 12 +++++----- .../impl/RustClientSessionDelegateTest.kt | 11 +++++---- .../impl/util/SessionPathsProviderTest.kt | 13 +++++----- .../DefaultFirebaseNewTokenHandlerTest.kt | 24 ++++++++++--------- .../test/InMemorySessionStore.kt | 6 +++-- 9 files changed, 64 insertions(+), 58 deletions(-) diff --git a/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration02Test.kt b/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration02Test.kt index 59a9c11500..fb7dbf5281 100644 --- a/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration02Test.kt +++ b/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration02Test.kt @@ -20,12 +20,12 @@ import org.junit.Test class AppMigration02Test { @Test fun `test migration`() = runTest { - val sessionStore = InMemorySessionStore().apply { - updateData(aSessionData()) - } + val sessionStore = InMemorySessionStore( + initialList = listOf(aSessionData()), + ) val sessionPreferencesStore = InMemorySessionPreferencesStore(isSessionVerificationSkipped = false) val sessionPreferencesStoreFactory = FakeSessionPreferencesStoreFactory( - getLambda = lambdaRecorder { _, _, -> sessionPreferencesStore }, + getLambda = lambdaRecorder { _, _ -> sessionPreferencesStore }, removeLambda = lambdaRecorder { _ -> } ) val migration = AppMigration02(sessionStore = sessionStore, sessionPreferenceStoreFactory = sessionPreferencesStoreFactory) diff --git a/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration05Test.kt b/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration05Test.kt index 2962f95f05..af71905635 100644 --- a/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration05Test.kt +++ b/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration05Test.kt @@ -18,14 +18,14 @@ import java.io.File class AppMigration05Test { @Test fun `empty session path should be set to an expected path`() = runTest { - val sessionStore = InMemorySessionStore().apply { - updateData( + val sessionStore = InMemorySessionStore( + initialList = listOf( aSessionData( sessionId = A_SESSION_ID.value, sessionPath = "", ) ) - } + ) val migration = AppMigration05(sessionStore = sessionStore, baseDirectory = File("/a/path")) migration.migrate() val storedData = sessionStore.getSession(A_SESSION_ID.value)!! @@ -34,14 +34,14 @@ class AppMigration05Test { @Test fun `non empty session path should not be impacted by the migration`() = runTest { - val sessionStore = InMemorySessionStore().apply { - updateData( + val sessionStore = InMemorySessionStore( + initialList = listOf( aSessionData( sessionId = A_SESSION_ID.value, sessionPath = "/a/path/existing", ) ) - } + ) val migration = AppMigration05(sessionStore = sessionStore, baseDirectory = File("/a/path")) migration.migrate() val storedData = sessionStore.getSession(A_SESSION_ID.value)!! diff --git a/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration06Test.kt b/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration06Test.kt index 5613d7ab9e..095085cd17 100644 --- a/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration06Test.kt +++ b/features/migration/impl/src/test/kotlin/io/element/android/features/migration/impl/migrations/AppMigration06Test.kt @@ -18,15 +18,15 @@ import java.io.File class AppMigration06Test { @Test fun `empty cache path should be set to an expected path`() = runTest { - val sessionStore = InMemorySessionStore().apply { - updateData( + val sessionStore = InMemorySessionStore( + initialList = listOf( aSessionData( sessionId = A_SESSION_ID.value, sessionPath = "/a/path/to/a/session/AN_ID", cachePath = "", ) ) - } + ) val migration = AppMigration06(sessionStore = sessionStore, cacheDirectory = File("/a/path/cache")) migration.migrate() val storedData = sessionStore.getSession(A_SESSION_ID.value)!! @@ -35,14 +35,14 @@ class AppMigration06Test { @Test fun `non empty cache path should not be impacted by the migration`() = runTest { - val sessionStore = InMemorySessionStore().apply { - updateData( + val sessionStore = InMemorySessionStore( + initialList = listOf( aSessionData( sessionId = A_SESSION_ID.value, cachePath = "/a/path/existing", ) ) - } + ) val migration = AppMigration05(sessionStore = sessionStore, baseDirectory = File("/a/path/cache")) migration.migrate() val storedData = sessionStore.getSession(A_SESSION_ID.value)!! diff --git a/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt b/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt index e9b888d78c..ecb8d13b12 100755 --- a/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt +++ b/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt @@ -104,9 +104,9 @@ class DefaultBugReporterTest { ) server.start() - val mockSessionStore = InMemorySessionStore().apply { - storeData(aSessionData(sessionId = "@foo:example.com", deviceId = "ABCDEFGH")) - } + val mockSessionStore = InMemorySessionStore( + initialList = listOf(aSessionData(sessionId = "@foo:example.com", deviceId = "ABCDEFGH")) + ) val fakeEncryptionService = FakeEncryptionService() val matrixClient = FakeMatrixClient(encryptionService = fakeEncryptionService) @@ -165,9 +165,9 @@ class DefaultBugReporterTest { ) server.start() - val mockSessionStore = InMemorySessionStore().apply { - storeData(aSessionData("@foo:example.com", "ABCDEFGH")) - } + val mockSessionStore = InMemorySessionStore( + initialList = listOf(aSessionData("@foo:example.com", "ABCDEFGH")) + ) val fakeEncryptionService = FakeEncryptionService() val matrixClient = FakeMatrixClient(encryptionService = fakeEncryptionService) @@ -308,9 +308,9 @@ class DefaultBugReporterTest { fun `the log directory is initialized using the last session store data`() = runTest { val sut = createDefaultBugReporter( buildMeta = aBuildMeta(isEnterpriseBuild = true), - sessionStore = InMemorySessionStore().apply { - storeData(aSessionData(sessionId = "@alice:domain.com")) - } + sessionStore = InMemorySessionStore( + initialList = listOf(aSessionData(sessionId = "@alice:domain.com")) + ) ) assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs/domain.com") } @@ -318,9 +318,9 @@ class DefaultBugReporterTest { @Test fun `foss build - the log directory is initialized to the root log directory`() = runTest { val sut = createDefaultBugReporter( - sessionStore = InMemorySessionStore().apply { - storeData(aSessionData(sessionId = "@alice:domain.com")) - } + sessionStore = InMemorySessionStore( + initialList = listOf(aSessionData(sessionId = "@alice:domain.com")) + ) ) assertThat(sut.logDirectory().absolutePath).endsWith("/cache/logs") } diff --git a/features/signedout/impl/src/test/kotlin/io/element/android/features/signedout/impl/SignedOutPresenterTest.kt b/features/signedout/impl/src/test/kotlin/io/element/android/features/signedout/impl/SignedOutPresenterTest.kt index fc9d0e6484..8674020f1e 100644 --- a/features/signedout/impl/src/test/kotlin/io/element/android/features/signedout/impl/SignedOutPresenterTest.kt +++ b/features/signedout/impl/src/test/kotlin/io/element/android/features/signedout/impl/SignedOutPresenterTest.kt @@ -31,9 +31,9 @@ class SignedOutPresenterTest { @Test fun `present - initial state`() = runTest { val aSessionData = aSessionData() - val sessionStore = InMemorySessionStore().apply { - storeData(aSessionData) - } + val sessionStore = InMemorySessionStore( + initialList = listOf(aSessionData) + ) val presenter = createSignedOutPresenter(sessionStore = sessionStore) moleculeFlow(RecompositionMode.Immediate) { presenter.present() @@ -48,9 +48,9 @@ class SignedOutPresenterTest { @Test fun `present - sign in again`() = runTest { val aSessionData = aSessionData() - val sessionStore = InMemorySessionStore().apply { - storeData(aSessionData) - } + val sessionStore = InMemorySessionStore( + initialList = listOf(aSessionData) + ) val presenter = createSignedOutPresenter(sessionStore = sessionStore) moleculeFlow(RecompositionMode.Immediate) { presenter.present() diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/RustClientSessionDelegateTest.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/RustClientSessionDelegateTest.kt index 623577fced..224c4118fb 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/RustClientSessionDelegateTest.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/RustClientSessionDelegateTest.kt @@ -23,11 +23,12 @@ import org.junit.Test class RustClientSessionDelegateTest { @Test fun `saveSessionInKeychain should update the store`() = runTest { - val sessionStore = InMemorySessionStore() - sessionStore.storeData( - aSessionData( - accessToken = "anAccessToken", - refreshToken = "aRefreshToken", + val sessionStore = InMemorySessionStore( + initialList = listOf( + aSessionData( + accessToken = "anAccessToken", + refreshToken = "aRefreshToken", + ) ) ) val sut = aRustClientSessionDelegate(sessionStore) diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/util/SessionPathsProviderTest.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/util/SessionPathsProviderTest.kt index d14dfac3f4..43e5a98e50 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/util/SessionPathsProviderTest.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/util/SessionPathsProviderTest.kt @@ -24,14 +24,15 @@ class SessionPathsProviderTest { @Test fun `if session is found, provides returns the data`() = runTest { - val store = InMemorySessionStore() - val sut = SessionPathsProvider(store) - store.storeData( - aSessionData( - sessionPath = "/a/path/to/a/session", - cachePath = "/a/path/to/a/cache", + val store = InMemorySessionStore( + initialList = listOf( + aSessionData( + sessionPath = "/a/path/to/a/session", + cachePath = "/a/path/to/a/cache", + ) ) ) + val sut = SessionPathsProvider(store) val result = sut.provides(A_SESSION_ID)!! assertThat(result.fileDirectory.absolutePath).isEqualTo("/a/path/to/a/session") assertThat(result.cacheDirectory.absolutePath).isEqualTo("/a/path/to/a/cache") diff --git a/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/DefaultFirebaseNewTokenHandlerTest.kt b/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/DefaultFirebaseNewTokenHandlerTest.kt index 9951a6450f..70cd24e6c8 100644 --- a/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/DefaultFirebaseNewTokenHandlerTest.kt +++ b/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/DefaultFirebaseNewTokenHandlerTest.kt @@ -49,11 +49,13 @@ class DefaultFirebaseNewTokenHandlerTest { val registerPusherResult = lambdaRecorder> { _, _, _ -> Result.success(Unit) } val pusherSubscriber = FakePusherSubscriber(registerPusherResult = registerPusherResult) val firebaseNewTokenHandler = createDefaultFirebaseNewTokenHandler( - sessionStore = InMemorySessionStore().apply { - storeData(aSessionData(A_USER_ID.value)) - storeData(aSessionData(A_USER_ID_2.value)) - storeData(aSessionData(A_USER_ID_3.value)) - }, + sessionStore = InMemorySessionStore( + initialList = listOf( + aSessionData(A_USER_ID.value), + aSessionData(A_USER_ID_2.value), + aSessionData(A_USER_ID_3.value), + ) + ), matrixClientProvider = FakeMatrixClientProvider { sessionId -> when (sessionId) { A_USER_ID -> Result.success(aMatrixClient1) @@ -88,9 +90,9 @@ class DefaultFirebaseNewTokenHandlerTest { val registerPusherResult = lambdaRecorder> { _, _, _ -> Result.success(Unit) } val pusherSubscriber = FakePusherSubscriber(registerPusherResult = registerPusherResult) val firebaseNewTokenHandler = createDefaultFirebaseNewTokenHandler( - sessionStore = InMemorySessionStore().apply { - storeData(aSessionData(A_USER_ID.value)) - }, + sessionStore = InMemorySessionStore( + initialList = listOf(aSessionData(A_USER_ID.value)) + ), matrixClientProvider = FakeMatrixClientProvider { Result.failure(IllegalStateException()) }, @@ -112,9 +114,9 @@ class DefaultFirebaseNewTokenHandlerTest { val registerPusherResult = lambdaRecorder> { _, _, _ -> Result.failure(AN_EXCEPTION) } val pusherSubscriber = FakePusherSubscriber(registerPusherResult = registerPusherResult) val firebaseNewTokenHandler = createDefaultFirebaseNewTokenHandler( - sessionStore = InMemorySessionStore().apply { - storeData(aSessionData(A_USER_ID.value)) - }, + sessionStore = InMemorySessionStore( + initialList = listOf(aSessionData(A_USER_ID.value)) + ), matrixClientProvider = FakeMatrixClientProvider { Result.success(aMatrixClient1) }, diff --git a/libraries/session-storage/test/src/main/kotlin/io/element/android/libraries/sessionstorage/test/InMemorySessionStore.kt b/libraries/session-storage/test/src/main/kotlin/io/element/android/libraries/sessionstorage/test/InMemorySessionStore.kt index 5312c04361..95968afb8f 100644 --- a/libraries/session-storage/test/src/main/kotlin/io/element/android/libraries/sessionstorage/test/InMemorySessionStore.kt +++ b/libraries/session-storage/test/src/main/kotlin/io/element/android/libraries/sessionstorage/test/InMemorySessionStore.kt @@ -15,8 +15,10 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.map -class InMemorySessionStore : SessionStore { - private val sessionDataListFlow = MutableStateFlow>(emptyList()) +class InMemorySessionStore( + initialList: List = emptyList(), +) : SessionStore { + private val sessionDataListFlow = MutableStateFlow(initialList) override fun isLoggedIn(): Flow { return sessionDataListFlow.map {