Skip to content

Commit

Permalink
feat: separate rust unit and wasm-pack tests (#1662)
Browse files Browse the repository at this point in the history
* feat: separate rust unit and wasm-pack tests

* feat: rename
  • Loading branch information
vacekj authored Aug 12, 2024
1 parent 6fd8ce6 commit 380a013
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 211 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/turbo-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ jobs:
- run: pnpm turbo lint:rust --cache-dir=.turbo

turbo-test-rust:
name: test:rust
name: test:wasm
runs-on: buildjet-16vcpu-ubuntu-2204
needs: turbo-compile
steps:
Expand All @@ -126,7 +126,7 @@ jobs:
uses: buildjet/cache@v4
with:
path: .turbo
key: ${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.ref }}-${{ github.sha }}-test:rust
key: ${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.ref }}-${{ github.sha }}-test:wasm
restore-keys: ${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.ref }}-${{ github.sha }}-compiled
- uses: pnpm/action-setup@v4
- uses: buildjet/setup-node@v4
Expand All @@ -142,4 +142,5 @@ jobs:
version: 'latest'
- uses: browser-actions/setup-firefox@v1
- run: pnpm turbo telemetry disable
- run: pnpm turbo test:rust --cache-dir=.turbo
- run: pnpm turbo test:wasm --cache-dir=.turbo
- run: pnpm turbo test:cargo --cache-dir=.turbo
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repo, in which case they use turbo or the monorepo configuration.
- `pnpm build`: transform and bundle all packages and apps.
- `pnpm dev`: build all, serve local apps. watch and rebuild continuously.
- `pnpm test`: run vitest only. cargo tests are omitted.
- `pnpm test:rust`: run cargo tests only.
- `pnpm test:wasm`: run rust tests only.
- `pnpm format`, `pnpm lint`
- `pnpm all-check`: check all!

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"packageManager": "[email protected]+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a",
"type": "module",
"scripts": {
"all-check": "pnpm clean:vitest-mjs && pnpm install && pnpm compile && pnpm lint:strict && pnpm lint:rust && pnpm build && pnpm test && pnpm test:rust",
"all-check": "pnpm clean:vitest-mjs && pnpm install && pnpm compile && pnpm lint:strict && pnpm lint:rust && pnpm build && pnpm test && pnpm test:wasm",
"all-check:clean": "pnpm clean && pnpm clean:modules && pnpm all-check",
"build": "turbo build",
"clean": "turbo clean",
Expand All @@ -29,7 +29,7 @@
"postinstall": "syncpack list-mismatches",
"pretest": "playwright install",
"test": "turbo test",
"test:rust": "turbo test:rust"
"test:wasm": "turbo test:wasm"
},
"dependencies": {
"@buf/connectrpc_eliza.bufbuild_es": "1.10.0-20230913231627-233fca715f49.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ executed with `wasm-bindgen-test`. You can run both with package scripts,

```sh
pnpm test
pnpm test:rust
pnpm test:wasm
```
42 changes: 42 additions & 0 deletions packages/wasm/crate/src/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,45 @@ 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: 174 additions & 0 deletions packages/wasm/crate/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,177 @@ 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: 0 additions & 38 deletions packages/wasm/crate/tests/test_auction.rs

This file was deleted.

Loading

0 comments on commit 380a013

Please sign in to comment.