vecboost 0.2.0

High-performance embedding vector service written in Rust
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
// Copyright (c) 2025-2026 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

use crate::VecboostState;
#[cfg(feature = "cli")]
use crate::api::embedding::{cli_compute_similarity, cli_embed};
use crate::api::embedding::{compute_similarity, embed, embed_batch};
#[cfg(feature = "http")]
use crate::api::embedding::{forge_compute_similarity, forge_embed, forge_embed_batch};
#[cfg(any(feature = "http", feature = "cli"))]
use crate::api::embedding::{to_api_error, uuid_like_id};
use crate::config::model::{DeviceType, EngineType, ModelConfig, Precision};
use crate::domain::{BatchEmbedRequest, EmbedRequest, SimilarityRequest};
use crate::engine::InferenceEngine;
use crate::error::VecboostError;
use crate::module_registry::EmbeddingModule;
use crate::service::embedding::EmbeddingService;
use async_trait::async_trait;
#[cfg(any(feature = "http", feature = "cli"))]
use sdforge::prelude::ApiError;
use std::path::PathBuf;
use std::sync::Arc;
use tempfile::tempdir;
use tokio::sync::RwLock;

/// Ensure the global STATE is initialized for forge handler tests.
///
/// Builds a minimal `AsyncKit` with only `EmbeddingModule` registered and
/// injects via `init_state`. Idempotent: subsequent calls are no-ops (OnceLock
/// first-writer-wins semantics). Safe under parallel test execution.
async fn ensure_state_initialized() {
    if crate::api::state().is_ok() {
        return;
    }
    let svc = Arc::new(RwLock::new(make_service(384)));
    let mut kit = trait_kit::AsyncKit::new();
    kit.set_config(svc);
    kit.register::<EmbeddingModule>()
        .expect("register EmbeddingModule in test kit");
    let kit = kit.build().await.expect("build test kit");
    crate::api::init_state(VecboostState { kit: Arc::new(kit) });
}

/// Deterministic mock engine for API layer tests.
///
/// Generates a normalized embedding by hashing the text bytes, so the same
/// input always yields the same vector. This mirrors the TestEngine used in
/// `service/embedding.rs` tests.
struct TestEngine {
    dimension: usize,
}

impl TestEngine {
    fn new(dimension: usize) -> Self {
        Self { dimension }
    }

    fn generate_embedding(&self, text: &str) -> Vec<f32> {
        let mut embedding = vec![0.0f32; self.dimension];
        let bytes = text.as_bytes();

        let mut hash: u64 = 1469598103934665603;
        for &byte in bytes {
            hash ^= byte as u64;
            hash = hash.wrapping_mul(1099511628211);
        }

        let mut state = hash;
        for val in embedding.iter_mut() {
            state = state.wrapping_mul(1664525).wrapping_add(1013904223);
            let float_val = (state as f32 / u32::MAX as f32) * 2.0 - 1.0;
            *val = float_val;
        }

        let norm: f32 = embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 0.0 {
            for val in embedding.iter_mut() {
                *val /= norm;
            }
        }

        embedding
    }
}

#[async_trait]
impl InferenceEngine for TestEngine {
    fn embed(&self, text: &str) -> Result<Vec<f32>, VecboostError> {
        Ok(self.generate_embedding(text))
    }

    fn embed_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, VecboostError> {
        Ok(texts.iter().map(|t| self.generate_embedding(t)).collect())
    }

    fn precision(&self) -> &Precision {
        const PRECISION: Precision = Precision::Fp32;
        &PRECISION
    }

    fn supports_mixed_precision(&self) -> bool {
        false
    }

    async fn try_fallback_to_cpu(&mut self, _config: &ModelConfig) -> Result<(), VecboostError> {
        Ok(())
    }
}

fn make_service(dimension: usize) -> EmbeddingService {
    let temp_dir = tempdir().unwrap();
    let mock_engine = TestEngine::new(dimension);
    let model_config = ModelConfig {
        name: "test-model".to_string(),
        engine_type: EngineType::Candle,
        model_path: PathBuf::from(temp_dir.path()),
        tokenizer_path: None,
        device: DeviceType::Cpu,
        max_batch_size: 32,
        pooling_mode: None,
        expected_dimension: Some(dimension),
        memory_limit_bytes: None,
        oom_fallback_enabled: true,
        model_sha256: None,
    };
    let engine: Arc<RwLock<dyn InferenceEngine + Send + Sync>> = Arc::new(RwLock::new(mock_engine));
    let _ = temp_dir; // keep temp dir alive for the test
    EmbeddingService::new(engine, Some(model_config))
}

#[tokio::test]
async fn test_embed_returns_vector() {
    let service = make_service(384);
    let req = EmbedRequest {
        text: "Hello world".to_string(),
        normalize: Some(true),
    };
    let result = embed(&service, req).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.dimension, 384);
    assert_eq!(response.embedding.len(), 384);
    // Verify the vector is L2-normalized
    let norm: f32 = response.embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
    assert!((norm - 1.0).abs() < 1e-5);
}

#[tokio::test]
async fn test_embed_empty_text_returns_error() {
    let service = make_service(384);
    let req = EmbedRequest {
        text: "".to_string(),
        normalize: None,
    };
    let result = embed(&service, req).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        VecboostError::InvalidInput(_) => {}
        other => panic!("Expected InvalidInput, got {:?}", other),
    }
}

#[tokio::test]
async fn test_embed_batch_returns_vectors() {
    let service = make_service(384);
    let texts = vec![
        "Hello world".to_string(),
        "Rust is great".to_string(),
        "Embedding vectors".to_string(),
    ];
    let req = BatchEmbedRequest {
        texts: texts.clone(),
        mode: None,
        normalize: Some(true),
    };
    let result = embed_batch(&service, req).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.embeddings.len(), 3);
    assert_eq!(response.dimension, 384);
    for (i, emb) in response.embeddings.iter().enumerate() {
        assert_eq!(emb.embedding.len(), 384);
        assert_eq!(emb.text_preview, texts[i]);
    }
}

#[tokio::test]
async fn test_embed_batch_empty_returns_error() {
    let service = make_service(384);
    let req = BatchEmbedRequest {
        texts: vec![],
        mode: None,
        normalize: None,
    };
    let result = embed_batch(&service, req).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_compute_similarity_returns_score() {
    let service = make_service(384);
    let req = SimilarityRequest {
        source: "Hello world".to_string(),
        target: "Hello rust".to_string(),
    };
    let result = compute_similarity(&service, req).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    // Cosine similarity of normalized vectors is in [-1, 1]
    assert!(response.score >= -1.0 && response.score <= 1.0);
}

#[tokio::test]
async fn test_compute_similarity_identical_texts_returns_one() {
    let service = make_service(384);
    let req = SimilarityRequest {
        source: "identical text".to_string(),
        target: "identical text".to_string(),
    };
    let result = compute_similarity(&service, req).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    // Identical normalized vectors have cosine similarity 1.0
    assert!((response.score - 1.0).abs() < 1e-5);
}

#[tokio::test]
async fn test_compute_similarity_empty_source_returns_error() {
    let service = make_service(384);
    let req = SimilarityRequest {
        source: "".to_string(),
        target: "valid target".to_string(),
    };
    let result = compute_similarity(&service, req).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        VecboostError::InvalidInput(_) => {}
        other => panic!("Expected InvalidInput, got {:?}", other),
    }
}

#[tokio::test]
async fn test_compute_similarity_empty_target_returns_error() {
    let service = make_service(384);
    let req = SimilarityRequest {
        source: "valid source".to_string(),
        target: "".to_string(),
    };
    let result = compute_similarity(&service, req).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        VecboostError::InvalidInput(_) => {}
        other => panic!("Expected InvalidInput, got {:?}", other),
    }
}

// ---------------------------------------------------------------------------
// to_api_error / uuid_like_id tests
// ---------------------------------------------------------------------------

#[cfg(any(feature = "http", feature = "cli"))]
#[test]
fn test_uuid_like_id_format() {
    let id = uuid_like_id();
    assert!(id.starts_with("err-"));
    let suffix = &id[4..];
    assert!(
        suffix.parse::<u128>().is_ok(),
        "suffix should be numeric, got: {}",
        suffix
    );
}

#[cfg(any(feature = "http", feature = "cli"))]
#[test]
fn test_uuid_like_id_unique() {
    let id1 = uuid_like_id();
    let id2 = uuid_like_id();
    assert_ne!(id1, id2, "consecutive ids should differ");
}

#[cfg(any(feature = "http", feature = "cli"))]
#[test]
fn test_to_api_error_invalid_input() {
    let err = VecboostError::InvalidInput("bad input".to_string());
    let api_err = to_api_error(err);
    match api_err {
        ApiError::InvalidInput {
            message,
            field,
            value,
        } => {
            assert_eq!(message, "bad input");
            assert!(field.is_none());
            assert!(value.is_none());
        }
        other => panic!("Expected InvalidInput, got {:?}", other),
    }
}

#[cfg(any(feature = "http", feature = "cli"))]
#[test]
fn test_to_api_error_model_load_error() {
    let err = VecboostError::ModelLoadError("model not found".to_string());
    let api_err = to_api_error(err);
    match api_err {
        ApiError::Internal {
            message, error_id, ..
        } => {
            assert!(message.contains("Model load error"));
            assert!(message.contains("model not found"));
            assert!(error_id.starts_with("err-"));
        }
        other => panic!("Expected Internal, got {:?}", other),
    }
}

#[cfg(any(feature = "http", feature = "cli"))]
#[test]
fn test_to_api_error_other_variants_become_internal() {
    let variants = vec![
        VecboostError::ConfigError("cfg err".to_string()),
        VecboostError::InferenceError("inf err".to_string()),
        VecboostError::AuthenticationError("auth err".to_string()),
        VecboostError::DatabaseError("db err".to_string()),
        VecboostError::InternalError("internal err".to_string()),
        VecboostError::RateLimitExceeded("rl err".to_string()),
        VecboostError::ValidationError("val err".to_string()),
        VecboostError::IoError("io err".to_string()),
        VecboostError::NotFound("nf err".to_string()),
        VecboostError::SecurityError("sec err".to_string()),
        VecboostError::ModelNotLoaded("not loaded".to_string()),
        VecboostError::ModelFileCorrupted("corrupted".to_string()),
        VecboostError::ModelIntegrityError("integrity".to_string()),
        VecboostError::TokenizationError("tok err".to_string()),
        VecboostError::OutOfMemory("oom".to_string()),
    ];
    for err in variants {
        let api_err = to_api_error(err.clone());
        match api_err {
            ApiError::Internal {
                message, error_id, ..
            } => {
                assert!(!message.is_empty(), "message should not be empty");
                assert!(error_id.starts_with("err-"));
            }
            other => panic!("Expected Internal for {:?}, got {:?}", err, other),
        }
    }
}

// ---------------------------------------------------------------------------
// forge_embed / forge_embed_batch / forge_compute_similarity tests
// ---------------------------------------------------------------------------

#[cfg(feature = "http")]
#[tokio::test]
async fn test_forge_embed_success() {
    ensure_state_initialized().await;
    let req = EmbedRequest {
        text: "hello forge".to_string(),
        normalize: Some(true),
    };
    let result = forge_embed(req).await;
    assert!(
        result.is_ok(),
        "forge_embed should succeed: {:?}",
        result.err()
    );
    let response = result.unwrap();
    assert_eq!(response.dimension, 384);
    assert_eq!(response.embedding.len(), 384);
}

#[cfg(feature = "http")]
#[tokio::test]
async fn test_forge_embed_empty_text_returns_error() {
    ensure_state_initialized().await;
    let req = EmbedRequest {
        text: "".to_string(),
        normalize: None,
    };
    let result = forge_embed(req).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        ApiError::InvalidInput { message, .. } => {
            assert!(
                message.contains("empty") || message.contains("invalid"),
                "expected empty/invalid message, got: {}",
                message
            );
        }
        other => panic!("Expected InvalidInput, got {:?}", other),
    }
}

#[cfg(feature = "http")]
#[tokio::test]
async fn test_forge_embed_batch_success() {
    ensure_state_initialized().await;
    let req = BatchEmbedRequest {
        texts: vec!["text1".to_string(), "text2".to_string()],
        mode: None,
        normalize: Some(true),
    };
    let result = forge_embed_batch(req).await;
    assert!(result.is_ok(), "forge_embed_batch should succeed");
    let response = result.unwrap();
    assert_eq!(response.embeddings.len(), 2);
    assert_eq!(response.dimension, 384);
}

#[cfg(feature = "http")]
#[tokio::test]
async fn test_forge_embed_batch_empty_returns_error() {
    ensure_state_initialized().await;
    let req = BatchEmbedRequest {
        texts: vec![],
        mode: None,
        normalize: None,
    };
    let result = forge_embed_batch(req).await;
    assert!(result.is_err());
}

#[cfg(feature = "http")]
#[tokio::test]
async fn test_forge_compute_similarity_success() {
    ensure_state_initialized().await;
    let req = SimilarityRequest {
        source: "hello".to_string(),
        target: "world".to_string(),
    };
    let result = forge_compute_similarity(req).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    assert!(response.score >= -1.0 && response.score <= 1.0);
}

#[cfg(feature = "http")]
#[tokio::test]
async fn test_forge_compute_similarity_empty_source_error() {
    ensure_state_initialized().await;
    let req = SimilarityRequest {
        source: "".to_string(),
        target: "target".to_string(),
    };
    let result = forge_compute_similarity(req).await;
    assert!(result.is_err());
}

// ---------------------------------------------------------------------------
// cli_embed / cli_compute_similarity tests
// ---------------------------------------------------------------------------

#[cfg(feature = "cli")]
#[tokio::test]
async fn test_cli_embed_success() {
    ensure_state_initialized().await;
    let req = EmbedRequest {
        text: "hello cli".to_string(),
        normalize: None,
    };
    let result = cli_embed(req).await;
    assert!(
        result.is_ok(),
        "cli_embed should succeed: {:?}",
        result.err()
    );
    let response = result.unwrap();
    assert!(!response.embedding.is_empty());
    assert!(response.dimension > 0);
}

#[cfg(feature = "cli")]
#[tokio::test]
async fn test_cli_embed_empty_text_returns_error() {
    ensure_state_initialized().await;
    let req = EmbedRequest {
        text: "".to_string(),
        normalize: None,
    };
    let result = cli_embed(req).await;
    assert!(result.is_err());
}

#[cfg(feature = "cli")]
#[tokio::test]
async fn test_cli_compute_similarity_success() {
    ensure_state_initialized().await;
    let req = SimilarityRequest {
        source: "source text".to_string(),
        target: "target text".to_string(),
    };
    let result = cli_compute_similarity(req).await;
    assert!(
        result.is_ok(),
        "cli_compute_similarity should succeed: {:?}",
        result.err()
    );
    let response = result.unwrap();
    assert!(response.score >= -1.0 && response.score <= 1.0);
}

#[cfg(feature = "cli")]
#[tokio::test]
async fn test_cli_compute_similarity_empty_source_returns_error() {
    ensure_state_initialized().await;
    let req = SimilarityRequest {
        source: "".to_string(),
        target: "target".to_string(),
    };
    let result = cli_compute_similarity(req).await;
    assert!(result.is_err());
}

// ---------------------------------------------------------------------------
// T023: forge handler require calls bounded under 100 requests (R-api-routing-004)
// ---------------------------------------------------------------------------

/// Verify that forge handlers make a bounded number of `kit.require::<Module>()`
/// calls per request. R-api-routing-004 acceptance criterion 2 requires total
/// require calls ≤ 4 × request_count (≤ 400 for 100 requests).
///
/// `forge_embed` (src/api/embedding.rs L128-141) calls `require::<EmbeddingModule>()`
/// exactly once per invocation. 100 requests × 1 require = 100 requires ≤ 400 ✓.
///
/// `trait_kit::AsyncKit` does not expose a require-counter API, so we use an
/// indirect verification: run 100 successful `forge_embed` calls and assert all
/// succeed. A successful `require` is a precondition for a successful response,
/// so 100 successes imply 100 successful requires, which is within the 400 budget.
///
/// Per-handler require budget (code review, src/api/auth.rs L40-53):
/// - forge_login:  UserStoreModule + AuthModule + AuditModule = 3 requires
/// - forge_logout: AuthModule + AuditModule = 2 requires
/// - forge_embed:  EmbeddingModule = 1 require
/// All handlers stay within the 4-require-per-request budget.
#[cfg(feature = "http")]
#[tokio::test]
async fn test_forge_handler_require_calls_bounded_under_100_requests() {
    ensure_state_initialized().await;
    // 100 requests × 1 require/request (EmbeddingModule) = 100 requires ≤ 400
    for i in 0..100 {
        let req = EmbedRequest {
            text: format!("benchmark require bound {}", i),
            normalize: Some(true),
        };
        let result = forge_embed(req).await;
        assert!(result.is_ok(), "iteration {} failed: {:?}", i, result.err());
    }
}

// ---------------------------------------------------------------------------
// T026: AuditLogger called by forge handler pattern (R-audit-004)
// ---------------------------------------------------------------------------

/// Verify that `AuditLogger` correctly records login_success and logout events
/// when invoked using the same call pattern as `forge_login` and `forge_logout`.
///
/// R-audit-004 acceptance criteria 4-5:
/// - After forge_login: `log_login_success` is called and the ip argument is non-empty.
/// - After forge_logout: `log_logout` is called and the username matches the login user.
///
/// `AuditLogger` is a concrete struct (not a trait), so we use a real logger with
/// a tempdir file backend (per task spec 方案 B). The test mirrors the exact audit
/// call sites in `forge_login` (src/api/auth.rs L72) and `forge_logout`
/// (src/api/auth.rs L159), exercising the full Event → channel → file write path.
#[cfg(feature = "http")]
#[tokio::test]
async fn test_audit_logger_called_by_forge_handler_pattern() {
    use crate::audit::{AuditConfig, AuditLogger};

    let temp_dir = tempdir().unwrap();
    let log_path = temp_dir.path().join("forge_audit.log");
    let audit_config = AuditConfig {
        enabled: true,
        log_file_path: log_path.clone(),
        log_level: "info".to_string(),
        max_file_size_mb: 100,
        max_files: 10,
        async_write: true,
    };
    let logger = AuditLogger::new(audit_config);

    // Mirror forge_login audit call (src/api/auth.rs L72):
    //   logger.log_login_success(&req.username, Some(peer_ip.clone()));
    let peer_ip = "192.168.1.100".to_string();
    let username = "testuser";
    logger.log_login_success(username, Some(peer_ip.clone()));

    // Mirror forge_logout audit call (src/api/auth.rs L159):
    //   logger.log_logout(&auth_ctx.user.username, Some(peer_ip));
    logger.log_logout(username, Some(peer_ip.clone()));

    logger.flush().await.unwrap();

    let content = tokio::fs::read_to_string(&log_path)
        .await
        .expect("audit log file should exist after flush");

    // R-audit-004 验收点 4: log_login_success 被调用且 ip 非空
    assert!(
        content.contains("login_success"),
        "login_success event should be logged"
    );
    assert!(
        content.contains("192.168.1.100"),
        "ip should be non-empty in login event"
    );

    // R-audit-004 验收点 5: log_logout 被调用且 username 匹配登录用户
    assert!(content.contains("logout"), "logout event should be logged");
    assert!(
        content.contains(username),
        "username should match the login user in logout event"
    );
}