1use crate::app::AppConfig;
7use crate::auth::{AuthStorage, CredentialType};
8use crate::config::LlmConfigProvider;
9use std::sync::Arc;
10
11pub 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
63pub fn test_llm_config_provider() -> LlmConfigProvider {
65 let storage = Arc::new(InMemoryAuthStorage::new());
66 LlmConfigProvider::new(storage)
67}
68
69pub fn test_app_config() -> AppConfig {
71 AppConfig {
72 llm_config_provider: test_llm_config_provider(),
73 }
74}