use std::path::PathBuf;
use str_format::bundle::Bundle;
use str_format::cmd;
use str_format::cmd::EntryNew;
fn tmp(name: &str) -> PathBuf {
let p = std::env::temp_dir().join(format!("strquery-{name}-{}.str", std::process::id()));
let _ = std::fs::remove_dir_all(&p);
p
}
fn fixture(name: &str) -> (PathBuf, String, String) {
let raw = tmp(name);
let parent = raw.parent().unwrap().to_path_buf();
let stem = raw.file_stem().unwrap().to_string_lossy().to_string();
cmd::init(
&parent.join(&stem),
None,
Some("查询测试束".into()),
None,
7,
)
.unwrap();
cmd::meta_set(&raw, None, None, None, None, None, Some(vec!["crm".into(), "demo".into()])).unwrap();
cmd::node_add(&raw, Some("a.one".into()), Some("Alpha".into()), Some("重点客户档案".into())).unwrap();
let bundle = Bundle::new(raw.clone()).unwrap();
let scan = bundle.scan().unwrap();
let alpha = scan
.visits
.iter()
.find(|v| v.depth == 1)
.map(|v| v.dir.file_name().unwrap().to_string_lossy().to_string())
.unwrap();
cmd::meta_set(
&raw,
Some(alpha.clone()),
None,
None,
None,
None,
Some(vec!["vip".into()]),
)
.unwrap();
cmd::branch_add(&raw, Some(alpha.clone()), Some("b.two".into()), Some("Beta".into()), Some("跟进记录分支".into()), None).unwrap();
let scan = bundle.scan().unwrap();
let beta = scan
.visits
.iter()
.find(|v| v.depth == 2)
.map(|v| v.dir.file_name().unwrap().to_string_lossy().to_string())
.unwrap();
cmd::meta_set(
&raw,
Some(beta.clone()),
None,
None,
None,
None,
Some(vec!["followup".into()]),
)
.unwrap();
let notes = raw.join(&alpha).join(&beta).join("notes.md");
std::fs::write(¬es, "line1 普通\n客户:张伟\nline3 跟进计划\n").unwrap();
cmd::entry_add(&raw, Some(beta.clone()), "notes.md", &EntryNew::default()).unwrap();
let root_md = raw.join("root.md");
std::fs::write(&root_md, "ROOT 提到张伟\n").unwrap();
cmd::entry_add(&raw, None, "root.md", &EntryNew::default()).unwrap();
(raw, alpha, beta)
}
#[test]
fn find_matches_branch_and_entry_fields() {
let (root, alpha, _beta) = fixture("find");
let hits = cmd::find_hits(&root, Some("重点客户".into()), None, &[], None, None, None, None).unwrap();
assert_eq!(hits.len(), 1);
assert_eq!(hits[0]["path"].as_str(), Some(alpha.as_str()));
assert!(hits[0]["matched"]
.as_array()
.unwrap()
.iter()
.any(|m| m.as_str() == Some("summary")));
let hits = cmd::find_hits(&root, Some("跟进".into()), None, &[], None, None, None, None).unwrap();
assert!(hits.iter().any(|h| h["title"].as_str() == Some("Beta")));
let hits = cmd::find_hits(&root, Some("ALPHA".into()), None, &[], None, None, None, None).unwrap();
assert_eq!(hits.len(), 1);
assert_eq!(hits[0]["title"].as_str(), Some("Alpha"));
}
#[test]
fn find_filters_by_tag_type_and_field() {
let (root, alpha, _beta) = fixture("findfilter");
let hits = cmd::find_hits(&root, None, None, &["vip".into()], None, None, None, None).unwrap();
assert_eq!(hits.len(), 1);
assert_eq!(hits[0]["id"].as_str(), Some(alpha.as_str()));
let hits = cmd::find_hits(
&root,
None,
None,
&["vip".into(), "crm".into()],
None,
None,
None,
None,
)
.unwrap();
assert!(hits.is_empty(), "crm 在 ROOT、vip 在 Alpha,无交集");
let hits = cmd::find_hits(&root, None, Some("b.two"), &[], None, None, None, None).unwrap();
assert_eq!(hits.len(), 1);
assert_eq!(hits[0]["title"].as_str(), Some("Beta"));
let hits = cmd::find_hits(&root, Some("重点".into()), None, &[], Some("title"), None, None, None).unwrap();
assert!(hits.is_empty());
}
#[test]
fn find_respects_depth_and_limit() {
let (root, _alpha, _beta) = fixture("finddepth");
let all = cmd::find_hits(&root, None, None, &[], None, None, None, None).unwrap();
assert_eq!(all.len(), 3, "ROOT + Alpha + Beta");
let deep = cmd::find_hits(&root, None, None, &[], None, None, Some(1), None).unwrap();
assert_eq!(deep.len(), 2, "depth ≤ 1:ROOT + Alpha");
let limited = cmd::find_hits(&root, None, None, &[], None, None, None, Some(2)).unwrap();
assert_eq!(limited.len(), 2);
assert_eq!(all[0]["path"].as_str(), Some("."));
}
#[test]
fn grep_finds_payload_text_with_branch_context() {
let (root, alpha, beta) = fixture("grep");
let (hits, stopped) = cmd::grep_hits(&root, "张伟", None, false, false, None, None).unwrap();
assert!(!stopped);
assert_eq!(hits.len(), 2, "root.md 与 notes.md 各一处");
assert_eq!(hits[0]["branch"]["path"].as_str(), Some("."));
assert_eq!(hits[0]["entry"].as_str(), Some("root.md"));
assert_eq!(hits[0]["line"].as_u64(), Some(1));
let second = hits
.iter()
.find(|h| h["branch"]["path"].as_str() != Some("."))
.unwrap();
assert_eq!(
second["branch"]["path"].as_str(),
Some(format!("{alpha}/{beta}").as_str())
);
assert_eq!(second["entry"].as_str(), Some("notes.md"));
assert_eq!(second["line"].as_u64(), Some(2));
}
#[test]
fn grep_ignore_case_and_glob() {
let (root, _alpha, _beta) = fixture("grepcase");
assert!(cmd::grep_hits(&root, "LINE1", None, false, false, None, None).unwrap().0.is_empty());
let (hits, _) = cmd::grep_hits(&root, "LINE1", None, true, false, None, None).unwrap();
assert_eq!(hits.len(), 1);
let (hits, _) = cmd::grep_hits(&root, "张伟", Some("*.txt"), false, false, None, None).unwrap();
assert!(hits.is_empty());
let (hits, _) = cmd::grep_hits(&root, "张伟", Some("*.md"), false, false, None, None).unwrap();
assert_eq!(hits.len(), 2);
}
#[test]
fn grep_limit_and_binary_skip() {
let (root, alpha, beta) = fixture("greplimit");
let (hits, stopped) = cmd::grep_hits(&root, "张伟", None, false, false, None, Some(1)).unwrap();
assert_eq!(hits.len(), 1);
assert!(stopped);
let bin = root.join(&alpha).join(&beta).join("blob.bin");
std::fs::write(&bin, [0x00u8, 0x01, 0x02]).unwrap();
cmd::entry_add(&root, Some(beta.clone()), "blob.bin", &EntryNew::default()).unwrap();
let (hits, _) = cmd::grep_hits(&root, "张伟", None, false, false, None, None).unwrap();
assert_eq!(hits.len(), 2, "二进制文件不参与检索");
}
#[test]
fn tags_counts_branch_level_tags() {
let (root, _alpha, _beta) = fixture("tags");
let rows = cmd::tag_counts(&root).unwrap();
assert_eq!(
rows,
vec![
("crm".into(), 1),
("demo".into(), 1),
("followup".into(), 1),
("vip".into(), 1),
]
);
}
#[test]
fn where_builds_breadcrumb_chain() {
let (root, alpha, beta) = fixture("where");
let j = cmd::where_json(&root, Some(beta.clone())).unwrap();
assert!(j["bundle"]
.as_str()
.unwrap()
.starts_with("strquery-where"));
let chain = j["chain"].as_array().unwrap();
assert_eq!(chain.len(), 3);
assert_eq!(chain[0]["path"].as_str(), Some("."));
assert_eq!(chain[1]["path"].as_str(), Some(alpha.as_str()));
assert_eq!(
chain[2]["path"].as_str(),
Some(format!("{alpha}/{beta}").as_str())
);
assert_eq!(j["target"]["id"].as_str(), Some(beta.as_str()));
}
#[test]
fn get_returns_registered_entry_body_and_info() {
let (root, _alpha, _beta) = fixture("get");
let bytes = cmd::get_bytes(&root, None, "root.md").unwrap();
assert_eq!(bytes, "ROOT 提到张伟\n".as_bytes());
let info = cmd::get_info(&root, None, "root.md").unwrap();
assert_eq!(info["entry"]["role"].as_str(), Some("payload"));
assert!(info["entry"]["sha256"].as_str().is_some());
assert_eq!(info["branch"]["path"].as_str(), Some("."));
}
#[test]
fn get_rejects_unregistered_and_structural_entries() {
let (root, _alpha, _beta) = fixture("getreject");
assert!(cmd::get_bytes(&root, None, "nope.md").is_err());
assert!(cmd::get_bytes(&root, None, "a/b.md").is_err());
let bundle = Bundle::new(root.clone()).unwrap();
let scan = bundle.scan().unwrap();
let alpha = scan
.visits
.iter()
.find(|v| v.depth == 1)
.map(|v| v.dir.file_name().unwrap().to_string_lossy().to_string())
.unwrap();
assert!(cmd::get_bytes(&root, None, &alpha).is_err(), "role = node 拒绝");
}
fn with_content_folder(name: &str) -> (PathBuf, String, String) {
let (root, alpha, beta) = fixture(name);
let beta_dir = root.join(&alpha).join(&beta);
std::fs::create_dir_all(beta_dir.join("reports/deep")).unwrap();
std::fs::write(beta_dir.join("reports/r.md"), "报告正文:季度目标\n").unwrap();
std::fs::write(beta_dir.join("reports/deep/note.txt"), "深层笔记提到张伟\n").unwrap();
cmd::entry_add(&root, Some(beta.clone()), "reports", &EntryNew::default()).unwrap();
(root, alpha, beta)
}
#[test]
fn grep_covers_unregistered_files_under_content_folders() {
let (root, _alpha, _beta) = with_content_folder("grepdir");
let (hits, _) = cmd::grep_hits(&root, "季度目标", None, false, false, None, None).unwrap();
assert_eq!(hits.len(), 1);
assert_eq!(hits[0]["entry"].as_str(), Some("reports/r.md"));
assert_eq!(hits[0]["registered"].as_bool(), Some(false));
let file = hits[0]["file"].as_str().unwrap();
assert!(std::path::Path::new(file).is_file());
let (hits, _) = cmd::grep_hits(&root, "张伟", None, false, false, None, None).unwrap();
assert!(hits
.iter()
.any(|h| h["entry"].as_str() == Some("reports/deep/note.txt")));
let (hits, _) = cmd::grep_hits(&root, "季度目标", None, false, true, None, None).unwrap();
assert!(hits.is_empty());
}
#[test]
fn grep_respects_ignore_and_branch_boundaries() {
let (root, alpha, beta) = with_content_folder("grepignore");
std::fs::write(root.join(".gitignore"), "reports/\n").unwrap();
let (hits, _) = cmd::grep_hits(&root, "季度目标", None, false, false, None, None).unwrap();
assert!(hits.is_empty(), "被 .gitignore 剪中的路径不参与检索");
std::fs::remove_file(root.join(".gitignore")).unwrap();
std::fs::write(root.join(&alpha).join("stray.md"), "散落文件:季度目标\n").unwrap();
let (hits, _) = cmd::grep_hits(&root, "季度目标", None, false, false, None, None).unwrap();
assert_eq!(hits.len(), 2);
let stray = hits
.iter()
.find(|h| h["entry"].as_str() == Some("stray.md"))
.unwrap();
assert_eq!(stray["branch"]["path"].as_str(), Some(alpha.as_str()));
assert_eq!(stray["registered"].as_bool(), Some(false));
}
#[test]
fn get_reads_unregistered_children_of_registered_dir() {
let (root, _alpha, beta) = with_content_folder("getdir");
let bytes = cmd::get_bytes(&root, Some(beta.clone()), "reports/r.md").unwrap();
assert_eq!(bytes, "报告正文:季度目标\n".as_bytes());
let info = cmd::get_info(&root, Some(beta.clone()), "reports/r.md").unwrap();
assert_eq!(info["registered"].as_bool(), Some(false));
assert_eq!(info["entry"]["role"].as_str(), Some("payload"));
assert!(info["entry"]["sha256"].as_str().is_some());
assert!(cmd::get_bytes(&root, Some(beta.clone()), "nope/r.md").is_err());
assert!(
cmd::get_bytes(&root, Some(beta.clone()), "reports").is_err(),
"dir 条目本身拒绝"
);
assert!(
cmd::get_bytes(&root, Some(beta.clone()), "reports/deep").is_err(),
"子目录拒绝"
);
}
#[test]
fn find_and_grep_emit_absolute_paths() {
let (root, alpha, _beta) = fixture("realpath");
let hits = cmd::find_hits(&root, Some("Alpha".into()), None, &[], None, None, None, None).unwrap();
let abs = hits[0]["path_abs"].as_str().unwrap();
assert!(std::path::Path::new(abs).is_dir(), "path_abs 是真实存在的目录");
assert!(abs.ends_with(alpha.as_str()));
let (ghits, _) = cmd::grep_hits(&root, "张伟", None, false, false, None, None).unwrap();
let file = ghits[0]["file"].as_str().unwrap();
assert!(std::path::Path::new(file).is_file(), "file 是真实存在的文件");
}
#[test]
fn scope_limits_find_and_grep_to_subtree() {
let (root, alpha, beta) = fixture("scope");
let all = cmd::find_hits(&root, Some("跟进".into()), None, &[], None, None, None, None).unwrap();
assert!(all.iter().any(|h| h["title"].as_str() == Some("Beta")));
let hits =
cmd::find_hits(&root, Some("跟进".into()), None, &[], None, Some(&alpha), None, None)
.unwrap();
assert_eq!(hits.len(), 1);
assert_eq!(hits[0]["title"].as_str(), Some("Beta"));
let rel = format!("{alpha}/{beta}");
let hits =
cmd::find_hits(&root, None, None, &[], None, Some(rel.as_str()), None, None).unwrap();
assert_eq!(hits.len(), 1);
assert_eq!(hits[0]["id"].as_str(), Some(beta.as_str()));
let hits = cmd::find_hits(&root, None, None, &[], None, Some("."), None, None).unwrap();
assert_eq!(hits.len(), 3);
assert_eq!(hits[0]["path"].as_str(), Some("."));
let alpha_dir = root.join(&alpha);
let hits =
cmd::find_hits(&alpha_dir, Some("跟进".into()), None, &[], None, None, None, None)
.unwrap();
assert_eq!(hits.len(), 1, "只搜 Alpha 子树,不含 ROOT");
assert_eq!(hits[0]["title"].as_str(), Some("Beta"));
let (hits, _) = cmd::grep_hits(&root, "张伟", None, false, false, Some(&alpha), None).unwrap();
assert_eq!(hits.len(), 1, "只命中 notes.md,不含 root.md");
assert_eq!(hits[0]["entry"].as_str(), Some("notes.md"));
assert!(cmd::find_hits(&root, None, None, &[], None, Some("nope"), None, None).is_err());
}