From 4d5903e71fef4d9dd9a382d4ce1108036f98efd5 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 18 Dec 2024 09:42:00 +0100 Subject: [PATCH 1/9] test(snapshot): Use snapshot testing in sdk-common --- CONTRIBUTING.md | 27 ++++ Cargo.lock | 20 +++ Cargo.toml | 3 + crates/matrix-sdk-common/Cargo.toml | 1 + .../src/deserialized_responses.rs | 135 +++++++++++++++++- ...__tests__snapshot_test_algorithm_info.snap | 13 ++ ..._tests__snapshot_test_encryption_info.snap | 15 ++ ...__tests__snapshot_test_shield_codes-2.snap | 5 + ...__tests__snapshot_test_shield_codes-3.snap | 5 + ...__tests__snapshot_test_shield_codes-4.snap | 5 + ...__tests__snapshot_test_shield_codes-5.snap | 5 + ...__tests__snapshot_test_shield_codes-6.snap | 5 + ...es__tests__snapshot_test_shield_codes.snap | 5 + ..._tests__snapshot_test_shield_states-2.snap | 10 ++ ..._tests__snapshot_test_shield_states-3.snap | 10 ++ ...s__tests__snapshot_test_shield_states.snap | 5 + ...s__snapshot_test_verification_level-2.snap | 5 + ...s__snapshot_test_verification_level-3.snap | 7 + ...s__snapshot_test_verification_level-4.snap | 7 + ...sts__snapshot_test_verification_level.snap | 5 + ...__snapshot_test_verification_states-2.snap | 7 + ...__snapshot_test_verification_states-3.snap | 9 ++ ...__snapshot_test_verification_states-4.snap | 9 ++ ...__snapshot_test_verification_states-5.snap | 5 + ...ts__snapshot_test_verification_states.snap | 7 + 25 files changed, 324 insertions(+), 6 deletions(-) create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_algorithm_info.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_encryption_info.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-2.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-3.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-4.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-5.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-6.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states-2.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states-3.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-2.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-3.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-4.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-2.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-3.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-4.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-5.snap create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states.snap diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 82a802cffe0..6a7385e4319 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -30,6 +30,33 @@ integration tests that need a running synapse instance. These tests reside in synapse for testing purposes. +### Snapshot Testing + +You can add/review snapshot tests using [insta.rs](https://insta.rs) + +Every new struct/enum that derives `Serialize` `Deserialise` should have a snapshot test for it. +Any code change that breaks serialisation will then break a test, the author will then have to decide +how to handle migration and test it if needed. + + +And for an improved review experience it's recommended (but not necessary) to install the cargo-insta tool: + +Unix: +``` +curl -LsSf https://insta.rs/install.sh | sh +``` + +Windows: +``` +powershell -c "irm https://insta.rs/install.ps1 | iex" +``` + +Usual flow is to first run the test, then review them. +``` +cargo insta test +cargo insta review +``` + ## Pull requests Ideally, a PR should have a *proper title*, with *atomic logical commits*, and diff --git a/Cargo.lock b/Cargo.lock index 36ee61dd332..32c12a5aafa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2595,6 +2595,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "insta" +version = "1.41.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8" +dependencies = [ + "console", + "lazy_static", + "linked-hash-map", + "serde", + "similar", +] + [[package]] name = "instant" version = "0.1.13" @@ -2789,6 +2802,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -3170,6 +3189,7 @@ dependencies = [ "getrandom", "gloo-timers", "imbl", + "insta", "js-sys", "matrix-sdk-test-macros", "proptest", diff --git a/Cargo.toml b/Cargo.toml index d9f068ae18b..03d56463ad5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -124,6 +124,9 @@ debug = 0 # for the extra time of optimizing it for a clean build of matrix-sdk-ffi. quote = { opt-level = 2 } sha2 = { opt-level = 2 } +# faster runs for insta.rs snapshot testing +insta.opt-level = 3 +similar.opt-level = 3 # Custom profile with full debugging info, use `--profile dbg` to select [profile.dbg] diff --git a/crates/matrix-sdk-common/Cargo.toml b/crates/matrix-sdk-common/Cargo.toml index 78837937e11..7b876f8f16a 100644 --- a/crates/matrix-sdk-common/Cargo.toml +++ b/crates/matrix-sdk-common/Cargo.toml @@ -46,6 +46,7 @@ assert_matches = { workspace = true } proptest = { workspace = true } matrix-sdk-test-macros = { path = "../../testing/matrix-sdk-test-macros" } wasm-bindgen-test = { workspace = true } +insta = { version = "1.41.1", features = ["json"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # Enable the test macro. diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index a1b07c0267e..7b7a42752af 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -904,22 +904,23 @@ impl From for SyncTimelineEvent { #[cfg(test)] mod tests { - use std::collections::BTreeMap; + use std::{collections::BTreeMap, vec}; use assert_matches::assert_matches; use ruma::{ - event_id, + device_id, event_id, events::{room::message::RoomMessageEventContent, AnySyncTimelineEvent}, serde::Raw, - user_id, + user_id, DeviceKeyAlgorithm, }; use serde::Deserialize; use serde_json::json; use super::{ - AlgorithmInfo, DecryptedRoomEvent, EncryptionInfo, SyncTimelineEvent, TimelineEvent, - TimelineEventKind, UnableToDecryptInfo, UnableToDecryptReason, UnsignedDecryptionResult, - UnsignedEventLocation, VerificationState, WithheldCode, + AlgorithmInfo, DecryptedRoomEvent, EncryptionInfo, ShieldState, ShieldStateCode, + SyncTimelineEvent, TimelineEvent, TimelineEventKind, UnableToDecryptInfo, + UnableToDecryptReason, UnsignedDecryptionResult, UnsignedEventLocation, VerificationState, + WithheldCode, }; use crate::deserialized_responses::{DeviceLinkProblem, VerificationLevel}; @@ -1247,4 +1248,126 @@ mod tests { let reason = UnableToDecryptReason::UnknownMegolmMessageIndex; assert!(reason.is_missing_room_key()); } + + #[test] + fn snapshot_test_verification_level() { + let level = VerificationLevel::VerificationViolation; + insta::assert_json_snapshot! { + serde_json::to_value(&level).unwrap(), + } + + let level = VerificationLevel::UnsignedDevice; + insta::assert_json_snapshot! { + serde_json::to_value(&level).unwrap(), + } + + let level = VerificationLevel::None(DeviceLinkProblem::InsecureSource); + insta::assert_json_snapshot! { + serde_json::to_value(&level).unwrap(), + } + + let level = VerificationLevel::None(DeviceLinkProblem::MissingDevice); + insta::assert_json_snapshot! { + serde_json::to_value(&level).unwrap(), + } + + let level = VerificationLevel::UnverifiedIdentity; + insta::assert_json_snapshot! { + serde_json::to_value(&level).unwrap(), + @r###""UnverifiedIdentity""### + } + } + + #[test] + fn snapshot_test_verification_states() { + let states = vec![ + VerificationState::Unverified(VerificationLevel::UnsignedDevice), + VerificationState::Unverified(VerificationLevel::VerificationViolation), + VerificationState::Unverified(VerificationLevel::None( + DeviceLinkProblem::InsecureSource, + )), + VerificationState::Unverified(VerificationLevel::None( + DeviceLinkProblem::MissingDevice, + )), + VerificationState::Verified, + ]; + + for state in states { + insta::assert_json_snapshot! { + serde_json::to_value(&state).unwrap(), + } + } + } + + #[test] + fn snapshot_test_shield_states() { + let state = ShieldState::None; + insta::assert_json_snapshot! { + serde_json::to_value(&state).unwrap(), + } + + let state = + ShieldState::Red { code: ShieldStateCode::UnverifiedIdentity, message: "a message" }; + insta::assert_json_snapshot! { + serde_json::to_value(&state).unwrap(), + } + + let state = ShieldState::Grey { + code: ShieldStateCode::AuthenticityNotGuaranteed, + message: "authenticity of this message cannot be guaranteed", + }; + insta::assert_json_snapshot! { + serde_json::to_value(&state).unwrap(), + } + } + + #[test] + fn snapshot_test_shield_codes() { + let codes = vec![ + ShieldStateCode::AuthenticityNotGuaranteed, + ShieldStateCode::UnknownDevice, + ShieldStateCode::UnsignedDevice, + ShieldStateCode::UnverifiedIdentity, + ShieldStateCode::SentInClear, + ShieldStateCode::VerificationViolation, + ]; + + for code in codes { + insta::assert_json_snapshot! { + serde_json::to_value(code).unwrap(), + } + } + } + + #[test] + fn snapshot_test_algorithm_info() { + let mut map = BTreeMap::new(); + map.insert(DeviceKeyAlgorithm::Curve25519, "claimedclaimedcurve25519".to_owned()); + map.insert(DeviceKeyAlgorithm::Ed25519, "claimedclaimeded25519".to_owned()); + let info = AlgorithmInfo::MegolmV1AesSha2 { + curve25519_key: "curvecurvecurve".into(), + sender_claimed_keys: map, + }; + + insta::assert_json_snapshot! { + serde_json::to_value(&info).unwrap(), + } + } + + #[test] + fn snapshot_test_encryption_info() { + let info = EncryptionInfo { + sender: user_id!("@alice:localhost").to_owned(), + sender_device: Some(device_id!("ABCDEFGH").to_owned()), + algorithm_info: AlgorithmInfo::MegolmV1AesSha2 { + curve25519_key: "curvecurvecurve".into(), + sender_claimed_keys: Default::default(), + }, + verification_state: VerificationState::Verified, + }; + + insta::assert_json_snapshot! { + serde_json::to_value(&info).unwrap(), + } + } } diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_algorithm_info.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_algorithm_info.snap new file mode 100644 index 00000000000..9b4a8b46750 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_algorithm_info.snap @@ -0,0 +1,13 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&info).unwrap()" +--- +{ + "MegolmV1AesSha2": { + "curve25519_key": "curvecurvecurve", + "sender_claimed_keys": { + "curve25519": "claimedclaimedcurve25519", + "ed25519": "claimedclaimeded25519" + } + } +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_encryption_info.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_encryption_info.snap new file mode 100644 index 00000000000..cc548f41f0a --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_encryption_info.snap @@ -0,0 +1,15 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&info).unwrap()" +--- +{ + "algorithm_info": { + "MegolmV1AesSha2": { + "curve25519_key": "curvecurvecurve", + "sender_claimed_keys": {} + } + }, + "sender": "@alice:localhost", + "sender_device": "ABCDEFGH", + "verification_state": "Verified" +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-2.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-2.snap new file mode 100644 index 00000000000..9b4668eadc4 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-2.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&code).unwrap()" +--- +"UnknownDevice" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-3.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-3.snap new file mode 100644 index 00000000000..d9719968ddb --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-3.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&code).unwrap()" +--- +"UnsignedDevice" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-4.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-4.snap new file mode 100644 index 00000000000..2962f08e5e1 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-4.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&code).unwrap()" +--- +"UnverifiedIdentity" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-5.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-5.snap new file mode 100644 index 00000000000..f547dd10e90 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-5.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&code).unwrap()" +--- +"SentInClear" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-6.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-6.snap new file mode 100644 index 00000000000..fddf47c7d44 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes-6.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&code).unwrap()" +--- +"VerificationViolation" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes.snap new file mode 100644 index 00000000000..ad123c7c7bf --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_codes.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&code).unwrap()" +--- +"AuthenticityNotGuaranteed" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states-2.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states-2.snap new file mode 100644 index 00000000000..e399c209db8 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states-2.snap @@ -0,0 +1,10 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&state).unwrap()" +--- +{ + "Red": { + "code": "UnverifiedIdentity", + "message": "a message" + } +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states-3.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states-3.snap new file mode 100644 index 00000000000..e3f80a05261 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states-3.snap @@ -0,0 +1,10 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&state).unwrap()" +--- +{ + "Grey": { + "code": "AuthenticityNotGuaranteed", + "message": "authenticity of this message cannot be guaranteed" + } +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states.snap new file mode 100644 index 00000000000..24a9120e3ac --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_shield_states.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&state).unwrap()" +--- +"None" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-2.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-2.snap new file mode 100644 index 00000000000..fc816009b11 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-2.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&level).unwrap()" +--- +"UnsignedDevice" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-3.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-3.snap new file mode 100644 index 00000000000..2d27ef8c55d --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-3.snap @@ -0,0 +1,7 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&level).unwrap()" +--- +{ + "None": "InsecureSource" +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-4.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-4.snap new file mode 100644 index 00000000000..180ef3de9cb --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-4.snap @@ -0,0 +1,7 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&level).unwrap()" +--- +{ + "None": "MissingDevice" +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level.snap new file mode 100644 index 00000000000..e68abafa23a --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&level).unwrap()" +--- +"VerificationViolation" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-2.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-2.snap new file mode 100644 index 00000000000..716d8ff1374 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-2.snap @@ -0,0 +1,7 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&state).unwrap()" +--- +{ + "Unverified": "VerificationViolation" +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-3.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-3.snap new file mode 100644 index 00000000000..fae5d49deb1 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-3.snap @@ -0,0 +1,9 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&state).unwrap()" +--- +{ + "Unverified": { + "None": "InsecureSource" + } +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-4.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-4.snap new file mode 100644 index 00000000000..a721a9e0a9e --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-4.snap @@ -0,0 +1,9 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&state).unwrap()" +--- +{ + "Unverified": { + "None": "MissingDevice" + } +} diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-5.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-5.snap new file mode 100644 index 00000000000..6a8559c1f7c --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states-5.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&state).unwrap()" +--- +"Verified" diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states.snap new file mode 100644 index 00000000000..80c229057d8 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_states.snap @@ -0,0 +1,7 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&state).unwrap()" +--- +{ + "Unverified": "UnsignedDevice" +} From ce171386269260c499d771f4bcc2bd93ee2f9525 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 18 Dec 2024 13:53:35 +0100 Subject: [PATCH 2/9] dependency: move insta rs to workspace --- Cargo.toml | 1 + crates/matrix-sdk-common/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 03d56463ad5..f8da5d8e391 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ hmac = "0.12.1" http = "1.1.0" imbl = "3.0.0" indexmap = "2.6.0" +insta = { version = "1.41.1", features = ["json"] } itertools = "0.13.0" js-sys = "0.3.69" mime = "0.3.17" diff --git a/crates/matrix-sdk-common/Cargo.toml b/crates/matrix-sdk-common/Cargo.toml index 7b876f8f16a..3b6059e5e4c 100644 --- a/crates/matrix-sdk-common/Cargo.toml +++ b/crates/matrix-sdk-common/Cargo.toml @@ -46,7 +46,7 @@ assert_matches = { workspace = true } proptest = { workspace = true } matrix-sdk-test-macros = { path = "../../testing/matrix-sdk-test-macros" } wasm-bindgen-test = { workspace = true } -insta = { version = "1.41.1", features = ["json"] } +insta = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # Enable the test macro. From cd7392824b4f46a9e7dba3a96273498f813b70a1 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 18 Dec 2024 14:08:00 +0100 Subject: [PATCH 3/9] review: import assert_json_snapshot instead of using qualified name --- .../src/deserialized_responses.rs | 28 +++++++++---------- ...s__snapshot_test_verification_level-5.snap | 5 ++++ 2 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-5.snap diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index 7b7a42752af..8f3c8925778 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -907,6 +907,7 @@ mod tests { use std::{collections::BTreeMap, vec}; use assert_matches::assert_matches; + use insta::assert_json_snapshot; use ruma::{ device_id, event_id, events::{room::message::RoomMessageEventContent, AnySyncTimelineEvent}, @@ -1252,29 +1253,28 @@ mod tests { #[test] fn snapshot_test_verification_level() { let level = VerificationLevel::VerificationViolation; - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&level).unwrap(), } let level = VerificationLevel::UnsignedDevice; - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&level).unwrap(), } let level = VerificationLevel::None(DeviceLinkProblem::InsecureSource); - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&level).unwrap(), } let level = VerificationLevel::None(DeviceLinkProblem::MissingDevice); - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&level).unwrap(), } let level = VerificationLevel::UnverifiedIdentity; - insta::assert_json_snapshot! { - serde_json::to_value(&level).unwrap(), - @r###""UnverifiedIdentity""### + assert_json_snapshot! { + serde_json::to_value(&level).unwrap() } } @@ -1293,7 +1293,7 @@ mod tests { ]; for state in states { - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&state).unwrap(), } } @@ -1302,13 +1302,13 @@ mod tests { #[test] fn snapshot_test_shield_states() { let state = ShieldState::None; - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&state).unwrap(), } let state = ShieldState::Red { code: ShieldStateCode::UnverifiedIdentity, message: "a message" }; - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&state).unwrap(), } @@ -1316,7 +1316,7 @@ mod tests { code: ShieldStateCode::AuthenticityNotGuaranteed, message: "authenticity of this message cannot be guaranteed", }; - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&state).unwrap(), } } @@ -1333,7 +1333,7 @@ mod tests { ]; for code in codes { - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(code).unwrap(), } } @@ -1349,7 +1349,7 @@ mod tests { sender_claimed_keys: map, }; - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&info).unwrap(), } } @@ -1366,7 +1366,7 @@ mod tests { verification_state: VerificationState::Verified, }; - insta::assert_json_snapshot! { + assert_json_snapshot! { serde_json::to_value(&info).unwrap(), } } diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-5.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-5.snap new file mode 100644 index 00000000000..540df71e467 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_verification_level-5.snap @@ -0,0 +1,5 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&level).unwrap()" +--- +"UnverifiedIdentity" From 08cc49e996e036c0b4041685ba09f4b07fcd52b5 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 18 Dec 2024 14:58:47 +0100 Subject: [PATCH 4/9] fixup: try with_settings sort_map --- .../src/deserialized_responses.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index 8f3c8925778..91b166524fc 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -907,7 +907,7 @@ mod tests { use std::{collections::BTreeMap, vec}; use assert_matches::assert_matches; - use insta::assert_json_snapshot; + use insta::{assert_json_snapshot, with_settings}; use ruma::{ device_id, event_id, events::{room::message::RoomMessageEventContent, AnySyncTimelineEvent}, @@ -1349,9 +1349,11 @@ mod tests { sender_claimed_keys: map, }; - assert_json_snapshot! { - serde_json::to_value(&info).unwrap(), - } + with_settings!({sort_maps =>true}, { + assert_json_snapshot! { + serde_json::to_value(&info).unwrap(), + } + }) } #[test] @@ -1366,8 +1368,10 @@ mod tests { verification_state: VerificationState::Verified, }; - assert_json_snapshot! { - serde_json::to_value(&info).unwrap(), - } + with_settings!({sort_maps =>true}, { + assert_json_snapshot! { + serde_json::to_value(&info).unwrap(), + } + }); } } From a26f30a248f10d50a35a432a491dec7e572f1f32 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 18 Dec 2024 15:15:24 +0100 Subject: [PATCH 5/9] fixup imports --- crates/matrix-sdk-common/src/deserialized_responses.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index 91b166524fc..ae870f3c99f 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -918,12 +918,11 @@ mod tests { use serde_json::json; use super::{ - AlgorithmInfo, DecryptedRoomEvent, EncryptionInfo, ShieldState, ShieldStateCode, - SyncTimelineEvent, TimelineEvent, TimelineEventKind, UnableToDecryptInfo, - UnableToDecryptReason, UnsignedDecryptionResult, UnsignedEventLocation, VerificationState, - WithheldCode, + AlgorithmInfo, DecryptedRoomEvent, DeviceLinkProblem, EncryptionInfo, ShieldState, + ShieldStateCode, SyncTimelineEvent, TimelineEvent, TimelineEventKind, UnableToDecryptInfo, + UnableToDecryptReason, UnsignedDecryptionResult, UnsignedEventLocation, VerificationLevel, + VerificationState, WithheldCode, }; - use crate::deserialized_responses::{DeviceLinkProblem, VerificationLevel}; fn example_event() -> serde_json::Value { json!({ From d331c7ba724f94cd8933db4851254f4c91dafa6e Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 18 Dec 2024 17:27:33 +0100 Subject: [PATCH 6/9] add simple sync timeline event snapshot test --- .../src/deserialized_responses.rs | 43 +++++++++++++++++ ...ts__snapshot_test_sync_timeline_event.snap | 47 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_sync_timeline_event.snap diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index 0e40127944d..890f1645fc0 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -1443,4 +1443,47 @@ mod tests { } }); } + + #[test] + fn snapshot_test_sync_timeline_event() { + let room_event = SyncTimelineEvent { + kind: TimelineEventKind::Decrypted(DecryptedRoomEvent { + event: Raw::new(&example_event()).unwrap().cast(), + encryption_info: EncryptionInfo { + sender: user_id!("@sender:example.com").to_owned(), + sender_device: Some(device_id!("ABCDEFGHIJ").to_owned()), + algorithm_info: AlgorithmInfo::MegolmV1AesSha2 { + curve25519_key: "xxx".to_owned(), + sender_claimed_keys: BTreeMap::from([ + ( + DeviceKeyAlgorithm::Ed25519, + "I3YsPwqMZQXHkSQbjFNEs7b529uac2xBpI83eN3LUXo".to_owned(), + ), + ( + DeviceKeyAlgorithm::Curve25519, + "qzdW3F5IMPFl0HQgz5w/L5Oi/npKUFn8Um84acIHfPY".to_owned(), + ), + ]), + }, + verification_state: VerificationState::Verified, + }, + unsigned_encryption_info: Some(BTreeMap::from([( + UnsignedEventLocation::RelationsThreadLatestEvent, + UnsignedDecryptionResult::UnableToDecrypt(UnableToDecryptInfo { + session_id: Some("xyz".to_owned()), + reason: UnableToDecryptReason::MissingMegolmSession { + withheld_code: Some(WithheldCode::Unverified), + }, + }), + )])), + }), + push_actions: Default::default(), + }; + + with_settings!({sort_maps =>true}, { + assert_json_snapshot! { + serde_json::to_value(&room_event).unwrap(), + } + }); + } } diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_sync_timeline_event.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_sync_timeline_event.snap new file mode 100644 index 00000000000..f40882141c1 --- /dev/null +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_sync_timeline_event.snap @@ -0,0 +1,47 @@ +--- +source: crates/matrix-sdk-common/src/deserialized_responses.rs +expression: "serde_json::to_value(&room_event).unwrap()" +--- +{ + "kind": { + "Decrypted": { + "encryption_info": { + "algorithm_info": { + "MegolmV1AesSha2": { + "curve25519_key": "xxx", + "sender_claimed_keys": { + "curve25519": "qzdW3F5IMPFl0HQgz5w/L5Oi/npKUFn8Um84acIHfPY", + "ed25519": "I3YsPwqMZQXHkSQbjFNEs7b529uac2xBpI83eN3LUXo" + } + } + }, + "sender": "@sender:example.com", + "sender_device": "ABCDEFGHIJ", + "verification_state": "Verified" + }, + "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" + }, + "unsigned_encryption_info": { + "RelationsThreadLatestEvent": { + "UnableToDecrypt": { + "reason": { + "MissingMegolmSession": { + "withheld_code": "m.unverified" + } + }, + "session_id": "xyz" + } + } + } + } + } +} From a79adbc8e5b8a4392dbd659a546af215bc182c35 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 19 Dec 2024 10:45:07 +0100 Subject: [PATCH 7/9] CI: change default insta behavior for new snapshots --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 881b5010dfe..1c6e6a8981a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,9 @@ concurrency: env: CARGO_TERM_COLOR: always + # Insta.rs is run directly via cargo test. We don't want insta.rs to create new snapshots files. + # Just want it to run the tests (option `no` instead of `auto`). + INSTA_UPDATE: no jobs: xtask: From 0cbdaeba0f4dc24c5d7480bd7cd82abb684e7bd0 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 19 Dec 2024 13:31:47 +0100 Subject: [PATCH 8/9] add unreviewed test --- .../src/deserialized_responses.rs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index 890f1645fc0..eec0997b952 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -1486,4 +1486,40 @@ mod tests { } }); } + + #[test] + fn snapshot_old_to_remove() { + let serialized = json!({ + "kind": { + "UnableToDecrypt": { + "event": { + "content": { + "algorithm": "m.megolm.v1.aes-sha2", + "ciphertext": "AwgAEoABzL1JYhqhjW9jXrlT3M6H8mJ4qffYtOQOnPuAPNxsuG20oiD/Fnpv6jnQGhU6YbV9pNM+1mRnTvxW3CbWOPjLKqCWTJTc7Q0vDEVtYePg38ncXNcwMmfhgnNAoW9S7vNs8C003x3yUl6NeZ8bH+ci870BZL+kWM/lMl10tn6U7snNmSjnE3ckvRdO+11/R4//5VzFQpZdf4j036lNSls/WIiI67Fk9iFpinz9xdRVWJFVdrAiPFwb8L5xRZ8aX+e2JDMlc1eW8gk", + "device_id": "SKCGPNUWAU", + "sender_key": "Gim/c7uQdSXyrrUbmUOrBT6sMC0gO7QSLmOK6B7NOm0", + "session_id": "hgLyeSqXfb8vc5AjQLsg6TSHVu0HJ7HZ4B6jgMvxkrs" + }, + "event_id": "$xxxxx:example.org", + "origin_server_ts": 2189, + "room_id": "!someroom:example.com", + "sender": "@carl:example.com", + "type": "m.room.message" + }, + "utd_info": { + "reason": "MissingMegolmSession", + "session_id": "session000" + } + } + } + }); + + let result: SyncTimelineEvent = serde_json::from_value(serialized).unwrap(); + + with_settings!({sort_maps =>true}, { + assert_json_snapshot! { + serde_json::to_value(&result).unwrap(), + } + }); + } } From afd88407bf75dcc53d3fa083a4b106df0d022829 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 19 Dec 2024 13:43:21 +0100 Subject: [PATCH 9/9] touch --- crates/matrix-sdk-common/src/deserialized_responses.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index eec0997b952..4e7fe823f92 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -1508,7 +1508,7 @@ mod tests { }, "utd_info": { "reason": "MissingMegolmSession", - "session_id": "session000" + "session_id": "session0001" } } }