From 55c5f69dc99528df0c3df9a93ba330da3b7c6bd6 Mon Sep 17 00:00:00 2001 From: Max Korsunov Date: Fri, 2 Aug 2024 12:22:44 +0200 Subject: [PATCH] fix: test wasm crate in ci correctly (#1616) * test: CI * fix: extract tests from src into tests so that they run as part of wasm-pack test * chore: make tests pass again --------- Co-authored-by: Atris --- packages/wasm/crate/src/auction.rs | 42 ----- packages/wasm/crate/src/metadata.rs | 174 --------------------- packages/wasm/crate/tests/test_auction.rs | 38 +++++ packages/wasm/crate/tests/test_metadata.rs | 164 +++++++++++++++++++ 4 files changed, 202 insertions(+), 216 deletions(-) create mode 100644 packages/wasm/crate/tests/test_auction.rs create mode 100644 packages/wasm/crate/tests/test_metadata.rs diff --git a/packages/wasm/crate/src/auction.rs b/packages/wasm/crate/src/auction.rs index c7e90c6062..fa9602db35 100644 --- a/packages/wasm/crate/src/auction.rs +++ b/packages/wasm/crate/src/auction.rs @@ -27,45 +27,3 @@ pub fn get_auction_nft_metadata(auction_id: &[u8], seq: u64) -> WasmResult(&result_bytes).unwrap(); - let result_proto = result.to_proto(); - - assert!(result_proto.symbol.starts_with("auction@1234(")); - assert!(result_proto.display.starts_with("auctionnft_1234_pauctid1")); - } -} diff --git a/packages/wasm/crate/src/metadata.rs b/packages/wasm/crate/src/metadata.rs index 2f20f33a82..421fc47d0c 100644 --- a/packages/wasm/crate/src/metadata.rs +++ b/packages/wasm/crate/src/metadata.rs @@ -97,177 +97,3 @@ fn get_proposal_id(captures: ®ex::Captures) -> WasmResult { .ok_or_else(|| anyhow!(" not matched in token regex"))?; Ok(id_match.as_str().to_string()) } - -#[cfg(test)] -mod test_helpers { - use penumbra_proto::core::asset::v1::DenomUnit; - - use super::*; - - pub fn get_metadata_for(display_denom: &str, base_denom_is_display_denom: bool) -> Metadata { - let mut denom_units = Vec::new(); - denom_units.push(DenomUnit { - aliases: Vec::new(), - denom: if base_denom_is_display_denom { - String::from(display_denom) - } else { - format!("u{display_denom}") - }, - exponent: 0, - }); - - if !base_denom_is_display_denom { - denom_units.push(DenomUnit { - aliases: Vec::new(), - denom: String::from(display_denom), - exponent: 6, - }); - } - - Metadata { - base: if base_denom_is_display_denom { - String::from(display_denom) - } else { - format!("u{display_denom}") - }, - description: String::from(""), - denom_units, - display: String::from(display_denom), - images: Vec::new(), - name: String::from(""), - penumbra_asset_id: None, - symbol: String::from(""), - priority_score: 0, - } - } - - #[test] - fn it_interpolates_display_denom() { - assert_eq!(get_metadata_for("penumbra", false).base, "upenumbra"); - assert_eq!(get_metadata_for("penumbra", false).display, "penumbra"); - assert_eq!( - get_metadata_for("penumbra", false).denom_units[0].denom, - "upenumbra" - ); - assert_eq!( - get_metadata_for("penumbra", false).denom_units[1].denom, - "penumbra" - ); - } -} - -#[cfg(test)] -mod customize_symbol_inner_tests { - use super::*; - - #[test] - fn it_returns_non_staking_metadata_as_is() { - let metadata = Metadata { - name: String::from("Penumbra"), - symbol: String::from("UM"), - ..test_helpers::get_metadata_for("penumbra", false) - }; - let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); - - assert_eq!(metadata, customized_metadata); - } - - #[test] - fn it_modifies_unbonding_token_symbol() { - let metadata = Metadata { - name: String::from("Unbonding Token"), - symbol: String::from(""), - ..test_helpers::get_metadata_for( - "unbonding_start_at_1234_penumbravalid1abcdef123456", - false, - ) - }; - let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); - - assert_eq!(customized_metadata.symbol, "unbondUMat1234(abcdef123456)"); - } - - #[test] - fn it_modifies_delegation_token_symbol() { - let metadata = Metadata { - name: String::from("Delegation Token"), - symbol: String::from(""), - ..test_helpers::get_metadata_for("delegation_penumbravalid1abcdef123456", false) - }; - let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); - - assert_eq!(customized_metadata.symbol, "delUM(abcdef123456)"); - } - - #[test] - fn it_modifies_auction_nft_symbol_with_seq_num() { - let metadata = Metadata { - name: String::from(""), - symbol: String::from(""), - ..test_helpers::get_metadata_for( - "auctionnft_0_pauctid1jqyupqnzznyfpq940mv0ac33pyx77s7af3kgdw4nstjmp3567dks8n5amh", - true, - ) - }; - let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); - - assert_eq!( - customized_metadata.symbol, - "auction@0(jqyupqnzznyfpq940mv0ac33pyx77s7af3kgdw4nstjmp3567dks8n5amh)" - ); - - let metadata = Metadata { - name: String::from(""), - symbol: String::from(""), - ..test_helpers::get_metadata_for( - "auctionnft_123_pauctid1jqyupqnzznyfpq940mv0ac33pyx77s7af3kgdw4nstjmp3567dks8n5amh", - true, - ) - }; - let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); - - assert_eq!( - customized_metadata.symbol, - "auction@123(jqyupqnzznyfpq940mv0ac33pyx77s7af3kgdw4nstjmp3567dks8n5amh)" - ); - } - - #[test] - fn it_modifies_voting_receipt_token() { - let metadata = Metadata { - name: String::from(""), - symbol: String::from(""), - ..test_helpers::get_metadata_for("voted_on_234", false) - }; - let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); - - assert_eq!(customized_metadata.symbol, "VotedOn234"); - } -} - -#[cfg(test)] -mod customize_symbol_tests { - use super::*; - - #[test] - /// `customize_symbol` is just a thin wrapper around - /// `customize_symbol_inner` that allows metadata to be passed in as a byte - /// array. So we'll just do a basic test to make sure it works as expected, - /// rather than exercising every use case. - fn it_works() { - let metadata = Metadata { - name: String::from("Delegation Token"), - symbol: String::from(""), - ..test_helpers::get_metadata_for("delegation_penumbravalid1abcdef123456", false) - }; - let metadata_as_bytes = MetadataDomainType::try_from(metadata) - .unwrap() - .encode_to_vec(); - let customized_metadata_bytes = customize_symbol(&metadata_as_bytes).unwrap(); - let customized_metadata_result = - MetadataDomainType::decode::<&[u8]>(&customized_metadata_bytes); - let customized_metadata_proto = customized_metadata_result.unwrap().to_proto(); - - assert_eq!(customized_metadata_proto.symbol, "delUM(abcdef123456)"); - } -} diff --git a/packages/wasm/crate/tests/test_auction.rs b/packages/wasm/crate/tests/test_auction.rs new file mode 100644 index 0000000000..fa20ae1f58 --- /dev/null +++ b/packages/wasm/crate/tests/test_auction.rs @@ -0,0 +1,38 @@ +use ark_ff::Zero; +use decaf377::Fq; +use penumbra_asset::{ + asset::{Id, Metadata}, + Value, +}; +use penumbra_auction::auction::dutch::DutchAuctionDescription; +use penumbra_num::Amount; +use penumbra_proto::DomainType; +use penumbra_wasm::auction::{get_auction_id, get_auction_nft_metadata}; +use wasm_bindgen_test::wasm_bindgen_test; + +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + +#[wasm_bindgen_test] +fn it_gets_correct_id_and_metadata() { + let description = DutchAuctionDescription { + start_height: 0, + end_height: 100, + input: Value { + amount: Amount::default(), + asset_id: Id(Fq::zero()), + }, + min_output: Amount::default(), + max_output: Amount::default(), + nonce: [0; 32], + output_id: Id(Fq::zero()), + step_count: 100u64, + }; + + let auction_id_bytes = get_auction_id(&description.encode_to_vec()).unwrap(); + let result_bytes = get_auction_nft_metadata(&auction_id_bytes, 1234).unwrap(); + let result = Metadata::decode::<&[u8]>(&result_bytes).unwrap(); + let result_proto = result.to_proto(); + + assert!(result_proto.symbol.starts_with("auction@1234(")); + assert!(result_proto.display.starts_with("auctionnft_1234_pauctid1")); +} diff --git a/packages/wasm/crate/tests/test_metadata.rs b/packages/wasm/crate/tests/test_metadata.rs new file mode 100644 index 0000000000..c88d89c334 --- /dev/null +++ b/packages/wasm/crate/tests/test_metadata.rs @@ -0,0 +1,164 @@ +use penumbra_asset::asset::Metadata as MetadataDomainType; +use penumbra_proto::core::asset::v1::DenomUnit; +use penumbra_proto::{core::asset::v1::Metadata, DomainType}; +use wasm_bindgen_test::wasm_bindgen_test; + +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + +pub fn get_metadata_for(display_denom: &str, base_denom_is_display_denom: bool) -> Metadata { + let mut denom_units = Vec::new(); + denom_units.push(DenomUnit { + aliases: Vec::new(), + denom: if base_denom_is_display_denom { + String::from(display_denom) + } else { + format!("u{display_denom}") + }, + exponent: 0, + }); + + if !base_denom_is_display_denom { + denom_units.push(DenomUnit { + aliases: Vec::new(), + denom: String::from(display_denom), + exponent: 6, + }); + } + + Metadata { + base: if base_denom_is_display_denom { + String::from(display_denom) + } else { + format!("u{display_denom}") + }, + description: String::from(""), + denom_units, + display: String::from(display_denom), + images: Vec::new(), + name: String::from(""), + penumbra_asset_id: None, + symbol: String::from(""), + priority_score: 0, + } +} + +#[wasm_bindgen_test] +fn it_interpolates_display_denom() { + assert_eq!(get_metadata_for("penumbra", false).base, "upenumbra"); + assert_eq!(get_metadata_for("penumbra", false).display, "penumbra"); + assert_eq!( + get_metadata_for("penumbra", false).denom_units[0].denom, + "upenumbra" + ); + assert_eq!( + get_metadata_for("penumbra", false).denom_units[1].denom, + "penumbra" + ); +} + +use penumbra_wasm::metadata::customize_symbol_inner; + +#[wasm_bindgen_test] +fn it_returns_non_staking_metadata_as_is() { + let metadata = Metadata { + name: String::from("Penumbra"), + symbol: String::from("UM"), + ..get_metadata_for("penumbra", false) + }; + let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); + + assert_eq!(metadata, customized_metadata); +} + +#[wasm_bindgen_test] +fn it_modifies_unbonding_token_symbol() { + let metadata = Metadata { + name: String::from("Unbonding Token"), + symbol: String::from(""), + ..get_metadata_for("unbonding_start_at_1234_penumbravalid1abcdef123456", false) + }; + let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); + + assert_eq!(customized_metadata.symbol, "unbondUMat1234(abcdef123456)"); +} + +#[wasm_bindgen_test] +fn it_modifies_delegation_token_symbol() { + let metadata = Metadata { + name: String::from("Delegation Token"), + symbol: String::from(""), + ..get_metadata_for("delegation_penumbravalid1abcdef123456", false) + }; + let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); + + assert_eq!(customized_metadata.symbol, "delUM(abcdef123456)"); +} + +#[wasm_bindgen_test] +fn it_modifies_auction_nft_symbol_with_seq_num() { + let metadata = Metadata { + name: String::from(""), + symbol: String::from(""), + ..get_metadata_for( + "auctionnft_0_pauctid1jqyupqnzznyfpq940mv0ac33pyx77s7af3kgdw4nstjmp3567dks8n5amh", + true, + ) + }; + let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); + + assert_eq!( + customized_metadata.symbol, + "auction@0(jqyupqnzznyfpq940mv0ac33pyx77s7af3kgdw4nstjmp3567dks8n5amh)" + ); + + let metadata = Metadata { + name: String::from(""), + symbol: String::from(""), + ..get_metadata_for( + "auctionnft_123_pauctid1jqyupqnzznyfpq940mv0ac33pyx77s7af3kgdw4nstjmp3567dks8n5amh", + true, + ) + }; + let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); + + assert_eq!( + customized_metadata.symbol, + "auction@123(jqyupqnzznyfpq940mv0ac33pyx77s7af3kgdw4nstjmp3567dks8n5amh)" + ); +} + +#[wasm_bindgen_test] +fn it_modifies_voting_receipt_token() { + let metadata = Metadata { + name: String::from(""), + symbol: String::from(""), + ..get_metadata_for("voted_on_234", false) + }; + let customized_metadata = customize_symbol_inner(metadata.clone()).unwrap(); + + assert_eq!(customized_metadata.symbol, "VotedOn234"); +} + +use penumbra_wasm::metadata::customize_symbol; + +#[wasm_bindgen_test] +/// `customize_symbol` is just a thin wrapper around +/// `customize_symbol_inner` that allows metadata to be passed in as a byte +/// array. So we'll just do a basic test to make sure it works as expected, +/// rather than exercising every use case. +fn it_works() { + let metadata = Metadata { + name: String::from("Delegation Token"), + symbol: String::from(""), + ..get_metadata_for("delegation_penumbravalid1abcdef123456", false) + }; + let metadata_as_bytes = MetadataDomainType::try_from(metadata) + .unwrap() + .encode_to_vec(); + let customized_metadata_bytes = customize_symbol(&metadata_as_bytes).unwrap(); + let customized_metadata_result = + MetadataDomainType::decode::<&[u8]>(&customized_metadata_bytes); + let customized_metadata_proto = customized_metadata_result.unwrap().to_proto(); + + assert_eq!(customized_metadata_proto.symbol, "delUM(abcdef123456)"); +}