reflex-server 0.2.2

OpenAI-compatible HTTP gateway for Reflex cache
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
//! Test server harness.

use reflex::cache::TieredCache;
use reflex::cache::{BqSearchBackend, L1CacheHandle, L2Config, L2SemanticCache, NvmeStorageLoader};
use reflex::embedding::RerankerConfig;
use reflex::embedding::sinter::{SinterConfig, SinterEmbedder};
use reflex::scoring::CrossEncoderScorer;
use reflex::vectordb::bq::{BqClient, MockBqClient};
use reflex_server::gateway::{HandlerState, create_router_with_state};
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tempfile::TempDir;
use tokio::net::TcpListener;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;

const STARTUP_WAIT_TIMEOUT_SECS: u64 = 5;
const STARTUP_POLL_INTERVAL_MS: u64 = 50;
const TEST_COLLECTION_NAME: &str = "reflex_test_bq";

#[derive(Debug, Clone)]
pub struct TestServerConfig {
    pub port: u16,
    pub collection_name: Option<String>,
    pub storage_path: Option<std::path::PathBuf>,
    pub reranker_threshold: f32,
}

impl Default for TestServerConfig {
    fn default() -> Self {
        Self {
            port: 0,
            collection_name: None,
            storage_path: None,
            reranker_threshold: 0.70,
        }
    }
}

impl TestServerConfig {}

pub struct TestServer {
    pub addr: SocketAddr,
    _server_handle: JoinHandle<()>,
    shutdown_tx: Option<oneshot::Sender<()>>,
    _temp_dir: Option<TempDir>,
}

impl TestServer {
    pub fn url(&self) -> String {
        format!("http://{}", self.addr)
    }

    pub async fn shutdown(mut self) {
        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(());
        }
    }
}

impl Drop for TestServer {
    fn drop(&mut self) {
        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(());
        }
    }
}

pub async fn find_available_port() -> std::io::Result<u16> {
    let listener = TcpListener::bind("127.0.0.1:0").await?;
    let addr = listener.local_addr()?;
    Ok(addr.port())
}

pub async fn wait_for_server_ready(
    addr: SocketAddr,
    timeout: Duration,
    interval: Duration,
) -> Result<(), ServerStartupError> {
    let start = std::time::Instant::now();

    loop {
        if start.elapsed() > timeout {
            return Err(ServerStartupError::Timeout);
        }

        match tokio::net::TcpStream::connect(addr).await {
            Ok(_) => return Ok(()),
            Err(_) => {
                tokio::time::sleep(interval).await;
            }
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum ServerStartupError {
    #[error("Server failed to start within timeout")]
    Timeout,
    #[error("Failed to bind to address: {0}")]
    BindError(#[from] std::io::Error),
    #[error("Server startup failed: {0}")]
    StartupFailed(String),
}

/// Spawns a fully-mocked test server for unit and integration tests.
///
/// # What "test" means
///
/// This function creates a server with **all external dependencies mocked**:
/// - **Vector database**: Uses `MockBqClient` (in-memory, no real Qdrant required)
/// - **LLM provider**: Uses mock provider (`mock_provider=true`)
/// - **Embedder**: Uses stub embedder (deterministic, fast)
/// - **Reranker**: Uses stub reranker (deterministic scores)
///
/// # Use cases
///
/// - Unit tests that need a running server but not real infrastructure
/// - Fast CI tests without external service dependencies
/// - Testing HTTP routing, request/response handling, and business logic
///
/// # Comparison with `spawn_real_server`
///
/// | Component       | `spawn_test_server` | `spawn_real_server`          |
/// |-----------------|---------------------|------------------------------|
/// | Qdrant          | Mock (in-memory)    | Real (requires running instance) |
/// | LLM Provider    | Mock                | Mock                         |
/// | Embedder        | Stub                | Real (if `REFLEX_MODEL_PATH` set) |
/// | Reranker        | Stub                | Real (if `REFLEX_RERANKER_PATH` set) |
///
/// # Example
///
/// ```ignore
/// let server = spawn_test_server(TestServerConfig::default()).await?;
/// let client = reqwest::Client::new();
/// let resp = client.get(format!("{}/health", server.url())).send().await?;
/// assert!(resp.status().is_success());
/// ```
pub async fn spawn_test_server(config: TestServerConfig) -> Result<TestServer, ServerStartupError> {
    let port = if config.port == 0 {
        find_available_port().await?
    } else {
        config.port
    };

    let addr = SocketAddr::from(([127, 0, 0, 1], port));
    let listener = TcpListener::bind(addr).await?;
    let local_addr = listener.local_addr()?;

    let (storage_path, _temp_dir) = if let Some(path) = config.storage_path {
        (path, None)
    } else {
        let temp_dir =
            TempDir::new().map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?;
        (temp_dir.path().to_path_buf(), Some(temp_dir))
    };

    let collection_name = config
        .collection_name
        .unwrap_or_else(|| TEST_COLLECTION_NAME.to_string());

    let bq_client = MockBqClient::new();
    bq_client
        .ensure_bq_collection(&collection_name, reflex::constants::DEFAULT_VECTOR_SIZE_U64)
        .await
        .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?;

    let loader = NvmeStorageLoader::new(storage_path.clone());

    let embedder = SinterEmbedder::load(SinterConfig::stub())
        .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?;

    let l2_config = L2Config::default().collection_name(&collection_name);
    let l2_cache = L2SemanticCache::new(embedder, bq_client.clone(), loader, l2_config)
        .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?;

    let l1_cache = L1CacheHandle::new();
    let tiered_cache = TieredCache::new(l1_cache, l2_cache);
    let tiered_cache = Arc::new(tiered_cache);

    let scorer =
        CrossEncoderScorer::new(RerankerConfig::stub().with_threshold(config.reranker_threshold))
            .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?;
    let scorer = Arc::new(scorer);

    let state = HandlerState::new_with_mock_provider(
        tiered_cache,
        scorer,
        storage_path,
        bq_client,
        collection_name,
        true,
    );

    let app = create_router_with_state(state);

    let (shutdown_tx, shutdown_rx) = oneshot::channel();

    let server_handle = tokio::spawn(async move {
        axum::serve(listener, app)
            .with_graceful_shutdown(async {
                let _ = shutdown_rx.await;
            })
            .await
            .unwrap();
    });

    wait_for_server_ready(
        local_addr,
        Duration::from_secs(STARTUP_WAIT_TIMEOUT_SECS),
        Duration::from_millis(STARTUP_POLL_INTERVAL_MS),
    )
    .await?;

    Ok(TestServer {
        addr: local_addr,
        _server_handle: server_handle,
        shutdown_tx: Some(shutdown_tx),
        _temp_dir,
    })
}

/// Spawns a test server with a **real Qdrant** vector database connection.
///
/// # What "real" means (and what it does NOT mean)
///
/// The "real" in this function name refers specifically to the **vector database**:
/// - **Vector database**: Uses real `BqClient` connected to an actual Qdrant instance
/// - **LLM provider**: Still uses mock provider (`mock_provider=true`) -- NOT a real LLM
///
/// This naming convention exists because Qdrant integration is the primary differentiator
/// for end-to-end vector search testing, while LLM calls are typically mocked to avoid
/// costs and rate limits during testing.
///
/// # Component configuration
///
/// | Component       | Default                          | With env var                    |
/// |-----------------|----------------------------------|----------------------------------|
/// | Qdrant          | Real (`localhost:6334`)          | `REFLEX_QDRANT_URL`             |
/// | LLM Provider    | Mock (always)                    | N/A                             |
/// | Embedder        | Stub                             | Real if `REFLEX_MODEL_PATH`     |
/// | Reranker        | Stub                             | Real if `REFLEX_RERANKER_PATH`  |
///
/// # Prerequisites
///
/// Requires a running Qdrant instance. Start one with:
/// ```bash
/// docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant
/// ```
///
/// # Use cases
///
/// - Integration tests validating vector storage and retrieval
/// - Testing semantic search with real embeddings
/// - Verifying Qdrant collection management
///
/// # Comparison with `spawn_test_server`
///
/// Use `spawn_test_server` when you do NOT need real Qdrant (faster, no dependencies).
/// Use `spawn_real_server` when you need to validate actual vector database operations.
///
/// # Example
///
/// ```ignore
/// // Ensure Qdrant is running at localhost:6334 (or set REFLEX_QDRANT_URL)
/// let server = spawn_real_server(TestServerConfig::default()).await?;
/// // The server now uses real Qdrant for vector operations
/// ```
pub async fn spawn_real_server(config: TestServerConfig) -> Result<TestServer, ServerStartupError> {
    let port = if config.port == 0 {
        find_available_port().await?
    } else {
        config.port
    };

    let addr = SocketAddr::from(([127, 0, 0, 1], port));
    let listener = TcpListener::bind(addr).await?;
    let local_addr = listener.local_addr()?;

    let (storage_path, _temp_dir) = if let Some(path) = config.storage_path {
        (path, None)
    } else {
        let temp_dir =
            TempDir::new().map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?;
        (temp_dir.path().to_path_buf(), Some(temp_dir))
    };

    let collection_name = config
        .collection_name
        .unwrap_or_else(|| format!("{}_{}", TEST_COLLECTION_NAME, uuid::Uuid::new_v4().simple()));

    // Connect to real Qdrant instance (default: localhost:6334)
    let qdrant_url =
        std::env::var("REFLEX_QDRANT_URL").unwrap_or_else(|_| "http://localhost:6334".to_string());
    let bq_client = BqClient::new(&qdrant_url).await.map_err(|e| {
        ServerStartupError::StartupFailed(format!("Failed to connect to Qdrant: {}", e))
    })?;

    bq_client
        .ensure_collection(&collection_name, reflex::constants::DEFAULT_VECTOR_SIZE_U64)
        .await
        .map_err(|e| {
            ServerStartupError::StartupFailed(format!("Failed to ensure collection: {}", e))
        })?;

    let loader = NvmeStorageLoader::new(storage_path.clone());

    let embedder = if let Ok(path) = std::env::var("REFLEX_MODEL_PATH") {
        println!("Using Real Embedder: {}", path);
        SinterEmbedder::load(SinterConfig::new(path))
            .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?
    } else {
        println!("Using Stub Embedder");
        SinterEmbedder::load(SinterConfig::stub())
            .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?
    };

    let l2_config = L2Config::default().collection_name(&collection_name);
    let l2_cache = L2SemanticCache::new(embedder, bq_client.clone(), loader, l2_config)
        .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?;

    let l1_cache = L1CacheHandle::new();
    let tiered_cache = TieredCache::new(l1_cache, l2_cache);
    let tiered_cache = Arc::new(tiered_cache);

    let scorer = if let Ok(path) = std::env::var("REFLEX_RERANKER_PATH") {
        println!("Using Real Reranker: {}", path);
        let config = RerankerConfig::new(path).with_threshold(config.reranker_threshold);
        CrossEncoderScorer::new(config)
            .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?
    } else {
        println!("Using Stub Reranker");
        CrossEncoderScorer::new(RerankerConfig::stub().with_threshold(config.reranker_threshold))
            .map_err(|e| ServerStartupError::StartupFailed(e.to_string()))?
    };
    let scorer = Arc::new(scorer);

    // NOTE: Even though this is spawn_real_server, we still use mock_provider=true
    // for the LLM. "Real" refers to real Qdrant, not real LLM provider.
    // This avoids API costs and rate limits during integration tests.
    let state = HandlerState::new_with_mock_provider(
        tiered_cache,
        scorer,
        storage_path,
        bq_client,
        collection_name,
        true, // mock_provider: true = mock LLM, false = real LLM
    );

    let app = create_router_with_state(state);

    let (shutdown_tx, shutdown_rx) = oneshot::channel();

    let server_handle = tokio::spawn(async move {
        axum::serve(listener, app)
            .with_graceful_shutdown(async {
                let _ = shutdown_rx.await;
            })
            .await
            .unwrap();
    });

    wait_for_server_ready(
        local_addr,
        Duration::from_secs(STARTUP_WAIT_TIMEOUT_SECS),
        Duration::from_millis(STARTUP_POLL_INTERVAL_MS),
    )
    .await?;

    Ok(TestServer {
        addr: local_addr,
        _server_handle: server_handle,
        shutdown_tx: Some(shutdown_tx),
        _temp_dir,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_find_available_port() {
        let port = find_available_port()
            .await
            .expect("Should find available port");
        assert!(port > 0);
    }

    #[tokio::test]
    async fn test_server_config_defaults() {
        let config = TestServerConfig::default();
        assert_eq!(config.port, 0);
    }

    #[tokio::test]
    async fn test_server_helpers_are_callable() {
        let (shutdown_tx, _shutdown_rx) = oneshot::channel();
        let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();

        let server = TestServer {
            addr,
            _server_handle: tokio::spawn(async {}),
            shutdown_tx: Some(shutdown_tx),
            _temp_dir: None,
        };

        let _ = server.url();
        server.shutdown().await;
    }

    #[test]
    fn test_spawners_are_referenced() {
        std::mem::drop(spawn_test_server(TestServerConfig::default()));
        std::mem::drop(spawn_real_server(TestServerConfig::default()));
    }

    #[test]
    fn test_server_url_formatting() {
        let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
        let url = format!("http://{}", addr);
        assert_eq!(url, "http://127.0.0.1:8080");
    }
}