Add test on wasLastSession value.

This commit is contained in:
Benoit Marty
2025-10-23 16:37:16 +02:00
committed by Benoit Marty
parent 2d1b4b1ede
commit b8c7e16c50
3 changed files with 30 additions and 6 deletions

View File

@@ -10,8 +10,10 @@ package io.element.android.libraries.sessionstorage.impl
import io.element.android.libraries.matrix.session.SessionData
import io.element.android.libraries.sessionstorage.api.LoginType
internal fun aDbSessionData() = SessionData(
userId = "userId",
internal fun aDbSessionData(
userId: String = "userId",
) = SessionData(
userId = userId,
deviceId = "deviceId",
accessToken = "accessToken",
refreshToken = "refreshToken",

View File

@@ -23,7 +23,8 @@ import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
@OptIn(ExperimentalCoroutinesApi::class) class DefaultSessionObserverTest {
@OptIn(ExperimentalCoroutinesApi::class)
class DefaultSessionObserverTest {
private lateinit var database: SessionDatabase
private lateinit var databaseSessionStore: DatabaseSessionStore
@@ -69,7 +70,28 @@ import org.junit.Test
databaseSessionStore.removeSession(sessionData.userId)
listener.assertEvents(
TestSessionListener.Event.Created(sessionData.userId),
TestSessionListener.Event.Deleted(sessionData.userId),
TestSessionListener.Event.Deleted(sessionData.userId, true),
)
coroutineContext.cancelChildren()
}
@Test
fun `adding and deleting data twice invokes onSessionCreated and onSessionDeleted`() = runTest {
val sessionData1 = aDbSessionData(userId = "user1")
val sessionData2 = aDbSessionData(userId = "user2")
val sut = createDefaultSessionObserver()
runCurrent()
val listener = TestSessionListener()
sut.addListener(listener)
databaseSessionStore.addSession(sessionData1.toApiModel())
databaseSessionStore.addSession(sessionData2.toApiModel())
databaseSessionStore.removeSession(sessionData2.userId)
databaseSessionStore.removeSession(sessionData1.userId)
listener.assertEvents(
TestSessionListener.Event.Created(sessionData1.userId),
TestSessionListener.Event.Created(sessionData2.userId),
TestSessionListener.Event.Deleted(sessionData2.userId, wasLastSession = false),
TestSessionListener.Event.Deleted(sessionData1.userId, wasLastSession = true),
)
coroutineContext.cancelChildren()
}

View File

@@ -13,7 +13,7 @@ import io.element.android.libraries.sessionstorage.api.observer.SessionListener
class TestSessionListener : SessionListener {
sealed interface Event {
data class Created(val userId: String) : Event
data class Deleted(val userId: String) : Event
data class Deleted(val userId: String, val wasLastSession: Boolean) : Event
}
private val trackRecord: MutableList<Event> = mutableListOf()
@@ -23,7 +23,7 @@ class TestSessionListener : SessionListener {
}
override suspend fun onSessionDeleted(userId: String, wasLastSession: Boolean) {
trackRecord.add(Event.Deleted(userId))
trackRecord.add(Event.Deleted(userId, wasLastSession))
}
fun assertEvents(vararg events: Event) {