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_anthropic_config(alias: &str) -> OmpConfiguration {
62 OmpConfiguration {
63 alias_name: alias.to_string(),
64 auth_mode: "anthropic".to_string(),
65 anthropic_api_key: Some("sk-ant-test".to_string()),
66 anthropic_oauth_token: None,
67 openai_api_key: None,
68 base_url: None,
69 slow_model: None,
70 smol_model: None,
71 plan_model: None,
72 }
73 }
74
75 fn make_openai_config(alias: &str) -> OmpConfiguration {
76 OmpConfiguration {
77 alias_name: alias.to_string(),
78 auth_mode: "openai".to_string(),
79 anthropic_api_key: None,
80 anthropic_oauth_token: None,
81 openai_api_key: Some("sk-test".to_string()),
82 base_url: None,
83 slow_model: None,
84 smol_model: None,
85 plan_model: None,
86 }
87 }
88
89 #[test]
90 fn test_add_omp_configuration() {
91 let mut storage = ConfigStorage::default();
92 assert!(storage.omp_configurations.is_none());
93
94 let config = make_anthropic_config("test");
95 storage.add_omp_configuration(config.clone());
96
97 assert!(storage.omp_configurations.is_some());
98 assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
99 assert_eq!(
100 storage.omp_configurations.as_ref().unwrap().get("test"),
101 Some(&config)
102 );
103 }
104
105 #[test]
106 fn test_add_overwrites_existing() {
107 let mut storage = ConfigStorage::default();
108 storage.add_omp_configuration(make_anthropic_config("test"));
109
110 let mut updated = make_anthropic_config("test");
111 updated.anthropic_api_key = Some("sk-ant-updated".to_string());
112 storage.add_omp_configuration(updated.clone());
113
114 assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
115 assert_eq!(
116 storage
117 .get_omp_configuration("test")
118 .unwrap()
119 .anthropic_api_key,
120 Some("sk-ant-updated".to_string())
121 );
122 }
123
124 #[test]
125 fn test_get_omp_configuration() {
126 let mut storage = ConfigStorage::default();
127 let config = make_openai_config("foo");
128 storage.add_omp_configuration(config.clone());
129
130 assert_eq!(storage.get_omp_configuration("foo"), Some(&config));
131 assert_eq!(storage.get_omp_configuration("bar"), None);
132 }
133
134 #[test]
135 fn test_get_omp_configuration_empty_storage() {
136 let storage = ConfigStorage::default();
137 assert_eq!(storage.get_omp_configuration("any"), None);
138 }
139
140 #[test]
141 fn test_remove_omp_configuration() {
142 let mut storage = ConfigStorage::default();
143 storage.add_omp_configuration(make_anthropic_config("test"));
144
145 assert!(storage.remove_omp_configuration("test"));
146 assert_eq!(storage.get_omp_configuration("test"), None);
147 assert!(storage.omp_configurations.as_ref().unwrap().is_empty());
148 }
149
150 #[test]
151 fn test_remove_nonexistent_omp_configuration() {
152 let mut storage = ConfigStorage::default();
153 storage.add_omp_configuration(make_anthropic_config("test"));
154
155 assert!(!storage.remove_omp_configuration("nonexistent"));
156 assert_eq!(storage.omp_configurations.as_ref().unwrap().len(), 1);
157 }
158
159 #[test]
160 fn test_remove_from_empty_storage() {
161 let mut storage = ConfigStorage::default();
162 assert!(!storage.remove_omp_configuration("test"));
163 }
164
165 #[test]
166 fn test_update_omp_configuration() {
167 let mut storage = ConfigStorage::default();
168 storage.add_omp_configuration(make_anthropic_config("old"));
169
170 let mut updated = make_anthropic_config("new");
171 updated.slow_model = Some("claude-3-5-sonnet".to_string());
172 storage.update_omp_configuration("old", updated).unwrap();
173
174 assert_eq!(storage.get_omp_configuration("old"), None);
175 let new_config = storage.get_omp_configuration("new").unwrap();
176 assert_eq!(new_config.slow_model, Some("claude-3-5-sonnet".to_string()));
177 }
178
179 #[test]
180 fn test_update_nonexistent_omp_configuration() {
181 let mut storage = ConfigStorage::default();
182 storage.add_omp_configuration(make_anthropic_config("test"));
183
184 let result = storage.update_omp_configuration("nonexistent", make_anthropic_config("new"));
185 assert!(result.is_err());
186 }
187}