From 68674aeaabd704669f00c1caf710c791c177a38a Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 2 Nov 2022 14:42:35 +0000 Subject: [PATCH] update tests to reflect muting --- .../testFixtures/kotlin/fixture/Fixtures.kt | 5 +++-- .../dapk/st/directory/DirectoryReducerTest.kt | 3 +-- .../dapk/st/messenger/MessengerReducerTest.kt | 18 ++---------------- .../kotlin/app/dapk/st/engine/MatrixFactory.kt | 12 +++++++----- .../app/dapk/st/engine/TimelineUseCase.kt | 2 +- ...erveUnreadRenderNotificationsUseCaseTest.kt | 10 +++++----- .../app/dapk/st/engine/TimelineUseCaseTest.kt | 9 +++++++-- .../app/dapk/st/matrix/room/RoomService.kt | 2 +- .../matrix/room/internal/DefaultRoomService.kt | 2 +- .../testFixtures/kotlin/fake/FakeRoomStore.kt | 4 ++++ .../src/test/kotlin/test/TestMatrix.kt | 13 ++++++++++++- 11 files changed, 44 insertions(+), 36 deletions(-) diff --git a/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt b/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt index 829902e..b36c6be 100644 --- a/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt +++ b/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt @@ -6,8 +6,9 @@ import app.dapk.st.matrix.common.* fun aMessengerState( self: UserId = aUserId(), roomState: RoomState, - typing: Typing? = null -) = MessengerPageState(self, roomState, typing) + typing: Typing? = null, + isMuted: Boolean = false, +) = MessengerPageState(self, roomState, typing, isMuted) fun aRoomOverview( roomId: RoomId = aRoomId(), diff --git a/features/directory/src/test/kotlin/app/dapk/st/directory/DirectoryReducerTest.kt b/features/directory/src/test/kotlin/app/dapk/st/directory/DirectoryReducerTest.kt index 78767a2..ca04e8d 100644 --- a/features/directory/src/test/kotlin/app/dapk/st/directory/DirectoryReducerTest.kt +++ b/features/directory/src/test/kotlin/app/dapk/st/directory/DirectoryReducerTest.kt @@ -2,7 +2,6 @@ package app.dapk.st.directory import app.dapk.st.core.JobBag import app.dapk.st.directory.state.* -import app.dapk.st.engine.DirectoryItem import app.dapk.st.engine.UnreadCount import fake.FakeChatEngine import fixture.aRoomOverview @@ -13,7 +12,7 @@ import test.expect import test.testReducer private val AN_OVERVIEW = aRoomOverview() -private val AN_OVERVIEW_STATE = DirectoryItem(AN_OVERVIEW, UnreadCount(1), null) +private val AN_OVERVIEW_STATE = app.dapk.st.engine.DirectoryItem(AN_OVERVIEW, UnreadCount(1), null, isMuted = false) class DirectoryReducerTest { diff --git a/features/messenger/src/test/kotlin/app/dapk/st/messenger/MessengerReducerTest.kt b/features/messenger/src/test/kotlin/app/dapk/st/messenger/MessengerReducerTest.kt index efcd8f3..5e1e760 100644 --- a/features/messenger/src/test/kotlin/app/dapk/st/messenger/MessengerReducerTest.kt +++ b/features/messenger/src/test/kotlin/app/dapk/st/messenger/MessengerReducerTest.kt @@ -3,12 +3,10 @@ package app.dapk.st.messenger import android.os.Build import app.dapk.st.core.* import app.dapk.st.design.components.BubbleModel -import app.dapk.st.domain.room.MutedRoomsStore import app.dapk.st.engine.RoomEvent import app.dapk.st.engine.RoomState import app.dapk.st.engine.SendMessage import app.dapk.st.matrix.common.EventId -import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.common.UserId import app.dapk.st.matrix.common.asString import app.dapk.st.messenger.state.* @@ -16,7 +14,6 @@ import app.dapk.st.navigator.MessageAttachment import fake.FakeChatEngine import fake.FakeMessageOptionsStore import fixture.* -import io.mockk.coEvery import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.flow.flowOf @@ -52,7 +49,6 @@ class MessengerReducerTest { private val fakeChatEngine = FakeChatEngine() private val fakeCopyToClipboard = FakeCopyToClipboard() private val fakeDeviceMeta = FakeDeviceMeta() - private val fakeMutedRoomsStore = FakeMutedRoomsStore() private val fakeJobBag = FakeJobBag() private val runReducerTest = testReducer { fakeEventSource -> @@ -62,7 +58,6 @@ class MessengerReducerTest { fakeCopyToClipboard.instance, fakeDeviceMeta.instance, fakeMessageOptionsStore.instance, - fakeMutedRoomsStore, A_ROOM_ID, emptyList(), fakeEventSource, @@ -77,7 +72,6 @@ class MessengerReducerTest { roomState = Lce.Loading(), composerState = ComposerState.Text(value = "", reply = null), viewerState = null, - isMuted = false, ) ) } @@ -90,7 +84,6 @@ class MessengerReducerTest { roomState = Lce.Loading(), composerState = ComposerState.Text(value = "", reply = null), viewerState = null, - isMuted = false, ) ) } @@ -103,14 +96,12 @@ class MessengerReducerTest { roomState = Lce.Loading(), composerState = ComposerState.Attachments(listOf(A_MESSAGE_ATTACHMENT), reply = null), viewerState = null, - isMuted = false, ) ) } @Test - fun `given room is muted and messages emits state, when Visible, then dispatches content and mute changes`() = runReducerTest { - fakeMutedRoomsStore.givenIsMuted(A_ROOM_ID).returns(ROOM_IS_MUTED) + fun `given messages emits state, when Visible, then dispatches content`() = runReducerTest { fakeJobBag.instance.expect { it.replace("messages", any()) } fakeMessageOptionsStore.givenReadReceiptsDisabled().returns(READ_RECEIPTS_ARE_DISABLED) val state = aMessengerStateWithEvent(AN_EVENT_ID, A_SELF_ID) @@ -118,7 +109,7 @@ class MessengerReducerTest { reduce(ComponentLifecycle.Visible) - assertOnlyDispatches(listOf(MessagesStateChange.MuteContent(isMuted = ROOM_IS_MUTED), MessagesStateChange.Content(state))) + assertOnlyDispatches(listOf(MessagesStateChange.Content(state))) } @Test @@ -340,7 +331,6 @@ class MessengerReducerTest { fakeCopyToClipboard.instance, fakeDeviceMeta.instance, fakeMessageOptionsStore.instance, - FakeMutedRoomsStore(), A_ROOM_ID, initialAttachments = initialAttachments, fakeEventSource, @@ -371,7 +361,3 @@ class FakeDeviceMeta { fun givenApiVersion() = every { instance.apiVersion }.delegateReturn() } - -class FakeMutedRoomsStore : MutedRoomsStore by mockk() { - fun givenIsMuted(roomId: RoomId) = coEvery { isMuted(roomId) }.delegateReturn() -} diff --git a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixFactory.kt b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixFactory.kt index 63b00cd..a2eb715 100644 --- a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixFactory.kt +++ b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixFactory.kt @@ -146,11 +146,7 @@ internal object MatrixFactory { roomInviteRemover = { overviewStore.removeInvites(listOf(it)) }, - singleRoomStore = object : SingleRoomStore { - override suspend fun mute(roomId: RoomId) = roomStore.mute(roomId) - override suspend fun unmute(roomId: RoomId) = roomStore.unmute(roomId) - override fun isMuted(roomId: RoomId): Flow = roomStore.observeMuted().map { it.contains(roomId) }.distinctUntilChanged() - } + singleRoomStore = singleRoomStoreAdapter(roomStore) ) installProfileService(profileStore, singletonFlows, credentialsStore) @@ -253,4 +249,10 @@ internal object MatrixFactory { } } + private fun singleRoomStoreAdapter(roomStore: RoomStore) = object : SingleRoomStore { + override suspend fun mute(roomId: RoomId) = roomStore.mute(roomId) + override suspend fun unmute(roomId: RoomId) = roomStore.unmute(roomId) + override fun isMuted(roomId: RoomId): Flow = roomStore.observeMuted().map { it.contains(roomId) }.distinctUntilChanged() + } + } \ No newline at end of file diff --git a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/TimelineUseCase.kt b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/TimelineUseCase.kt index f8f7048..1f7f930 100644 --- a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/TimelineUseCase.kt +++ b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/TimelineUseCase.kt @@ -24,7 +24,7 @@ internal class TimelineUseCaseImpl( roomDatasource(roomId), messageService.localEchos(roomId), syncService.events(roomId), - roomService.observeIssMuted(roomId), + roomService.observeIsMuted(roomId), ) { roomState, localEchos, events, isMuted -> MessengerPageState( roomState = when { diff --git a/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/ObserveUnreadRenderNotificationsUseCaseTest.kt b/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/ObserveUnreadRenderNotificationsUseCaseTest.kt index 075de8e..7cb1de1 100644 --- a/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/ObserveUnreadRenderNotificationsUseCaseTest.kt +++ b/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/ObserveUnreadRenderNotificationsUseCaseTest.kt @@ -61,7 +61,7 @@ class ObserveUnreadRenderNotificationsUseCaseTest { @Test fun `given initial unreads, when receiving new message, then emits all messages`() = runTest { - fakeRoomStore.givenUnreadEvents( + fakeRoomStore.givenNotMutedUnreadEvents( flowOf(A_ROOM_OVERVIEW.withUnreads(A_MESSAGE), A_ROOM_OVERVIEW.withUnreads(A_MESSAGE, A_MESSAGE_2)) ) @@ -74,7 +74,7 @@ class ObserveUnreadRenderNotificationsUseCaseTest { @Test fun `given initial unreads, when reading a message, then emits nothing`() = runTest { - fakeRoomStore.givenUnreadEvents( + fakeRoomStore.givenNotMutedUnreadEvents( flowOf(A_ROOM_OVERVIEW.withUnreads(A_MESSAGE) + A_ROOM_OVERVIEW_2.withUnreads(A_MESSAGE_2), A_ROOM_OVERVIEW.withUnreads(A_MESSAGE)) ) @@ -85,7 +85,7 @@ class ObserveUnreadRenderNotificationsUseCaseTest { @Test fun `given new and then historical message, when reading a message, then only emits the latest`() = runTest { - fakeRoomStore.givenUnreadEvents( + fakeRoomStore.givenNotMutedUnreadEvents( flowOf( NO_UNREADS, A_ROOM_OVERVIEW.withUnreads(A_MESSAGE), @@ -105,7 +105,7 @@ class ObserveUnreadRenderNotificationsUseCaseTest { @Test fun `given initial unreads, when reading a duplicate unread, then emits nothing`() = runTest { - fakeRoomStore.givenUnreadEvents( + fakeRoomStore.givenNotMutedUnreadEvents( flowOf(A_ROOM_OVERVIEW.withUnreads(A_MESSAGE), A_ROOM_OVERVIEW.withUnreads(A_MESSAGE)) ) @@ -115,7 +115,7 @@ class ObserveUnreadRenderNotificationsUseCaseTest { } private fun givenNoInitialUnreads(vararg unreads: Map>) = - fakeRoomStore.givenUnreadEvents(flowOf(NO_UNREADS, *unreads)) + fakeRoomStore.givenNotMutedUnreadEvents(flowOf(NO_UNREADS, *unreads)) } private fun Map>.engine() = this diff --git a/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/TimelineUseCaseTest.kt b/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/TimelineUseCaseTest.kt index 9775a0d..2c553fe 100644 --- a/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/TimelineUseCaseTest.kt +++ b/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/TimelineUseCaseTest.kt @@ -22,6 +22,7 @@ import org.junit.Test import test.FlowTestObserver import test.delegateReturn +private const val IS_ROOM_MUTED = false private val A_ROOM_ID = aRoomId() private val AN_USER_ID = aUserId() private val A_ROOM_STATE = aMatrixRoomState() @@ -63,6 +64,7 @@ class TimelineUseCaseTest { fakeMergeWithLocalEchosUseCase.givenMerging(A_ROOM_STATE, A_ROOM_MEMBER, A_LOCAL_ECHOS_LIST).returns(A_MERGED_ROOM_STATE.engine()) + timelineUseCase.invoke(A_ROOM_ID, AN_USER_ID) .test(this) .assertValues( @@ -103,6 +105,7 @@ class TimelineUseCaseTest { fakeSyncService.givenRoom(A_ROOM_ID).returns(flowOf(roomState)) fakeMessageService.givenEchos(A_ROOM_ID).returns(flowOf(echos)) fakeSyncService.givenEvents(A_ROOM_ID).returns(flowOf(events)) + fakeRoomService.givenMuted(A_ROOM_ID).returns(flowOf(IS_ROOM_MUTED)) } } @@ -129,10 +132,12 @@ class FakeMessageService : MessageService by mockk() { class FakeRoomService : RoomService by mockk() { fun givenFindMember(roomId: RoomId, userId: UserId) = coEvery { findMember(roomId, userId) }.delegateReturn() + fun givenMuted(roomId: RoomId) = every { observeIsMuted(roomId) }.delegateReturn() } fun aMessengerState( self: UserId = aUserId(), roomState: app.dapk.st.engine.RoomState, - typing: Typing? = null -) = MessengerPageState(self, roomState, typing) \ No newline at end of file + typing: Typing? = null, + isMuted: Boolean = IS_ROOM_MUTED, +) = MessengerPageState(self, roomState, typing, isMuted) \ No newline at end of file diff --git a/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/RoomService.kt b/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/RoomService.kt index 1109d6b..1da2df3 100644 --- a/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/RoomService.kt +++ b/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/RoomService.kt @@ -27,7 +27,7 @@ interface RoomService : MatrixService { suspend fun muteRoom(roomId: RoomId) suspend fun unmuteRoom(roomId: RoomId) - fun observeIssMuted(roomId: RoomId): Flow + fun observeIsMuted(roomId: RoomId): Flow data class JoinedMember( val userId: UserId, diff --git a/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/internal/DefaultRoomService.kt b/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/internal/DefaultRoomService.kt index e641b20..b5b4472 100644 --- a/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/internal/DefaultRoomService.kt +++ b/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/internal/DefaultRoomService.kt @@ -102,7 +102,7 @@ class DefaultRoomService( singleRoomStore.unmute(roomId) } - override fun observeIssMuted(roomId: RoomId): Flow = singleRoomStore.isMuted(roomId) + override fun observeIsMuted(roomId: RoomId): Flow = singleRoomStore.isMuted(roomId) } interface SingleRoomStore { diff --git a/matrix/services/sync/src/testFixtures/kotlin/fake/FakeRoomStore.kt b/matrix/services/sync/src/testFixtures/kotlin/fake/FakeRoomStore.kt index 7719a53..3b1cbb2 100644 --- a/matrix/services/sync/src/testFixtures/kotlin/fake/FakeRoomStore.kt +++ b/matrix/services/sync/src/testFixtures/kotlin/fake/FakeRoomStore.kt @@ -34,4 +34,8 @@ class FakeRoomStore : RoomStore by mockk() { every { observeUnread() } returns unreadEvents } + fun givenNotMutedUnreadEvents(unreadEvents: Flow>>) { + every { observeNotMutedUnread() } returns unreadEvents + } + } \ No newline at end of file diff --git a/test-harness/src/test/kotlin/test/TestMatrix.kt b/test-harness/src/test/kotlin/test/TestMatrix.kt index fce9c4f..088340d 100644 --- a/test-harness/src/test/kotlin/test/TestMatrix.kt +++ b/test-harness/src/test/kotlin/test/TestMatrix.kt @@ -23,6 +23,7 @@ import app.dapk.st.matrix.message.internal.ImageContentReader import app.dapk.st.matrix.push.installPushService import app.dapk.st.matrix.room.RoomMessenger import app.dapk.st.matrix.room.installRoomService +import app.dapk.st.matrix.room.internal.SingleRoomStore import app.dapk.st.matrix.room.roomService import app.dapk.st.matrix.sync.* import app.dapk.st.matrix.sync.internal.request.ApiToDeviceEvent @@ -31,6 +32,9 @@ import app.dapk.st.olm.DeviceKeyFactory import app.dapk.st.olm.OlmPersistenceWrapper import app.dapk.st.olm.OlmWrapper import kotlinx.coroutines.* +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.map import kotlinx.serialization.json.Json import org.amshove.kluent.fail import test.impl.InMemoryDatabase @@ -178,7 +182,8 @@ class TestMatrix( } } }, - roomInviteRemover = { storeModule.overviewStore().removeInvites(listOf(it)) } + roomInviteRemover = { storeModule.overviewStore().removeInvites(listOf(it)) }, + singleRoomStore = singleRoomStoreAdapter(storeModule.roomStore()) ) installSyncService( @@ -378,4 +383,10 @@ class ProxyDeviceService(private val deviceService: DeviceService) : DeviceServi } +private fun singleRoomStoreAdapter(roomStore: RoomStore) = object : SingleRoomStore { + override suspend fun mute(roomId: RoomId) = roomStore.mute(roomId) + override suspend fun unmute(roomId: RoomId) = roomStore.unmute(roomId) + override fun isMuted(roomId: RoomId): Flow = roomStore.observeMuted().map { it.contains(roomId) }.distinctUntilChanged() +} + fun MatrixClient.proxyDeviceService() = this.deviceService() as ProxyDeviceService \ No newline at end of file