#![deny(unsafe_code)]
use std::path::{Path, PathBuf};
use super::link::{self, Target};
use super::Navigator;
use crate::md;
use crate::md::ast::{Block, SlugSet};
fn codex() -> Option<PathBuf> {
let home = std::env::var("HOME").ok()?;
let p = PathBuf::from(home).join("notes");
p.join("README.md").is_file().then_some(p)
}
#[test]
fn real_codex_index_lists_the_real_documents() {
let root = match codex() {
Some(p) => p,
None => return,
};
let nav = Navigator::new(&root.join("README.md"), None, Path::new("/"));
let entries = nav.entries();
assert!(entries.len() > 80, "only {} entries", entries.len());
let dns = entries
.iter()
.find(|e| e.path.ends_with("models/SAMPLE_MODEL.md"))
.expect("SAMPLE_MODEL.md is in the index");
assert_eq!(dns.section, "Models");
assert!(dns.desc.contains("sample architecture"), "{}", dns.desc);
for e in entries {
assert!(e.path.is_file(), "{} is missing", e.path.display());
assert!(link::is_markdown(&e.path));
assert!(e.path.starts_with(&root));
}
let sections: Vec<&str> = entries.iter().map(|e| e.section.as_str()).collect();
for want in ["Foundations", "Models", "Plans", "Decisions", "Operational"] {
assert!(sections.contains(&want), "no section {want}");
}
}
#[test]
fn real_codex_anchors_match_generated_heading_ids() {
let root = match codex() {
Some(p) => p,
None => return,
};
let text = std::fs::read_to_string(root.join("README.md")).unwrap();
let doc = md::parse(&text);
let ids: Vec<String> = doc
.blocks
.iter()
.filter_map(|b| match b {
Block::Heading { id, .. } => Some(id.clone()),
_ => None,
})
.collect();
assert!(ids.contains(&"how-to-read-this".to_string()), "{ids:?}");
assert!(ids.contains(&"future-directions".to_string()));
let anchor = match link::split_fragment("README.md#Future directions") {
(_, Some(a)) => a,
_ => panic!("no anchor"),
};
assert!(ids.contains(&anchor));
let mut set = SlugSet::new();
assert_eq!(set.unique("How to read this"), "how-to-read-this");
assert_eq!(set.unique("How to read this"), "how-to-read-this-1");
}
#[test]
fn real_codex_root_is_discovered_by_walking_up() {
let root = match codex() {
Some(p) => p,
None => return,
};
let deep = root.join("models/SAMPLE_MODEL.md");
let nav = Navigator::new(&deep, None, Path::new("/"));
assert_eq!(nav.root(), root.as_path());
assert_eq!(nav.index_path(), Some(root.join("README.md").as_path()));
assert_eq!(nav.label(&deep), "models/SAMPLE_MODEL.md");
assert!(matches!(
nav.resolve("../CONVENTIONS.md"),
Target::Doc { ref path, .. } if path == &root.join("CONVENTIONS.md")
));
assert!(matches!(
nav.resolve("../../etc/passwd"),
Target::Broken { .. }
));
}