use aleo_rust_sdk::AleoAccount;
use aleo_rust_sdk::AleoClient;
use aleo_rust_sdk::AleoHttpClient;
use aleo_rust_sdk::AleoProgram;
use aleo_rust_sdk::ExecutionEngine;
use snarkvm::prelude::{FromStr, TestRng};
#[test]
fn test_account_creation() {
let mut rng = TestRng::default();
let account = AleoAccount::new_random(&mut rng).unwrap();
assert!(!account.private_key_str().is_empty());
assert!(!account.view_key.to_string().is_empty());
assert!(!account.address_str().is_empty());
assert_ne!(account.private_key_str(), account.address_str());
}
#[test]
fn test_account_from_private_key() {
let mut rng = TestRng::default();
let a1 = AleoAccount::new_random(&mut rng).unwrap();
let pk = a1.private_key_str();
let a2 = AleoAccount::from_private_key_str(&pk).unwrap();
assert_eq!(a1.address_str(), a2.address_str());
assert_eq!(a1.view_key.to_string(), a2.view_key.to_string());
}
#[test]
fn test_credits_program_id() {
let program = AleoProgram::credits().unwrap();
assert_eq!(
program.id().to_string(),
"credits.aleo",
"Built-in credits program must be credits.aleo"
);
}
#[test]
fn test_program_from_source() {
let src = r#"
program hello.aleo;
function greet:
input r0 as u32.public;
output r0 as u32.public;
"#;
let program = AleoProgram::from_source(src).unwrap();
assert_eq!(program.id().to_string(), "hello.aleo");
}
#[test]
fn test_execution_engine_new() {
let engine = ExecutionEngine::new().unwrap();
let guard = engine.process.lock();
let pids = guard.program_ids();
assert!(
pids.contains(&snarkvm::console::program::ProgramID::from_str("credits.aleo").unwrap()),
"Built-in credits.aleo program should be loaded"
);
}
#[test]
fn test_http_client_creation() {
let client = AleoHttpClient::new("https://api.explorer.provable.com/v2/testnet").unwrap();
assert_eq!(
client.base_url,
"https://api.explorer.provable.com/v2/testnet"
);
}
#[test]
fn test_client_new() {
let client = AleoClient::new("https://api.explorer.provable.com/v2/testnet").unwrap();
assert!(
client.require_account().is_err(),
"Fresh client should have no account"
);
}
#[test]
fn test_client_set_account() {
let mut rng = TestRng::default();
let account = AleoAccount::new_random(&mut rng).unwrap();
let pk = account.private_key_str();
let mut client = AleoClient::new("https://api.explorer.provable.com/v2/testnet").unwrap();
client.set_account_from_private_key_str(&pk).unwrap();
assert!(client.require_account().is_ok());
assert_eq!(
client.require_account().unwrap().address_str(),
account.address_str()
);
}
#[test]
fn test_client_program_load() {
let mut client = AleoClient::new("https://api.explorer.provable.com/v2/testnet").unwrap();
client.load_program_from_source(
"program hello.aleo;\nfunction f:\n input r0 as u32.public;\n output r0 as u32.public;\n",
).unwrap();
assert!(client.require_program().is_ok());
}
#[test]
fn test_client_credits_program() {
let credits = AleoProgram::credits().unwrap();
let mut client = AleoClient::new("https://api.explorer.provable.com/v2/testnet").unwrap();
client.set_program(credits);
assert_eq!(
client.require_program().unwrap().id().to_string(),
"credits.aleo"
);
}