ruwuma/crates/ruma-common/tests/events/without_relation.rs
Kévin Commaille 699c2daf39
events: Remove mixed types and conversion functions for extensible events
Mixed types have been removed from MSC1767.
2023-02-09 16:25:51 +01:00

59 lines
1.7 KiB
Rust

use assert_matches::assert_matches;
use ruma_common::{
event_id,
events::{
relation::InReplyTo,
room::message::{MessageType, Relation, RoomMessageEventContent},
},
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
#[test]
fn serialize_room_message_content_without_relation() {
let mut content = RoomMessageEventContent::text_plain("Hello, world!");
content.relates_to =
Some(Relation::Reply { in_reply_to: InReplyTo::new(event_id!("$eventId").to_owned()) });
let without_relation = MessageType::from(content);
assert_eq!(
to_json_value(&without_relation).unwrap(),
json!({
"body": "Hello, world!",
"msgtype": "m.text",
})
);
}
#[test]
fn deserialize_room_message_content_without_relation() {
let json_data = json!({
"body": "Hello, world!",
"msgtype": "m.text",
});
let text = assert_matches!(
from_json_value::<MessageType>(json_data),
Ok(MessageType::Text(text)) => text
);
assert_eq!(text.body, "Hello, world!");
}
#[test]
fn convert_room_message_content_without_relation_to_full() {
let mut content = RoomMessageEventContent::text_plain("Hello, world!");
content.relates_to =
Some(Relation::Reply { in_reply_to: InReplyTo::new(event_id!("$eventId").to_owned()) });
let new_content = RoomMessageEventContent::from(MessageType::from(content));
let (text, relates_to) = assert_matches!(
new_content,
RoomMessageEventContent {
msgtype: MessageType::Text(text),
relates_to,
..
} => (text, relates_to)
);
assert_eq!(text.body, "Hello, world!");
assert_matches!(relates_to, None);
}