Skip to content

Commit

Permalink
tests: setup basic wasm-bindgen tests for the sdk (#1614)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard authored Nov 22, 2024
1 parent bc629a1 commit 3f32f19
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci-sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ jobs:
- name: Install wasm-pack
run: npm install -g wasm-pack

- name: Run wasm-bindgen-test
run: wasm-pack test --node components/clarinet-sdk-wasm

- name: Build Wasm packages
run: npm run build:sdk-wasm

Expand Down
57 changes: 57 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ reqwest = { version = "0.12", default-features = false, features = [
"json",
"rustls-tls",
] }
web-sys = { version = "0.3" }
wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3" }

[patch.crates-io]
chainhook-sdk = { git = "https://github.com/hirosystems/chainhook.git" }
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ clarity-repl = { path = "../clarity-repl", default-features = false, optional =
js-sys = { version = "0.3", optional = true }
serde-wasm-bindgen = { version = "0.6.4", optional = true }
wasm-bindgen = { workspace = true, optional = true }
wasm-bindgen-futures = { version = "0.4.41", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
serde_json = "1.0.114"

[features]
Expand Down
6 changes: 5 additions & 1 deletion components/clarinet-sdk-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
serde-wasm-bindgen = { version = "0.6.4", optional = true }
wasm-bindgen = { workspace = true, optional = true }
wasm-bindgen-futures = { version = "0.4.41", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
web-sys = { workspace = true, features = ["console"], optional = true }
colored = "2.1.0"

clarinet-files = { path = "../clarinet-files", default-features = false }
clarity-repl = { path = "../clarity-repl", default-features = false, optional = true }
clarinet-deployments = { path = "../clarinet-deployments", default-features = false }

[dev-dependencies]
wasm-bindgen-test = "0.3.0"
clarity = { workspace = true }

[features]
default = ["wasm"]
wasm = [
Expand Down
8 changes: 8 additions & 0 deletions components/clarinet-sdk-wasm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ extern "C" {
pub type IContractInterfaces;
}

impl EpochString {
pub fn new(obj: &str) -> Self {
Self {
obj: JsValue::from_str(obj),
}
}
}

macro_rules! log {
( $( $t:tt )* ) => {
web_sys::console::log_1(&format!( $( $t )* ).into());
Expand Down
3 changes: 3 additions & 0 deletions components/clarinet-sdk-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ pub mod core;
mod ts_types;

mod utils;

#[cfg(all(target_arch = "wasm32", test))]
mod test_wasm;
63 changes: 63 additions & 0 deletions components/clarinet-sdk-wasm/src/test_wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use super::core::DeployContractArgs;
use crate::core::{CallFnArgs, ContractOptions, EpochString, TransactionRes, SDK};
use clarity::vm::Value as ClarityValue;
use js_sys::Function as JsFunction;
use wasm_bindgen_test::*;

async fn init_sdk() -> SDK {
let js_noop = JsFunction::new_no_args("return");
let mut sdk = SDK::new(js_noop, None);
let _ = sdk.init_empty_session().await;
sdk.set_epoch(EpochString::new("3.0"));
sdk
}

#[track_caller]
fn deploy_basic_contract(sdk: &mut SDK) -> TransactionRes {
let contract = DeployContractArgs::new(
"basic-contract".into(),
"(define-private (two) (+ u1 u1))".into(),
ContractOptions::new(None),
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM".into(),
);
sdk.deploy_contract(&contract).unwrap()
}

#[wasm_bindgen_test]
async fn it_cn_execute_clarity_code() {
let mut sdk = init_sdk().await;
let tx = sdk.execute("(+ u41 u1)".into()).unwrap();
let expected = format!("0x{}", ClarityValue::UInt(42).serialize_to_hex().unwrap());
assert_eq!(tx.result, expected);
}

#[wasm_bindgen_test]
async fn it_can_set_epoch() {
let mut sdk = init_sdk().await;
assert_eq!(sdk.block_height(), 1);
assert_eq!(sdk.current_epoch(), "3.0");
}

#[wasm_bindgen_test]
async fn it_can_deploy_contract() {
let mut sdk = init_sdk().await;
let tx = deploy_basic_contract(&mut sdk);
let expected = format!("0x{}", ClarityValue::Bool(true).serialize_to_hex().unwrap());
assert_eq!(tx.result, expected);
}

#[wasm_bindgen_test]
async fn it_can_call_a_private_function() {
let mut sdk = init_sdk().await;
let _ = deploy_basic_contract(&mut sdk);
let tx = sdk
.call_private_fn(&CallFnArgs::new(
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.basic-contract".into(),
"two".into(),
vec![],
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM".into(),
))
.unwrap();
let expected = format!("0x{}", ClarityValue::UInt(2).serialize_to_hex().unwrap());
assert_eq!(tx.result, expected);
}
6 changes: 2 additions & 4 deletions components/clarinet-sdk/clarinet-sdk.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
"rust-analyzer.check.overrideCommand": [
"cargo",
"clippy",
"--no-default-features",
"--package=clarinet-sdk-wasm",
"--features=wasm",
"--target=wasm32-unknown-unknown",
"--tests",
"--message-format=json",
],
"rust-analyzer.cargo.allTargets": false,
Expand All @@ -16,10 +15,9 @@
"rust-analyzer.cargo.buildScripts.overrideCommand": [
"cargo",
"build",
"--no-default-features",
"--package=clarinet-sdk-wasm",
"--features=wasm",
"--target=wasm32-unknown-unknown",
"--tests",
"--message-format=json",
],
},
Expand Down
2 changes: 1 addition & 1 deletion components/clarity-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ console_error_panic_hook = { version = "0.1", optional = true }
js-sys = { version = "0.3", optional = true }
serde-wasm-bindgen = { version = "0.6.4", optional = true }
wasm-bindgen = { workspace = true, optional = true }
wasm-bindgen-futures = { version = "0.4.41", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
web-sys = { workspace = true, features = ["console"], optional = true }

[features]
Expand Down

0 comments on commit 3f32f19

Please sign in to comment.