#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]
use tempfile::TempDir;
use x0x::Agent;
async fn agent_with_cache(temp_dir: &TempDir) -> Agent {
Agent::builder()
.with_machine_key(temp_dir.path().join("machine.key"))
.with_agent_key_path(temp_dir.path().join("agent.key"))
.with_peer_cache_dir(temp_dir.path().join("peers"))
.build()
.await
.expect("failed to build agent")
}
#[tokio::test]
async fn test_agent_builds_with_peer_cache_dir() {
let temp = TempDir::new().unwrap();
let agent = agent_with_cache(&temp).await;
assert!(agent.network().is_none());
}
#[tokio::test]
async fn test_agent_with_network_creates_cache_dir() {
let temp = TempDir::new().unwrap();
let cache_dir = temp.path().join("peers");
let _agent = Agent::builder()
.with_machine_key(temp.path().join("machine.key"))
.with_agent_key_path(temp.path().join("agent.key"))
.with_peer_cache_dir(&cache_dir)
.with_network_config(x0x::network::NetworkConfig::default())
.build()
.await
.expect("failed to build agent");
assert!(cache_dir.exists(), "Cache directory should be created");
}
#[tokio::test]
async fn test_shutdown_saves_cache() {
let temp = TempDir::new().unwrap();
let cache_dir = temp.path().join("peers");
let agent = Agent::builder()
.with_machine_key(temp.path().join("machine.key"))
.with_agent_key_path(temp.path().join("agent.key"))
.with_peer_cache_dir(&cache_dir)
.with_network_config(x0x::network::NetworkConfig::default())
.build()
.await
.expect("failed to build agent");
agent.shutdown().await;
}
#[tokio::test]
async fn test_cache_persists_across_restarts() {
let temp = TempDir::new().unwrap();
let cache_dir = temp.path().join("peers");
{
let agent = Agent::builder()
.with_machine_key(temp.path().join("machine.key"))
.with_agent_key_path(temp.path().join("agent.key"))
.with_peer_cache_dir(&cache_dir)
.with_network_config(x0x::network::NetworkConfig::default())
.build()
.await
.expect("failed to build first agent");
agent.shutdown().await;
}
{
let agent = Agent::builder()
.with_machine_key(temp.path().join("machine.key"))
.with_agent_key_path(temp.path().join("agent.key"))
.with_peer_cache_dir(&cache_dir)
.with_network_config(x0x::network::NetworkConfig::default())
.build()
.await
.expect("failed to build second agent");
agent.shutdown().await;
}
}
#[tokio::test]
async fn test_default_cache_dir_when_not_specified() {
let temp = TempDir::new().unwrap();
let agent = Agent::builder()
.with_machine_key(temp.path().join("machine.key"))
.with_agent_key_path(temp.path().join("agent.key"))
.with_network_config(x0x::network::NetworkConfig::default())
.build()
.await
.expect("failed to build agent with default cache dir");
agent.shutdown().await;
}