Lutra/crates/index/tests/properties.rs
danny 053441d066 M2.5: symbol index and go-to commands
crates/index: walks the project with the search/VFS rules (vendor as
library), parses .php files from tree-sitter outlines on a worker pool
off the engine lock, updates incrementally from disk and buffers through
an updater thread, caps at 200,000 files, and answers fuzzy file, class
and symbol queries (nucleo-matcher, MPL-2.0 as a separate crate).

Engine: index.status, goto.file, goto.class, goto.symbol; events
indexProgress and indexReady drained by the pump; the index starts on
project.open and is cancelled on close. Tests: unit, incremental,
property (random queries and sources), engine and TCP control tests,
release timing on a generated 20k-file project (3.9-4.2 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 12:53:33 +02:00

88 lines
3.2 KiB
Rust

//! Property tests: random queries and random file contents never panic, and
//! query results respect the limit and the ranking order.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic
)]
use lutra_index::{Index, IndexedFile, index_text};
use lutra_syntax::Parser;
use proptest::prelude::*;
/// A small index with project and library files, unicode names included.
fn sample() -> Index {
let mut parser = Parser::new().unwrap();
let mut index = Index::new();
let files = [
(
"src/Models/User.php",
"<?php namespace App\\Models; class User { public function find() {} private $név; const MAX = 1; }",
),
(
"src/Ünïcode/Grüße.php",
"<?php namespace Ü; final class Grüße { public function sägen() {} }",
),
(
"src/helpers.php",
"<?php function format_user() {} const LIMIT = 3;",
),
(
"vendor/acme/User.php",
"<?php namespace Acme; interface User { public function find(); } enum E: int { case A = 1; }",
),
("templates/a😀b.twig", ""),
];
for (path, text) in files {
index.insert(index_text(&mut parser, path.into(), text));
}
index
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(512))]
#[test]
fn random_queries_never_panic(query in "\\PC{0,40}|[\\\\/:$ _a-zA-Z]{0,12}", limit in 0usize..2_000) {
let index = sample();
let files = index.find_files(&query, limit);
let classes = index.find_classes(&query, limit);
let symbols = index.find_symbols(&query, limit);
let cap = limit.min(lutra_index::MAX_LIMIT);
prop_assert!(files.len() <= cap && classes.len() <= cap && symbols.len() <= cap);
for pair in files.windows(2) {
prop_assert!(pair[0].library <= pair[1].library);
if pair[0].library == pair[1].library {
prop_assert!(pair[0].score >= pair[1].score);
}
}
for hit in &files {
let len: usize = hit.path.encode_utf16().count();
for range in &hit.matches {
prop_assert!(range[0] < range[1] && range[1] <= len);
}
prop_assert!(hit.matches.windows(2).all(|w| w[0][1] < w[1][0]));
}
prop_assert!(classes.iter().all(|h| h.symbol.kind.is_class_like()));
prop_assert!(symbols.iter().all(|h| !h.symbol.kind.is_class_like()));
}
#[test]
fn random_sources_never_panic(bytes in proptest::collection::vec(any::<u8>(), 0..2_000)) {
let mut parser = Parser::new().unwrap();
let mut text = String::from("<?php ");
text.push_str(&String::from_utf8_lossy(&bytes));
let file = index_text(&mut parser, "src/x.php".into(), &text);
let mut index = Index::new();
index.insert(file.clone());
for symbol in &file.symbols {
prop_assert!(!symbol.name.is_empty());
let _ = index.find_symbols(&symbol.name, 10);
let _ = index.find_classes(&symbol.name, 10);
}
index.insert(IndexedFile { path: "src/x.php".into(), library: false, symbols: Vec::new() });
prop_assert_eq!(index.symbol_count(), 0);
}
}