steer_core/session/
store_config.rs

1use crate::error::{Error, Result};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5/// Configuration for session store creation
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(tag = "type", rename_all = "snake_case")]
8pub enum SessionStoreConfig {
9    /// SQLite database store
10    Sqlite {
11        /// Path to the database file
12        path: PathBuf,
13    },
14    /// Future: PostgreSQL store
15    #[allow(dead_code)]
16    Postgres {
17        /// Connection string
18        connection_string: String,
19    },
20    /// Future: In-memory store for testing
21    #[allow(dead_code)]
22    Memory,
23}
24
25impl SessionStoreConfig {
26    /// Create a new SQLite store configuration
27    pub fn sqlite(path: PathBuf) -> Self {
28        Self::Sqlite { path }
29    }
30
31    /// Get the default session store configuration
32    pub fn default_sqlite() -> Result<Self> {
33        let home_dir = dirs::home_dir().ok_or_else(|| {
34            Error::Configuration("Could not determine home directory".to_string())
35        })?;
36        let db_path = home_dir.join(".steer").join("sessions.db");
37        Ok(Self::sqlite(db_path))
38    }
39}
40
41impl Default for SessionStoreConfig {
42    fn default() -> Self {
43        Self::default_sqlite().unwrap_or_else(|_| {
44            // Fallback to current directory if home directory cannot be determined
45            Self::sqlite(PathBuf::from("./sessions.db"))
46        })
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_sqlite_config_creation() {
56        let path = PathBuf::from("/tmp/test.db");
57        let config = SessionStoreConfig::sqlite(path.clone());
58
59        let SessionStoreConfig::Sqlite { path: config_path } = config else {
60            unreachable!("expected SQLite config")
61        };
62        assert_eq!(config_path, path);
63    }
64
65    #[test]
66    fn test_default_sqlite_config() {
67        let config = SessionStoreConfig::default_sqlite();
68        assert!(config.is_ok());
69
70        let SessionStoreConfig::Sqlite { path } = config.unwrap() else {
71            unreachable!("expected SQLite config")
72        };
73        assert!(path.to_string_lossy().contains(".steer"));
74        assert!(path.to_string_lossy().contains("sessions.db"));
75    }
76
77    #[test]
78    fn test_default_config() {
79        let config = SessionStoreConfig::default();
80
81        let SessionStoreConfig::Sqlite { path } = config else {
82            unreachable!("expected SQLite config")
83        };
84        // Should either be in .steer or current directory
85        let path_str = path.to_string_lossy();
86        assert!(path_str.contains("sessions.db"));
87    }
88}