steer_core/
test_utils.rs

1//! Test utilities for steer-core
2//!
3//! This module provides helpers for testing that need to be accessible
4//! across crate boundaries.
5
6use crate::app::AppConfig;
7use crate::auth::{AuthStorage, CredentialType};
8use crate::config::LlmConfigProvider;
9use std::sync::Arc;
10
11/// In-memory storage for testing - doesn't use keyring or filesystem
12pub struct InMemoryAuthStorage {
13    credentials:
14        Arc<tokio::sync::Mutex<std::collections::HashMap<String, crate::auth::Credential>>>,
15}
16
17impl Default for InMemoryAuthStorage {
18    fn default() -> Self {
19        Self::new()
20    }
21}
22
23impl InMemoryAuthStorage {
24    pub fn new() -> Self {
25        Self {
26            credentials: Arc::new(tokio::sync::Mutex::new(std::collections::HashMap::new())),
27        }
28    }
29}
30
31#[async_trait::async_trait]
32impl AuthStorage for InMemoryAuthStorage {
33    async fn get_credential(
34        &self,
35        provider: &str,
36        credential_type: CredentialType,
37    ) -> crate::auth::Result<Option<crate::auth::Credential>> {
38        let key = format!("{provider}-{credential_type}");
39        Ok(self.credentials.lock().await.get(&key).cloned())
40    }
41
42    async fn set_credential(
43        &self,
44        provider: &str,
45        credential: crate::auth::Credential,
46    ) -> crate::auth::Result<()> {
47        let key = format!("{}-{}", provider, credential.credential_type());
48        self.credentials.lock().await.insert(key, credential);
49        Ok(())
50    }
51
52    async fn remove_credential(
53        &self,
54        provider: &str,
55        credential_type: CredentialType,
56    ) -> crate::auth::Result<()> {
57        let key = format!("{provider}-{credential_type}");
58        self.credentials.lock().await.remove(&key);
59        Ok(())
60    }
61}
62
63/// Create an `LlmConfigProvider` backed by in-memory auth storage for tests
64pub fn test_llm_config_provider() -> LlmConfigProvider {
65    let storage = Arc::new(InMemoryAuthStorage::new());
66    LlmConfigProvider::new(storage)
67}
68
69/// Convenience to build an `AppConfig` for tests with a fresh provider
70pub fn test_app_config() -> AppConfig {
71    AppConfig {
72        llm_config_provider: test_llm_config_provider(),
73    }
74}