c6c3a7b1be
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
157 lines
7.3 KiB
Rust
157 lines
7.3 KiB
Rust
//! Queries: outgoing links, backlinks with context, orphans, tags, headings
|
|
//! and the graph node/edge lists.
|
|
|
|
use notescraft_index::{Index, LinkKind};
|
|
|
|
const PROJECT: &str =
|
|
"---\ntags: [Work/Active, journal]\naliases: [Proj]\n---\n# Goal\n\nSome body text. ^goal\n\nSee [[Project]] and [[notes/Idea]] and [[#Goal]].\n";
|
|
|
|
fn vault() -> Index {
|
|
let mut index = Index::new();
|
|
index.update_note("work/Project.md", PROJECT).unwrap();
|
|
index.update_note("notes/Idea.md", "---\ntags: [work/active]\n---\n# Idea\n\n[[Proj#Goal]] ![[assets/diagram.png]]\n").unwrap();
|
|
index.update_note("home/Index.md", "[[Project#Goal]] [[Project#Nope]] [[Nowhere]]\n").unwrap();
|
|
index.add_file("assets/diagram.png").unwrap();
|
|
index
|
|
}
|
|
|
|
#[test]
|
|
fn outgoing_links_carry_kind_target_range_and_context() {
|
|
let index = vault();
|
|
let outgoing = index.outgoing("work/Project.md");
|
|
assert_eq!(outgoing.len(), 3);
|
|
assert!(outgoing.iter().all(|l| l.source == "work/Project.md"));
|
|
assert_eq!(outgoing.first().map(|l| l.target.as_str()), Some("Project"));
|
|
assert_eq!(outgoing.first().map(|l| l.kind), Some(LinkKind::Wiki));
|
|
assert_eq!(outgoing.first().map(|l| l.context.as_str()), Some("See [[Project]] and [[notes/Idea]] and [[#Goal]]."));
|
|
assert_eq!(outgoing.first().map(|l| l.range.clone()), Some(88..99));
|
|
assert_eq!(PROJECT.get(outgoing.first().map(|l| l.range.clone()).unwrap_or(0..0)), Some("[[Project]]"));
|
|
assert_eq!(outgoing[1].resolution.path(), Some("notes/Idea.md"));
|
|
assert_eq!(outgoing[2].resolution.path(), Some("work/Project.md"));
|
|
}
|
|
|
|
#[test]
|
|
fn backlinks_are_sorted_by_source_and_point_at_the_range() {
|
|
let index = vault();
|
|
let backlinks = index.backlinks("work/Project.md");
|
|
let sources: Vec<&str> = backlinks.iter().map(|l| l.source.as_str()).collect();
|
|
assert_eq!(
|
|
sources,
|
|
vec!["home/Index.md", "home/Index.md", "notes/Idea.md", "work/Project.md", "work/Project.md"],
|
|
"a self link and a same-note subpath link both count"
|
|
);
|
|
assert!(backlinks.iter().all(|l| l.context.contains("[[") || l.context.contains('!')));
|
|
assert!(backlinks.first().is_some_and(|l| l.range.end > l.range.start));
|
|
assert_eq!(index.backlinks("assets/diagram.png").len(), 1);
|
|
assert_eq!(index.backlinks("notes/Idea.md").len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn unresolved_links_keep_target_and_context() {
|
|
let index = vault();
|
|
let unresolved = index.unresolved();
|
|
assert_eq!(unresolved.len(), 1, "a missing heading is not an unresolved link: {unresolved:?}");
|
|
assert!(unresolved.iter().all(|u| u.target == "Nowhere"));
|
|
assert_eq!(unresolved.first().map(|u| u.source.as_str()), Some("home/Index.md"));
|
|
}
|
|
|
|
#[test]
|
|
fn orphans_are_the_notes_nobody_links_and_who_link_nobody() {
|
|
let mut index = vault();
|
|
index.update_note("solo/alone.md", "nothing here\n").unwrap();
|
|
index.update_note("solo/self.md", "[[self]]\n").unwrap();
|
|
let orphans = index.orphans();
|
|
assert_eq!(orphans, vec!["solo/alone.md", "solo/self.md"]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_tag_tree_is_nested_sorted_and_counted_once_per_note() {
|
|
let index = vault();
|
|
let tree = index.tags();
|
|
let tags: Vec<&str> = tree.iter().map(|n| n.tag.as_str()).collect();
|
|
assert_eq!(tags, vec!["journal", "work", "work/active"], "sorted, nested under the parent");
|
|
assert_eq!(tree.find("work").map(|n| n.count), Some(2), "two notes under work, counted once each");
|
|
assert_eq!(tree.find("work/active").map(|n| n.count), Some(2));
|
|
assert_eq!(tree.find("journal").map(|n| n.count), Some(1));
|
|
assert_eq!(tree.find("work").map(|n| n.children.len()), Some(1));
|
|
assert!(tree.find("missing").is_none());
|
|
assert_eq!(tree.len(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn notes_with_tag_honours_nesting() {
|
|
let index = vault();
|
|
assert_eq!(index.notes_with_tag("journal", false), vec!["work/Project.md"]);
|
|
assert_eq!(index.notes_with_tag("work", false), Vec::<&str>::new(), "nobody carries `work` itself");
|
|
assert_eq!(index.notes_with_tag("Work", true), vec!["notes/Idea.md", "work/Project.md"], "nested tags included, sorted, case-insensitive");
|
|
assert_eq!(index.notes_with_tag("work/active", true), vec!["notes/Idea.md", "work/Project.md"]);
|
|
assert_eq!(index.notes_with_tag("#work", true), vec!["notes/Idea.md", "work/Project.md"]);
|
|
assert_eq!(index.notes_with_tag("nope", true), Vec::<&str>::new());
|
|
}
|
|
|
|
#[test]
|
|
fn headings_keep_level_text_anchor_and_range() {
|
|
let mut index = Index::new();
|
|
index.update_note("n.md", "intro\n\n## The **Goal** here\n\ntext\n").unwrap();
|
|
let headings = index.headings("n.md").expect("indexed");
|
|
assert_eq!(headings.len(), 1);
|
|
assert_eq!(headings.first().map(|h| (h.level, h.text.as_str(), h.anchor.as_str())), Some((2, "The Goal here", "The Goal here")));
|
|
let content = "intro\n\n## The **Goal** here\n\ntext\n";
|
|
assert_eq!(content.get(headings.first().map(|h| h.range.clone()).unwrap_or(0..0)), Some("## The **Goal** here"));
|
|
assert!(index.headings("nope.md").is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn headings_of_a_note_keep_level_text_anchor_and_range() {
|
|
let index = vault();
|
|
let headings = index.headings("work/Project.md").expect("indexed");
|
|
assert_eq!(headings.len(), 1);
|
|
assert_eq!(headings.first().map(|h| (h.level, h.text.as_str(), h.anchor.as_str())), Some((1, "Goal", "Goal")));
|
|
assert_eq!(PROJECT.get(headings.first().map(|h| h.range.clone()).unwrap_or(0..0)), Some("# Goal"));
|
|
assert!(index.headings("nope.md").is_none());
|
|
assert_eq!(index.note("WORK/project.md").map(|n| n.title.as_str()), Some("Project"), "lookup is case-insensitive");
|
|
}
|
|
|
|
#[test]
|
|
fn node_ids_are_stable_and_edges_follow_them() {
|
|
let index = vault();
|
|
let nodes = index.node_ids();
|
|
assert_eq!(nodes.first().map(String::as_str), Some("assets/diagram.png"));
|
|
assert_eq!(nodes.last().map(String::as_str), Some("work/Project.md"));
|
|
assert_eq!(nodes, vec!["assets/diagram.png", "home/Index.md", "notes/Idea.md", "work/Project.md"]);
|
|
|
|
let edges = index.all_link_edges();
|
|
assert_eq!(edges, vec![(1, 3), (2, 0), (2, 3), (3, 2)], "sorted, deduplicated, no self loop, no unresolved target");
|
|
|
|
let mut again = vault();
|
|
again.update_note("zzz.md", "[[Project]]\n").unwrap();
|
|
assert!(again.node_ids().windows(2).all(|w| w[0] <= w[1]), "sorted even after an insert");
|
|
assert_eq!(again.node_ids().last().map(String::as_str), Some("zzz.md"));
|
|
}
|
|
|
|
#[test]
|
|
fn note_info_exposes_what_was_stored() {
|
|
let index = vault();
|
|
let note = index.note("notes/Idea.md").expect("indexed");
|
|
assert_eq!(note.path, "notes/Idea.md");
|
|
assert_eq!(note.title, "Idea");
|
|
assert_eq!(note.tags, vec!["work/active"]);
|
|
assert_eq!(note.aliases, Vec::<String>::new());
|
|
assert_eq!(note.block_ids, Vec::<String>::new());
|
|
assert_eq!(note.links.len(), 2);
|
|
assert_eq!(index.len(), 3);
|
|
assert_eq!(index.files(), vec!["assets/diagram.png"]);
|
|
}
|
|
|
|
#[test]
|
|
fn removing_an_attachment_makes_embeds_of_it_unresolved_again() {
|
|
let mut index = Index::new();
|
|
index.update_note("a.md", "![[pic.png]]\n").expect("note");
|
|
index.add_file("img/pic.png").expect("file");
|
|
assert!(index.resolve("a.md", "pic.png").is_resolved());
|
|
index.remove_file("img/pic.png").expect("remove");
|
|
assert!(!index.resolve("a.md", "pic.png").is_resolved());
|
|
assert!(index.remove_file("img/pic.png").is_err(), "second removal reports NotIndexed");
|
|
assert!(!index.files().contains(&"img/pic.png"));
|
|
}
|