1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
9pub struct OmpConfiguration {
10 pub alias_name: String,
12 pub auth_mode: String,
14 #[serde(skip_serializing_if = "Option::is_none")]
16 pub anthropic_api_key: Option<String>,
17 #[serde(skip_serializing_if = "Option::is_none")]
19 pub anthropic_oauth_token: Option<String>,
20 #[serde(skip_serializing_if = "Option::is_none")]
22 pub openai_api_key: Option<String>,
23 #[serde(skip_serializing_if = "Option::is_none")]
25 pub base_url: Option<String>,
26 #[serde(skip_serializing_if = "Option::is_none")]
28 pub slow_model: Option<String>,
29 #[serde(skip_serializing_if = "Option::is_none")]
31 pub smol_model: Option<String>,
32 #[serde(skip_serializing_if = "Option::is_none")]
34 pub plan_model: Option<String>,
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40
41 #[test]
42 fn test_serialize_anthropic_mode() {
43 let config = OmpConfiguration {
44 alias_name: "test".to_string(),
45 auth_mode: "anthropic".to_string(),
46 anthropic_api_key: Some("sk-ant-test".to_string()),
47 anthropic_oauth_token: None,
48 openai_api_key: None,
49 base_url: None,
50 slow_model: Some("claude-3-5-sonnet-20241022".to_string()),
51 smol_model: None,
52 plan_model: None,
53 };
54
55 let json = serde_json::to_string(&config).expect("Should serialize");
56 assert!(json.contains("\"auth_mode\":\"anthropic\""));
57 assert!(json.contains("\"anthropic_api_key\":\"sk-ant-test\""));
58 assert!(!json.contains("openai_api_key"));
59 }
60
61 #[test]
62 fn test_deserialize_anthropic_mode() {
63 let json = r#"{
64 "alias_name": "test",
65 "auth_mode": "anthropic",
66 "anthropic_api_key": "sk-ant-test",
67 "slow_model": "claude-3-5-sonnet-20241022"
68 }"#;
69
70 let config: OmpConfiguration = serde_json::from_str(json).expect("Should deserialize");
71 assert_eq!(config.auth_mode, "anthropic");
72 assert_eq!(config.anthropic_api_key, Some("sk-ant-test".to_string()));
73 assert_eq!(
74 config.slow_model,
75 Some("claude-3-5-sonnet-20241022".to_string())
76 );
77 assert_eq!(config.openai_api_key, None);
78 }
79
80 #[test]
81 fn test_serialize_openai_mode() {
82 let config = OmpConfiguration {
83 alias_name: "test".to_string(),
84 auth_mode: "openai".to_string(),
85 anthropic_api_key: None,
86 anthropic_oauth_token: None,
87 openai_api_key: Some("sk-test".to_string()),
88 base_url: None,
89 slow_model: Some("openai/gpt-5.3-codex:high".to_string()),
90 smol_model: Some("openai/gpt-4o-mini".to_string()),
91 plan_model: None,
92 };
93
94 let json = serde_json::to_string(&config).expect("Should serialize");
95 assert!(json.contains("\"auth_mode\":\"openai\""));
96 assert!(json.contains("\"openai_api_key\":\"sk-test\""));
97 assert!(!json.contains("anthropic_api_key"));
98 }
99
100 #[test]
101 fn test_deserialize_openai_mode() {
102 let json = r#"{
103 "alias_name": "test",
104 "auth_mode": "openai",
105 "openai_api_key": "sk-test",
106 "slow_model": "openai/gpt-5.3-codex:high"
107 }"#;
108
109 let config: OmpConfiguration = serde_json::from_str(json).expect("Should deserialize");
110 assert_eq!(config.auth_mode, "openai");
111 assert_eq!(config.openai_api_key, Some("sk-test".to_string()));
112 assert_eq!(
113 config.slow_model,
114 Some("openai/gpt-5.3-codex:high".to_string())
115 );
116 assert_eq!(config.anthropic_api_key, None);
117 }
118
119 #[test]
120 fn test_roundtrip_serialization() {
121 let original = OmpConfiguration {
122 alias_name: "work".to_string(),
123 auth_mode: "anthropic".to_string(),
124 anthropic_api_key: Some("sk-ant-work".to_string()),
125 anthropic_oauth_token: None,
126 openai_api_key: None,
127 base_url: None,
128 slow_model: Some("claude-3-5-sonnet-20241022".to_string()),
129 smol_model: Some("claude-3-haiku-20240307".to_string()),
130 plan_model: Some("claude-3-5-sonnet-20241022".to_string()),
131 };
132
133 let json = serde_json::to_string(&original).expect("Should serialize");
134 let deserialized: OmpConfiguration =
135 serde_json::from_str(&json).expect("Should deserialize");
136 assert_eq!(original, deserialized);
137 }
138}