Skip to main content

slack_rs/profile/
resolver.rs

1use crate::profile::storage::{load_config, Result as StorageResult};
2use crate::profile::types::{Profile, ProfilesConfig};
3use std::path::Path;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum ResolverError {
8    #[error("Profile not found: {0}")]
9    ProfileNotFound(String),
10    #[error("Storage error: {0}")]
11    Storage(#[from] crate::profile::storage::StorageError),
12}
13
14pub type Result<T> = std::result::Result<T, ResolverError>;
15
16/// Resolve a profile name to (team_id, user_id)
17pub fn resolve_profile(config_path: &Path, profile_name: &str) -> Result<(String, String)> {
18    let config = load_config(config_path)?;
19    let profile = config
20        .get(profile_name)
21        .ok_or_else(|| ResolverError::ProfileNotFound(profile_name.to_string()))?;
22
23    Ok((profile.team_id.clone(), profile.user_id.clone()))
24}
25
26/// Resolve a profile name to the full Profile object
27pub fn resolve_profile_full(config_path: &Path, profile_name: &str) -> Result<Profile> {
28    let config = load_config(config_path)?;
29    config
30        .get(profile_name)
31        .cloned()
32        .ok_or_else(|| ResolverError::ProfileNotFound(profile_name.to_string()))
33}
34
35/// Get all profiles from config
36pub fn list_profiles(config_path: &Path) -> StorageResult<ProfilesConfig> {
37    load_config(config_path)
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::profile::storage::save_config;
44    use crate::profile::types::Profile;
45    use tempfile::TempDir;
46
47    fn setup_test_config() -> (TempDir, std::path::PathBuf) {
48        let temp_dir = TempDir::new().unwrap();
49        let config_path = temp_dir.path().join("profiles.json");
50
51        let mut config = ProfilesConfig::new();
52        config.set(
53            "default".to_string(),
54            Profile {
55                team_id: "T123".to_string(),
56                user_id: "U456".to_string(),
57                team_name: Some("Test Team".to_string()),
58                user_name: Some("Test User".to_string()),
59                client_id: None,
60                redirect_uri: None,
61                scopes: None,
62                bot_scopes: None,
63                user_scopes: None,
64                default_token_type: None,
65            },
66        );
67        config.set(
68            "work".to_string(),
69            Profile {
70                team_id: "T789".to_string(),
71                user_id: "U012".to_string(),
72                team_name: Some("Work Team".to_string()),
73                user_name: Some("Work User".to_string()),
74                client_id: None,
75                redirect_uri: None,
76                scopes: None,
77                bot_scopes: None,
78                user_scopes: None,
79                default_token_type: None,
80            },
81        );
82
83        save_config(&config_path, &config).unwrap();
84        (temp_dir, config_path)
85    }
86
87    #[test]
88    fn test_resolve_profile_existing() {
89        let (_temp_dir, config_path) = setup_test_config();
90
91        let result = resolve_profile(&config_path, "default");
92        assert!(result.is_ok());
93        let (team_id, user_id) = result.unwrap();
94        assert_eq!(team_id, "T123");
95        assert_eq!(user_id, "U456");
96    }
97
98    #[test]
99    fn test_resolve_profile_nonexistent() {
100        let (_temp_dir, config_path) = setup_test_config();
101
102        let result = resolve_profile(&config_path, "nonexistent");
103        assert!(result.is_err());
104        match result {
105            Err(ResolverError::ProfileNotFound(name)) => {
106                assert_eq!(name, "nonexistent");
107            }
108            _ => panic!("Expected ProfileNotFound error"),
109        }
110    }
111
112    #[test]
113    fn test_resolve_profile_full() {
114        let (_temp_dir, config_path) = setup_test_config();
115
116        let result = resolve_profile_full(&config_path, "default");
117        assert!(result.is_ok());
118        let profile = result.unwrap();
119        assert_eq!(profile.team_id, "T123");
120        assert_eq!(profile.user_id, "U456");
121        assert_eq!(profile.team_name, Some("Test Team".to_string()));
122        assert_eq!(profile.user_name, Some("Test User".to_string()));
123    }
124
125    #[test]
126    fn test_resolve_profile_multiple() {
127        let (_temp_dir, config_path) = setup_test_config();
128
129        let (team_id1, user_id1) = resolve_profile(&config_path, "default").unwrap();
130        assert_eq!(team_id1, "T123");
131        assert_eq!(user_id1, "U456");
132
133        let (team_id2, user_id2) = resolve_profile(&config_path, "work").unwrap();
134        assert_eq!(team_id2, "T789");
135        assert_eq!(user_id2, "U012");
136    }
137
138    #[test]
139    fn test_list_profiles() {
140        let (_temp_dir, config_path) = setup_test_config();
141
142        let config = list_profiles(&config_path).unwrap();
143        assert_eq!(config.profiles.len(), 2);
144        assert!(config.get("default").is_some());
145        assert!(config.get("work").is_some());
146    }
147
148    #[test]
149    fn test_list_profiles_empty() {
150        let temp_dir = TempDir::new().unwrap();
151        let config_path = temp_dir.path().join("empty_profiles.json");
152
153        let config = list_profiles(&config_path).unwrap();
154        assert_eq!(config.profiles.len(), 0);
155    }
156}