Instead of using a magic number, explicitly test for the events we expect.

This permits a clear error when the events are missing / extra and while
not making the test invulnerable to future changes in events, should be
explicit on what's changed.
This commit is contained in:
Michael Kaye 2022-05-18 11:20:21 +01:00
parent f7303789a0
commit 5a1545058b

View file

@ -140,9 +140,24 @@ class TimelineForwardPaginationTest : InstrumentedTest {
aliceTimeline.hasMoreToLoad(Timeline.Direction.BACKWARDS).shouldBeFalse()
assertEquals(EventType.STATE_ROOM_CREATE, snapshot.lastOrNull()?.root?.getClearType())
// 6 for room creation item (backward pagination), 1 for the context, and 50 for the forward pagination
// 6 + 1 + 50
assertEquals(57, snapshot.size)
// We explicitly test all the types we expect here, as we expect 51 messages and "some" state events
// But state events can change over time. So this acts as a kinda documentation of what we expect and
// provides a good error message if it doesn't match
val snapshotTypes = mutableMapOf<String?, Int>()
snapshot.groupingBy { it -> it.root.type }.eachCountTo(snapshotTypes)
// Some state events on room creation
assertEquals("m.room.name", 1, snapshotTypes.remove("m.room.name"))
assertEquals("m.room.guest_access", 1, snapshotTypes.remove("m.room.guest_access"))
assertEquals("m.room.history_visibility", 1, snapshotTypes.remove("m.room.history_visibility"))
assertEquals("m.room.join_rules", 1, snapshotTypes.remove("m.room.join_rules"))
assertEquals("m.room.power_levels", 1, snapshotTypes.remove("m.room.power_levels"))
assertEquals("m.room.create", 1, snapshotTypes.remove("m.room.create"))
assertEquals("m.room.member", 1, snapshotTypes.remove("m.room.member"))
// 50 from pagination + 1 context
assertEquals("m.room.message", 51, snapshotTypes.remove("m.room.message"))
assertEquals("Additional events found in timeline", setOf<String>(), snapshotTypes.keys)
}
// Alice paginates once again FORWARD for 50 events
@ -152,8 +167,8 @@ class TimelineForwardPaginationTest : InstrumentedTest {
val snapshot = runBlocking {
aliceTimeline.awaitPaginate(Timeline.Direction.FORWARDS, 50)
}
// 6 for room creation item (backward pagination),and numberOfMessagesToSend (all the message of the room)
snapshot.size == 6 + numberOfMessagesToSend &&
// 7 for room creation item (backward pagination),and numberOfMessagesToSend (all the message of the room)
snapshot.size == 7 + numberOfMessagesToSend &&
snapshot.checkSendOrder(message, numberOfMessagesToSend, 0)
// The timeline is fully loaded