Files
dht/dht-search/tests/storage_search_flow.rs
T

109 lines
3.7 KiB
Rust

// 负责从 crate 外部验证持久化索引过滤搜索和重启恢复的公开组合契约
#![cfg(feature = "rocksdb-storage")]
use dht_crawler::{FileInfo, TorrentInfo};
use dht_search::{
domain::{
ContentFilter, ContentFilterConfig, FileFilterRule, FileMatchField, FileMatchKind,
FileRuleAction, TorrentRecord,
},
search::SearchEngine,
storage::{RocksTorrentRepository, TorrentRepository, UpsertOutcome},
};
use std::sync::Arc;
use tempfile::TempDir;
#[test]
fn public_components_compose_into_a_restart_safe_search_flow() {
let directory = TempDir::new().unwrap();
let rocksdb = directory.path().join("rocksdb");
let tantivy = directory.path().join("tantivy");
let record = TorrentRecord::try_from(TorrentInfo {
info_hash: "1212121212121212121212121212121212121212".into(),
magnet_link: String::new(),
name: "Public API 测试资源".into(),
total_size: 42,
files: vec![FileInfo {
path: "docs/public-api.txt".into(),
size: 42,
}],
piece_length: 16_384,
peers: Vec::new(),
timestamp: 100,
})
.unwrap();
{
let repository = RocksTorrentRepository::open(&rocksdb).unwrap();
assert_eq!(
repository.upsert(record.clone()).unwrap(),
UpsertOutcome::Inserted
);
let search = SearchEngine::open(&tantivy).unwrap();
assert_eq!(search.index_pending(&repository, 100, 100).unwrap(), 1);
let page = search.search("public-api", 0, 10).unwrap();
assert_eq!(page.total, 1);
assert_eq!(page.hits[0].info_hash, record.info_hash.to_string());
}
let reopened = RocksTorrentRepository::open(&rocksdb).unwrap();
assert_eq!(reopened.get(record.info_hash).unwrap(), Some(record));
}
#[test]
fn content_filter_excludes_padding_from_search_and_visible_details() {
let directory = TempDir::new().unwrap();
let filter = Arc::new(
ContentFilter::compile(ContentFilterConfig {
version: 1,
file_rules: vec![FileFilterRule {
id: "bitcomet-padding".into(),
enabled: true,
field: FileMatchField::FileName,
match_kind: FileMatchKind::Prefix,
value: "_____padding_file_".into(),
case_sensitive: false,
action: FileRuleAction::Hide,
reason: "测试".into(),
}],
})
.unwrap(),
);
let repository = RocksTorrentRepository::open_with_rules(
directory.path().join("rocksdb"),
dht_search::domain::MetadataLimits::default().rule_id(),
filter,
)
.unwrap();
let record = TorrentRecord::try_from(TorrentInfo {
info_hash: "3434343434343434343434343434343434343434".into(),
magnet_link: String::new(),
name: "Filtered Movie".into(),
total_size: 142,
files: vec![
FileInfo {
path: "movie.mkv".into(),
size: 42,
},
FileInfo {
path: "_____padding_file_1_请升级____".into(),
size: 100,
},
],
piece_length: 16_384,
peers: Vec::new(),
timestamp: 100,
})
.unwrap();
repository.upsert(record.clone()).unwrap();
let search = SearchEngine::open(directory.path().join("tantivy")).unwrap();
assert_eq!(search.index_pending(&repository, 100, 100).unwrap(), 1);
assert_eq!(search.search("padding_file", 0, 10).unwrap().total, 0);
assert_eq!(search.search("*.mkv", 0, 10).unwrap().total, 1);
let visible = repository.get_visible(record.info_hash).unwrap().unwrap();
assert_eq!(visible.files.len(), 1);
assert_eq!(visible.total_size, 42);
}