Skip to content

Commit

Permalink
fix: test wasm crate in ci correctly (#1616)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
VanishMax and vacekj authored Aug 2, 2024
1 parent 6d02104 commit 55c5f69
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 216 deletions.
42 changes: 0 additions & 42 deletions packages/wasm/crate/src/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,3 @@ pub fn get_auction_nft_metadata(auction_id: &[u8], seq: u64) -> WasmResult<Vec<u

Ok(metadata_domain_type.encode_to_vec())
}

#[cfg(test)]
mod tests {
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 crate::auction::get_auction_id;

use super::get_auction_nft_metadata;

#[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"));
}
}
174 changes: 0 additions & 174 deletions packages/wasm/crate/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,177 +97,3 @@ fn get_proposal_id(captures: &regex::Captures) -> WasmResult<String> {
.ok_or_else(|| anyhow!("<proposal_id> 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)");
}
}
38 changes: 38 additions & 0 deletions packages/wasm/crate/tests/test_auction.rs
Original file line number Diff line number Diff line change
@@ -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"));
}
Loading

0 comments on commit 55c5f69

Please sign in to comment.