mod common;
use common::{dbmd, write_file};
const TWO_SECTIONS: &str = "\
---
type: wiki-page
summary: a page
---
# Title
Intro paragraph.
## Timeline
- a point
### Sub-detail
more.
## Commercials
closing.
";
#[test]
fn regression_outline_reads_single_file_without_a_store() {
let tmp = tempfile::TempDir::new().unwrap();
assert!(
!tmp.path().join("DB.md").exists(),
"fixture must have no DB.md to reproduce the bug"
);
let file = write_file(tmp.path(), "notes.md", TWO_SECTIONS);
let out = dbmd().arg("outline").arg(&file).assert().success();
let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
assert_eq!(stdout, "Timeline\n Sub-detail\nCommercials\n");
}
#[test]
fn regression_outline_json_works_without_a_store() {
let tmp = tempfile::TempDir::new().unwrap();
assert!(!tmp.path().join("DB.md").exists());
let file = write_file(tmp.path(), "notes.md", TWO_SECTIONS);
let out = dbmd()
.arg("--json")
.arg("outline")
.arg(&file)
.assert()
.success();
let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&stdout).expect("valid JSON object");
let sections = parsed.get("sections").and_then(|s| s.as_array()).unwrap();
assert_eq!(
sections,
&serde_json::json!([
{ "heading": "Timeline", "level": 2, "line": 6 },
{ "heading": "Sub-detail", "level": 3, "line": 10 },
{ "heading": "Commercials", "level": 2, "line": 14 },
])
.as_array()
.unwrap()
.clone()
);
}
#[test]
fn regression_outline_and_sections_agree_without_a_store() {
let tmp = tempfile::TempDir::new().unwrap();
assert!(!tmp.path().join("DB.md").exists());
let file = write_file(tmp.path(), "notes.md", TWO_SECTIONS);
let outline_out = dbmd()
.arg("--json")
.arg("outline")
.arg(&file)
.assert()
.success();
let outline_json: serde_json::Value =
serde_json::from_slice(&outline_out.get_output().stdout).unwrap();
let outline_sections = outline_json.get("sections").unwrap().as_array().unwrap();
let sections_out = dbmd()
.arg("--json")
.arg("sections")
.arg(&file)
.assert()
.success();
let sections_json: serde_json::Value =
serde_json::from_slice(§ions_out.get_output().stdout).unwrap();
let sections_arr = sections_json.as_array().unwrap();
assert_eq!(
outline_sections, sections_arr,
"outline and sections must report identical (heading, level, line) triples"
);
}
#[test]
fn regression_outline_is_lenient_about_missing_frontmatter() {
let tmp = tempfile::TempDir::new().unwrap();
assert!(!tmp.path().join("DB.md").exists());
let file = write_file(tmp.path(), "plain.md", "# Title\n\n## One\n\ntext\n");
let out = dbmd().arg("outline").arg(&file).assert().success();
let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
assert_eq!(stdout, "One\n");
}
#[test]
fn regression_outline_missing_file_is_runtime_error_exit_1() {
let tmp = tempfile::TempDir::new().unwrap();
let missing = tmp.path().join("does-not-exist.md");
dbmd()
.arg("outline")
.arg(&missing)
.assert()
.failure()
.code(1); }