timeline: test for SyncTimelineEvent serialization

I'm going to change the internal structure of `SyncTimelineEvent`, and since
it implements `Deserialize`, we need to not break it. Let's add a test for the
current format.
This commit is contained in:
Richard van der Hoff
2024-10-03 09:51:35 +01:00
committed by Richard van der Hoff
parent b36a9ad781
commit ce231e6c2b
@@ -537,14 +537,19 @@ pub struct UnableToDecryptInfo {
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use ruma::{
event_id,
events::{room::message::RoomMessageEventContent, AnySyncTimelineEvent},
serde::Raw,
user_id,
};
use serde::Deserialize;
use serde_json::json;
use super::{SyncTimelineEvent, TimelineEvent, VerificationState};
use super::{
AlgorithmInfo, EncryptionInfo, SyncTimelineEvent, TimelineEvent, VerificationState,
};
use crate::deserialized_responses::{DeviceLinkProblem, VerificationLevel};
fn example_event() -> serde_json::Value {
@@ -619,4 +624,58 @@ mod tests {
VerificationState::Unverified(VerificationLevel::UnsignedDevice)
);
}
#[test]
fn sync_timeline_event_serialisation() {
let room_event = SyncTimelineEvent {
event: Raw::new(&example_event()).unwrap().cast(),
encryption_info: Some(EncryptionInfo {
sender: user_id!("@sender:example.com").to_owned(),
sender_device: None,
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
curve25519_key: "xxx".to_owned(),
sender_claimed_keys: Default::default(),
},
verification_state: VerificationState::Verified,
}),
push_actions: Default::default(),
unsigned_encryption_info: None,
};
let serialized = serde_json::to_value(&room_event).unwrap();
// Test that the serialization is as expected
assert_eq!(
serialized,
json!({
"event": {
"content": {"body": "secret", "msgtype": "m.text"},
"event_id": "$xxxxx:example.org",
"origin_server_ts": 2189,
"room_id": "!someroom:example.com",
"sender": "@carl:example.com",
"type": "m.room.message",
},
"encryption_info": {
"sender": "@sender:example.com",
"sender_device": null,
"algorithm_info": {
"MegolmV1AesSha2": {
"curve25519_key": "xxx",
"sender_claimed_keys": {}
}
},
"verification_state": "Verified",
},
})
);
// And it can be properly deserialized from the new format.
let event: SyncTimelineEvent = serde_json::from_value(serialized).unwrap();
assert_eq!(event.event_id(), Some(event_id!("$xxxxx:example.org").to_owned()));
assert_matches!(
event.encryption_info.unwrap().algorithm_info,
AlgorithmInfo::MegolmV1AesSha2 { .. }
)
}
}