litellm-rs 0.6.0

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! Storage layer for the Gateway
//!
//! This module provides data persistence and caching functionality.

/// Database storage module
pub mod database;
/// Per-dependency runtime status (Healthy / Degraded / Disabled / ...)
pub mod dependency_status;
/// File storage module
pub mod files;
/// Redis cache module
pub mod redis;
/// Vector storage module
pub mod vector;

pub use dependency_status::DependencyStatus;

use crate::config::models::storage::{DatabaseConfig, StorageConfig};
use crate::utils::error::gateway_error::{GatewayError, Result};
use std::path::PathBuf;
use std::sync::Arc;
use tracing::{debug, error, info, warn};

const BUDGET_LIMIT_SNAPSHOT_MIGRATION: &str = "m20240501_000001_create_budget_limit_snapshots";

/// Returns the default base directory for local gateway state.
///
/// Resolution order:
/// 1. `LITELLM_DATA_DIR` environment variable (if set and non-empty)
/// 2. `<data_local_dir>/litellm-rs` (platform-specific)
/// 3. `/tmp/litellm-rs` (ultimate fallback)
pub fn default_data_dir() -> PathBuf {
    if let Ok(p) = std::env::var("LITELLM_DATA_DIR")
        && !p.is_empty()
    {
        return PathBuf::from(p);
    }
    dirs::data_local_dir()
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join("litellm-rs")
}

#[cfg(test)]
pub(crate) static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

/// Main storage layer that orchestrates all storage backends
#[derive(Debug, Clone)]
pub struct StorageLayer {
    /// Database connection pool
    pub database: Arc<database::Database>,
    /// Redis connection pool
    pub redis: Arc<redis::RedisPool>,
    /// File storage backend
    pub files: Arc<files::FileStorage>,
    /// Vector database client (optional)
    /// Note: Using concrete type instead of trait object for now
    pub vector: Option<Arc<vector::VectorStoreBackend>>,
    /// Status of the Redis dependency after init.
    pub redis_status: DependencyStatus,
    /// Status of the vector DB dependency after init.
    pub vector_status: DependencyStatus,
}

impl StorageLayer {
    /// Create a new storage layer.
    ///
    /// For each optional dependency:
    /// * if `enabled` is `false` (or the section is absent), the dependency is
    ///   treated as `Disabled` and a no-op fallback is used,
    /// * if `enabled` is `true` and init succeeds, status is `Healthy`,
    /// * if `enabled` is `true`, init fails, and `allow_degraded` is `false`,
    ///   the call returns `Err` so startup fails loudly,
    /// * if `enabled` is `true`, init fails, and `allow_degraded` is `true`,
    ///   the error is logged at `error!` level and the dependency status is
    ///   set to `Degraded` while the gateway keeps running.
    pub async fn new(config: &StorageConfig) -> Result<Self> {
        info!("Initializing storage layer");

        // Initialize database
        debug!("Connecting to database");
        let database = Arc::new(database::Database::new(&config.database).await?);
        Self::initialize_database_schema(&database, &config.database).await?;

        // Initialize Redis (fail-fast unless allow_degraded is set)
        debug!("Creating Redis connection pool");
        let (redis, redis_status) = if !config.redis.enabled {
            // Explicitly disabled — no fallback, no error.
            info!("Redis disabled in config; using no-op pool");
            (
                Arc::new(redis::RedisPool::create_noop()),
                DependencyStatus::Disabled,
            )
        } else {
            match redis::RedisPool::new(&config.redis).await {
                Ok(pool) => {
                    if pool.is_noop() {
                        info!("Redis pool initialized in no-op mode");
                    } else {
                        info!("Redis connection established");
                    }
                    (Arc::new(pool), DependencyStatus::Healthy)
                }
                Err(e) => {
                    if config.redis.allow_degraded {
                        error!(
                            "Redis init failed but allow_degraded=true; continuing without \
                             cache. Error: {}",
                            e
                        );
                        (
                            Arc::new(redis::RedisPool::create_noop()),
                            DependencyStatus::Degraded,
                        )
                    } else {
                        error!(
                            "Redis init failed and allow_degraded=false; failing startup. \
                             Set storage.redis.allow_degraded=true to keep running in no-op \
                             cache mode. Error: {}",
                            e
                        );
                        return Err(e);
                    }
                }
            }
        };

        // Initialize file storage
        debug!("Initializing file storage");
        let files = Arc::new(files::FileStorage::new(&config.files).await?);

        // Initialize vector database (fail-fast unless allow_degraded is set)
        let (vector, vector_status) = if let Some(ref vector_config) = config.vector_db {
            debug!("Initializing vector database");
            match vector::VectorStoreBackend::new(vector_config).await {
                Ok(v) => (Some(Arc::new(v)), DependencyStatus::Healthy),
                Err(e) => {
                    if vector_config.allow_degraded {
                        error!(
                            "Vector DB init failed but allow_degraded=true; continuing \
                             without vector backend. Error: {}",
                            e
                        );
                        (None, DependencyStatus::Degraded)
                    } else {
                        error!(
                            "Vector DB init failed and allow_degraded=false; failing \
                             startup. Set storage.vector_db.allow_degraded=true to keep \
                             running without a vector backend. Error: {}",
                            e
                        );
                        return Err(e);
                    }
                }
            }
        } else {
            debug!("Vector database not configured, skipping");
            (None, DependencyStatus::Disabled)
        };

        info!("Storage layer initialized successfully");

        Ok(Self {
            database,
            redis,
            files,
            vector,
            redis_status,
            vector_status,
        })
    }

    async fn initialize_database_schema(
        database: &database::Database,
        config: &DatabaseConfig,
    ) -> Result<()> {
        if Self::should_run_startup_migrations(database, config) {
            info!("Running database migrations during storage startup");
            database.migrate().await?;
            return Ok(());
        }

        info!(
            "Database startup migrations disabled; verifying configured schema is already present"
        );
        let allowed_pending = if config.allow_degraded {
            &[BUDGET_LIMIT_SNAPSHOT_MIGRATION][..]
        } else {
            &[][..]
        };
        database
            .verify_migrations_applied_except(allowed_pending)
            .await
            .map_err(|e| {
                GatewayError::Storage(format!(
                    "Database schema check failed while storage.database.auto_migrate=false. \
                 Run migrations before startup or set storage.database.auto_migrate=true: {}",
                    e
                ))
            })?;
        database.health_check().await.map_err(|e| {
            GatewayError::Storage(format!(
                "Database health check failed while storage.database.auto_migrate=false: {}",
                e
            ))
        })?;
        Ok(())
    }

    fn should_run_startup_migrations(
        database: &database::Database,
        config: &DatabaseConfig,
    ) -> bool {
        !config.enabled || config.auto_migrate || database.is_sqlite_fallback()
    }

    /// Run database migrations
    pub async fn migrate(&self) -> Result<()> {
        info!("Running database migrations");
        self.database.migrate().await?;
        info!("Database migrations completed");
        Ok(())
    }

    /// Health check for all storage backends
    pub async fn health_check(&self) -> Result<StorageHealthStatus> {
        let mut status = StorageHealthStatus {
            database: false,
            redis: false,
            files: false,
            vector: false,
            overall: false,
        };

        // Check database health
        match self.database.health_check().await {
            Ok(_) => status.database = true,
            Err(e) => {
                warn!("Database health check failed: {}", e);
            }
        }

        // Check Redis health
        match self.redis.health_check().await {
            Ok(_) => status.redis = true,
            Err(e) => {
                warn!("Redis health check failed: {}", e);
            }
        }

        // Check file storage health
        match self.files.health_check().await {
            Ok(_) => status.files = true,
            Err(e) => {
                warn!("File storage health check failed: {}", e);
            }
        }

        // Check vector database health (if configured)
        if let Some(vector) = &self.vector {
            match vector.health_check().await {
                Ok(_) => status.vector = true,
                Err(e) => {
                    warn!("Vector database health check failed: {}", e);
                }
            }
        } else {
            status.vector = true; // Not configured, so consider it healthy
        }

        // Overall health is true if all configured backends are healthy
        status.overall = status.database && status.redis && status.files && status.vector;

        Ok(status)
    }

    /// Close all connections
    pub async fn close(&self) -> Result<()> {
        info!("Closing storage connections");

        // Database connections will be closed when Arc is dropped
        // self.database.close().await?;

        // Close Redis connections
        self.redis.close().await?;

        // Close file storage
        self.files.close().await?;

        // Close vector database connections
        if let Some(vector) = &self.vector {
            vector.close().await?;
        }

        info!("Storage connections closed");
        Ok(())
    }

    /// Get database pool
    pub fn db(&self) -> &database::Database {
        &self.database
    }

    /// Get Redis pool
    pub fn redis(&self) -> &redis::RedisPool {
        &self.redis
    }

    /// Get file storage
    pub fn files(&self) -> &files::FileStorage {
        &self.files
    }

    /// Get vector store (if available)
    pub fn vector(&self) -> Option<&vector::VectorStoreBackend> {
        self.vector.as_deref()
    }

    // Transaction support removed - SeaORM handles transactions differently
    // Use the database connection directly for transactional operations

    /// Get a Redis connection
    pub async fn redis_conn(&self) -> Result<redis::RedisConnection> {
        self.redis.get_connection().await
    }

    /// Store file and return file ID
    pub async fn store_file(&self, filename: &str, content: &[u8]) -> Result<String> {
        self.files.store(filename, content).await
    }

    /// Retrieve file content
    pub async fn get_file(&self, file_id: &str) -> Result<Vec<u8>> {
        self.files.get(file_id).await
    }

    /// Delete file
    pub async fn delete_file(&self, file_id: &str) -> Result<()> {
        self.files.delete(file_id).await
    }

    /// Store vector embeddings
    pub async fn store_embeddings(
        &self,
        id: &str,
        embeddings: &[f32],
        metadata: Option<serde_json::Value>,
    ) -> Result<()> {
        if let Some(vector) = &self.vector {
            vector.store(id, embeddings, metadata).await
        } else {
            Err(GatewayError::Config(
                "Vector database not configured".to_string(),
            ))
        }
    }

    /// Search similar vectors
    pub async fn search_similar(
        &self,
        query_vector: &[f32],
        limit: usize,
        threshold: Option<f32>,
    ) -> Result<Vec<vector::SearchResult>> {
        if let Some(vector) = &self.vector {
            vector.search(query_vector, limit, threshold).await
        } else {
            Err(GatewayError::Config(
                "Vector database not configured".to_string(),
            ))
        }
    }

    /// Cache operations
    pub async fn cache_get(&self, key: &str) -> Result<Option<String>> {
        self.redis.get(key).await
    }

    /// Set cache value with optional TTL
    pub async fn cache_set(&self, key: &str, value: &str, ttl: Option<u64>) -> Result<()> {
        self.redis.set(key, value, ttl).await
    }

    /// Delete cache key
    pub async fn cache_delete(&self, key: &str) -> Result<()> {
        self.redis.delete(key).await
    }

    /// Check if cache key exists
    pub async fn cache_exists(&self, key: &str) -> Result<bool> {
        self.redis.exists(key).await
    }

    /// Batch cache operations
    pub async fn cache_mget(&self, keys: &[String]) -> Result<Vec<Option<String>>> {
        self.redis.mget(keys).await
    }

    /// Set multiple cache values with optional TTL
    pub async fn cache_mset(&self, pairs: &[(String, String)], ttl: Option<u64>) -> Result<()> {
        self.redis.mset(pairs, ttl).await
    }

    /// List operations
    /// Push value to list
    pub async fn list_push(&self, key: &str, value: &str) -> Result<()> {
        self.redis.list_push(key, value).await
    }

    /// Pop value from list
    pub async fn list_pop(&self, key: &str) -> Result<Option<String>> {
        self.redis.list_pop(key).await
    }

    /// Get list length
    pub async fn list_length(&self, key: &str) -> Result<usize> {
        self.redis.list_length(key).await
    }

    /// Set operations
    /// Add member to set
    pub async fn set_add(&self, key: &str, member: &str) -> Result<()> {
        self.redis.set_add(key, member).await
    }

    /// Remove member from set
    pub async fn set_remove(&self, key: &str, member: &str) -> Result<()> {
        self.redis.set_remove(key, member).await
    }

    /// Get all members of set
    pub async fn set_members(&self, key: &str) -> Result<Vec<String>> {
        self.redis.set_members(key).await
    }

    /// Hash operations
    /// Set hash field value
    pub async fn hash_set(&self, key: &str, field: &str, value: &str) -> Result<()> {
        self.redis.hash_set(key, field, value).await
    }

    /// Get hash field value
    pub async fn hash_get(&self, key: &str, field: &str) -> Result<Option<String>> {
        self.redis.hash_get(key, field).await
    }

    /// Delete hash field
    pub async fn hash_delete(&self, key: &str, field: &str) -> Result<()> {
        self.redis.hash_delete(key, field).await
    }

    /// Get all hash fields and values
    pub async fn hash_get_all(
        &self,
        key: &str,
    ) -> Result<std::collections::HashMap<String, String>> {
        self.redis.hash_get_all(key).await
    }

    /// Pub/Sub operations
    pub async fn publish(&self, channel: &str, message: &str) -> Result<()> {
        self.redis.publish(channel, message).await
    }

    /// Subscribe to Redis channels
    /// Subscribe to Redis channels for pub/sub messaging
    pub async fn subscribe(&self, channels: &[String]) -> Result<redis::Subscription> {
        self.redis.subscribe(channels).await
    }
}

/// Storage health status
#[derive(Debug, Clone, serde::Serialize)]
pub struct StorageHealthStatus {
    /// Database health status
    pub database: bool,
    /// Redis health status
    pub redis: bool,
    /// File storage health status
    pub files: bool,
    /// Vector storage health status
    pub vector: bool,
    /// Overall health status
    pub overall: bool,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::models::file_storage::FileStorageConfig;
    use crate::config::models::storage::{DatabaseConfig, RedisConfig};
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_storage_layer_creation() {
        let config = StorageConfig {
            database: DatabaseConfig {
                url: "postgresql://localhost:5432/test".to_string(),
                max_connections: 5,
                connection_timeout: 5,
                ssl: false,
                enabled: true,
                auto_migrate: false,
                auto_migrate_configured: false,
                fallback_to_sqlite: false,
                allow_degraded: false,
            },
            redis: RedisConfig {
                url: "redis://localhost:6379".to_string(),
                enabled: true,
                max_connections: 10,
                connection_timeout: 5,
                cluster: false,
                allow_degraded: false,
            },
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        // This test would require actual database connections
        // For now, we'll just test that the config is properly structured
        assert_eq!(config.database.url, "postgresql://localhost:5432/test");
        assert_eq!(config.redis.url, "redis://localhost:6379");
    }

    #[tokio::test]
    async fn test_storage_layer_uses_configured_file_storage_path() {
        let temp_dir = TempDir::new().expect("temp dir should be created");
        let configured_path = temp_dir.path().join("configured-files");
        let config = StorageConfig {
            database: DatabaseConfig::default(),
            redis: RedisConfig::default(),
            files: FileStorageConfig {
                storage_type: "local".to_string(),
                local_path: Some(configured_path.to_string_lossy().to_string()),
                s3: None,
            },
            vector_db: None,
        };

        let storage = StorageLayer::new(&config)
            .await
            .expect("storage layer should initialize with configured local file storage");
        let file_id = storage
            .store_file("configured.txt", b"configured storage")
            .await
            .expect("file should be stored");

        let expected_path = configured_path
            .join(&file_id[..2.min(file_id.len())])
            .join(&file_id);
        assert!(
            expected_path.exists(),
            "stored file should use configured path: {}",
            expected_path.display()
        );
    }

    #[tokio::test]
    async fn default_storage_layer_runs_in_memory_sqlite_migrations() {
        let config = StorageConfig {
            database: DatabaseConfig::default(),
            redis: RedisConfig::default(),
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        let storage = StorageLayer::new(&config)
            .await
            .expect("default storage layer should initialize");
        let snapshots = storage
            .database
            .load_budget_limit_snapshots()
            .await
            .expect("default in-memory SQLite should have migrated budget tables");

        assert!(snapshots.is_empty());
    }

    #[tokio::test]
    async fn configured_database_without_auto_migrate_requires_existing_schema() {
        let config = StorageConfig {
            database: DatabaseConfig {
                url: "sqlite::memory:".to_string(),
                max_connections: 1,
                connection_timeout: 1,
                ssl: false,
                enabled: true,
                auto_migrate: false,
                auto_migrate_configured: false,
                fallback_to_sqlite: false,
                allow_degraded: false,
            },
            redis: RedisConfig::default(),
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        let err = match StorageLayer::new(&config).await {
            Ok(_) => panic!("configured database without schema must fail when auto_migrate=false"),
            Err(err) => err,
        };
        assert!(
            err.to_string().contains("auto_migrate=false"),
            "error should explain how to enable or run migrations, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn configured_database_with_auto_migrate_runs_startup_migrations() {
        let config = StorageConfig {
            database: sqlite_db_config(),
            redis: RedisConfig::default(),
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        let storage = match StorageLayer::new(&config).await {
            Ok(storage) => storage,
            Err(err) => panic!("auto_migrate=true should initialize SQLite schema: {}", err),
        };
        let snapshots = match storage.database.load_budget_limit_snapshots().await {
            Ok(snapshots) => snapshots,
            Err(err) => panic!("startup migration should create budget tables: {}", err),
        };

        assert!(snapshots.is_empty());
    }

    #[tokio::test]
    async fn storage_layer_migrate_is_idempotent_after_startup_migration() {
        let config = StorageConfig {
            database: DatabaseConfig::default(),
            redis: RedisConfig::default(),
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        let storage = StorageLayer::new(&config)
            .await
            .expect("default storage layer should initialize");

        storage
            .migrate()
            .await
            .expect("explicit migration after startup migration should be idempotent");
    }

    fn unreachable_redis_config(allow_degraded: bool) -> RedisConfig {
        RedisConfig {
            // Port 1 reserved/unreachable; connection_timeout=1 keeps the
            // test fast.
            url: "redis://127.0.0.1:1".to_string(),
            enabled: true,
            max_connections: 1,
            connection_timeout: 1,
            cluster: false,
            allow_degraded,
        }
    }

    fn sqlite_db_config() -> DatabaseConfig {
        DatabaseConfig {
            url: "sqlite::memory:".to_string(),
            max_connections: 1,
            connection_timeout: 1,
            ssl: false,
            enabled: true,
            auto_migrate: true,
            auto_migrate_configured: false,
            fallback_to_sqlite: false,
            allow_degraded: false,
        }
    }

    #[tokio::test]
    async fn redis_enabled_failing_without_allow_degraded_fails_startup() {
        let config = StorageConfig {
            database: sqlite_db_config(),
            redis: unreachable_redis_config(false),
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        let result = StorageLayer::new(&config).await;
        assert!(
            result.is_err(),
            "enabled redis that cannot connect must fail startup when allow_degraded=false"
        );
    }

    #[tokio::test]
    async fn redis_enabled_failing_with_allow_degraded_continues_with_noop() {
        let config = StorageConfig {
            database: sqlite_db_config(),
            redis: unreachable_redis_config(true),
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        let storage = StorageLayer::new(&config)
            .await
            .expect("allow_degraded=true should allow startup with no-op redis pool");
        assert!(
            storage.redis.is_noop(),
            "degraded redis must fall back to a no-op pool"
        );
        assert_eq!(storage.redis_status, DependencyStatus::Degraded);
    }

    #[tokio::test]
    async fn redis_disabled_uses_noop_without_error() {
        let redis = RedisConfig {
            enabled: false,
            ..RedisConfig::default()
        };
        let config = StorageConfig {
            database: sqlite_db_config(),
            redis,
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        let storage = StorageLayer::new(&config)
            .await
            .expect("disabled redis must always succeed");
        assert!(storage.redis.is_noop());
        assert_eq!(storage.redis_status, DependencyStatus::Disabled);
    }

    fn unreachable_vector_config(
        allow_degraded: bool,
    ) -> crate::config::models::file_storage::VectorDbConfig {
        crate::config::models::file_storage::VectorDbConfig {
            // Unsupported db_type forces VectorStoreBackend::new to return Err
            // synchronously, which keeps the test fast and offline-friendly.
            db_type: "weaviate".to_string(),
            url: "http://127.0.0.1:1".to_string(),
            api_key: "test".to_string(),
            index_name: "test".to_string(),
            allow_degraded,
        }
    }

    #[tokio::test]
    async fn vector_db_failing_without_allow_degraded_fails_startup() {
        let config = StorageConfig {
            database: sqlite_db_config(),
            redis: RedisConfig::default(),
            files: FileStorageConfig::default(),
            vector_db: Some(unreachable_vector_config(false)),
        };

        let result = StorageLayer::new(&config).await;
        assert!(
            result.is_err(),
            "configured vector DB that cannot init must fail startup when allow_degraded=false"
        );
    }

    #[tokio::test]
    async fn vector_db_failing_with_allow_degraded_continues_without_vector() {
        let config = StorageConfig {
            database: sqlite_db_config(),
            redis: RedisConfig::default(),
            files: FileStorageConfig::default(),
            vector_db: Some(unreachable_vector_config(true)),
        };

        let storage = StorageLayer::new(&config)
            .await
            .expect("allow_degraded=true must allow startup without a vector backend");
        assert!(storage.vector.is_none());
        assert_eq!(storage.vector_status, DependencyStatus::Degraded);
    }

    #[tokio::test]
    async fn vector_db_disabled_is_status_disabled() {
        let config = StorageConfig {
            database: sqlite_db_config(),
            redis: RedisConfig::default(),
            files: FileStorageConfig::default(),
            vector_db: None,
        };

        let storage = StorageLayer::new(&config)
            .await
            .expect("storage layer must init without vector DB");
        assert_eq!(storage.vector_status, DependencyStatus::Disabled);
    }
}