adding test for invites use case
This commit is contained in:
parent
093ca6df3b
commit
9048f9fb0c
|
@ -64,3 +64,9 @@ fun aRoomState(
|
|||
roomOverview: RoomOverview = aRoomOverview(),
|
||||
events: List<RoomEvent> = listOf(aRoomMessageEvent()),
|
||||
) = RoomState(roomOverview, events)
|
||||
|
||||
fun aRoomInvite(
|
||||
from: RoomMember = aRoomMember(),
|
||||
roomId: RoomId = aRoomId(),
|
||||
inviteMeta: RoomInvite.InviteMeta = RoomInvite.InviteMeta.DirectMessage,
|
||||
) = RoomInvite(from, roomId, inviteMeta)
|
||||
|
|
|
@ -2,7 +2,6 @@ package app.dapk.st.engine
|
|||
|
||||
import app.dapk.st.matrix.sync.SyncService
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class InviteUseCase(
|
||||
|
@ -14,6 +13,6 @@ class InviteUseCase(
|
|||
private fun invitesDatasource() = combine(
|
||||
syncService.startSyncing(),
|
||||
syncService.invites().map { it.map { it.engine() } }
|
||||
) { _, invites -> invites }.filterNotNull()
|
||||
) { _, invites -> invites }
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@ package app.dapk.st.engine
|
|||
import app.dapk.st.core.Base64
|
||||
import app.dapk.st.core.BuildMeta
|
||||
import app.dapk.st.core.CoroutineDispatchers
|
||||
import app.dapk.st.core.JobBag
|
||||
import app.dapk.st.core.extensions.ErrorTracker
|
||||
import app.dapk.st.matrix.MatrixClient
|
||||
import app.dapk.st.matrix.MatrixTaskRunner
|
||||
|
@ -190,7 +191,16 @@ class MatrixEngine internal constructor(
|
|||
}
|
||||
|
||||
val mediaDecrypter = unsafeLazy { MatrixMediaDecrypter(base64) }
|
||||
val pushHandler = unsafeLazy { MatrixPushHandler(backgroundScheduler, credentialsStore, lazyMatrix.value.syncService(), roomStore) }
|
||||
val pushHandler = unsafeLazy {
|
||||
MatrixPushHandler(
|
||||
backgroundScheduler,
|
||||
credentialsStore,
|
||||
lazyMatrix.value.syncService(),
|
||||
roomStore,
|
||||
coroutineDispatchers,
|
||||
JobBag(),
|
||||
)
|
||||
}
|
||||
|
||||
val invitesUseCase = unsafeLazy { InviteUseCase(lazyMatrix.value.syncService()) }
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package app.dapk.st.engine
|
||||
|
||||
import app.dapk.st.core.AppLogTag
|
||||
import app.dapk.st.core.CoroutineDispatchers
|
||||
import app.dapk.st.core.JobBag
|
||||
import app.dapk.st.core.log
|
||||
import app.dapk.st.matrix.common.CredentialsStore
|
||||
import app.dapk.st.matrix.common.EventId
|
||||
|
@ -9,17 +11,20 @@ import app.dapk.st.matrix.common.RoomId
|
|||
import app.dapk.st.matrix.message.BackgroundScheduler
|
||||
import app.dapk.st.matrix.sync.RoomStore
|
||||
import app.dapk.st.matrix.sync.SyncService
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
|
||||
private var previousJob: Job? = null
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
class MatrixPushHandler(
|
||||
private val backgroundScheduler: BackgroundScheduler,
|
||||
private val credentialsStore: CredentialsStore,
|
||||
private val syncService: SyncService,
|
||||
private val roomStore: RoomStore,
|
||||
private val dispatchers: CoroutineDispatchers,
|
||||
private val jobBag: JobBag,
|
||||
) : PushHandler {
|
||||
|
||||
override fun onNewToken(payload: JsonString) {
|
||||
|
@ -35,13 +40,12 @@ class MatrixPushHandler(
|
|||
|
||||
override fun onMessageReceived(eventId: EventId?, roomId: RoomId?) {
|
||||
log(AppLogTag.PUSH, "push received")
|
||||
previousJob?.cancel()
|
||||
previousJob = GlobalScope.launch {
|
||||
jobBag.replace(MatrixPushHandler::class, dispatchers.global.launch {
|
||||
when (credentialsStore.credentials()) {
|
||||
null -> log(AppLogTag.PUSH, "push ignored due to missing api credentials")
|
||||
else -> doSync(roomId, eventId)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private suspend fun doSync(roomId: RoomId?, eventId: EventId?) {
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package app.dapk.st.engine
|
||||
|
||||
import app.dapk.st.matrix.common.RoomId
|
||||
import app.dapk.st.matrix.common.RoomMember
|
||||
import app.dapk.st.matrix.sync.InviteMeta
|
||||
import fake.FakeSyncService
|
||||
import fixture.aRoomId
|
||||
import fixture.aRoomMember
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.Test
|
||||
import app.dapk.st.matrix.sync.RoomInvite as MatrixRoomInvite
|
||||
|
||||
class InviteUseCaseTest {
|
||||
|
||||
private val fakeSyncService = FakeSyncService()
|
||||
private val useCase = InviteUseCase(fakeSyncService)
|
||||
|
||||
@Test
|
||||
fun `reads invites from sync service and maps to engine`() = runTest {
|
||||
val aMatrixRoomInvite = aMatrixRoomInvite()
|
||||
fakeSyncService.givenStartsSyncing()
|
||||
fakeSyncService.givenInvites().returns(flowOf(listOf(aMatrixRoomInvite)))
|
||||
|
||||
val result = useCase.invites().first()
|
||||
|
||||
result shouldBeEqualTo listOf(aMatrixRoomInvite.engine())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun aMatrixRoomInvite(
|
||||
from: RoomMember = aRoomMember(),
|
||||
roomId: RoomId = aRoomId(),
|
||||
inviteMeta: InviteMeta = InviteMeta.DirectMessage,
|
||||
) = MatrixRoomInvite(from, roomId, inviteMeta)
|
||||
|
|
@ -17,4 +17,6 @@ class FakeSyncService : SyncService by mockk() {
|
|||
|
||||
fun givenEvents(roomId: RoomId) = every { events(roomId) }.delegateReturn()
|
||||
|
||||
fun givenInvites() = every { invites() }.delegateReturn()
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue