use crate::config::ConfigStorage;
use crate::omp::OmpConfiguration;
use std::collections::BTreeMap;
impl ConfigStorage {
pub fn add_omp_configuration(&mut self, config: OmpConfiguration) {
self.omp_configurations
.get_or_insert_with(BTreeMap::new)
.insert(config.alias_name.clone(), config);
}
pub fn get_omp_configuration(&self, alias_name: &str) -> Option<&OmpConfiguration> {
self.omp_configurations.as_ref()?.get(alias_name)
}
pub fn remove_omp_configuration(&mut self, alias_name: &str) -> bool {
if let Some(ref mut map) = self.omp_configurations {
map.remove(alias_name).is_some()
} else {
false
}
}
pub fn update_omp_configuration(
&mut self,
old_alias: &str,
new_config: OmpConfiguration,
) -> anyhow::Result<()> {
let map = self
.omp_configurations
.as_mut()
.ok_or_else(|| anyhow::anyhow!("No OMP configurations in storage"))?;
if !map.contains_key(old_alias) {
anyhow::bail!("OMP configuration '{}' not found", old_alias);
}
if old_alias != new_config.alias_name {
map.remove(old_alias);
}
map.insert(new_config.alias_name.clone(), new_config);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_anthropic_config(alias: &str) -> OmpConfiguration {
OmpConfiguration {
alias_name: alias.to_string(),
auth_mode: "anthropic".to_string(),
anthropic_api_key: Some("sk-ant-test".to_string()),
anthropic_oauth_token: None,
openai_api_key: None,
base_url: None,
slow_model: None,
smol_model: None,
plan_model: None,
}
}
fn make_openai_config(alias: &str) -> OmpConfiguration {
OmpConfiguration {
alias_name: alias.to_string(),
auth_mode: "openai".to_string(),
anthropic_api_key: None,
anthropic_oauth_token: None,
openai_api_key: Some("sk-test".to_string()),
base_url: None,
slow_model: None,
smol_model: None,
plan_model: None,
}
}
#[test]
fn test_add_omp_configuration() {
let mut storage = ConfigStorage::default();
assert!(storage.omp_configurations.is_none());
let config = make_anthropic_config("test");
storage.add_omp_configuration(config.clone());
assert!(storage.omp_configurations.is_some());
assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
assert_eq!(
storage.omp_configurations.as_ref().unwrap().get("test"),
Some(&config)
);
}
#[test]
fn test_add_overwrites_existing() {
let mut storage = ConfigStorage::default();
storage.add_omp_configuration(make_anthropic_config("test"));
let mut updated = make_anthropic_config("test");
updated.anthropic_api_key = Some("sk-ant-updated".to_string());
storage.add_omp_configuration(updated.clone());
assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
assert_eq!(
storage
.get_omp_configuration("test")
.unwrap()
.anthropic_api_key,
Some("sk-ant-updated".to_string())
);
}
#[test]
fn test_get_omp_configuration() {
let mut storage = ConfigStorage::default();
let config = make_openai_config("foo");
storage.add_omp_configuration(config.clone());
assert_eq!(storage.get_omp_configuration("foo"), Some(&config));
assert_eq!(storage.get_omp_configuration("bar"), None);
}
#[test]
fn test_get_omp_configuration_empty_storage() {
let storage = ConfigStorage::default();
assert_eq!(storage.get_omp_configuration("any"), None);
}
#[test]
fn test_remove_omp_configuration() {
let mut storage = ConfigStorage::default();
storage.add_omp_configuration(make_anthropic_config("test"));
assert!(storage.remove_omp_configuration("test"));
assert_eq!(storage.get_omp_configuration("test"), None);
assert!(storage.omp_configurations.as_ref().unwrap().is_empty());
}
#[test]
fn test_remove_nonexistent_omp_configuration() {
let mut storage = ConfigStorage::default();
storage.add_omp_configuration(make_anthropic_config("test"));
assert!(!storage.remove_omp_configuration("nonexistent"));
assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
}
#[test]
fn test_remove_from_empty_storage() {
let mut storage = ConfigStorage::default();
assert!(!storage.remove_omp_configuration("test"));
}
#[test]
fn test_update_omp_configuration() {
let mut storage = ConfigStorage::default();
storage.add_omp_configuration(make_anthropic_config("old"));
let mut updated = make_anthropic_config("new");
updated.slow_model = Some("claude-3-5-sonnet".to_string());
storage.update_omp_configuration("old", updated).unwrap();
assert_eq!(storage.get_omp_configuration("old"), None);
let new_config = storage.get_omp_configuration("new").unwrap();
assert_eq!(new_config.slow_model, Some("claude-3-5-sonnet".to_string()));
}
#[test]
fn test_update_nonexistent_omp_configuration() {
let mut storage = ConfigStorage::default();
storage.add_omp_configuration(make_anthropic_config("test"));
let result = storage.update_omp_configuration("nonexistent", make_anthropic_config("new"));
assert!(result.is_err());
}
}