test(event cache): add a test that the event cache correctly stores updates

This commit is contained in:
Benjamin Bouvier
2024-11-28 12:46:57 +01:00
parent 9f1e3c179b
commit 019de4ffa0

View File

@@ -630,15 +630,28 @@ pub(super) use private::RoomEventCacheState;
#[cfg(test)]
mod tests {
use std::sync::Arc;
use assert_matches::assert_matches;
use assert_matches2::assert_let;
use matrix_sdk_base::{
event_cache::store::{EventCacheStore as _, MemoryStore},
linked_chunk::ChunkContent,
store::StoreConfig,
sync::{JoinedRoomUpdate, Timeline},
};
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
use matrix_sdk_test::{async_test, event_factory::EventFactory};
use matrix_sdk_test::{async_test, event_factory::EventFactory, ALICE};
use ruma::{
event_id,
events::{relation::RelationType, room::message::RoomMessageEventContentWithoutRelation},
events::{
relation::RelationType, room::message::RoomMessageEventContentWithoutRelation,
AnySyncMessageLikeEvent, AnySyncTimelineEvent,
},
room_id, user_id, RoomId,
};
use crate::test_utils::logged_in_client;
use crate::test_utils::{client::MockClientBuilder, logged_in_client};
#[async_test]
async fn test_event_with_redaction_relation() {
@@ -875,6 +888,73 @@ mod tests {
assert_eq!(related_event_id, associated_related_id);
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_write_to_storage() {
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
let event_cache_store = Arc::new(MemoryStore::new());
let client = MockClientBuilder::new("http://localhost".to_owned())
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
.build()
.await;
let event_cache = client.event_cache();
// Don't forget to subscribe and like^W enable storage!
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
// Propagate an update for a message and a prev-batch token.
let timeline = Timeline {
limited: false,
prev_batch: Some("raclette".to_owned()),
events: vec![f.text_msg("hey yo").sender(*ALICE).into_sync()],
};
room_event_cache
.inner
.handle_joined_room_update(JoinedRoomUpdate { timeline, ..Default::default() })
.await
.unwrap();
let linked_chunk = event_cache_store.reload_linked_chunk(room_id).await.unwrap().unwrap();
assert_eq!(linked_chunk.chunks().count(), 3);
let mut chunks = linked_chunk.chunks();
// Invariant: there's always an empty items chunk at the beginning.
assert_matches!(chunks.next().unwrap().content(), ChunkContent::Items(events) => {
assert_eq!(events.len(), 0)
});
// Then we have the gap.
assert_matches!(chunks.next().unwrap().content(), ChunkContent::Gap(gap) => {
assert_eq!(gap.prev_token, "raclette");
});
// Then we have the stored event.
assert_matches!(chunks.next().unwrap().content(), ChunkContent::Items(events) => {
assert_eq!(events.len(), 1);
let deserialized = events[0].raw().deserialize().unwrap();
assert_let!(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(msg)) = deserialized);
assert_eq!(msg.as_original().unwrap().content.body(), "hey yo");
});
// That's all, folks!
assert!(chunks.next().is_none());
}
async fn assert_relations(
room_id: &RoomId,
original_event: SyncTimelineEvent,