use searchi_core::{SearchIndex, SourceLocation}; use std::fs; #[test] fn document_source_is_bounded_current_and_index_scoped() { let directory = tempfile::tempdir().unwrap(); let files = directory.path().join("files"); fs::create_dir(&files).unwrap(); let file = files.join("notes.txt"); fs::write(&file, "héllo supplier notice").unwrap(); let mut index = SearchIndex::open(directory.path().join("index.sqlite")).unwrap(); index.index_folder(&files).unwrap(); let path = fs::canonicalize(&file) .unwrap() .to_string_lossy() .into_owned(); let original_version = index.file_signature(&path).unwrap().unwrap(); let source = SourceLocation { kind: "line".into(), number: 1, }; let passage = index.source_passage(&path, &source, 5).unwrap().unwrap(); assert_eq!(passage.text, "héllo"); assert!(index.source_passage(&path, &source, 8193).is_err()); assert!(index .source_passage( &path, &SourceLocation { kind: "timestamp_ms".into(), number: 0 }, 100 ) .is_err()); assert!(index .source_passage( &path, &SourceLocation { kind: "line".into(), number: 99 }, 100 ) .unwrap() .is_none()); let other = files.join("unindexed.txt"); fs::write(&other, "private unrelated content").unwrap(); assert!(index .file_signature(&other.to_string_lossy()) .unwrap() .is_none()); assert!(index .source_passage(&other.to_string_lossy(), &source, 100) .unwrap() .is_none()); fs::write(&file, "changed content with different size").unwrap(); assert!(index.source_passage(&path, &source, 100).unwrap().is_none()); index.index_folder(&files).unwrap(); assert_ne!( index.file_signature(&path).unwrap().unwrap(), original_version ); assert!(index.source_passage(&path, &source, 100).unwrap().is_some()); let old_time = fs::metadata(&file).unwrap().modified().unwrap(); let same_size = "x".repeat(fs::metadata(&file).unwrap().len() as usize); fs::write(&file, same_size).unwrap(); fs::File::options() .write(true) .open(&file) .unwrap() .set_modified(old_time + std::time::Duration::from_secs(2)) .unwrap(); assert!(index.source_passage(&path, &source, 100).unwrap().is_none()); fs::remove_file(&file).unwrap(); assert!(index.source_passage(&path, &source, 100).unwrap().is_none()); }