Files
notescraft/crates/engine/tests/include_properties.rs
2026-10-15 12:48:15 -03:00

66 lines
2.9 KiB
Rust

//! `export.pdf` `include_properties` (SPEC 16): the frontmatter appears as
//! drawn page text only when the parameter asks for it.
//!
//! Exports store text as glyph id runs inside compressed streams; the shared
//! `common::pdf::page_text` reader recovers what is actually drawn, so the
//! assertions are on real page text, not raw file bytes.
mod common;
use common::pdf::page_text;
use std::path::Path;
use common::{seed, temp_vault};
use notescraft_engine::{Registry, Session, register_core};
use serde_json::json;
fn fixture(name: &str) -> (common::TempVault, Session, Registry<Session>) {
let note = "---\ncolor: blue\nsize: 10\n---\n# T\n\nBody\n";
let vault = temp_vault(name);
seed(vault.path(), &[("Note.md", note)]);
let session = Session::open(vault.path()).unwrap_or_else(|error| panic!("open: {error}"));
let mut registry = Registry::new();
register_core(&mut registry).unwrap_or_else(|error| panic!("register: {error}"));
(vault, session, registry)
}
/// The exported file's drawn text.
fn drawn(vault: &Path, value: &serde_json::Value) -> String {
let pdf = Path::new(value["path"].as_str().unwrap_or_default());
let bytes = std::fs::read(vault.join(pdf)).unwrap_or_else(|error| panic!("read {}: {error}", pdf.display()));
assert!(!bytes.is_empty(), "the export produced a file");
page_text(&bytes)
}
/// The keys of the frontmatter are drawn only with `include_properties`.
#[test]
fn frontmatter_is_drawn_only_when_asked_for() {
let (vault, mut session, registry) = fixture("export-props-on");
let result = registry.run("export.pdf", &mut session, &json!({ "path": "Note.md", "include_properties": true }));
let value = result.unwrap_or_else(|error| panic!("{error:?}"));
let text = drawn(vault.path(), &value);
assert!(text.contains("color"), "the key is rendered as a property row: {text}");
assert!(text.contains("blue"), "and its value too");
assert!(text.contains("size"), "the other key is rendered too");
}
/// Without the parameter (or with false) the frontmatter stays hidden.
#[test]
fn frontmatter_is_hidden_without_the_param() {
let (vault, mut session, registry) = fixture("export-props-off");
let result = registry.run("export.pdf", &mut session, &json!({ "path": "Note.md" }));
let value = result.unwrap_or_else(|error| panic!("{error:?}"));
let text = drawn(vault.path(), &value);
assert!(!text.contains("color"), "no property row without `include_properties`: {text}");
assert!(!text.contains("size"), "and none of the other keys either");
}
/// A boolean that is not a truth value does not turn the option on; the
/// registry validates the parameter kind first.
#[test]
fn a_bad_param_type_is_refused() {
let (_vault, mut session, registry) = fixture("export-props-bad");
let result = registry.run("export.pdf", &mut session, &json!({ "path": "Note.md", "include_properties": "yes" }));
assert!(result.is_err(), "a string is not a bool: {result:?}");
}