mod common;
use common::{mock_embedding, test_chunk, TestStore};
use std::sync::Arc;
use std::thread;
#[test]
#[ignore]
fn test_large_chunk_count() {
let ts = TestStore::new();
let chunk_count = 5_000;
for i in 0..chunk_count {
let chunk = test_chunk(
&format!("func_{}", i),
&format!("fn func_{}() {{ /* content {} */ }}", i, i),
);
let embedding = mock_embedding(i as f32 / chunk_count as f32);
ts.upsert_chunk(&chunk, &embedding, Some(12345))
.expect("Failed to upsert chunk");
}
let stats = ts.stats().expect("Failed to get stats");
assert_eq!(
stats.total_chunks, chunk_count,
"Should have all {} chunks",
chunk_count
);
}
#[test]
#[ignore]
fn test_concurrent_searches() {
let ts = TestStore::new();
let chunk_count = 500;
for i in 0..chunk_count {
let chunk = test_chunk(&format!("func_{}", i), &format!("fn func_{}() {{}}", i));
let embedding = mock_embedding(i as f32 / chunk_count as f32);
ts.upsert_chunk(&chunk, &embedding, Some(12345))
.expect("Failed to upsert");
}
let store = Arc::new(ts);
let thread_count = 4;
let searches_per_thread = 50;
let handles: Vec<_> = (0..thread_count)
.map(|t| {
let store = Arc::clone(&store);
thread::spawn(move || {
for i in 0..searches_per_thread {
let query = mock_embedding((t * searches_per_thread + i) as f32 / 1000.0);
let results = store.search_embedding_only(&query, 5, 0.0);
assert!(results.is_ok(), "Search should succeed");
}
})
})
.collect();
for handle in handles {
handle.join().expect("Thread panicked");
}
}
#[test]
#[ignore]
fn test_many_small_operations() {
let ts = TestStore::new();
for i in 0..500 {
let chunk = test_chunk(&format!("func_{}", i), &format!("fn func_{}() {{}}", i));
let embedding = mock_embedding(i as f32 / 500.0);
ts.upsert_chunk(&chunk, &embedding, Some(12345))
.expect("Failed to upsert");
}
let stats = ts.stats().expect("Failed to get stats");
assert_eq!(stats.total_chunks, 500);
for i in 0..200 {
let query = mock_embedding(i as f32 / 200.0);
let results = ts
.search_embedding_only(&query, 5, 0.0)
.expect("Search failed");
assert!(!results.is_empty(), "Should find results");
}
}
#[test]
#[ignore]
fn test_search_threshold_performance() {
let ts = TestStore::new();
for i in 0..1000 {
let chunk = test_chunk(&format!("func_{}", i), &format!("fn func_{}() {{}}", i));
let seed = (i as f32 * 0.618_034) % 1.0;
let embedding = mock_embedding(seed);
ts.upsert_chunk(&chunk, &embedding, Some(12345))
.expect("Failed to upsert");
}
let query = mock_embedding(0.5);
let results_low = ts
.search_embedding_only(&query, 100, 0.0)
.expect("Search failed");
assert!(
!results_low.is_empty(),
"Should find results with low threshold"
);
let results_high = ts
.search_embedding_only(&query, 100, 0.8)
.expect("Search failed");
assert!(
results_high.len() <= results_low.len(),
"High threshold should return <= low threshold results"
);
}
#[test]
#[ignore]
fn test_fts_stress() {
let ts = TestStore::new();
for i in 0..500 {
let names = ["calculate", "process", "handle", "validate", "transform"];
let name = format!("{}_{}", names[i % names.len()], i);
let content = format!("fn {}() {{ // operation {} }}", name, i);
let chunk = test_chunk(&name, &content);
let embedding = mock_embedding(i as f32 / 500.0);
ts.upsert_chunk(&chunk, &embedding, Some(12345))
.expect("Failed to upsert");
}
let queries = ["calculate", "process", "handle", "validate", "transform"];
for query in &queries {
let results = ts.search_fts(query, 100).expect("FTS search failed");
assert!(!results.is_empty(), "Should find results for '{}'", query);
}
}