use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::time::{Duration, UNIX_EPOCH};
use reference_query::index;
use reference_query::search::{self, ActiveFiles};
use reference_query::store::Store;
fn indexed_src() -> Store {
let src = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src");
let dir = std::env::temp_dir().join(format!("rq-aspirations-{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
copy_with_uniform_mtime(&src, &dir);
let mut store = Store::open_in_memory().unwrap();
index::index_path(&mut store, &dir).unwrap();
let _ = fs::remove_dir_all(&dir);
store
}
fn copy_with_uniform_mtime(src: &Path, dst: &Path) {
let stamp = UNIX_EPOCH + Duration::from_secs(1_000_000);
fs::create_dir_all(dst).unwrap();
for entry in fs::read_dir(src).unwrap() {
let entry = entry.unwrap();
let to = dst.join(entry.file_name());
if entry.file_type().unwrap().is_dir() {
copy_with_uniform_mtime(&entry.path(), &to);
} else {
fs::copy(entry.path(), &to).unwrap();
File::open(&to).unwrap().set_modified(stamp).unwrap();
}
}
}
fn top(store: &Store, query: &str) -> (String, String) {
search::search(store, query, None, None, &ActiveFiles::default(), 10)
.unwrap()
.first()
.map(|h| (h.name.clone(), h.kind.clone()))
.unwrap_or_else(|| ("<none>".into(), String::new()))
}
#[test]
fn aspirational_queries() {
let store = indexed_src();
let wants = [
("lp", "LanguagePlugin", "trait"), ("recency", "recency_boost", "function"), ("budgeted", "index_budgeted", "function"), ("parsefile", "parse_file", "function"), ("search", "search", "function"), ("store", "Store", "struct"), ("kind", "Kind", "enum"), ];
let mut misses = Vec::new();
for (q, name, kind) in wants {
let (got_name, got_kind) = top(&store, q);
if got_name != name || got_kind != kind {
misses.push(format!(
" {q:<10} want {name} ({kind}) got {got_name} ({got_kind})"
));
}
}
assert!(
misses.is_empty(),
"ranking gaps ({} of {}):\n{}",
misses.len(),
wants.len(),
misses.join("\n")
);
}