From 2ba5cba0a5bf1e5c9bfafb0dfc2c3fe395257103 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Tue, 11 Jun 2024 15:10:28 +0200 Subject: [PATCH] added tests ] --- crates/matrix-sdk-base/src/store/mod.rs | 4 +-- crates/matrix-sdk-base/src/store/traits.rs | 22 ++++++++++------ crates/matrix-sdk/src/room/mod.rs | 29 +++++++++++++++++++++- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/crates/matrix-sdk-base/src/store/mod.rs b/crates/matrix-sdk-base/src/store/mod.rs index da5c1918139..1dc4f3a7e6f 100644 --- a/crates/matrix-sdk-base/src/store/mod.rs +++ b/crates/matrix-sdk-base/src/store/mod.rs @@ -70,8 +70,8 @@ pub use self::integration_tests::StateStoreIntegrationTests; pub use self::{ memory_store::MemoryStore, traits::{ - ComposerDraft, DynStateStore, IntoStateStore, StateStore, StateStoreDataKey, - StateStoreDataValue, StateStoreExt, + ComposerDraft, ComposerDraftType, DynStateStore, IntoStateStore, StateStore, + StateStoreDataKey, StateStoreDataValue, StateStoreExt, }, }; diff --git a/crates/matrix-sdk-base/src/store/traits.rs b/crates/matrix-sdk-base/src/store/traits.rs index a69b6ab4841..70f191f550d 100644 --- a/crates/matrix-sdk-base/src/store/traits.rs +++ b/crates/matrix-sdk-base/src/store/traits.rs @@ -823,28 +823,34 @@ pub enum StateStoreDataValue { } /// Current draft of the composer for the room. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[cfg_attr(feature = "uniffi", derive(uniffi::Record))] pub struct ComposerDraft { /// The draft content in plain text. - plain_text: String, + pub plain_text: String, /// If the message is formatted in HTML, the HTML representation of the /// message. - html_text: Option, + pub html_text: Option, /// The type of draft. - draft_type: ComposerDraftType, + pub draft_type: ComposerDraftType, } /// The type of draft of the composer. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[cfg_attr(feature = "uniffi", derive(uniffi::Enum))] pub enum ComposerDraftType { /// The draft is a new message. NewMessage, - /// The draft is a rseply to an event. - Reply { event_id: String }, + /// The draft is a reply to an event. + Reply { + /// The ID of the event being replied to. + event_id: String, + }, /// The draft is an edit of an event. - Edit { event_id: String }, + Edit { + /// The ID of the event being edited. + event_id: String, + }, } impl StateStoreDataValue { diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 38964789848..296f5fe0828 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -2969,7 +2969,7 @@ pub struct TryFromReportedContentScoreError(()); #[cfg(all(test, not(target_arch = "wasm32")))] mod tests { - use matrix_sdk_base::SessionMeta; + use matrix_sdk_base::{store::ComposerDraftType, ComposerDraft, SessionMeta}; use matrix_sdk_test::{ async_test, test_json, JoinedRoomBuilder, StateTestEvent, SyncResponseBuilder, }; @@ -2983,6 +2983,7 @@ mod tests { use crate::{ config::RequestConfig, matrix_auth::{MatrixSession, MatrixSessionTokens}, + test_utils::logged_in_client, Client, }; @@ -3131,4 +3132,30 @@ mod tests { ReportedContentScore::try_from(int!(10)).unwrap_err(); ReportedContentScore::try_from(int!(-110)).unwrap_err(); } + + #[async_test] + async fn test_composer_draft() { + use matrix_sdk_test::DEFAULT_TEST_ROOM_ID; + + let client = logged_in_client(None).await; + + let response = SyncResponseBuilder::default() + .add_joined_room(JoinedRoomBuilder::default()) + .build_sync_response(); + client.base_client().receive_sync_response(response).await.unwrap(); + let room = client.get_room(&DEFAULT_TEST_ROOM_ID).expect("Room should exist"); + + assert_eq!(room.load_composer_draft().await.unwrap(), None); + + let draft = ComposerDraft { + plain_text: "Hello, world!".to_owned(), + html_text: Some("Hello, world!".to_owned()), + draft_type: ComposerDraftType::NewMessage, + }; + room.save_composer_draft(draft.clone()).await.unwrap(); + assert_eq!(room.load_composer_draft().await.unwrap(), Some(draft)); + + room.clear_composer_draft().await.unwrap(); + assert_eq!(room.load_composer_draft().await.unwrap(), None); + } }