use std::env;
use std::sync::Arc;
use tokio::runtime::Runtime;
use agio::{
AgentBuilder, Config, Error,
};
use agio::persistence::{MemoryStore, PersistenceStore, ConversationMetadata, PostgresStore};
use agio::server::AgentManager;
#[test]
fn test_memory_persistence() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("OPENAI_API_KEY")
.expect("Set the OPENAI_API_KEY env var before running this test.");
let rt = Runtime::new()?;
rt.block_on(async {
let store = Arc::new(MemoryStore::new());
let mut agent = AgentBuilder::new()
.with_config(Config::new()
.with_api_key(api_key.clone())
.with_model("gpt-3.5-turbo")
.with_temperature(0.0))
.with_system_prompt("You are a helpful assistant for testing.")
.with_persistence(store.clone())
.build_async()
.await?;
let agent_id = agent.id().to_string();
println!("Created agent with ID: {}", agent_id);
let response = agent.run("Hello, this is a test message.").await?;
println!("Response: {}", response);
let conversations = store.list_conversations(10, 0).await?;
assert!(!conversations.is_empty(), "No conversations found in store");
let found = conversations.iter().any(|c| c.id == agent_id);
assert!(found, "Agent ID not found in conversations");
let loaded_agent = AgentBuilder::new()
.with_id(agent_id.clone())
.with_config(Config::new()
.with_api_key(api_key.clone())
.with_model("gpt-3.5-turbo")
.with_temperature(0.0))
.with_persistence(store.clone())
.build_async()
.await?;
assert_eq!(loaded_agent.id(), agent_id, "Loaded agent has different ID");
assert!(loaded_agent.state().message_count() > 0, "Loaded agent has no messages");
loaded_agent.delete().await?;
let conversations_after_delete = store.list_conversations(10, 0).await?;
let found_after_delete = conversations_after_delete.iter().any(|c| c.id == agent_id);
assert!(!found_after_delete, "Agent still exists after deletion");
Ok(())
})
}
#[test]
fn test_agent_manager() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("OPENAI_API_KEY")
.expect("Set the OPENAI_API_KEY env var before running this test.");
let rt = Runtime::new()?;
rt.block_on(async {
let store = Arc::new(MemoryStore::new());
let config = Config::new()
.with_api_key(api_key)
.with_model("gpt-3.5-turbo")
.with_temperature(0.0);
let manager = Arc::new(AgentManager::new(config, store, 10));
let agent_id = manager.create_agent().await?;
println!("Created agent with ID: {}", agent_id);
let response = manager.run_message(&agent_id, "Hello, this is a test message.").await?;
println!("Response: {}", response);
let conversations = manager.list_conversations(10, 0).await?;
assert!(!conversations.is_empty(), "No conversations found");
let found = conversations.iter().any(|c| c.id == agent_id);
assert!(found, "Agent ID not found in conversations");
let mut agent_ids = vec![agent_id.clone()];
for i in 0..15 {
let id = manager.create_agent().await?;
println!("Created additional agent {}: {}", i, id);
agent_ids.push(id);
}
let response = manager.run_message(&agent_id, "Testing cache eviction.").await?;
println!("Response after cache eviction: {}", response);
manager.delete_agent(&agent_id).await?;
let result = manager.get_agent(&agent_id).await;
assert!(result.is_err(), "Agent still exists after deletion");
for id in agent_ids.iter().skip(1) {
let _ = manager.delete_agent(id).await;
}
Ok(())
})
}
#[test]
fn test_postgres_persistence() -> Result<(), Box<dyn std::error::Error>> {
let db_url = match env::var("DATABASE_URL") {
Ok(url) => url,
Err(_) => {
println!("Skipping PostgreSQL test: DATABASE_URL not set");
return Ok(());
}
};
let api_key = env::var("OPENAI_API_KEY")
.expect("Set the OPENAI_API_KEY env var before running this test.");
let rt = Runtime::new()?;
rt.block_on(async {
let store = match PostgresStore::new(&db_url).await {
Ok(store) => Arc::new(store),
Err(e) => {
println!("Skipping PostgreSQL test: Failed to connect to database: {}", e);
return Ok(());
}
};
let mut agent = AgentBuilder::new()
.with_config(Config::new()
.with_api_key(api_key.clone())
.with_model("gpt-3.5-turbo")
.with_temperature(0.0))
.with_system_prompt("You are a helpful assistant for testing PostgreSQL persistence.")
.with_persistence(store.clone())
.build_async()
.await?;
let agent_id = agent.id().to_string();
println!("Created agent with ID: {}", agent_id);
let response = agent.run("Hello, this is a PostgreSQL test message.").await?;
println!("Response: {}", response);
let conversations = store.list_conversations(10, 0).await?;
assert!(!conversations.is_empty(), "No conversations found in PostgreSQL store");
let found = conversations.iter().any(|c| c.id == agent_id);
assert!(found, "Agent ID not found in PostgreSQL conversations");
let loaded_agent = AgentBuilder::new()
.with_id(agent_id.clone())
.with_config(Config::new()
.with_api_key(api_key.clone())
.with_model("gpt-3.5-turbo")
.with_temperature(0.0))
.with_persistence(store.clone())
.build_async()
.await?;
assert_eq!(loaded_agent.id(), agent_id, "Loaded agent has different ID");
assert!(loaded_agent.state().message_count() > 0, "Loaded agent has no messages");
loaded_agent.delete().await?;
let conversations_after_delete = store.list_conversations(10, 0).await?;
let found_after_delete = conversations_after_delete.iter().any(|c| c.id == agent_id);
assert!(!found_after_delete, "Agent still exists after deletion in PostgreSQL");
Ok(())
})
}