Built for people who think better out loud.
1use atproto_identity::key::{KeyType, generate_key};
2use sqlx::PgPool;
3
4use crate::config::Config;
5use crate::logging::Logger;
6use crate::infrastructure::oauth::build_oauth_dependencies;
7use crate::infrastructure::whisper::WhisperClient;
8use crate::state::AppState;
9
10pub struct TestStateBuilder {
11 config: Config,
12 logger: Logger,
13 db_pool: PgPool,
14 whisper_client: Option<WhisperClient>,
15}
16
17impl TestStateBuilder {
18 pub fn new() -> Self {
19 let config = test_config();
20 let logger = Logger::disabled();
21 let db_pool =
22 PgPool::connect_lazy("postgres://test:test@localhost:5432/test").expect("pool");
23 Self {
24 config,
25 logger,
26 db_pool,
27 whisper_client: None,
28 }
29 }
30
31 pub fn with_logger(mut self, logger: Logger) -> Self {
32 self.logger = logger;
33 self
34 }
35
36 pub fn with_whisper_client(mut self, client: WhisperClient) -> Self {
37 self.whisper_client = Some(client);
38 self
39 }
40
41 pub fn build(self) -> AppState {
42 let whisper_client = self
43 .whisper_client
44 .unwrap_or_else(|| WhisperClient::new(self.config.clone()));
45 let oauth_dependencies = build_oauth_dependencies(&self.config, self.db_pool.clone());
46 AppState {
47 config: self.config,
48 whisper_client,
49 logger: self.logger,
50 db_pool: self.db_pool,
51 http_client: oauth_dependencies.http_client,
52 oauth_client_config: oauth_dependencies.oauth_client_config,
53 oauth_request_storage: oauth_dependencies.oauth_request_storage,
54 did_document_storage: oauth_dependencies.did_document_storage,
55 key_resolver: oauth_dependencies.key_resolver,
56 identity_resolver: oauth_dependencies.identity_resolver,
57 oauth_signing_key: oauth_dependencies.oauth_signing_key,
58 }
59 }
60}
61
62pub fn test_config() -> Config {
63 let oauth_signing_key =
64 generate_key(KeyType::P256Private).expect("oauth signing key").to_string();
65 Config {
66 openai_api_key: "test-key".to_string(),
67 openai_base_url: "http://localhost".to_string(),
68 openai_whisper_model: "whisper-1".to_string(),
69 openai_whisper_response_format: "json".to_string(),
70 bind_addr: "0.0.0.0:3001".to_string(),
71 database_url: "postgres://test:test@localhost:5432/test".to_string(),
72 slipnote_env: "local".to_string(),
73 log_sample_rate: 1.0,
74 transcription_cost_per_second_dollars: 0.000_094_34,
75 axiom_token: None,
76 axiom_dataset: None,
77 axiom_url: None,
78 oauth_client_id: "https://example.com/oauth/client-metadata.json".to_string(),
79 oauth_redirect_uri: "https://example.com/oauth/callback".to_string(),
80 oauth_client_name: None,
81 oauth_client_uri: None,
82 oauth_jwks_uri: None,
83 oauth_signing_key,
84 oauth_scopes: "atproto transition:generic".to_string(),
85 oauth_post_auth_redirect: "/".to_string(),
86 oauth_cookie_name: "slipnote_session".to_string(),
87 oauth_session_ttl_seconds: 60 * 60 * 24 * 7,
88 plc_hostname: "plc.directory".to_string(),
89 oauth_base_url: None,
90 }
91}