sdk_client/tests/merkle_proof.rs
2025-11-27 16:59:07 +01:00

163 lines
5.7 KiB
Rust

use wasm_bindgen_test::*;
use sdk_client::api::*;
use sdk_common::pcd::{Pcd, PcdCommitments, Roles};
use sdk_common::spdk_core::bitcoin::{Network, OutPoint};
use std::collections::BTreeMap;
use serde_json::json;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn test_get_merkle_proof() {
// This is a basic test to ensure the function compiles and can be called
// We'll create a mock process state object and test the function
// Create a mock process state object (this will fail deserialization which is expected)
let mock_process_state = JsValue::from_str("invalid_process_state");
let result = get_merkle_proof(
mock_process_state,
"test_attribute".to_string()
);
// This should fail with a deserialization error, which is expected
assert!(result.is_err());
}
#[wasm_bindgen_test]
fn test_get_merkle_proof_with_valid_state() {
// This test would create a valid ProcessState and test the actual merkle proof generation
// For now, we'll just test that the function exists and can be called with valid parameters
// Create a minimal valid process state structure
let process_state_json = json!({
"state_id": "0000000000000000000000000000000000000000000000000000000000000000",
"pcd_commitment": {
"test_attribute": "0000000000000000000000000000000000000000000000000000000000000000"
},
"roles": {},
"public_data": {},
"keys": {},
"validation_tokens": [],
"commited_in": "0000000000000000000000000000000000000000000000000000000000000000:0"
});
let process_state = serde_wasm_bindgen::to_value(&process_state_json).unwrap();
let result = get_merkle_proof(
process_state,
"test_attribute".to_string()
);
// This should work if the ProcessState structure is correct
// For now, we just test that the function can be called
assert!(result.is_ok() || result.is_err()); // Either outcome is acceptable for this test
}
#[wasm_bindgen_test]
fn test_get_merkle_proof_invalid_state_id() {
// Create a mock process state object
let mock_process_state = JsValue::from_str("invalid_process_state");
// Test with invalid process state (should fail deserialization)
let result = get_merkle_proof(
mock_process_state,
"test_attribute".to_string()
);
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.message, "Failed to deserialize process state");
}
#[wasm_bindgen_test]
fn test_get_merkle_proof_with_real_process() {
// Reset device to start with a clean state
reset_device().unwrap();
// Create a new device
create_new_device(0, "signet".to_string()).unwrap();
// Create some test data
let test_data = json!({
"name": "test_name",
"value": "test_value",
"number": 42
});
// Encode the data as PCD
let pcd = encode_json(JsValue::from_serde(&test_data).unwrap()).unwrap();
// Create roles
let roles = Roles::new(BTreeMap::new());
// Create a mock relay address (this would normally be a real address)
let relay_address = "sprt1qqfmqt0ngq99y8t4ke6uhtm2a2vc2zxvhj7hjrqu599kn30d4cs9rwqn6n079mdr4dfqg72yrtvuxf43yswscw86nvvl09mc5ljx65vfh75fkza35".to_string();
// Create an empty members list
let members_list = OutPointMemberMap::new();
// Create a new process
let result = create_new_process(
pcd.clone(),
roles.clone(),
pcd.clone(),
relay_address,
1, // fee_rate
members_list
);
// The process creation might fail due to missing relay or other dependencies,
// but we can still test the merkle proof function structure
if let Ok(api_return) = result {
if let Some(updated_process) = api_return.updated_process {
let process = updated_process.current_process;
let state = process.get_latest_commited_state().unwrap();
// Convert the process state to JsValue for the function call
let state_js = serde_wasm_bindgen::to_value(&state).unwrap();
// Now test the merkle proof generation
let proof_result = get_merkle_proof(
state_js,
"name".to_string()
);
// The proof should be generated successfully
assert!(proof_result.is_ok());
let proof = proof_result.unwrap();
// Check that the proof struct has the expected fields
assert!(!proof.proof.is_empty());
assert!(!proof.root.is_empty());
assert_eq!(proof.attribute, "name");
assert!(proof.attribute_index >= 0);
assert!(proof.total_leaves_count > 0);
}
}
}
#[wasm_bindgen_test]
fn test_validate_merkle_proof() {
// Create a mock MerkleProofResult
let mock_proof_result = MerkleProofResult {
proof: "0000000000000000000000000000000000000000000000000000000000000000".to_string(),
root: "0000000000000000000000000000000000000000000000000000000000000000".to_string(),
attribute: "test_attribute".to_string(),
attribute_index: 0,
total_leaves_count: 1,
};
// Create mock hash data
let mock_hash = "0000000000000000000000000000000000000000000000000000000000000000".to_string();
// Test the function (this will likely fail validation, which is expected)
let result = validate_merkle_proof(mock_proof_result, mock_hash);
// The function should return a boolean result
assert!(result.is_ok());
let is_valid = result.unwrap();
// For now, we expect it to be false since we're using dummy data
assert!(!is_valid);
}