#[tokio::test]
async fn test_trust_initialization_and_staking() -> anyhow::Result<()> {
use synapse::trust::{TrustManager, TrustConfig};
use synapse::blockchain::{BlockchainManager, BlockchainConfig};
use std::collections::HashMap;
println!("Testing trust system initialization and staking...");
let blockchain_config = BlockchainConfig::default();
let blockchain_manager = BlockchainManager::new(blockchain_config).await?;
let trust_config = TrustConfig::default();
let mut trust_manager = TrustManager::new(trust_config, blockchain_manager).await?;
let entity_id = "test_entity_1";
let stake_amount = 100.0;
let registration_result = trust_manager.register_entity_with_stake(
entity_id,
stake_amount,
HashMap::new(),
).await;
assert!(registration_result.is_ok(), "Entity registration with stake should succeed");
let stake_info = trust_manager.get_stake_info(entity_id).await?;
assert_eq!(stake_info.amount, stake_amount, "Stake amount should match");
assert!(stake_info.is_active, "Stake should be active");
let trust_score = trust_manager.calculate_trust_score(entity_id).await?;
assert!(trust_score > 0.0, "Trust score should be positive with stake");
assert!(trust_score <= 1.0, "Trust score should not exceed 1.0");
println!("✓ Trust initialization and staking test completed successfully");
Ok(())
}
#[tokio::test]
async fn test_trust_report_submission() -> anyhow::Result<()> {
use synapse::trust::{TrustManager, TrustConfig, TrustReport, TrustInteraction};
use synapse::blockchain::{BlockchainManager, BlockchainConfig};
use std::collections::HashMap;
println!("Testing trust report submission...");
let blockchain_config = BlockchainConfig::default();
let blockchain_manager = BlockchainManager::new(blockchain_config).await?;
let trust_config = TrustConfig::default();
let mut trust_manager = TrustManager::new(trust_config, blockchain_manager).await?;
let reporter_id = "reporter_entity";
let target_id = "target_entity";
trust_manager.register_entity_with_stake(reporter_id, 50.0, HashMap::new()).await?;
trust_manager.register_entity_with_stake(target_id, 50.0, HashMap::new()).await?;
let trust_report = TrustReport {
reporter_id: reporter_id.to_string(),
target_id: target_id.to_string(),
interaction_type: TrustInteraction::MessageDelivery,
success: true,
timestamp: chrono::Utc::now(),
context: "successful message delivery".to_string(),
evidence: Some("message_id_12345".to_string()),
};
let submission_result = trust_manager.submit_trust_report(trust_report).await;
assert!(submission_result.is_ok(), "Trust report submission should succeed");
let updated_trust_score = trust_manager.calculate_trust_score(target_id).await?;
assert!(updated_trust_score > 0.0, "Trust score should be positive after successful report");
let negative_report = TrustReport {
reporter_id: reporter_id.to_string(),
target_id: target_id.to_string(),
interaction_type: TrustInteraction::MessageDelivery,
success: false,
timestamp: chrono::Utc::now(),
context: "failed message delivery".to_string(),
evidence: Some("timeout_evidence".to_string()),
};
let negative_submission = trust_manager.submit_trust_report(negative_report).await;
assert!(negative_submission.is_ok(), "Negative trust report should be accepted");
let post_negative_score = trust_manager.calculate_trust_score(target_id).await?;
assert!(post_negative_score < updated_trust_score, "Trust score should decrease after negative report");
println!("✓ Trust report submission test completed successfully");
Ok(())
}
#[tokio::test]
async fn test_trust_decay() -> anyhow::Result<()> {
use synapse::trust::{TrustManager, TrustConfig};
use synapse::blockchain::{BlockchainManager, BlockchainConfig};
use std::collections::HashMap;
use tokio::time::{sleep, Duration};
println!("Testing trust decay...");
let blockchain_config = BlockchainConfig::default();
let blockchain_manager = BlockchainManager::new(blockchain_config).await?;
let mut trust_config = TrustConfig::default();
trust_config.decay_rate = 0.1; trust_config.decay_interval = Duration::from_secs(1);
let mut trust_manager = TrustManager::new(trust_config, blockchain_manager).await?;
let entity_id = "decay_test_entity";
trust_manager.register_entity_with_stake(entity_id, 100.0, HashMap::new()).await?;
let trust_report = synapse::trust::TrustReport {
reporter_id: "reporter".to_string(),
target_id: entity_id.to_string(),
interaction_type: synapse::trust::TrustInteraction::MessageDelivery,
success: true,
timestamp: chrono::Utc::now(),
context: "building trust".to_string(),
evidence: None,
};
for _ in 0..5 {
trust_manager.submit_trust_report(trust_report.clone()).await?;
}
let initial_trust_score = trust_manager.calculate_trust_score(entity_id).await?;
println!("Initial trust score: {}", initial_trust_score);
trust_manager.start_decay_process().await?;
sleep(Duration::from_secs(3)).await;
let decayed_trust_score = trust_manager.calculate_trust_score(entity_id).await?;
println!("Decayed trust score: {}", decayed_trust_score);
assert!(decayed_trust_score < initial_trust_score,
"Trust score should decrease due to decay");
assert!(decayed_trust_score > 0.0,
"Trust score should not go below zero with sufficient stake");
let stake_info = trust_manager.get_stake_info(entity_id).await?;
assert!(stake_info.is_active, "Stake should still be active after decay");
trust_manager.stop_decay_process().await?;
println!("✓ Trust decay test completed successfully");
Ok(())
}