use nt_memory::*;
use tempfile::TempDir;
#[tokio::test]
async fn test_memory_system_full_workflow() {
let temp_dir = TempDir::new().unwrap();
let _config = MemoryConfig {
cache_config: CacheConfig {
max_entries: 1000,
ttl: std::time::Duration::from_secs(60),
track_access: true,
},
agentdb_url: "http://localhost:3000".to_string(),
storage_path: temp_dir.path().to_str().unwrap().to_string(),
enable_compression: true,
max_memory_bytes: 1_000_000,
};
let memory = MemorySystem::new(config).await;
if memory.is_err() {
println!("Skipping test: AgentDB not available");
return;
}
let memory = memory.unwrap();
let key = "test_key";
let value = b"test_value".to_vec();
memory.put("agent_1", key, value.clone()).await.unwrap();
let retrieved = memory.get("agent_1", key).await.unwrap();
assert_eq!(retrieved, Some(value));
let retrieved_again = memory.get("agent_1", key).await.unwrap();
assert!(retrieved_again.is_some());
let stats = memory.stats();
assert!(stats.l1_entries > 0);
assert!(stats.l1_hit_rate > 0.0);
}
#[tokio::test]
async fn test_trajectory_tracking() {
let temp_dir = TempDir::new().unwrap();
let _config = MemoryConfig {
storage_path: temp_dir.path().to_str().unwrap().to_string(),
..Default::default()
};
let memory = MemorySystem::new(config).await;
if memory.is_err() {
return;
}
let memory = memory.unwrap();
let mut trajectory = Trajectory::new("agent_1".to_string());
trajectory.add_observation(
serde_json::json!({"price": 100.0}),
Some(vec![0.1; 384]),
);
trajectory.add_action(
"buy".to_string(),
serde_json::json!({"quantity": 10}),
Some(110.0),
);
trajectory.add_outcome(105.0);
memory.track_trajectory(trajectory).await.unwrap();
}
#[tokio::test]
async fn test_pubsub_messaging() {
let temp_dir = TempDir::new().unwrap();
let _config = MemoryConfig {
storage_path: temp_dir.path().to_str().unwrap().to_string(),
..Default::default()
};
let memory = MemorySystem::new(config).await;
if memory.is_err() {
return;
}
let memory = memory.unwrap();
let mut rx = memory.subscribe("test_topic").await.unwrap();
let message = b"test message".to_vec();
memory.publish("test_topic", message.clone()).await.unwrap();
let received = rx.recv().await.unwrap();
assert_eq!(received, message);
}
#[tokio::test]
async fn test_distributed_locks() {
let temp_dir = TempDir::new().unwrap();
let _config = MemoryConfig {
storage_path: temp_dir.path().to_str().unwrap().to_string(),
..Default::default()
};
let memory = MemorySystem::new(config).await;
if memory.is_err() {
return;
}
let memory = memory.unwrap();
let token = memory
.acquire_lock("resource_1", std::time::Duration::from_secs(1))
.await
.unwrap();
assert!(!token.is_empty());
memory.release_lock(&token).await.unwrap();
}
#[tokio::test]
async fn test_cross_agent_coordination() {
let temp_dir = TempDir::new().unwrap();
let _config = MemoryConfig {
storage_path: temp_dir.path().to_str().unwrap().to_string(),
..Default::default()
};
let memory = MemorySystem::new(config).await;
if memory.is_err() {
return;
}
let memory = memory.unwrap();
memory
.put("agent_1", "shared_state", b"value_1".to_vec())
.await
.unwrap();
let value = memory.get("agent_1", "shared_state").await.unwrap();
assert_eq!(value, Some(b"value_1".to_vec()));
memory
.publish("agent_1/updates", b"new_data".to_vec())
.await
.unwrap();
}
#[tokio::test]
async fn test_namespace_isolation() {
let temp_dir = TempDir::new().unwrap();
let _config = MemoryConfig {
storage_path: temp_dir.path().to_str().unwrap().to_string(),
..Default::default()
};
let memory = MemorySystem::new(config).await;
if memory.is_err() {
return;
}
let memory = memory.unwrap();
memory
.put("agent_1", "position", b"100".to_vec())
.await
.unwrap();
memory
.put("agent_2", "position", b"200".to_vec())
.await
.unwrap();
let val1 = memory.get("agent_1", "position").await.unwrap();
let val2 = memory.get("agent_2", "position").await.unwrap();
assert_eq!(val1, Some(b"100".to_vec()));
assert_eq!(val2, Some(b"200".to_vec()));
}