1use crate::config::ConfigStorage;
2use crate::omp::OmpConfiguration;
3use std::collections::BTreeMap;
4
5impl ConfigStorage {
6 pub fn add_omp_configuration(&mut self, config: OmpConfiguration) {
10 self.omp_configurations
11 .get_or_insert_with(BTreeMap::new)
12 .insert(config.alias_name.clone(), config);
13 }
14
15 pub fn get_omp_configuration(&self, alias_name: &str) -> Option<&OmpConfiguration> {
17 self.omp_configurations.as_ref()?.get(alias_name)
18 }
19
20 pub fn remove_omp_configuration(&mut self, alias_name: &str) -> bool {
24 if let Some(ref mut map) = self.omp_configurations {
25 map.remove(alias_name).is_some()
26 } else {
27 false
28 }
29 }
30
31 pub fn update_omp_configuration(
36 &mut self,
37 old_alias: &str,
38 new_config: OmpConfiguration,
39 ) -> anyhow::Result<()> {
40 let map = self
41 .omp_configurations
42 .as_mut()
43 .ok_or_else(|| anyhow::anyhow!("No OMP configurations in storage"))?;
44
45 if !map.contains_key(old_alias) {
46 anyhow::bail!("OMP configuration '{}' not found", old_alias);
47 }
48
49 if old_alias != new_config.alias_name {
50 map.remove(old_alias);
51 }
52 map.insert(new_config.alias_name.clone(), new_config);
53 Ok(())
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 fn make_config(alias: &str) -> OmpConfiguration {
62 OmpConfiguration {
63 alias_name: alias.to_string(),
64 openai_api_key: Some("sk-test".to_string()),
65 base_url: None,
66 slow_model: None,
67 smol_model: None,
68 plan_model: None,
69 }
70 }
71
72 #[test]
73 fn test_add_omp_configuration() {
74 let mut storage = ConfigStorage::default();
75 assert!(storage.omp_configurations.is_none());
76
77 let config = make_config("test");
78 storage.add_omp_configuration(config.clone());
79
80 assert!(storage.omp_configurations.is_some());
81 assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
82 assert_eq!(
83 storage.omp_configurations.as_ref().unwrap().get("test"),
84 Some(&config)
85 );
86 }
87
88 #[test]
89 fn test_add_overwrites_existing() {
90 let mut storage = ConfigStorage::default();
91 storage.add_omp_configuration(make_config("test"));
92
93 let mut updated = make_config("test");
94 updated.openai_api_key = Some("sk-updated".to_string());
95 storage.add_omp_configuration(updated.clone());
96
97 assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
98 assert_eq!(
99 storage
100 .get_omp_configuration("test")
101 .unwrap()
102 .openai_api_key,
103 Some("sk-updated".to_string())
104 );
105 }
106
107 #[test]
108 fn test_get_omp_configuration() {
109 let mut storage = ConfigStorage::default();
110 let config = make_config("foo");
111 storage.add_omp_configuration(config.clone());
112
113 assert_eq!(storage.get_omp_configuration("foo"), Some(&config));
114 assert_eq!(storage.get_omp_configuration("bar"), None);
115 }
116
117 #[test]
118 fn test_get_omp_configuration_empty_storage() {
119 let storage = ConfigStorage::default();
120 assert_eq!(storage.get_omp_configuration("any"), None);
121 }
122
123 #[test]
124 fn test_remove_omp_configuration() {
125 let mut storage = ConfigStorage::default();
126 storage.add_omp_configuration(make_config("test"));
127
128 assert!(storage.remove_omp_configuration("test"));
129 assert_eq!(storage.get_omp_configuration("test"), None);
130 assert!(storage.omp_configurations.as_ref().unwrap().is_empty());
131 }
132
133 #[test]
134 fn test_remove_nonexistent_omp_configuration() {
135 let mut storage = ConfigStorage::default();
136 storage.add_omp_configuration(make_config("test"));
137
138 assert!(!storage.remove_omp_configuration("nonexistent"));
139 assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
140 }
141
142 #[test]
143 fn test_remove_from_empty_storage() {
144 let mut storage = ConfigStorage::default();
145 assert!(!storage.remove_omp_configuration("test"));
146 }
147
148 #[test]
149 fn test_update_omp_configuration() {
150 let mut storage = ConfigStorage::default();
151 storage.add_omp_configuration(make_config("old"));
152
153 let mut updated = make_config("new");
154 updated.slow_model = Some("claude-3-5-sonnet".to_string());
155 storage.update_omp_configuration("old", updated).unwrap();
156
157 assert_eq!(storage.get_omp_configuration("old"), None);
158 let new_config = storage.get_omp_configuration("new").unwrap();
159 assert_eq!(new_config.slow_model, Some("claude-3-5-sonnet".to_string()));
160 }
161
162 #[test]
163 fn test_update_nonexistent_omp_configuration() {
164 let mut storage = ConfigStorage::default();
165 storage.add_omp_configuration(make_config("test"));
166
167 let result = storage.update_omp_configuration("nonexistent", make_config("new"));
168 assert!(result.is_err());
169 }
170}