use agentroot_core::{Database, MetadataBuilder, SearchOptions};
use std::fs;
use tempfile::TempDir;
#[tokio::test]
async fn test_smart_search_basic_functionality() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.sqlite");
let content_dir = temp_dir.path().join("content");
fs::create_dir(&content_dir).unwrap();
let db = Database::open(&db_path).unwrap();
db.initialize().unwrap();
let rust_file = content_dir.join("rust_guide.md");
fs::write(&rust_file, "Complete guide to Rust programming language").unwrap();
let python_file = content_dir.join("python_guide.md");
fs::write(&python_file, "Complete guide to Python programming").unwrap();
db.add_collection(
"test",
content_dir.to_str().unwrap(),
"**/*.md",
"file",
None,
)
.unwrap();
db.reindex_collection("test").await.unwrap();
let options = SearchOptions::default();
let results = agentroot_core::smart_search(&db, "rust programming", &options).await;
match results {
Ok(results) => {
assert!(!results.is_empty(), "Should find rust document");
let has_rust = results.iter().any(|r| r.filepath.contains("rust_guide"));
assert!(has_rust, "Should find rust_guide.md");
}
Err(e) => {
println!("Smart search failed (model may not be installed): {}", e);
}
}
}
#[tokio::test]
async fn test_smart_search_with_metadata() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.sqlite");
let content_dir = temp_dir.path().join("content");
fs::create_dir(&content_dir).unwrap();
let db = Database::open(&db_path).unwrap();
db.initialize().unwrap();
let file1 = content_dir.join("doc1.md");
fs::write(&file1, "Tutorial about Rust by Alice").unwrap();
let file2 = content_dir.join("doc2.md");
fs::write(&file2, "Guide about Python by Bob").unwrap();
db.add_collection(
"test",
content_dir.to_str().unwrap(),
"**/*.md",
"file",
None,
)
.unwrap();
db.reindex_collection("test").await.unwrap();
let alice_meta = MetadataBuilder::new().text("author", "Alice").build();
let doc1_path = file1.to_str().unwrap();
if let Ok(docs) = db.get_documents_by_pattern("doc1.md") {
if let Some(_) = docs.first() {
db.add_metadata(doc1_path, &alice_meta).unwrap();
}
}
let bob_meta = MetadataBuilder::new().text("author", "Bob").build();
let doc2_path = file2.to_str().unwrap();
if let Ok(docs) = db.get_documents_by_pattern("doc2.md") {
if let Some(_) = docs.first() {
db.add_metadata(doc2_path, &bob_meta).unwrap();
}
}
let options = SearchOptions::default();
let results = agentroot_core::smart_search(&db, "rust", &options).await;
match results {
Ok(results) => {
assert!(!results.is_empty(), "Should find rust document");
}
Err(e) => {
println!(
"Smart search failed (expected if model not installed): {}",
e
);
}
}
}
#[tokio::test]
async fn test_smart_search_bm25_fallback() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.sqlite");
let content_dir = temp_dir.path().join("content");
fs::create_dir(&content_dir).unwrap();
let db = Database::open(&db_path).unwrap();
db.initialize().unwrap();
let file = content_dir.join("test.md");
fs::write(&file, "This is a test document about testing").unwrap();
db.add_collection(
"test",
content_dir.to_str().unwrap(),
"**/*.md",
"file",
None,
)
.unwrap();
db.reindex_collection("test").await.unwrap();
let options = SearchOptions::default();
let results = agentroot_core::smart_search(&db, "test document", &options).await;
match results {
Ok(results) => {
assert!(!results.is_empty(), "BM25 fallback should find document");
}
Err(e) => {
panic!("BM25 fallback should not fail: {}", e);
}
}
}