use anyhow::Result;
use do_memory_core::{MemoryConfig, SelfLearningMemory};
use do_memory_storage_redb::RedbStorage;
use do_memory_storage_turso::TursoStorage;
use std::sync::Arc;
use tempfile::TempDir;
pub struct TestEnvironment {
#[allow(dead_code)]
pub temp_dir: TempDir,
pub turso_url: String,
pub redb_path: String,
pub memory: SelfLearningMemory,
pub turso_storage: Arc<TursoStorage>,
pub redb_storage: Arc<RedbStorage>,
}
impl TestEnvironment {
pub async fn new() -> Result<Self> {
let temp_dir = TempDir::new()?;
let db_path = temp_dir.path().join("test.db");
let turso_url = format!("file:{}", db_path.display());
let redb_path = temp_dir.path().join("cache.redb");
let turso = TursoStorage::new(&turso_url, "").await?;
turso.initialize_schema().await?;
let turso_arc = Arc::new(turso);
let redb = RedbStorage::new(&redb_path)?;
let redb_arc = Arc::new(redb);
let memory = SelfLearningMemory::with_storage(
MemoryConfig::default(),
turso_arc.clone(),
redb_arc.clone(),
);
Ok(Self {
temp_dir,
turso_url,
redb_path: redb_path.display().to_string(),
memory,
turso_storage: turso_arc,
redb_storage: redb_arc,
})
}
pub async fn with_config(config: MemoryConfig) -> Result<Self> {
let temp_dir = TempDir::new()?;
let db_path = temp_dir.path().join("test.db");
let turso_url = format!("file:{}", db_path.display());
let redb_path = temp_dir.path().join("cache.redb");
let turso = TursoStorage::new(&turso_url, "").await?;
turso.initialize_schema().await?;
let turso_arc = Arc::new(turso);
let redb = RedbStorage::new(&redb_path)?;
let redb_arc = Arc::new(redb);
let memory = SelfLearningMemory::with_storage(config, turso_arc.clone(), redb_arc.clone());
Ok(Self {
temp_dir,
turso_url,
redb_path: redb_path.display().to_string(),
memory,
turso_storage: turso_arc,
redb_storage: redb_arc,
})
}
pub fn turso(&self) -> &TursoStorage {
&self.turso_storage
}
pub fn redb(&self) -> &RedbStorage {
&self.redb_storage
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_environment_creation() {
let env = TestEnvironment::new().await.unwrap();
assert!(env.turso_url.starts_with("file:"));
assert!(env.redb_path.ends_with("cache.redb"));
}
#[tokio::test]
async fn test_environment_with_custom_config() {
let mut config = MemoryConfig::default();
config.pattern_extraction_threshold = 0.5;
let env = TestEnvironment::with_config(config).await.unwrap();
assert!(env.turso_url.starts_with("file:"));
}
}