Skip to content

Commit fb9aaad

Browse files
committed
Add unit test on takeCurrentUserWithNeighbors
1 parent 279187f commit fb9aaad

File tree

2 files changed

+215
-1
lines changed

2 files changed

+215
-1
lines changed

features/home/impl/src/main/kotlin/io/element/android/features/home/impl/HomePresenter.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package io.element.android.features.home.impl
99

10+
import androidx.annotation.VisibleForTesting
1011
import androidx.compose.runtime.Composable
1112
import androidx.compose.runtime.LaunchedEffect
1213
import androidx.compose.runtime.collectAsState
@@ -115,7 +116,8 @@ class HomePresenter(
115116
}
116117
}
117118

118-
private fun List<SessionData>.takeCurrentUserWithNeighbors(matrixUser: MatrixUser): List<MatrixUser> {
119+
@VisibleForTesting
120+
internal fun List<SessionData>.takeCurrentUserWithNeighbors(matrixUser: MatrixUser): List<MatrixUser> {
119121
// Sort by position to always have the same order (not depending on last account usage)
120122
return sortedBy { it.position }
121123
.map {
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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.features.home.impl
9+
10+
import com.google.common.truth.Truth.assertThat
11+
import io.element.android.libraries.matrix.api.user.MatrixUser
12+
import io.element.android.libraries.matrix.test.A_USER_ID
13+
import io.element.android.libraries.matrix.test.A_USER_ID_2
14+
import io.element.android.libraries.matrix.test.A_USER_ID_3
15+
import io.element.android.libraries.matrix.ui.components.aMatrixUser
16+
import io.element.android.libraries.sessionstorage.api.SessionData
17+
import io.element.android.libraries.sessionstorage.test.aSessionData
18+
import org.junit.Test
19+
20+
class AccountNeighborsTest {
21+
@Test
22+
fun `takeCurrentUserWithNeighbors on empty list returns current user`() {
23+
val matrixUser = aMatrixUser()
24+
val list = listOf<SessionData>()
25+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
26+
assertThat(result).containsExactly(matrixUser)
27+
}
28+
29+
@Test
30+
fun `ensure that account are sorted by position`() {
31+
val matrixUser = aMatrixUser(id = A_USER_ID.value)
32+
val list = listOf(
33+
aSessionData(
34+
sessionId = A_USER_ID.value,
35+
position = 3,
36+
),
37+
aSessionData(
38+
sessionId = A_USER_ID_2.value,
39+
position = 2,
40+
),
41+
aSessionData(
42+
sessionId = A_USER_ID_3.value,
43+
position = 1,
44+
),
45+
)
46+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
47+
assertThat(result.map { it.userId }).containsExactly(
48+
A_USER_ID_3,
49+
A_USER_ID_2,
50+
A_USER_ID,
51+
)
52+
}
53+
54+
@Test
55+
fun `if current user is not found, return a singleton with current user`() {
56+
val matrixUser = aMatrixUser(id = A_USER_ID.value)
57+
val list = listOf(
58+
aSessionData(
59+
sessionId = A_USER_ID_2.value,
60+
),
61+
aSessionData(
62+
sessionId = A_USER_ID_3.value,
63+
),
64+
)
65+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
66+
assertThat(result.map { it.userId }).containsExactly(
67+
A_USER_ID,
68+
)
69+
}
70+
71+
@Test
72+
fun `one account, will return a singleton`() {
73+
val matrixUser = aMatrixUser(id = A_USER_ID.value)
74+
val list = listOf(
75+
aSessionData(
76+
sessionId = A_USER_ID.value,
77+
),
78+
)
79+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
80+
assertThat(result.map { it.userId }).containsExactly(
81+
A_USER_ID,
82+
)
83+
}
84+
85+
@Test
86+
fun `two accounts, first is current, will return 3 items`() {
87+
val matrixUser = aMatrixUser(id = A_USER_ID.value)
88+
val list = listOf(
89+
aSessionData(
90+
sessionId = A_USER_ID.value,
91+
),
92+
aSessionData(
93+
sessionId = A_USER_ID_2.value,
94+
),
95+
)
96+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
97+
assertThat(result.map { it.userId }).containsExactly(
98+
A_USER_ID_2,
99+
A_USER_ID,
100+
A_USER_ID_2,
101+
)
102+
}
103+
104+
@Test
105+
fun `two accounts, second is current, will return 3 items`() {
106+
val matrixUser = aMatrixUser(id = A_USER_ID_2.value)
107+
val list = listOf(
108+
aSessionData(
109+
sessionId = A_USER_ID.value,
110+
),
111+
aSessionData(
112+
sessionId = A_USER_ID_2.value,
113+
),
114+
)
115+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
116+
assertThat(result.map { it.userId }).containsExactly(
117+
A_USER_ID,
118+
A_USER_ID_2,
119+
A_USER_ID,
120+
)
121+
}
122+
123+
@Test
124+
fun `three accounts, first is current, will return last current and next`() {
125+
val matrixUser = aMatrixUser(id = A_USER_ID.value)
126+
val list = listOf(
127+
aSessionData(
128+
sessionId = A_USER_ID.value,
129+
),
130+
aSessionData(
131+
sessionId = A_USER_ID_2.value,
132+
),
133+
aSessionData(
134+
sessionId = A_USER_ID_3.value,
135+
),
136+
)
137+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
138+
assertThat(result.map { it.userId }).containsExactly(
139+
A_USER_ID_3,
140+
A_USER_ID,
141+
A_USER_ID_2,
142+
)
143+
}
144+
145+
@Test
146+
fun `three accounts, second is current, will return first current and last`() {
147+
val matrixUser = aMatrixUser(id = A_USER_ID_2.value)
148+
val list = listOf(
149+
aSessionData(
150+
sessionId = A_USER_ID.value,
151+
),
152+
aSessionData(
153+
sessionId = A_USER_ID_2.value,
154+
),
155+
aSessionData(
156+
sessionId = A_USER_ID_3.value,
157+
),
158+
)
159+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
160+
assertThat(result.map { it.userId }).containsExactly(
161+
A_USER_ID,
162+
A_USER_ID_2,
163+
A_USER_ID_3,
164+
)
165+
}
166+
167+
@Test
168+
fun `three accounts, current is last, will return middle, current and first`() {
169+
val matrixUser = aMatrixUser(id = A_USER_ID_3.value)
170+
val list = listOf(
171+
aSessionData(
172+
sessionId = A_USER_ID_2.value,
173+
),
174+
aSessionData(
175+
sessionId = A_USER_ID_3.value,
176+
),
177+
aSessionData(
178+
sessionId = A_USER_ID.value,
179+
),
180+
)
181+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
182+
assertThat(result.map { it.userId }).containsExactly(
183+
A_USER_ID,
184+
A_USER_ID_2,
185+
A_USER_ID_3,
186+
)
187+
}
188+
189+
@Test
190+
fun `one account, will return data from matrix user and not from db`() {
191+
val matrixUser = aMatrixUser(
192+
id = A_USER_ID.value,
193+
displayName = "Bob",
194+
avatarUrl = "avatarUrl",
195+
)
196+
val list = listOf(
197+
aSessionData(
198+
sessionId = A_USER_ID.value,
199+
userDisplayName = "Outdated Bob",
200+
userAvatarUrl = "outdatedAvatarUrl",
201+
),
202+
)
203+
val result = list.takeCurrentUserWithNeighbors(matrixUser)
204+
assertThat(result).containsExactly(
205+
MatrixUser(
206+
userId = A_USER_ID,
207+
displayName = "Bob",
208+
avatarUrl = "avatarUrl",
209+
)
210+
)
211+
}
212+
}

0 commit comments

Comments
 (0)