diff --git a/Cargo.lock b/Cargo.lock index 4299623faad69e00142959ce20afd58650451cfd..40fbed419eac057f801df1e7666e3e1838c545e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1663,6 +1663,12 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.9.0" @@ -2367,6 +2373,7 @@ dependencies = [ "inquire", "log", "parity-scale-codec", + "pretty_assertions", "reqwest", "rstest", "scrypt", @@ -4101,6 +4108,16 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "pretty_assertions" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "prettyplease" version = "0.2.22" diff --git a/Cargo.toml b/Cargo.toml index 2e952bc9668c455c9fe0582ad789672a5734250c..7b557429db04f8e4a9bdf39943bf2846814e8b2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ colored = "2.1.0" # Tests rstest = "0.23.0" +pretty_assertions = "1.4.1" # allows to build gcli for different runtimes and with different predefined networks [features] diff --git a/src/commands/vault/display.rs b/src/commands/vault/display.rs index b55189087e7d6083ffcc27b86873f3ada77c24be..100567e9b2038b3f16c8095f13f15deb0c6e5136 100644 --- a/src/commands/vault/display.rs +++ b/src/commands/vault/display.rs @@ -401,4 +401,202 @@ mod tests { assert_eq!(table_without_g1v1.to_string(), expected_table_without_g1v1); } } + + mod vault_accounts_json_view_tests { + use crate::commands::vault::display::{compute_vault_accounts_json, VaultAccountView}; + use crate::entities::vault_account::tests::account_tree_node_tests::{ + mother_account_tree_node, mother_g1v1_account_tree_node, + }; + use log::{debug, LevelFilter}; + use pretty_assertions::assert_eq; + use serde_json::{json, Value}; + + /// This allows to activate debug logs for the tests (to retrieve ACTUAL values in logs) + #[allow(dead_code)] + fn init_logger_in_debug() { + let _ = env_logger::builder() + .is_test(true) + .filter_level(LevelFilter::Debug) + .try_init(); + } + + #[test] + fn test_compute_vault_accounts_json_view_with_g1v1() { + // init_logger_in_debug(); + + let account_tree_node = mother_account_tree_node(); + let g1v1_account_tree_node = mother_g1v1_account_tree_node(); + let account_tree_nodes = vec![account_tree_node, g1v1_account_tree_node]; + + // Test with show_g1v1 = true (default behavior) + let view_with_g1v1 = compute_vault_accounts_json(&account_tree_nodes, true); + let expected_json_with_g1v1 = json! ([ + { + "address": "5DfhGyQdFobKM8NsWvEeAKk5EQQgYe9AydgJ7rMB6E1EqRzV", + "crypto_scheme": "sr25519", + "path": "<Base>", + "name": "Mother", + "g1v1_public_key": null, + "children": [ + { + "address": "5D34dL5prEUaGNQtPPZ3yN5Y6BnkfXunKXXz6fo7ZJbLwRRH", + "crypto_scheme": null, + "path": "//0", + "name": "Child 1", + "g1v1_public_key": null, + "children": [ + { + "address": "5Fh5PLQNt1xuEXm71dfDtQdnwceSew4oHewWBLsWAkKspV7d", + "crypto_scheme": null, + "path": "//0", + "name": "Grandchild 1", + "g1v1_public_key": null, + "children": [] + } + ] + }, + { + "address": "5GBNeWRhZc2jXu7D55rBimKYDk8PGk8itRYFTPfC8RJLKG5o", + "crypto_scheme": null, + "path": "//1", + "name": "<Mother//1>", + "g1v1_public_key": null, + "children": [ + { + "address": "5CvdJuB9HLXSi5FS9LW57cyHF13iCv5HDimo2C45KxnxriCT", + "crypto_scheme": null, + "path": "//1", + "name": "<Mother//1//1>", + "g1v1_public_key": null, + "children": [] + } + ] + } + ] + }, + { + "address": "5ET2jhgJFoNQUpgfdSkdwftK8DKWdqZ1FKm5GKWdPfMWhPr4", + "crypto_scheme": "ed25519", + "path": "<Base>", + "name": "MotherG1v1", + "g1v1_public_key": "86pW1doyJPVH3jeDPZNQa1UZFBo5zcdvHERcaeE758W7", + "children": [], + } + ]); + + assert_eq!(get_json_value(&view_with_g1v1), expected_json_with_g1v1); + + // Test with show_g1v1 = false + let view_without_g1v1 = compute_vault_accounts_json(&account_tree_nodes, false); + let expected_json_without_g1v1 = json! ([ + { + "address": "5DfhGyQdFobKM8NsWvEeAKk5EQQgYe9AydgJ7rMB6E1EqRzV", + "crypto_scheme": "sr25519", + "path": "<Base>", + "name": "Mother", + "g1v1_public_key": null, + "children": [ + { + "address": "5D34dL5prEUaGNQtPPZ3yN5Y6BnkfXunKXXz6fo7ZJbLwRRH", + "crypto_scheme": null, + "path": "//0", + "name": "Child 1", + "g1v1_public_key": null, + "children": [ + { + "address": "5Fh5PLQNt1xuEXm71dfDtQdnwceSew4oHewWBLsWAkKspV7d", + "crypto_scheme": null, + "path": "//0", + "name": "Grandchild 1", + "g1v1_public_key": null, + "children": [] + } + ] + }, + { + "address": "5GBNeWRhZc2jXu7D55rBimKYDk8PGk8itRYFTPfC8RJLKG5o", + "crypto_scheme": null, + "path": "//1", + "name": "<Mother//1>", + "g1v1_public_key": null, + "children": [ + { + "address": "5CvdJuB9HLXSi5FS9LW57cyHF13iCv5HDimo2C45KxnxriCT", + "crypto_scheme": null, + "path": "//1", + "name": "<Mother//1//1>", + "g1v1_public_key": null, + "children": [] + } + ] + } + ] + }, + { + "address": "5ET2jhgJFoNQUpgfdSkdwftK8DKWdqZ1FKm5GKWdPfMWhPr4", + "crypto_scheme": "ed25519", + "path": "<Base>", + "name": "MotherG1v1", + "g1v1_public_key": null, + "children": [] + } + ]); + + assert_eq!( + get_json_value(&view_without_g1v1), + expected_json_without_g1v1 + ); + } + + #[test] + fn test_compute_vault_accounts_json_view_with_g1v1_partial() { + // init_logger_in_debug(); + + let mother = mother_account_tree_node(); + let child1 = mother.borrow().children[0].clone(); + let account_tree_nodes = vec![child1]; + + // Test with show_g1v1 = true (default behavior) + let view_with_g1v1 = compute_vault_accounts_json(&account_tree_nodes, true); + let expected_json_with_g1v1_unused = json! ([ + { + "address": "5D34dL5prEUaGNQtPPZ3yN5Y6BnkfXunKXXz6fo7ZJbLwRRH", + "crypto_scheme": null, + "path": "//0", + "name": "Child 1", + "g1v1_public_key": null, + "children": [ + { + "address": "5Fh5PLQNt1xuEXm71dfDtQdnwceSew4oHewWBLsWAkKspV7d", + "crypto_scheme": null, + "path": "//0", + "name": "Grandchild 1", + "g1v1_public_key": null, + "children": [], + } + ] + } + ]); + + assert_eq!( + get_json_value(&view_with_g1v1), + expected_json_with_g1v1_unused + ); + + // Test with show_g1v1 = false + let view_without_g1v1 = compute_vault_accounts_json(&account_tree_nodes, false); + + assert_eq!( + get_json_value(&view_without_g1v1), + expected_json_with_g1v1_unused + ); + } + + fn get_json_value(views: &Vec<VaultAccountView>) -> Value { + let view_json_string = serde_json::to_string_pretty(&views).unwrap(); + debug!("Actual JSON:\n{}", view_json_string); + let view_json_value: Value = serde_json::from_str(&view_json_string).unwrap(); + view_json_value + } + } }