From ce231e6c2b41cf8ff759012bc51dbe551b3398a2 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 3 Oct 2024 09:51:35 +0100 Subject: [PATCH] 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. --- .../src/deserialized_responses.rs | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index 394f13bb5..891e22df8 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -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 { .. } + ) + } }