Skip to content

Commit 4869c0b

Browse files
committed
Add UT on SpaceListUpdateProcessor
1 parent 697652a commit 4869c0b

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
package io.element.android.libraries.matrix.impl.fixtures.factories
9+
10+
import io.element.android.libraries.matrix.api.core.RoomId
11+
import io.element.android.libraries.matrix.test.A_ROOM_ID
12+
import org.matrix.rustcomponents.sdk.JoinRule
13+
import org.matrix.rustcomponents.sdk.Membership
14+
import org.matrix.rustcomponents.sdk.RoomHero
15+
import org.matrix.rustcomponents.sdk.RoomType
16+
import org.matrix.rustcomponents.sdk.SpaceRoom
17+
18+
fun aRustSpaceRoom(
19+
roomId: RoomId = A_ROOM_ID,
20+
canonicalAlias: String? = null,
21+
name: String? = null,
22+
topic: String? = null,
23+
avatarUrl: String? = null,
24+
roomType: RoomType = RoomType.Space,
25+
numJoinedMembers: ULong = 0uL,
26+
joinRule: JoinRule? = null,
27+
worldReadable: Boolean? = null,
28+
guestCanJoin: Boolean = false,
29+
childrenCount: ULong = 0uL,
30+
state: Membership? = null,
31+
heroes: List<RoomHero> = emptyList(),
32+
) = SpaceRoom(
33+
roomId = roomId.value,
34+
canonicalAlias = canonicalAlias,
35+
name = name,
36+
topic = topic,
37+
avatarUrl = avatarUrl,
38+
roomType = roomType,
39+
numJoinedMembers = numJoinedMembers,
40+
joinRule = joinRule,
41+
worldReadable = worldReadable,
42+
guestCanJoin = guestCanJoin,
43+
childrenCount = childrenCount,
44+
state = state,
45+
heroes = heroes,
46+
)
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
package io.element.android.libraries.matrix.impl.spaces
9+
10+
import com.google.common.truth.Truth.assertThat
11+
import io.element.android.libraries.matrix.api.spaces.SpaceRoom
12+
import io.element.android.libraries.matrix.impl.fixtures.factories.aRustSpaceRoom
13+
import io.element.android.libraries.matrix.test.A_ROOM_ID
14+
import io.element.android.libraries.matrix.test.A_ROOM_ID_2
15+
import io.element.android.libraries.matrix.test.A_ROOM_ID_3
16+
import io.element.android.libraries.previewutils.room.aSpaceRoom
17+
import kotlinx.coroutines.flow.MutableSharedFlow
18+
import kotlinx.coroutines.flow.MutableStateFlow
19+
import kotlinx.coroutines.test.runTest
20+
import org.junit.Test
21+
import org.matrix.rustcomponents.sdk.SpaceListUpdate
22+
23+
class RoomSummaryListProcessorTest {
24+
private val spaceRoomsFlow = MutableStateFlow<List<SpaceRoom>>(emptyList())
25+
26+
@Test
27+
fun `Append adds new entries at the end of the list`() = runTest {
28+
spaceRoomsFlow.value = listOf(aSpaceRoom())
29+
val processor = createProcessor()
30+
31+
val newEntry = aRustSpaceRoom(roomId = A_ROOM_ID_2)
32+
processor.postUpdates(listOf(SpaceListUpdate.Append(listOf(newEntry, newEntry, newEntry))))
33+
34+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(4)
35+
assertThat(spaceRoomsFlow.value.subList(1, 4).all { it.roomId == A_ROOM_ID_2 }).isTrue()
36+
}
37+
38+
@Test
39+
fun `PushBack adds a new entry at the end of the list`() = runTest {
40+
spaceRoomsFlow.value = listOf(aSpaceRoom())
41+
val processor = createProcessor()
42+
processor.postUpdates(listOf(SpaceListUpdate.PushBack(aRustSpaceRoom(roomId = A_ROOM_ID_2))))
43+
44+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(2)
45+
assertThat(spaceRoomsFlow.value.last().roomId).isEqualTo(A_ROOM_ID_2)
46+
}
47+
48+
@Test
49+
fun `PushFront inserts a new entry at the start of the list`() = runTest {
50+
spaceRoomsFlow.value = listOf(aSpaceRoom())
51+
val processor = createProcessor()
52+
processor.postUpdates(listOf(SpaceListUpdate.PushFront(aRustSpaceRoom(roomId = A_ROOM_ID_2))))
53+
54+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(2)
55+
assertThat(spaceRoomsFlow.value.first().roomId).isEqualTo(A_ROOM_ID_2)
56+
}
57+
58+
@Test
59+
fun `Set replaces an entry at some index`() = runTest {
60+
spaceRoomsFlow.value = listOf(aSpaceRoom())
61+
val processor = createProcessor()
62+
val index = 0
63+
64+
processor.postUpdates(listOf(SpaceListUpdate.Set(index.toUInt(), aRustSpaceRoom(roomId = A_ROOM_ID_2))))
65+
66+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(1)
67+
assertThat(spaceRoomsFlow.value[index].roomId).isEqualTo(A_ROOM_ID_2)
68+
}
69+
70+
@Test
71+
fun `Insert inserts a new entry at the provided index`() = runTest {
72+
spaceRoomsFlow.value = listOf(aSpaceRoom())
73+
val processor = createProcessor()
74+
val index = 0
75+
76+
processor.postUpdates(listOf(SpaceListUpdate.Insert(index.toUInt(), aRustSpaceRoom(roomId = A_ROOM_ID_2))))
77+
78+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(2)
79+
assertThat(spaceRoomsFlow.value[index].roomId).isEqualTo(A_ROOM_ID_2)
80+
}
81+
82+
@Test
83+
fun `Remove removes an entry at some index`() = runTest {
84+
spaceRoomsFlow.value = listOf(
85+
aSpaceRoom(roomId = A_ROOM_ID),
86+
aSpaceRoom(roomId = A_ROOM_ID_2)
87+
)
88+
val processor = createProcessor()
89+
val index = 0
90+
91+
processor.postUpdates(listOf(SpaceListUpdate.Remove(index.toUInt())))
92+
93+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(1)
94+
assertThat(spaceRoomsFlow.value[index].roomId).isEqualTo(A_ROOM_ID_2)
95+
}
96+
97+
@Test
98+
fun `PopBack removes an entry at the end of the list`() = runTest {
99+
spaceRoomsFlow.value = listOf(
100+
aSpaceRoom(roomId = A_ROOM_ID),
101+
aSpaceRoom(roomId = A_ROOM_ID_2)
102+
)
103+
val processor = createProcessor()
104+
val index = 0
105+
106+
processor.postUpdates(listOf(SpaceListUpdate.PopBack))
107+
108+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(1)
109+
assertThat(spaceRoomsFlow.value[index].roomId).isEqualTo(A_ROOM_ID)
110+
}
111+
112+
@Test
113+
fun `PopFront removes an entry at the start of the list`() = runTest {
114+
spaceRoomsFlow.value = listOf(
115+
aSpaceRoom(roomId = A_ROOM_ID),
116+
aSpaceRoom(roomId = A_ROOM_ID_2)
117+
)
118+
val processor = createProcessor()
119+
val index = 0
120+
121+
processor.postUpdates(listOf(SpaceListUpdate.PopFront))
122+
123+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(1)
124+
assertThat(spaceRoomsFlow.value[index].roomId).isEqualTo(A_ROOM_ID_2)
125+
}
126+
127+
@Test
128+
fun `Clear removes all the entries`() = runTest {
129+
spaceRoomsFlow.value = listOf(
130+
aSpaceRoom(roomId = A_ROOM_ID),
131+
aSpaceRoom(roomId = A_ROOM_ID_2)
132+
)
133+
val processor = createProcessor()
134+
135+
processor.postUpdates(listOf(SpaceListUpdate.Clear))
136+
137+
assertThat(spaceRoomsFlow.value).isEmpty()
138+
}
139+
140+
@Test
141+
fun `Truncate removes all entries after the provided length`() = runTest {
142+
spaceRoomsFlow.value = listOf(
143+
aSpaceRoom(roomId = A_ROOM_ID),
144+
aSpaceRoom(roomId = A_ROOM_ID_2)
145+
)
146+
val processor = createProcessor()
147+
val index = 0
148+
149+
processor.postUpdates(listOf(SpaceListUpdate.Truncate(1u)))
150+
151+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(1)
152+
assertThat(spaceRoomsFlow.value[index].roomId).isEqualTo(A_ROOM_ID)
153+
}
154+
155+
@Test
156+
fun `Reset removes all entries and add the provided ones`() = runTest {
157+
spaceRoomsFlow.value = listOf(
158+
aSpaceRoom(roomId = A_ROOM_ID),
159+
aSpaceRoom(roomId = A_ROOM_ID_2)
160+
)
161+
val processor = createProcessor()
162+
val index = 0
163+
164+
processor.postUpdates(listOf(SpaceListUpdate.Reset(listOf(aRustSpaceRoom(A_ROOM_ID_3)))))
165+
166+
assertThat(spaceRoomsFlow.value.count()).isEqualTo(1)
167+
assertThat(spaceRoomsFlow.value[index].roomId).isEqualTo(A_ROOM_ID_3)
168+
}
169+
170+
@Test
171+
fun `When there is no replay cache SpaceListUpdateProcessor starts with an empty list`() = runTest {
172+
val spaceRoomsSharedFlow = MutableSharedFlow<List<SpaceRoom>>(replay = 1)
173+
val processor = createProcessor(
174+
spaceRoomsFlow = spaceRoomsSharedFlow,
175+
)
176+
assertThat(spaceRoomsSharedFlow.replayCache).isEmpty()
177+
val newEntry = aRustSpaceRoom(roomId = A_ROOM_ID)
178+
processor.postUpdates(listOf(SpaceListUpdate.Append(listOf(newEntry))))
179+
assertThat(spaceRoomsSharedFlow.replayCache).hasSize(1)
180+
assertThat(spaceRoomsSharedFlow.replayCache.first()).hasSize(1)
181+
}
182+
183+
private fun createProcessor(
184+
spaceRoomsFlow: MutableSharedFlow<List<SpaceRoom>> = this.spaceRoomsFlow,
185+
) = SpaceListUpdateProcessor(
186+
spaceRoomsFlow = spaceRoomsFlow,
187+
mapper = SpaceRoomMapper(),
188+
spaceRoomCache = SpaceRoomCache(),
189+
)
190+
}

0 commit comments

Comments
 (0)