|
| 1 | +use valori_node::config::NodeConfig; |
| 2 | +use valori_node::server::build_router; |
| 3 | +use valori_node::engine::Engine; |
| 4 | +use valori_node::api::{BatchInsertRequest, BatchInsertResponse, InsertRecordRequest}; |
| 5 | +use axum::{ |
| 6 | + body::Body, |
| 7 | + http::{Request, StatusCode}, |
| 8 | +}; |
| 9 | +use tower::ServiceExt; // for oneshot |
| 10 | +use std::sync::Arc; |
| 11 | +use tokio::sync::Mutex; |
| 12 | +use tempfile::tempdir; |
| 13 | + |
| 14 | +// Define concrete types matching server.rs |
| 15 | +const M: usize = 100; |
| 16 | +const D: usize = 16; |
| 17 | +const N: usize = 100; |
| 18 | +const E: usize = 200; |
| 19 | + |
| 20 | +#[tokio::test] |
| 21 | +async fn test_batch_ingest_success() { |
| 22 | + let dir = tempdir().unwrap(); |
| 23 | + let db_path = dir.path().join("valori.wal"); |
| 24 | + let event_log_path = dir.path().join("events.log"); |
| 25 | + |
| 26 | + let mut config = NodeConfig::default(); |
| 27 | + config.max_records = M; |
| 28 | + config.dim = D; |
| 29 | + config.max_nodes = N; |
| 30 | + config.max_edges = E; |
| 31 | + config.wal_path = Some(db_path.clone()); |
| 32 | + config.event_log_path = Some(event_log_path.clone()); // Enable Event Log for Batching |
| 33 | + |
| 34 | + let engine = Engine::<M, D, N, E>::new(&config); |
| 35 | + let shared_state = Arc::new(Mutex::new(engine)); |
| 36 | + let app = build_router(shared_state, None); |
| 37 | + |
| 38 | + // Prepare Batch |
| 39 | + let batch = vec![ |
| 40 | + vec![0.1; D], |
| 41 | + vec![0.2; D], |
| 42 | + vec![0.3; D], |
| 43 | + ]; |
| 44 | + |
| 45 | + let req = Request::builder() |
| 46 | + .method("POST") |
| 47 | + .uri("/v1/vectors/batch_insert") |
| 48 | + .header("content-type", "application/json") |
| 49 | + .body(Body::from(serde_json::to_vec(&BatchInsertRequest { batch }).unwrap())) |
| 50 | + .unwrap(); |
| 51 | + |
| 52 | + let response = app.oneshot(req).await.unwrap(); |
| 53 | + assert_eq!(response.status(), StatusCode::OK); |
| 54 | + |
| 55 | + let body_bytes = axum::body::to_bytes(response.into_body(), 1024).await.unwrap(); |
| 56 | + let resp: BatchInsertResponse = serde_json::from_slice(&body_bytes).unwrap(); |
| 57 | + |
| 58 | + assert_eq!(resp.ids.len(), 3); |
| 59 | + assert_eq!(resp.ids, vec![0, 1, 2]); // First batch should get 0, 1, 2 |
| 60 | +} |
| 61 | + |
| 62 | +#[tokio::test] |
| 63 | +async fn test_batch_ingest_atomicity_failure() { |
| 64 | + let dir = tempdir().unwrap(); |
| 65 | + let db_path = dir.path().join("valori.wal"); |
| 66 | + let event_log_path = dir.path().join("events.log"); |
| 67 | + |
| 68 | + let mut config = NodeConfig::default(); |
| 69 | + config.max_records = M; |
| 70 | + config.dim = D; |
| 71 | + config.max_nodes = N; |
| 72 | + config.max_edges = E; |
| 73 | + config.wal_path = Some(db_path.clone()); |
| 74 | + config.event_log_path = Some(event_log_path.clone()); |
| 75 | + |
| 76 | + let engine = Engine::<M, D, N, E>::new(&config); |
| 77 | + let shared_state = Arc::new(Mutex::new(engine)); |
| 78 | + let app = build_router(shared_state.clone(), None); |
| 79 | + |
| 80 | + // Invalid payload (one vector has wrong dim) |
| 81 | + let batch = vec![ |
| 82 | + vec![0.1; D], |
| 83 | + vec![0.2; D + 1], // INVALID DIM |
| 84 | + vec![0.3; D], |
| 85 | + ]; |
| 86 | + |
| 87 | + let req = Request::builder() |
| 88 | + .method("POST") |
| 89 | + .uri("/v1/vectors/batch_insert") |
| 90 | + .header("content-type", "application/json") |
| 91 | + .body(Body::from(serde_json::to_vec(&BatchInsertRequest { batch }).unwrap())) |
| 92 | + .unwrap(); |
| 93 | + |
| 94 | + let response = app.oneshot(req).await.unwrap(); |
| 95 | + // Should fail validation before commit |
| 96 | + // Since insert_batch validates strictly before commit, this should return 500 or 400 depending on error mapping |
| 97 | + // EngineError::InvalidInput maps to INTERNAL_SERVER_ERROR currently? or BAD_REQUEST? |
| 98 | + // Let's check api.rs/errors.rs mapping. Usually InvalidInput -> 400? |
| 99 | + // Actually, Axum doesn't auto-map EngineError. |
| 100 | + // Wait, EngineError needs IntoResponse. |
| 101 | + // Assuming standard error handling returns error code. |
| 102 | + assert!(response.status().is_client_error() || response.status().is_server_error()); |
| 103 | + |
| 104 | + // Verify NOTHING was inserted |
| 105 | + let engine = shared_state.lock().await; |
| 106 | + // Check ID 0 is empty |
| 107 | + assert!(engine.search_l2(&vec![0.1; D], 1).unwrap().is_empty()); |
| 108 | +} |
0 commit comments