use crate::Result;
use std::collections::HashMap;
use terraphim_settings::DeviceSettings;
pub fn create_memory_only_device_settings() -> Result<DeviceSettings> {
let mut profiles = HashMap::new();
let mut memory_profile = HashMap::new();
memory_profile.insert("type".to_string(), "memory".to_string());
profiles.insert("memory".to_string(), memory_profile);
let settings = DeviceSettings {
server_hostname: "localhost".to_string(),
api_endpoint: "http://localhost:8080".to_string(),
initialized: true,
default_data_path: "/tmp/terraphim_test".to_string(),
profiles,
role_config: None,
default_role: None,
};
Ok(settings)
}
pub fn create_test_device_settings() -> Result<DeviceSettings> {
create_memory_only_device_settings()
}
#[cfg(feature = "dashmap")]
pub fn create_multi_profile_device_settings() -> Result<DeviceSettings> {
let mut profiles = HashMap::new();
let mut memory_profile = HashMap::new();
memory_profile.insert("type".to_string(), "memory".to_string());
profiles.insert("memory".to_string(), memory_profile);
let mut dashmap_profile = HashMap::new();
dashmap_profile.insert("type".to_string(), "dashmap".to_string());
dashmap_profile.insert(
"root".to_string(),
"/tmp/terraphim_test_dashmap".to_string(),
);
profiles.insert("dashmap".to_string(), dashmap_profile);
let settings = DeviceSettings {
server_hostname: "localhost".to_string(),
api_endpoint: "http://localhost:8080".to_string(),
initialized: true,
default_data_path: "/tmp/terraphim_test".to_string(),
profiles,
role_config: None,
default_role: None,
};
Ok(settings)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::settings::parse_profiles;
#[tokio::test]
async fn test_memory_only_device_settings() -> Result<()> {
let settings = create_memory_only_device_settings()?;
assert!(settings.profiles.contains_key("memory"));
assert_eq!(
settings
.profiles
.get("memory")
.unwrap()
.get("type")
.unwrap(),
"memory"
);
let operators = parse_profiles(&settings).await?;
assert!(operators.contains_key("memory"));
let (memory_op, _speed) = operators.get("memory").unwrap();
memory_op.write("test_key", "test_value").await.unwrap();
let result = memory_op.read("test_key").await.unwrap();
assert_eq!(result.to_vec(), "test_value".as_bytes());
Ok(())
}
#[tokio::test]
async fn test_memory_persistable() -> Result<()> {
use crate::settings::parse_profiles;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct TestData {
name: String,
value: i32,
}
impl TestData {
async fn save_to_memory_operator(&self, operator: &opendal::Operator) -> Result<()> {
let key = format!("test_data_{}", self.name.to_lowercase());
let data = serde_json::to_string(self)?;
operator.write(&key, data).await?;
Ok(())
}
async fn load_from_memory_operator(
name: &str,
operator: &opendal::Operator,
) -> Result<Self> {
let key = format!("test_data_{}", name.to_lowercase());
let data = operator.read(&key).await?;
let obj: TestData = serde_json::from_slice(&data.to_vec())?;
Ok(obj)
}
}
let settings = create_memory_only_device_settings()?;
let operators = parse_profiles(&settings).await?;
let (memory_op, _speed) = operators.get("memory").unwrap();
let test_data = TestData {
name: "test_item".to_string(),
value: 42,
};
test_data.save_to_memory_operator(memory_op).await?;
let loaded_data = TestData::load_from_memory_operator("test_item", memory_op).await?;
assert_eq!(test_data, loaded_data);
Ok(())
}
#[tokio::test]
async fn test_thesaurus_memory_persistence() -> Result<()> {
use crate::settings::parse_profiles;
use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};
let settings = create_memory_only_device_settings()?;
let operators = parse_profiles(&settings).await?;
let (memory_op, _speed) = operators.get("memory").unwrap();
let mut thesaurus = Thesaurus::new("Test Engineer".to_string());
let term1 = NormalizedTerm::new(1, NormalizedTermValue::from("machine learning"));
let term2 = NormalizedTerm::new(2, NormalizedTermValue::from("artificial intelligence"));
thesaurus.insert(NormalizedTermValue::from("ml"), term1.clone());
thesaurus.insert(NormalizedTermValue::from("ai"), term2.clone());
thesaurus.insert(NormalizedTermValue::from("machine learning"), term1.clone());
thesaurus.insert(
NormalizedTermValue::from("artificial intelligence"),
term2.clone(),
);
let key = format!("thesaurus_{}.json", "testengineer");
let thesaurus_json = serde_json::to_string(&thesaurus)?;
memory_op.write(&key, thesaurus_json).await?;
let loaded_data = memory_op.read(&key).await?;
let loaded_thesaurus: Thesaurus = serde_json::from_slice(&loaded_data.to_vec())?;
assert_eq!(thesaurus.name(), loaded_thesaurus.name());
assert_eq!(thesaurus.len(), loaded_thesaurus.len());
assert_eq!(thesaurus.len(), 4);
let ml_term = loaded_thesaurus
.get(&NormalizedTermValue::from("ml"))
.unwrap();
assert_eq!(ml_term.id, 1);
assert_eq!(ml_term.value, NormalizedTermValue::from("machine learning"));
let ai_term = loaded_thesaurus
.get(&NormalizedTermValue::from("ai"))
.unwrap();
assert_eq!(ai_term.id, 2);
assert_eq!(
ai_term.value,
NormalizedTermValue::from("artificial intelligence")
);
println!("✅ Thesaurus memory persistence test passed");
println!(" Saved and loaded {} terms", loaded_thesaurus.len());
Ok(())
}
#[tokio::test]
async fn test_config_memory_persistence() -> Result<()> {
use crate::settings::parse_profiles;
use terraphim_config::{ConfigBuilder, ConfigId};
let settings = create_memory_only_device_settings()?;
let operators = parse_profiles(&settings).await?;
let (memory_op, _speed) = operators.get("memory").unwrap();
let config = ConfigBuilder::new().build().unwrap();
let key = format!(
"{}_config.json",
match config.id {
ConfigId::Desktop => "desktop",
ConfigId::Server => "server",
ConfigId::Embedded => "embedded",
}
);
let config_json = serde_json::to_string(&config)?;
memory_op.write(&key, config_json).await?;
let loaded_data = memory_op.read(&key).await?;
let loaded_config: terraphim_config::Config =
serde_json::from_slice(&loaded_data.to_vec())?;
assert_eq!(config.id, loaded_config.id);
assert_eq!(config.default_role, loaded_config.default_role);
println!("✅ Config memory persistence test passed");
Ok(())
}
}