Files
notescraft/crates/index/tests/operators_hostile.rs
Marcos Paulo 5e8020aa38
ci / ci (macos-latest) (push) Has been cancelled
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
feat: Obsidian basics, editor previews, graph rework, query blocks, stacked tabs, pop-out windows
Editor previews (colour swatches, arrows, embeds), the 18 obsidian-basics tasks (find and replace
with regex, back/forward, auto-pair, footnotes, folding, search operators, split view, PDF/audio/
video embeds, spelling, drag and drop, pinned tabs, settings) and round 2: ```query blocks, stacked
tabs, pop-out windows, tasks ticked inside embeds, text around an embed, embeds that follow edits,
block menu shortcut. New crates: notescraft-regex (linear-time engine) and notescraft-syntax.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-19 11:21:19 -03:00

114 lines
5.3 KiB
Rust

//! Hostile input for the new search operators: huge notes, huge operator
//! arguments, deeply nested groups, thousands of terms and a vault bigger
//! than the candidate cap. Nothing here may panic or take unbounded time.
use std::fmt::Write as _;
use notescraft_index::Error;
use notescraft_index::search::{MAX_CANDIDATES, SearchIndex, SearchOptions, SearchQuery};
/// Most terms a query may carry, mirroring the parser cap.
const TERM_CAP: usize = 32;
fn paths(notes: &SearchIndex, query: &str) -> Vec<String> {
let parsed = SearchQuery::parse(query);
assert!(parsed.is_ok(), "{query:?} must parse");
notes.search(&parsed.unwrap_or_default(), usize::MAX).into_iter().map(|hit| hit.path).collect()
}
/// `count` distinct space-separated terms.
fn terms(count: usize) -> String {
let mut out = String::new();
for n in 0..count {
let _ = write!(&mut out, "t{n} ");
}
out
}
#[test]
fn thousands_of_terms_are_refused_by_the_caps_not_by_a_crash() {
// Over the term cap first, then over the char cap.
let many = "t ".repeat(10_000);
assert!(matches!(SearchQuery::parse(many.trim()), Err(Error::QueryTooLong { max: 1000, .. })));
let over_cap = terms(TERM_CAP + 5);
assert!(matches!(SearchQuery::parse(over_cap.trim()), Err(Error::TooManyTerms { max: TERM_CAP, .. })));
let inside = terms(TERM_CAP);
assert!(SearchQuery::parse(inside.trim()).is_ok(), "exactly the cap passes");
// A group that carries the whole cap is fine, one more term is not.
assert!(SearchQuery::parse(&format!("section:({inside})")).is_ok());
assert!(SearchQuery::parse(&format!("section:({inside} one)")).is_err());
}
#[test]
fn deeply_nested_groups_and_huge_operators_are_errors() {
for depth in [9_usize, 64, 512] {
let query = format!("{}alpha{}", "(".repeat(depth), ")".repeat(depth));
assert!(SearchQuery::parse(&query).is_err(), "{depth} nested groups must not parse");
}
for query in [
format!("section:({})", "a ".repeat(2_000)),
format!("block:{}", "b".repeat(4_000)),
format!("task:{}", "c".repeat(4_000)),
format!("{}(", "(".repeat(1_000)),
"section:((((((((((".to_string(),
] {
assert!(SearchQuery::parse(&query).is_err(), "{query:?} must not parse");
}
// A huge operator with no token at all is refused, not silently empty.
assert!(SearchQuery::parse(&format!("section:{}", "#".repeat(2_000))).is_err());
assert!(SearchQuery::parse(&format!("task:{}", "漢".repeat(2_000))).is_err());
}
/// Bytes of note text the verification pass reads; a vault note is capped
/// at 2 MiB by the index, so the pass is bounded by construction.
const NOTE_BYTES: usize = 256 * 1024;
#[test]
fn a_note_of_a_quarter_megabyte_is_verified_once_per_clause() {
let mut body = String::with_capacity(NOTE_BYTES);
let mut section = 0_usize;
while body.len() < NOTE_BYTES {
section += 1;
let _ = write!(&mut body, "# Section {section}\n\n- [ ] task {section} filler\n\n```\n# not a heading\n- [ ] not a task\n```\n\n");
}
let mut notes = SearchIndex::new();
notes.update("huge.md", &body).unwrap();
assert_eq!(paths(&notes, &format!("section:(task {section})")), vec![String::from("huge.md")]);
assert_eq!(paths(&notes, "task-done:"), Vec::<String>::new(), "every task of the note is open");
assert_eq!(paths(&notes, "section:(not a task)"), vec![String::from("huge.md")], "the fenced text is section text");
assert_eq!(paths(&notes, "block:(not a task filler)"), Vec::<String>::new(), "the fence is not split");
assert_eq!(paths(&notes, "task:not"), Vec::<String>::new(), "a fenced checkbox is not a task");
assert_eq!(notes.len(), 1);
}
#[test]
fn a_vault_larger_than_the_candidate_cap_stays_bounded_and_stable() {
let mut notes = SearchIndex::new();
for i in 0..MAX_CANDIDATES + 10 {
notes.update(&format!("n/Note {i:05}.md"), "# Head\n\nhay filler\n").unwrap();
}
notes.update("z/last.md", "# Head\n\nneedle in a section\n").unwrap();
assert_eq!(notes.len(), MAX_CANDIDATES + 11);
// The structural operator needs the text pass, so its candidates are
// capped and the last note in path order is the one left out.
let capped = paths(&notes, "section:needle");
assert!(capped.len() <= MAX_CANDIDATES, "the cap holds: {}", capped.len());
assert_eq!(capped, paths(&notes, "section:needle"), "and the cut is stable");
// A plain term is decided by the index alone and stays exact.
assert_eq!(paths(&notes, "hay").len(), MAX_CANDIDATES + 10);
assert_eq!(paths(&notes, "head").len(), MAX_CANDIDATES + 11);
}
#[test]
fn the_case_switch_on_a_big_vault_is_exact() {
let mut notes = SearchIndex::new();
notes.update("a/Ship.md", "ship the Ship\n").unwrap();
notes.update("b/Other.md", "ship only lowercase\n").unwrap();
let insensitive = SearchQuery::parse_with("ship", &SearchOptions::matching_case()).unwrap_or_default();
assert_eq!(notes.search(&insensitive, 10).into_iter().map(|hit| hit.path).collect::<Vec<_>>(), vec![String::from("a/Ship.md"), String::from("b/Other.md")]);
let cased = SearchQuery::parse_with("Ship", &SearchOptions::matching_case()).unwrap_or_default();
assert_eq!(notes.search(&cased, 10).into_iter().map(|hit| hit.path).collect::<Vec<_>>(), vec![String::from("a/Ship.md")]);
}