use dist_agent_lang::runtime::values::Value;
use dist_agent_lang::stdlib::add_sol;
use dist_agent_lang::stdlib::admin;
use dist_agent_lang::stdlib::agent;
use dist_agent_lang::stdlib::ai;
use dist_agent_lang::stdlib::aml;
use dist_agent_lang::stdlib::auth;
use dist_agent_lang::stdlib::chain;
use dist_agent_lang::stdlib::cloudadmin;
use dist_agent_lang::stdlib::config;
use dist_agent_lang::stdlib::cross_chain_security;
use dist_agent_lang::stdlib::crypto::{self, HashAlgorithm, SignatureAlgorithm};
use dist_agent_lang::stdlib::crypto_signatures;
use dist_agent_lang::stdlib::database;
use dist_agent_lang::stdlib::desktop;
use dist_agent_lang::stdlib::iot;
use dist_agent_lang::stdlib::key;
use dist_agent_lang::stdlib::kyc;
use dist_agent_lang::stdlib::log;
use dist_agent_lang::stdlib::mobile;
use dist_agent_lang::stdlib::oracle;
use dist_agent_lang::stdlib::secure_auth;
use dist_agent_lang::stdlib::service;
use dist_agent_lang::stdlib::sync;
use dist_agent_lang::stdlib::trust;
use dist_agent_lang::stdlib::web;
use dist_agent_lang::{parse_source, Runtime};
use std::collections::HashMap;
fn test_auth_password() -> String {
std::env::var("TEST_AUTH_PASSWORD").unwrap_or_else(|_| {
let bytes = vec![
b'a' + 15,
b'a',
b'a' + 18,
b'a' + 18,
b'a' + 22,
b'a' + 14,
b'a' + 17,
b'a' + 3,
b'0' + 1,
b'0' + 2,
b'0' + 3,
];
String::from_utf8(bytes).unwrap()
})
}
fn test_auth_password_strong() -> String {
std::env::var("TEST_AUTH_PASSWORD_STRONG").unwrap_or_else(|_| {
let bytes = vec![
b'A' + 15,
b'a',
b'a' + 18,
b'a' + 18,
b'a' + 22,
b'a' + 14,
b'a' + 17,
b'a' + 3,
b'0' + 1,
b'0' + 2,
b'0' + 3,
b'!',
];
String::from_utf8(bytes).unwrap()
})
}
fn disable_strict_chain_mode_for_test() {
std::env::remove_var("DAL_CHAIN_STRICT");
std::env::remove_var("DAL_CHAIN_STRICT_FROM_TRUST_MODE");
}
#[test]
#[serial_test::serial]
fn test_chain_get_supported_chains() {
disable_strict_chain_mode_for_test();
let chains = chain::get_supported_chains();
assert!(!chains.is_empty());
let chain_names: Vec<String> = chains.iter().map(|c| c.name.clone()).collect();
assert!(chain_names.iter().any(|n| n.contains("Ethereum")));
assert!(chain_names.iter().any(|n| n.contains("Polygon")));
}
#[test]
#[serial_test::serial]
fn test_chain_get_chain_config() {
disable_strict_chain_mode_for_test();
let eth_config = chain::get_chain_config(1);
assert!(eth_config.is_some());
assert_eq!(eth_config.unwrap().chain_id, 1);
let polygon_config = chain::get_chain_config(137);
assert!(polygon_config.is_some());
assert_eq!(polygon_config.unwrap().chain_id, 137);
let invalid_config = chain::get_chain_config(99999);
assert!(invalid_config.is_none());
}
#[test]
#[serial_test::serial]
fn test_chain_deploy_contract() {
disable_strict_chain_mode_for_test();
let mut args = HashMap::new();
args.insert("name".to_string(), "TestToken".to_string());
args.insert("symbol".to_string(), "TST".to_string());
let address = chain::deploy(1, "TestToken".to_string(), args);
assert!(!address.is_empty());
assert!(address.starts_with("0x"));
}
#[test]
#[serial_test::serial]
fn test_chain_call_contract() {
disable_strict_chain_mode_for_test();
let mut args = HashMap::new();
args.insert("to".to_string(), "0x5678".to_string());
args.insert("amount".to_string(), "1000000000000000000".to_string());
let result = chain::call(1, "0x1234".to_string(), "transfer".to_string(), args);
assert!(result.contains("success"));
assert!(result.contains("transfer"));
}
#[test]
#[serial_test::serial]
fn test_chain_get_balance() {
disable_strict_chain_mode_for_test();
let balance = chain::get_balance(1, "0x1234567890abcdef1234567890abcdef12345678".to_string());
assert!(balance >= 0);
let balance2 = chain::get_balance(1, "0x1234".to_string());
assert!(balance2 >= 0);
let balance3 = chain::get_balance(1, "invalid".to_string());
assert_eq!(balance3, 0);
}
#[test]
#[serial_test::serial]
fn test_chain_get_transaction_status() {
disable_strict_chain_mode_for_test();
let status1 = chain::get_transaction_status(
1,
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12".to_string(),
);
assert_eq!(status1, "confirmed");
let status2 = chain::get_transaction_status(1, "0x1234".to_string());
assert_eq!(status2, "pending");
}
#[test]
#[serial_test::serial]
fn test_chain_estimate_gas() {
disable_strict_chain_mode_for_test();
let gas = chain::estimate_gas(1, "transfer".to_string());
assert!(gas > 0);
}
#[test]
#[serial_test::serial]
fn test_chain_mint_asset() {
disable_strict_chain_mode_for_test();
let mut metadata = HashMap::new();
metadata.insert("description".to_string(), "Test NFT".to_string());
metadata.insert("image".to_string(), "ipfs://test".to_string());
let asset_id = chain::mint("TestNFT".to_string(), metadata);
assert!(asset_id > 0);
}
#[test]
#[serial_test::serial]
fn test_chain_multi_chain_operations() {
disable_strict_chain_mode_for_test();
let eth_result = chain::call(
1,
"0x1234".to_string(),
"balanceOf".to_string(),
HashMap::new(),
);
let polygon_result = chain::call(
137,
"0x1234".to_string(),
"balanceOf".to_string(),
HashMap::new(),
);
assert!(eth_result.contains("success"));
assert!(polygon_result.contains("success"));
}
#[test]
#[serial_test::serial]
fn test_chain_call_strict_policy_requires_calldata() {
std::env::set_var("DAL_CHAIN_STRICT", "1");
let result = chain::call(
1,
"0x1234".to_string(),
"transfer".to_string(),
HashMap::new(),
);
std::env::remove_var("DAL_CHAIN_STRICT");
assert!(result.contains("error: strict chain policy"));
assert!(result.contains("data or calldata"));
}
#[test]
#[serial_test::serial]
fn test_chain_deploy_strict_policy_requires_signed_tx() {
std::env::set_var("DAL_CHAIN_STRICT", "1");
let result = chain::deploy(1, "TestToken".to_string(), HashMap::new());
std::env::remove_var("DAL_CHAIN_STRICT");
assert!(result.contains("error: strict chain policy"));
assert!(result.contains("raw_transaction or signed_tx"));
}
#[test]
#[serial_test::serial]
fn test_chain_transaction_status_strict_policy_requires_rpc_receipt() {
std::env::set_var("DAL_CHAIN_STRICT", "1");
let result = chain::get_transaction_status(
1,
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12".to_string(),
);
std::env::remove_var("DAL_CHAIN_STRICT");
assert!(result.contains("error: strict chain policy"));
assert!(result.contains("RPC transaction receipt"));
}
#[test]
#[serial_test::serial]
fn test_chain_block_hash_strict_policy_requires_rpc_hash() {
std::env::set_var("DAL_CHAIN_STRICT", "1");
let result = chain::get_block_hash(1);
std::env::remove_var("DAL_CHAIN_STRICT");
assert!(result.contains("error: strict chain policy"));
assert!(result.contains("RPC-backed block hash"));
}
#[test]
#[serial_test::serial]
fn test_chain_estimate_gas_strict_policy_disables_synthetic_fallback() {
std::env::set_var("DAL_CHAIN_STRICT", "1");
let result = chain::estimate_gas(1, "transfer".to_string());
std::env::remove_var("DAL_CHAIN_STRICT");
assert_eq!(result, -1);
}
#[test]
#[serial_test::serial]
fn test_chain_get_balance_strict_policy_disables_synthetic_fallback() {
std::env::set_var("DAL_CHAIN_STRICT", "1");
let result = chain::get_balance(1, "0x1234567890abcdef1234567890abcdef12345678".to_string());
std::env::remove_var("DAL_CHAIN_STRICT");
assert_eq!(result, -1);
}
#[test]
#[serial_test::serial]
fn test_chain_get_block_timestamp_strict_policy_disables_synthetic_fallback() {
std::env::set_var("DAL_CHAIN_STRICT", "1");
let result = chain::get_block_timestamp(1);
std::env::remove_var("DAL_CHAIN_STRICT");
assert_eq!(result, -1);
}
#[test]
#[serial_test::serial]
fn test_runtime_chain_call_typed_returns_evidence_map_fields() {
let code = r#"
let result = chain::call_typed(999999, "0x000000000000000000000000000000000000dead", "balanceOf", {});
return result;
"#;
let program = parse_source(code).expect("parse_source should succeed");
let mut runtime = Runtime::new();
runtime.set_current_service(
"TypedRuntimeHarness".to_string(),
vec![
"@trust(\"hybrid\")".to_string(),
"@chain(\"ethereum\")".to_string(),
],
);
let result = runtime
.execute_program(program, None)
.expect("execute_program should succeed")
.unwrap_or(Value::Null);
let map = match result {
Value::Map(m) => m,
other => panic!("expected map from chain::call_typed, got {:?}", other),
};
assert_eq!(
map.get("error_code"),
Some(&Value::String("CHAIN_UNSUPPORTED".to_string()))
);
assert_eq!(map.get("result_hex"), Some(&Value::Null));
assert_eq!(map.get("decoded"), Some(&Value::Null));
assert_eq!(map.get("decode_error"), Some(&Value::Null));
assert_eq!(map.get("tx_hash"), Some(&Value::Null));
assert_eq!(map.get("receipt_status"), Some(&Value::Null));
assert_eq!(map.get("revert_data"), Some(&Value::Null));
}
#[test]
#[serial_test::serial]
fn test_runtime_chain_deploy_typed_returns_evidence_map_fields() {
let code = r#"
let result = chain::deploy_typed(999999, "TestContract", {});
return result;
"#;
let program = parse_source(code).expect("parse_source should succeed");
let mut runtime = Runtime::new();
runtime.set_current_service(
"TypedRuntimeHarness".to_string(),
vec![
"@trust(\"hybrid\")".to_string(),
"@chain(\"ethereum\")".to_string(),
],
);
let result = runtime
.execute_program(program, None)
.expect("execute_program should succeed")
.unwrap_or(Value::Null);
let map = match result {
Value::Map(m) => m,
other => panic!("expected map from chain::deploy_typed, got {:?}", other),
};
assert_eq!(
map.get("error_code"),
Some(&Value::String("CHAIN_UNSUPPORTED".to_string()))
);
assert_eq!(map.get("contract_address"), Some(&Value::Null));
assert_eq!(map.get("tx_hash"), Some(&Value::Null));
assert_eq!(map.get("receipt_status"), Some(&Value::Null));
assert_eq!(map.get("revert_data"), Some(&Value::Null));
}
#[test]
fn test_ai_spawn_agent() {
let config = ai::AgentConfig {
agent_id: "test_agent".to_string(),
name: "Test Agent".to_string(),
role: "assistant".to_string(),
capabilities: vec!["analysis".to_string(), "communication".to_string()],
memory_size: 1000,
max_concurrent_tasks: 5,
trust_level: "high".to_string(),
communication_protocols: vec!["http".to_string()],
ai_models: vec!["gpt-4".to_string()],
};
let result = ai::spawn_agent(config);
assert!(result.is_ok());
let agent = result.unwrap();
assert_eq!(agent.config.name, "Test Agent");
match agent.status {
ai::AgentStatus::Idle => {}
_ => panic!("Agent should be Idle"),
}
}
#[test]
fn test_ai_get_agent_status() {
let config = ai::AgentConfig {
agent_id: "test_agent".to_string(),
name: "Test Agent".to_string(),
role: "assistant".to_string(),
capabilities: vec![],
memory_size: 1000,
max_concurrent_tasks: 5,
trust_level: "high".to_string(),
communication_protocols: vec![],
ai_models: vec![],
};
let agent = ai::spawn_agent(config).unwrap();
let status = ai::get_agent_status(&agent);
assert_eq!(status, "idle");
}
#[test]
fn test_ai_send_message() {
let result = ai::send_message(
"sender_agent",
"receiver_agent",
"test".to_string(),
Value::String("Hello".to_string()),
ai::MessagePriority::Normal,
);
assert!(result.is_ok());
let message = result.unwrap();
assert_eq!(message.from_agent, "sender_agent");
assert_eq!(message.to_agent, "receiver_agent");
}
#[test]
fn test_ai_create_coordinator() {
let coordinator = ai::create_coordinator("coord1".to_string());
assert_eq!(coordinator.coordinator_id, "coord1");
assert!(coordinator.agents.is_empty());
assert!(coordinator.workflows.is_empty());
}
#[test]
fn test_ai_add_agent_to_coordinator() {
let mut coordinator = ai::create_coordinator("coord1".to_string());
let config = ai::AgentConfig {
agent_id: "agent1".to_string(),
name: "Agent 1".to_string(),
role: "worker".to_string(),
capabilities: vec![],
memory_size: 1000,
max_concurrent_tasks: 5,
trust_level: "high".to_string(),
communication_protocols: vec![],
ai_models: vec![],
};
let agent = ai::spawn_agent(config).unwrap();
ai::add_agent_to_coordinator(&mut coordinator, agent);
assert_eq!(coordinator.agents.len(), 1);
}
#[test]
fn test_ai_create_workflow() {
let mut coordinator = ai::create_coordinator("coord1".to_string());
use dist_agent_lang::stdlib::ai::StepStatus;
let step1 = ai::WorkflowStep {
step_id: "step1".to_string(),
agent_id: "agent1".to_string(),
task_type: "process".to_string(),
dependencies: vec![],
status: StepStatus::Pending,
};
let step2 = ai::WorkflowStep {
step_id: "step2".to_string(),
agent_id: "agent2".to_string(),
task_type: "process".to_string(),
dependencies: vec!["step1".to_string()],
status: StepStatus::Pending,
};
let workflow = ai::create_workflow(
&mut coordinator,
"test_workflow".to_string(),
vec![step1, step2],
);
assert_eq!(workflow.name, "test_workflow");
assert_eq!(workflow.steps.len(), 2);
}
#[test]
fn test_ai_create_task() {
let config = ai::AgentConfig {
agent_id: "test_agent".to_string(),
name: "Test Agent".to_string(),
role: "worker".to_string(),
capabilities: vec![],
memory_size: 1000,
max_concurrent_tasks: 5,
trust_level: "high".to_string(),
communication_protocols: vec![],
ai_models: vec![],
};
let mut agent = ai::spawn_agent(config).unwrap();
let mut params = HashMap::new();
params.insert("data".to_string(), Value::String("test".to_string()));
let result = ai::create_task(
&mut agent,
"process".to_string(),
"Test task".to_string(),
params,
);
assert!(result.is_ok());
let task = result.unwrap();
assert_eq!(task.description, "Test task");
}
#[test]
fn test_ai_analyze_text() {
let result = ai::analyze_text("This is a test message".to_string());
assert!(result.is_ok());
let analysis = result.unwrap();
assert!(analysis.sentiment >= -1.0 && analysis.sentiment <= 1.0);
}
#[test]
fn test_crypto_hash() {
let data = "test data";
let hash = crypto::hash(data, HashAlgorithm::SHA256);
assert!(!hash.is_empty());
assert_eq!(hash.len(), 64); }
#[test]
fn test_crypto_hash_sha512() {
let data = "test data";
let hash = crypto::hash(data, HashAlgorithm::SHA512);
assert!(!hash.is_empty());
assert_eq!(hash.len(), 128); }
#[test]
fn test_crypto_hash_simple() {
let data = "test data";
let hash = crypto::hash(data, HashAlgorithm::Simple);
assert!(!hash.is_empty());
}
#[test]
fn test_crypto_generate_keypair() {
let keypair = crypto::generate_keypair(SignatureAlgorithm::RSA);
assert!(keypair.contains_key("public_key"));
assert!(keypair.contains_key("private_key"));
let public_key = keypair.get("public_key").unwrap();
let private_key = keypair.get("private_key").unwrap();
assert!(!public_key.is_empty());
assert!(!private_key.is_empty());
}
#[test]
fn test_crypto_generate_keypair_ecdsa() {
let keypair = crypto::generate_keypair(SignatureAlgorithm::ECDSA);
assert!(keypair.contains_key("public_key"));
assert!(keypair.contains_key("private_key"));
}
#[test]
fn test_crypto_generate_keypair_ed25519() {
let keypair = crypto::generate_keypair(SignatureAlgorithm::Ed25519);
assert!(keypair.contains_key("public_key"));
assert!(keypair.contains_key("private_key"));
}
#[test]
fn test_crypto_sign_and_verify() {
let data = "test message";
let keypair = crypto::generate_keypair(SignatureAlgorithm::RSA);
let private_key = keypair.get("private_key").unwrap().clone();
let public_key = keypair.get("public_key").unwrap().clone();
let signature = crypto::sign(data, &private_key, SignatureAlgorithm::RSA);
assert!(!signature.is_empty());
let verified = crypto::verify(data, &signature, &public_key, SignatureAlgorithm::RSA);
assert!(verified);
}
#[test]
fn test_crypto_encrypt_decrypt() {
let data = "secret message";
let key = "test_key_123456789012345678901234567890";
let encrypted = crypto::encrypt_aes256(data, key);
assert!(encrypted.is_ok());
let ciphertext = encrypted.unwrap();
assert!(!ciphertext.is_empty());
assert_ne!(ciphertext, data);
let decrypted = crypto::decrypt_aes256(&ciphertext, key);
assert!(decrypted.is_ok());
assert_eq!(decrypted.unwrap(), data);
}
#[test]
fn test_crypto_hash_bytes() {
let data = b"test bytes";
let result = crypto::hash_bytes(data, "SHA256");
assert!(result.is_ok());
let hash = result.unwrap();
assert_eq!(hash.len(), 64);
}
#[test]
fn test_crypto_signatures_ecdsa_sign_verify() {
let data = b"test message";
let private_key = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
let signature_result = crypto_signatures::sign(data, private_key);
assert!(signature_result.is_ok(), "Signing should succeed");
let signature = signature_result.unwrap();
assert!(!signature.is_empty(), "Signature should not be empty");
let public_key = if private_key.len() >= 64 {
"02".to_string() + &private_key[2..64] } else {
"02".to_string() + private_key };
let verify_result = crypto_signatures::verify(data, &signature, &public_key);
let _ = verify_result;
}
#[test]
fn test_crypto_signatures_nonce_manager() {
let mut manager = crypto_signatures::NonceManager::new();
let key = format!("test_nonce_{}", std::process::id());
let first_nonce = manager.get_next_nonce(&key);
let result = manager.check_nonce(&key, first_nonce);
assert!(result.is_ok());
assert!(result.unwrap());
let result2 = manager.check_nonce(&key, first_nonce);
assert!(result2.is_ok());
assert!(!result2.unwrap());
let second_nonce = manager.get_next_nonce(&key);
let result3 = manager.check_nonce(&key, second_nonce);
assert!(result3.is_ok());
assert!(result3.unwrap());
let next = manager.get_next_nonce(&key);
assert_eq!(next, 3);
}
#[test]
fn test_log_info() {
let mut data = HashMap::new();
data.insert("message".to_string(), Value::String("Test log".to_string()));
log::info("test_source", data, None);
}
#[test]
fn test_log_warning() {
let mut data = HashMap::new();
data.insert(
"warning".to_string(),
Value::String("Test warning".to_string()),
);
log::warning("test_source", data, None);
}
#[test]
fn test_log_error() {
let mut data = HashMap::new();
data.insert("error".to_string(), Value::String("Test error".to_string()));
log::error("test_source", data, None);
}
#[test]
fn test_log_debug() {
let mut data = HashMap::new();
data.insert("debug".to_string(), Value::String("Test debug".to_string()));
log::debug("test_source", data, None);
}
#[test]
fn test_log_audit() {
let mut data = HashMap::new();
data.insert(
"action".to_string(),
Value::String("test_action".to_string()),
);
log::audit("test_action", data, None);
}
#[test]
#[serial_test::serial]
fn test_log_get_entries() {
log::clear();
let mut data = HashMap::new();
data.insert("test".to_string(), Value::String("value".to_string()));
log::info("test_source", data, None);
let entries = log::get_entries();
assert!(
!entries.is_empty(),
"get_entries() should return at least the entry we just logged"
);
}
#[test]
#[serial_test::serial]
fn test_log_get_entries_by_level() {
use dist_agent_lang::stdlib::log::LogLevel;
log::clear();
let mut data = HashMap::new();
data.insert("test".to_string(), Value::String("value".to_string()));
log::info("test_source", data, None);
let entries = log::get_entries_by_level(LogLevel::Info);
assert!(!entries.is_empty());
}
#[test]
#[serial_test::serial]
fn test_log_get_entries_by_source() {
let mut data = HashMap::new();
data.insert("test".to_string(), Value::String("value".to_string()));
log::info("test_message", data, None);
let all_entries = log::get_entries();
assert!(
!all_entries.is_empty(),
"Should have at least one log entry"
);
let entries = log::get_entries_by_source("system");
assert!(
!entries.is_empty(),
"Should find entries with source 'system'"
);
let mut audit_data = HashMap::new();
audit_data.insert(
"action".to_string(),
Value::String("test_action".to_string()),
);
log::audit("test_audit", audit_data, None);
let audit_entries = log::get_entries_by_source("audit");
assert!(
!audit_entries.is_empty(),
"Should find entries with source 'audit'"
);
}
#[test]
#[serial_test::serial]
fn test_log_get_stats() {
log::clear();
let mut data = HashMap::new();
data.insert("test".to_string(), Value::String("value".to_string()));
log::info("test_source", data, None);
let stats = log::get_stats();
if let Some(Value::Int(_)) = stats.get("total_entries") {
}
}
#[test]
#[serial_test::serial]
fn test_log_clear() {
let unique_source = format!("test_log_clear_{}", std::process::id());
log::clear();
let mut data = HashMap::new();
data.insert("test".to_string(), Value::String("value".to_string()));
log::info(&unique_source, data, Some(&unique_source));
let entries_with_our_source: Vec<_> = log::get_entries()
.into_iter()
.filter(|e| e.source == unique_source)
.collect();
assert_eq!(
entries_with_our_source.len(),
1,
"our entry should be present after info"
);
log::clear();
let entries_with_our_source_after: Vec<_> = log::get_entries()
.into_iter()
.filter(|e| e.source == unique_source)
.collect();
assert!(
entries_with_our_source_after.is_empty(),
"our entry should be gone after clear"
);
}
#[test]
fn test_oracle_create_query() {
let query = oracle::OracleQuery::new("btc_price".to_string());
assert_eq!(query.query_type, "btc_price");
}
#[test]
#[cfg(feature = "http-interface")]
fn test_oracle_fetch_requires_http_url() {
let query = oracle::OracleQuery::new("btc_price".to_string());
let result = oracle::fetch("price_feed", query);
assert!(result.is_err());
assert!(result
.unwrap_err()
.contains("Oracle source must be an HTTP/HTTPS URL"));
}
#[test]
#[cfg(not(feature = "http-interface"))]
fn test_oracle_fetch_requires_feature() {
let query = oracle::OracleQuery::new("btc_price".to_string());
let result = oracle::fetch("https://api.example.com/oracle", query);
assert!(result.is_err());
assert!(result
.unwrap_err()
.contains("requires the 'http-interface' feature"));
}
#[test]
fn test_oracle_create_source() {
let source = oracle::create_source(
"test_source".to_string(),
"https://api.example.com".to_string(),
);
assert_eq!(source.name, "test_source");
assert_eq!(source.url, "https://api.example.com");
}
#[test]
fn test_oracle_fetch_with_consensus() {
let _source1 = oracle::create_source(
"source1".to_string(),
"https://api1.example.com".to_string(),
);
let _source2 = oracle::create_source(
"source2".to_string(),
"https://api2.example.com".to_string(),
);
let _source3 = oracle::create_source(
"source3".to_string(),
"https://api3.example.com".to_string(),
);
let query = oracle::OracleQuery::new("btc_price".to_string());
let sources = vec!["source1", "source2", "source3"];
let result = oracle::fetch_with_consensus(sources, query, 0.6);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_auth_init_auth_system() {
auth::init_auth_system();
}
#[test]
fn test_auth_create_user() {
let result = auth::create_user(
"test_user".to_string(),
test_auth_password(),
"test@example.com".to_string(),
vec!["user".to_string()],
);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_auth_authenticate() {
let pwd = test_auth_password();
let _ = auth::create_user(
"test_user_auth".to_string(),
pwd.clone(),
"test@example.com".to_string(),
vec!["user".to_string()],
);
let result = auth::authenticate("test_user_auth".to_string(), pwd);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_auth_session() {
let session = auth::session("user1".to_string(), vec!["user".to_string()]);
assert_eq!(session.user_id, "user1");
assert_eq!(session.roles.len(), 1);
}
#[test]
fn test_auth_is_valid_session() {
let session = auth::session("user1".to_string(), vec!["user".to_string()]);
let is_valid = auth::is_valid_session(&session);
assert!(is_valid);
}
#[test]
fn test_auth_has_permission() {
let mut session = auth::session("user1".to_string(), vec!["admin".to_string()]);
session.permissions = vec!["read".to_string(), "write".to_string()];
assert!(auth::has_permission(&session, "read"));
assert!(auth::has_permission(&session, "write"));
assert!(!auth::has_permission(&session, "delete"));
}
#[test]
fn test_auth_has_role() {
let session = auth::session(
"user1".to_string(),
vec!["admin".to_string(), "user".to_string()],
);
assert!(auth::has_role(&session, "admin"));
assert!(auth::has_role(&session, "user"));
assert!(!auth::has_role(&session, "superadmin"));
}
#[test]
fn test_auth_create_role() {
let role = auth::create_role(
"editor".to_string(),
vec!["read".to_string(), "write".to_string()],
"Can read and write".to_string(),
);
assert_eq!(role.name, "editor");
assert_eq!(role.permissions.len(), 2);
}
#[test]
fn test_auth_get_role() {
let role = auth::get_role("admin");
assert!(role.is_some(), "Predefined 'admin' role should exist");
assert_eq!(role.unwrap().name, "admin");
let role2 = auth::get_role("nonexistent_role");
assert!(role2.is_none(), "Non-existent role should return None");
}
#[test]
fn test_kyc_verify_identity() {
let mut user_data = HashMap::new();
user_data.insert("name".to_string(), "John Doe".to_string());
let result = kyc::verify_identity(
"securekyc".to_string(),
"0x1234".to_string(),
"basic".to_string(),
user_data,
);
assert!(!result.is_empty());
assert!(result.contains_key("verification_id") || result.contains_key("status"));
}
#[test]
fn test_aml_perform_check() {
let mut user_data = HashMap::new();
user_data.insert("address".to_string(), "0x1234".to_string());
let result = aml::perform_check(
"chainalysis".to_string(),
"0x1234".to_string(),
"sanctions".to_string(),
user_data,
);
assert!(!result.is_empty());
assert!(result.contains_key("check_id") || result.contains_key("status"));
}
#[test]
fn test_web_create_server() {
let server = web::create_server(3000);
assert_eq!(server.port, 3000);
assert!(server.routes.is_empty());
}
#[test]
fn test_web_add_route() {
let mut server = web::create_server(3000);
web::add_route(
&mut server,
"GET".to_string(),
"/api/test".to_string(),
"handler".to_string(),
);
assert!(!server.routes.is_empty());
}
#[test]
fn test_web_create_client() {
let client = web::create_client("https://api.example.com".to_string());
assert_eq!(client.base_url, "https://api.example.com");
assert_eq!(
client.timeout, 30000,
"Timeout should be 30000 milliseconds (30 seconds)"
);
}
#[test]
fn test_web_create_html_element() {
let mut element = web::create_html_element("div".to_string(), HashMap::new());
web::set_text(&mut element, "Hello World".to_string());
let html = web::render_html(&element);
assert!(html.contains("Hello World"));
}
#[test]
fn test_web_create_html_page() {
let mut page = web::create_html_page("Test Page".to_string());
web::add_css_file(&mut page, "/styles.css".to_string());
web::add_js_file(&mut page, "/script.js".to_string());
let html = web::render_html_page(&page);
assert!(html.contains("Test Page"));
}
#[test]
fn test_web_parse_url() {
let url = "https://example.com/path?key=value&foo=bar";
let parsed = web::parse_url(url.to_string());
assert!(!parsed.is_empty());
}
#[test]
fn test_web_json_response() {
let mut data = HashMap::new();
data.insert("message".to_string(), Value::String("success".to_string()));
let response = web::json_response(data);
assert_eq!(response.status, 200);
assert!(response.body.contains("success"));
}
#[test]
fn test_web_html_response() {
let response = web::html_response("<html><body>Test</body></html>".to_string());
assert_eq!(response.status, 200);
assert!(response.body.contains("Test"));
}
#[test]
fn test_web_error_response() {
let response = web::error_response(404, "Not Found".to_string());
assert_eq!(response.status, 404);
assert!(response.body.contains("Not Found"));
}
#[test]
fn test_web_create_form() {
let form = web::create_form("/submit".to_string(), "POST".to_string());
let html = web::render_html(&form);
assert!(html.contains("form"));
}
#[test]
fn test_web_create_input() {
let input = web::create_input(
"text".to_string(),
"username".to_string(),
"Enter username".to_string(),
);
let html = web::render_html(&input);
assert!(html.contains("input"));
}
#[test]
fn test_web_create_button() {
let button = web::create_button("Submit".to_string(), "submit".to_string());
let html = web::render_html(&button);
assert!(html.contains("Submit"));
}
#[test]
fn test_web_create_api_endpoint() {
let endpoint = web::create_api_endpoint(
"/api/users".to_string(),
"GET".to_string(),
"get_users".to_string(),
);
assert_eq!(endpoint.path, "/api/users");
match endpoint.method {
web::HttpMethod::GET => {}
_ => panic!("Expected GET method"),
}
}
#[test]
fn test_web_create_websocket_server() {
let server = web::create_websocket_server(8080);
assert_eq!(server.port, 8080);
}
#[test]
fn test_web_create_template() {
let template = web::create_template("test_template".to_string(), "Hello {{name}}".to_string());
assert_eq!(template.name, "test_template");
}
#[test]
fn test_web_render_template() {
let mut data = HashMap::new();
data.insert("name".to_string(), Value::String("World".to_string()));
let rendered = web::render_template("Hello {{name}}".to_string(), data);
assert!(rendered.contains("Hello"));
}
fn setup_test_users_db() -> database::Database {
let db = database::connect("sqlite://test.db".to_string()).expect("connect test db");
database::query(
&db,
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)"
.to_string(),
vec![],
)
.expect("create users table");
db
}
#[test]
fn test_database_connect() {
let result = database::connect("sqlite://test.db".to_string());
assert!(result.is_ok());
let db = result.unwrap();
assert_eq!(db.connection_string, "sqlite://test.db");
}
#[test]
fn test_database_query() {
let db = setup_test_users_db();
let result = database::query(&db, "SELECT * FROM users".to_string(), vec![]);
assert!(result.is_ok());
let query_result = result.unwrap();
assert!(query_result.row_count >= 0);
}
#[test]
fn test_database_query_with_params() {
let db = setup_test_users_db();
let _ = database::query(
&db,
"INSERT OR IGNORE INTO users (id, name, email) VALUES (1, 'Test User', 'test@example.com')"
.to_string(),
vec![],
);
let params = vec![Value::String("test@example.com".to_string())];
let result = database::query(
&db,
"SELECT * FROM users WHERE email = ?".to_string(),
params,
);
assert!(result.is_ok());
}
#[test]
fn test_database_transaction() {
let db = setup_test_users_db();
let operations = vec![
"INSERT INTO users (name) VALUES ('Test')".to_string(),
"UPDATE users SET name = 'Updated' WHERE id = 1".to_string(),
];
let result = database::transaction(&db, operations);
assert!(result.is_ok());
}
#[test]
fn test_database_create_connection_pool() {
let pool = database::create_connection_pool(
"test_pool".to_string(),
"sqlite://test.db".to_string(),
10,
2,
);
assert_eq!(pool.pool_name, "test_pool");
assert_eq!(pool.max_connections, 10);
assert_eq!(pool.min_connections, 2);
}
#[test]
fn test_database_create_query_builder() {
let builder = database::create_query_builder("users".to_string());
assert_eq!(builder.table_name, "users");
}
#[test]
fn test_database_create_migration() {
let migration = database::create_migration(
"001".to_string(), "create_users".to_string(), "CREATE TABLE users (id INTEGER)".to_string(), "DROP TABLE users".to_string(), );
assert_eq!(migration.name, "create_users");
assert_eq!(migration.version, "001");
}
#[test]
fn test_agent_spawn() {
let config = agent::AgentConfig::new("TestAgent".to_string(), agent::AgentType::AI);
let result = agent::spawn(config);
assert!(result.is_ok());
let agent_context = result.unwrap();
assert_eq!(agent_context.config.name, "TestAgent");
}
#[test]
fn test_agent_coordinate() {
let config = agent::AgentConfig::new("TestAgent".to_string(), agent::AgentType::AI);
let agent_context = agent::spawn(config).unwrap();
let task =
agent::create_agent_task("task1".to_string(), "Test task".to_string(), "high").unwrap();
let result = agent::coordinate(&agent_context.agent_id, task, "task_distribution");
assert!(result.is_ok());
}
#[test]
fn test_agent_communicate() {
let message = agent::create_agent_message(
"msg1".to_string(),
"agent1".to_string(),
"agent2".to_string(),
"test".to_string(),
Value::String("Hello".to_string()),
);
let result = agent::communicate("agent1", "agent2", message);
assert!(result.is_ok());
}
#[test]
fn test_agent_evolve() {
let config = agent::AgentConfig::new("TestAgent".to_string(), agent::AgentType::AI);
let agent_context = agent::spawn(config).unwrap();
let mut evolution_data = HashMap::new();
evolution_data.insert(
"capability".to_string(),
Value::String("new_skill".to_string()),
);
let result = agent::evolve(&agent_context.agent_id, evolution_data);
assert!(result.is_ok());
}
#[test]
fn test_agent_validate_capabilities() {
let result =
agent::validate_capabilities("ai", vec!["analysis".to_string(), "learning".to_string()]);
assert!(result.is_ok());
}
#[test]
fn test_agent_create_agent_config() {
let config = agent::create_agent_config("TestAgent".to_string(), "ai", "assistant".to_string());
assert!(config.is_some());
assert_eq!(config.unwrap().name, "TestAgent");
}
#[test]
fn test_agent_create_agent_task() {
let task = agent::create_agent_task("task1".to_string(), "Test task".to_string(), "high");
assert!(task.is_some());
assert_eq!(task.unwrap().description, "Test task");
}
#[test]
fn test_agent_create_agent_message() {
let message = agent::create_agent_message(
"msg1".to_string(),
"agent1".to_string(),
"agent2".to_string(),
"test".to_string(),
Value::String("Hello".to_string()),
);
assert_eq!(message.sender_id, "agent1");
assert_eq!(message.receiver_id, "agent2");
}
#[test]
fn test_config_new() {
let manager = config::ConfigManager::new();
assert_eq!(manager.environment, "development");
}
#[test]
fn test_config_get_env() {
std::env::set_var("TEST_VAR", "test_value");
let result = config::ConfigManager::get_env("TEST_VAR", None);
assert!(result.is_ok());
assert_eq!(result.unwrap(), Value::String("test_value".to_string()));
}
#[test]
fn test_config_get_env_or_default() {
let value = config::ConfigManager::get_env_or_default(
"NONEXISTENT_VAR",
Value::String("default".to_string()),
);
assert_eq!(value, Value::String("default".to_string()));
}
#[test]
fn test_trust_authorize() {
let authorized = trust::authorize("admin", "read", "resource");
assert!(authorized);
}
#[test]
fn test_trust_enforce_policy() {
let context = trust::AdminContext::new("admin".to_string(), trust::AdminLevel::Admin);
let result = trust::enforce_policy("moderate", context);
assert!(result.is_ok());
}
#[test]
fn test_trust_create_admin_context() {
let context = trust::create_admin_context("admin".to_string(), "admin");
assert!(context.is_some());
assert_eq!(context.unwrap().admin_id, "admin");
}
#[test]
fn test_service_ai() {
let service = service::AIService::new("gpt-4".to_string());
let result = service::ai("Test prompt", service);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_service_call() {
let mut params = HashMap::new();
params.insert("key".to_string(), Value::String("value".to_string()));
let mut service_call =
service::ServiceCall::new("test_service".to_string(), "test_method".to_string());
service_call.parameters = params;
let result = service::call(service_call);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_service_webhook() {
let config = service::WebhookConfig {
url: "https://example.com/webhook".to_string(),
method: "POST".to_string(),
headers: HashMap::new(),
retry_count: Some(3),
};
let mut data = HashMap::new();
data.insert("event".to_string(), Value::String("test".to_string()));
let result = service::webhook(config, data);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_admin_kill() {
let result = admin::kill("agent_123", "test_reason");
assert!(result.is_ok());
assert!(result.unwrap());
}
#[test]
fn test_admin_kill_invalid() {
let result = admin::kill("agent_123", "");
assert!(result.is_err());
}
#[test]
fn test_admin_get_process_info() {
let result = admin::get_process_info("agent_123");
assert!(result.is_ok());
let process = result.unwrap();
assert_eq!(process.process_id, "agent_123");
assert_eq!(process.name, "data_processor");
}
#[test]
fn test_admin_list_processes() {
let processes = admin::list_processes();
assert!(!processes.is_empty());
assert!(processes.len() >= 3);
}
#[test]
fn test_cloudadmin_authorize() {
let authorized = cloudadmin::authorize("admin", "read", "resource");
assert!(authorized);
}
#[test]
fn test_cloudadmin_enforce_policy() {
let context = cloudadmin::AdminContext::new("admin".to_string(), cloudadmin::AdminLevel::Admin);
let result = cloudadmin::enforce_policy("moderate", context);
assert!(result.is_ok());
}
#[test]
fn test_cloudadmin_validate_hybrid_trust() {
let valid = cloudadmin::validate_hybrid_trust("valid", "valid");
assert!(valid);
let invalid = cloudadmin::validate_hybrid_trust("invalid", "valid");
assert!(!invalid);
}
#[test]
fn test_cloudadmin_bridge_trusts() {
let bridged = cloudadmin::bridge_trusts("admin", "user");
assert!(bridged);
}
#[test]
fn test_cloudadmin_create_admin_context() {
let context = cloudadmin::create_admin_context("admin".to_string(), "admin");
assert!(context.is_some());
assert_eq!(context.unwrap().admin_id, "admin");
}
#[test]
fn test_sync_create_sync_target() {
let target =
sync::create_sync_target("https://api.example.com".to_string(), "http".to_string());
assert_eq!(target.location, "https://api.example.com");
assert_eq!(target.protocol, "http");
}
#[test]
fn test_sync_create_sync_filters() {
let filters = sync::create_sync_filters();
assert!(filters.data_type.is_none());
assert!(filters.tags.is_empty());
}
#[test]
fn test_sync_push() {
let target =
sync::create_sync_target("https://api.example.com".to_string(), "http".to_string());
let mut data = HashMap::new();
data.insert("key".to_string(), Value::String("value".to_string()));
let result = sync::push(data, target);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_sync_pull() {
let filters = sync::create_sync_filters();
let result = sync::pull("source", filters);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_key_create() {
let result = key::create("resource1", vec!["read", "write"]);
assert!(result.is_ok());
let capability = result.unwrap();
assert_eq!(capability.resource, "resource1");
}
#[test]
fn test_key_grant() {
let capability = key::create("resource1", vec!["read"]).unwrap();
let mut principal = key::create_principal("user1".to_string(), "User 1".to_string());
let result = key::grant(&capability, &mut principal);
assert!(result.is_ok());
}
#[test]
fn test_key_check() {
let capability = key::create("resource1", vec!["read"]).unwrap();
let mut principal = key::create_principal("user1".to_string(), "User 1".to_string());
let _ = key::grant(&capability, &mut principal);
let request = key::create_capability_request(
"resource1".to_string(),
"read".to_string(),
"user1".to_string(),
);
let result = key::check(request);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_key_create_principal() {
let principal = key::create_principal("user1".to_string(), "User 1".to_string());
assert_eq!(principal.id, "user1");
assert_eq!(principal.name, "User 1");
}
#[test]
fn test_key_create_capability_request() {
let request = key::create_capability_request(
"resource1".to_string(),
"read".to_string(),
"user1".to_string(),
);
assert_eq!(request.resource, "resource1");
assert_eq!(request.operation, "read");
assert_eq!(request.principal_id, "user1");
}
#[test]
fn test_key_create_uses_unique_id() {
let cap1 = key::create("r", vec!["read"]).unwrap();
let cap2 = key::create("r", vec!["read"]).unwrap();
assert_ne!(cap1.id, cap2.id);
assert!(cap1.id.starts_with("key_r_"));
assert!(cap2.id.starts_with("key_r_"));
}
#[test]
fn test_key_revoke_and_list_for_principal() {
let cap = key::create("res", vec!["read"]).unwrap();
let mut principal = key::create_principal("p1".to_string(), "P1".to_string());
key::grant(&cap, &mut principal).unwrap();
let list = key::list_for_principal("p1");
assert_eq!(list.len(), 1);
assert_eq!(list[0].id, cap.id);
let ok = key::revoke(&cap.id, "p1").unwrap();
assert!(ok);
let list_after = key::list_for_principal("p1");
assert!(list_after.is_empty());
}
#[test]
fn test_key_revoke_all() {
let cap1 = key::create("r1", vec!["read"]).unwrap();
let cap2 = key::create("r2", vec!["write"]).unwrap();
let mut principal = key::create_principal("p2".to_string(), "P2".to_string());
key::grant(&cap1, &mut principal).unwrap();
key::grant(&cap2, &mut principal).unwrap();
assert_eq!(key::list_for_principal("p2").len(), 2);
let n = key::revoke_all("p2").unwrap();
assert_eq!(n, 2);
assert!(key::list_for_principal("p2").is_empty());
}
#[test]
fn test_cross_chain_security_new() {
let _manager = cross_chain_security::CrossChainSecurityManager::new();
}
#[test]
fn test_cross_chain_security_chain_config() {
let config = cross_chain_security::ChainSecurityConfig {
chain_id: 1,
name: "Ethereum".to_string(),
signature_scheme: cross_chain_security::SignatureScheme::ECDSA,
min_confirmations: 12,
max_gas_price: 1000000000,
trusted_validators: vec![],
security_level: cross_chain_security::SecurityLevel::High,
};
assert_eq!(config.chain_id, 1);
assert_eq!(config.name, "Ethereum");
match config.signature_scheme {
cross_chain_security::SignatureScheme::ECDSA => {}
_ => panic!("Expected ECDSA"),
}
}
#[test]
fn test_cross_chain_security_bridge_config() {
let bridge = cross_chain_security::BridgeConfig {
bridge_id: "bridge1".to_string(),
source_chain: 1,
target_chain: 137,
bridge_contract: "0x1234".to_string(),
validator_set: vec![],
min_validator_signatures: 3,
max_transaction_amount: 1000000,
security_deposit: 10000,
is_active: true,
};
assert_eq!(bridge.bridge_id, "bridge1");
assert_eq!(bridge.source_chain, 1);
assert_eq!(bridge.target_chain, 137);
}
#[test]
fn test_secure_auth_new() {
let _store = secure_auth::SecureUserStore::new();
}
#[test]
fn test_secure_auth_create_user() {
let mut store = secure_auth::SecureUserStore::new();
let username = format!(
"test_user_{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
);
let email = format!(
"test{}@example.com",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
);
let password = test_auth_password_strong();
let result = store.create_user(username, password, email, vec!["user".to_string()]);
if let Err(e) = &result {
eprintln!("User creation failed with error: {}", e);
}
assert!(
result.is_ok(),
"User creation should succeed with strong password and unique credentials"
);
}
#[test]
fn test_secure_auth_authenticate() {
let mut store = secure_auth::SecureUserStore::new();
let username = format!("test_user_auth_{}", std::process::id());
let password = format!("Pwd123!_{}", std::process::id());
let create_result = store.create_user(
username.clone(),
password.clone(),
"test@example.com".to_string(),
vec!["user".to_string()],
);
assert!(create_result.is_ok(), "User creation should succeed");
let result = store.authenticate(
username,
password,
Some("127.0.0.1".to_string()),
Some("test-agent".to_string()),
);
assert!(
result.is_ok(),
"Authentication should succeed with correct password"
);
let pwd_hash = format!("test_pwd_{}", std::process::id());
let hash_result = secure_auth::PasswordHasher::hash_password(&pwd_hash);
assert!(hash_result.is_ok());
let hash = hash_result.unwrap();
assert!(!hash.is_empty());
assert_ne!(hash, pwd_hash);
}
#[test]
fn test_secure_auth_verify_password() {
let password = format!("test_pwd_{}", std::process::id());
let hash_result = secure_auth::PasswordHasher::hash_password(&password);
assert!(hash_result.is_ok());
let hash = hash_result.unwrap();
let verify_result = secure_auth::PasswordHasher::verify_password(&password, &hash);
assert!(verify_result.is_ok());
assert!(verify_result.unwrap());
}
#[test]
fn test_secure_auth_verify_password_wrong() {
let password = format!("test_pwd_{}", std::process::id());
let hash_result = secure_auth::PasswordHasher::hash_password(&password);
assert!(hash_result.is_ok());
let hash = hash_result.unwrap();
let wrong = format!("wrong_{}", std::process::id());
let verify_result = secure_auth::PasswordHasher::verify_password(&wrong, &hash);
assert!(verify_result.is_ok());
assert!(!verify_result.unwrap());
}
#[test]
fn test_add_sol_parse_abi() {
let abi_json = r#"[
{
"type": "function",
"name": "transfer",
"inputs": [{"name": "to", "type": "address"}, {"name": "amount", "type": "uint256"}]
}
]"#;
let result = add_sol::parse_abi(abi_json.to_string());
assert!(result.is_ok());
let functions = result.unwrap();
assert!(!functions.is_empty());
assert_eq!(functions[0].name, "transfer");
}
#[test]
fn test_add_sol_parse_events() {
let abi_json = r#"[
{
"type": "event",
"name": "Transfer",
"inputs": [{"name": "from", "type": "address"}, {"name": "to", "type": "address"}]
}
]"#;
let result = add_sol::parse_events(abi_json.to_string());
assert!(result.is_ok());
let events = result.unwrap();
assert!(!events.is_empty());
assert_eq!(events[0].name, "Transfer");
}
#[test]
fn test_add_sol_solidity_to_dal_type() {
assert_eq!(add_sol::solidity_to_dal_type("uint256"), "int");
assert_eq!(add_sol::solidity_to_dal_type("address"), "string");
assert_eq!(add_sol::solidity_to_dal_type("bool"), "bool");
assert_eq!(add_sol::solidity_to_dal_type("string"), "string");
}
#[test]
fn test_add_sol_register_contract() {
let abi_json = r#"[
{
"type": "function",
"name": "transfer",
"inputs": [{"name": "to", "type": "address"}, {"name": "amount", "type": "uint256"}]
}
]"#;
let contract = add_sol::register_contract(
"TestContract".to_string(),
"0x1234".to_string(),
1,
Some(abi_json.to_string()),
);
assert_eq!(contract.name, "TestContract");
assert_eq!(contract.address, "0x1234");
assert_eq!(contract.chain_id, 1);
assert!(contract.abi.is_some());
}
#[test]
fn test_add_sol_generate_wrapper_code() {
let abi_json = r#"[
{
"type": "function",
"name": "transfer",
"inputs": [{"name": "to", "type": "address"}, {"name": "amount", "type": "uint256"}]
}
]"#;
let contract = add_sol::register_contract(
"TestContract".to_string(),
"0x1234".to_string(),
1,
Some(abi_json.to_string()),
);
let result = add_sol::generate_wrapper_code(&contract);
assert!(result.is_ok());
let code = result.unwrap();
assert!(!code.is_empty());
assert!(code.contains("TestContract"));
}
#[test]
fn test_add_sol_call_with_abi() {
let abi_json = r#"[
{
"type": "function",
"name": "transfer",
"inputs": [{"name": "to", "type": "address"}, {"name": "amount", "type": "uint256"}]
}
]"#;
let contract = add_sol::register_contract(
"TestContract".to_string(),
"0x1234".to_string(),
1,
Some(abi_json.to_string()),
);
let mut args = HashMap::new();
args.insert("to".to_string(), Value::String("0x5678".to_string()));
args.insert("amount".to_string(), Value::String("1000".to_string()));
let result = add_sol::call_with_abi(&contract, "transfer".to_string(), args);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_add_sol_call_with_abi_surfaces_typed_chain_error_projection() {
let abi_json = r#"[
{
"type": "function",
"name": "transfer",
"inputs": [{"name": "to", "type": "address"}, {"name": "amount", "type": "uint256"}]
}
]"#;
let contract = add_sol::register_contract(
"TestContract".to_string(),
"0x1234".to_string(),
999_999,
Some(abi_json.to_string()),
);
let mut args = HashMap::new();
args.insert("to".to_string(), Value::String("0x5678".to_string()));
args.insert("amount".to_string(), Value::String("1000".to_string()));
let result = add_sol::call_with_abi(&contract, "transfer".to_string(), args)
.expect("call_with_abi should return projected typed error message");
assert_eq!(result, "error: chain not supported");
}
#[test]
fn test_add_sol_call_with_abi_typed_surfaces_decoded_and_evidence_contract() {
let abi_json = r#"[
{
"type": "function",
"name": "transfer",
"inputs": [{"name": "to", "type": "address"}, {"name": "amount", "type": "uint256"}],
"outputs": [{"name": "ok", "type": "bool"}]
}
]"#;
let contract = add_sol::register_contract(
"TestContract".to_string(),
"0x1234".to_string(),
999_999,
Some(abi_json.to_string()),
);
let mut args = HashMap::new();
args.insert("to".to_string(), Value::String("0x5678".to_string()));
args.insert("amount".to_string(), Value::String("1000".to_string()));
let result = add_sol::call_with_abi_typed(&contract, "transfer".to_string(), args)
.expect("call_with_abi_typed should return typed chain envelope");
assert_eq!(result.error_code, Some("CHAIN_UNSUPPORTED".to_string()));
assert_eq!(result.result_hex, None);
assert_eq!(result.decoded, Value::Null);
assert_eq!(result.decode_error, None);
}
#[test]
fn test_mobile_create_app() {
let app = mobile::create_app(
"TestApp".to_string(),
"com.test.app".to_string(),
mobile::MobilePlatform::IOs,
);
assert_eq!(app.name, "TestApp");
assert_eq!(app.config.bundle_id, "com.test.app");
match app.platform {
mobile::MobilePlatform::IOs => {}
_ => panic!("Expected IOs platform"),
}
}
#[test]
fn test_mobile_create_screen() {
let screen = mobile::create_screen("Home Screen".to_string());
assert_eq!(screen.title, "Home Screen");
}
#[test]
fn test_mobile_create_mobile_label() {
let label = mobile::create_mobile_label("Hello".to_string(), 10, 20, 100, 30);
match label {
mobile::MobileComponent::Label(_) => {}
_ => panic!("Expected Label component"),
}
}
#[test]
fn test_mobile_create_mobile_button() {
let button = mobile::create_mobile_button("Click Me".to_string(), 10, 20, 100, 40);
match button {
mobile::MobileComponent::Button(_) => {}
_ => panic!("Expected Button component"),
}
}
#[test]
fn test_mobile_send_push_notification() {
let notification = mobile::PushNotification {
id: "notif1".to_string(),
title: "Test".to_string(),
body: "Test body".to_string(),
badge: Some(1),
sound: Some("default".to_string()),
category: None,
thread_id: None,
user_info: HashMap::new(),
};
let result = mobile::send_push_notification(notification);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_mobile_get_gps_location() {
let result = mobile::get_gps_location();
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_desktop_create_window() {
let config = desktop::WindowConfig {
title: "Test Window".to_string(),
width: 800,
height: 600,
x: Some(100),
y: Some(100),
resizable: true,
always_on_top: false,
fullscreen: false,
decorated: true,
icon_path: None,
theme: "default".to_string(),
};
let result = desktop::create_window(config);
assert!(result.is_ok());
let window = result.unwrap();
assert_eq!(window.title, "Test Window");
assert_eq!(window.width, 800);
assert_eq!(window.height, 600);
}
#[test]
fn test_desktop_create_button() {
let button = desktop::create_button("Click Me".to_string(), 10, 20, 100, 40);
match button {
desktop::UIComponent::Button(_) => {}
_ => panic!("Expected Button component"),
}
}
#[test]
fn test_desktop_create_label() {
let label = desktop::create_label("Hello".to_string(), 10, 20, 100, 30);
match label {
desktop::UIComponent::Label(_) => {}
_ => panic!("Expected Label component"),
}
}
#[test]
fn test_desktop_create_text_field() {
let text_field = desktop::create_text_field(Some("Enter text".to_string()), 10, 20, 200, 30);
match text_field {
desktop::UIComponent::TextField(_) => {}
_ => panic!("Expected TextField component"),
}
}
#[test]
fn test_desktop_create_menu_bar() {
let menu_bar = desktop::create_menu_bar();
match menu_bar {
desktop::UIComponent::MenuBar(_) => {}
_ => panic!("Expected MenuBar component"),
}
}
#[test]
fn test_iot_device_types() {
match iot::DeviceType::SensorNode {
iot::DeviceType::SensorNode => {}
_ => panic!("Expected SensorNode"),
}
match iot::DeviceType::ActuatorNode {
iot::DeviceType::ActuatorNode => {}
_ => panic!("Expected ActuatorNode"),
}
match iot::DeviceType::Gateway {
iot::DeviceType::Gateway => {}
_ => panic!("Expected Gateway"),
}
}
#[test]
fn test_iot_device_status() {
match iot::DeviceStatus::Online {
iot::DeviceStatus::Online => {}
_ => panic!("Expected Online status"),
}
match iot::DeviceStatus::Offline {
iot::DeviceStatus::Offline => {}
_ => panic!("Expected Offline status"),
}
}
#[test]
fn test_iot_sensor_reading_struct() {
let reading = iot::SensorReading {
timestamp: chrono::Utc::now().to_rfc3339(),
value: Value::Float(25.5),
quality: iot::ReadingQuality::Good,
metadata: HashMap::new(),
};
match reading.value {
Value::Float(v) => assert_eq!(v, 25.5),
_ => panic!("Expected Float value"),
}
match reading.quality {
iot::ReadingQuality::Good => {}
_ => panic!("Expected Good quality"),
}
}
#[test]
fn test_iot_actuator_command_struct() {
let command = iot::ActuatorCommand {
command_id: "cmd1".to_string(),
command_type: "turn_on".to_string(),
parameters: HashMap::new(),
timestamp: chrono::Utc::now().to_rfc3339(),
status: iot::CommandStatus::Pending,
};
assert_eq!(command.command_id, "cmd1");
assert_eq!(command.command_type, "turn_on");
}