#[derive(Debug, Clone, PartialEq)]
pub struct ContentMatch {
pub token_id: u32,
pub score: f32,
pub content_type: String,
pub snippet: String,
}
#[cfg(test)]
mod specs {
use crate::sharding::PropertyStore;
#[test]
fn spec_fts5_index_and_search() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "docstring", "Parses input JSON into struct")
.unwrap();
store
.index_content(1001, "comment", "// TODO: refactor this function")
.unwrap();
store
.index_content(1002, "docstring", "Validates HTTP request headers")
.unwrap();
let results = store.search_content("parse json", 10).unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].token_id, 1000);
assert_eq!(results[0].content_type, "docstring");
assert!(results[0].snippet.contains("JSON") || results[0].snippet.contains("json"));
}
#[test]
fn spec_fts5_tokenization() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "body", "function validate_request() {}")
.unwrap();
store
.index_content(1001, "docstring", "Validating incoming HTTP requests")
.unwrap();
let results = store.search_content("validate request", 10).unwrap();
assert_eq!(results.len(), 2);
assert!(results.iter().any(|r| r.token_id == 1000));
assert!(results.iter().any(|r| r.token_id == 1001));
}
#[test]
fn spec_fts5_phrase_search() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "docstring", "Processes user authentication tokens")
.unwrap();
store
.index_content(1001, "docstring", "Processes user session tokens")
.unwrap();
let results = store
.search_content("\"authentication tokens\"", 10)
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].token_id, 1000);
}
#[test]
fn spec_fts5_content_type_filter() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "docstring", "Parses JSON")
.unwrap();
store
.index_content(1001, "comment", "// Parses JSON")
.unwrap();
store
.index_content(1002, "body", "fn parse_json() {}")
.unwrap();
let results = store.search_content_type("json", "docstring", 10).unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].token_id, 1000);
}
#[test]
fn spec_fts5_ranking() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "docstring", "parse json parse json parse")
.unwrap();
store
.index_content(1001, "docstring", "parse json once")
.unwrap();
let results = store.search_content("parse json", 10).unwrap();
assert_eq!(results.len(), 2);
assert_eq!(results[0].token_id, 1000);
assert!(results[0].score < results[1].score);
}
#[test]
fn spec_fts5_snippet_generation() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(
1000,
"docstring",
"This function parses JSON input from HTTP requests",
)
.unwrap();
let results = store.search_content("json", 10).unwrap();
assert_eq!(results.len(), 1);
assert!(results[0].snippet.contains("JSON") || results[0].snippet.contains("json"));
}
#[test]
fn spec_fts5_update_content() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "docstring", "Original content")
.unwrap();
store
.index_content(1000, "docstring", "Updated content with JSON parsing")
.unwrap();
let results = store.search_content("json", 10).unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].token_id, 1000);
}
#[test]
fn spec_fts5_delete_content() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "docstring", "Content to delete")
.unwrap();
store.delete_content(1000).unwrap();
let results = store.search_content("content delete", 10).unwrap();
assert_eq!(results.len(), 0);
}
#[test]
fn spec_fts5_empty_search() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "docstring", "Some content")
.unwrap();
let results = store.search_content("", 10).unwrap();
assert_eq!(results.len(), 0);
}
#[test]
fn spec_fts5_result_limit() {
let mut store = PropertyStore::in_memory().unwrap();
for i in 0..10 {
store
.index_content(i, "docstring", &format!("Content {}", i))
.unwrap();
}
let results = store.search_content("content", 3).unwrap();
assert_eq!(results.len(), 3);
}
#[test]
fn spec_fts5_case_insensitive() {
let mut store = PropertyStore::in_memory().unwrap();
store
.index_content(1000, "docstring", "JSON Parsing Function")
.unwrap();
let results_lower = store.search_content("json", 10).unwrap();
let results_upper = store.search_content("JSON", 10).unwrap();
assert_eq!(results_lower.len(), 1);
assert_eq!(results_upper.len(), 1);
assert_eq!(results_lower[0].token_id, results_upper[0].token_id);
}
}