qua 0.75.0

Structure-aware query CLI for the Quarb engine
//! A text node's record lists what the node answers (the user's
//! word of 2026-11-26: "properties like ::lemma should be included
//! in %"): its own fields under their canonical names, what its kind
//! adds, a document's declared identity, the fields the document or
//! the reading filed on it. The level listed nothing of its own, and
//! the adapters' default answered: a node's children by their names,
//! `%(paragraph = null; section = null)` for a section whose
//! `::lemma` is its heading. Not listed: the node's text, what is
//! read from where the node stands (`cite`, `href`, `anchor`,
//! `link`, the document's `head`), and what a token inherits from
//! the blocks around it.

use std::path::{Path, PathBuf};

fn fixture(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("quarb-textrec-{}-{tag}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(
        dir.join("raven.md"),
        "---\ntitle: The Raven\nauthor: Edgar Allan Poe\nlang: en\n---\n\n\
         # Midnight {#sec:midnight}\n\n\
         Once upon a midnight dreary. While I pondered, weak and weary.\n\n\
         ## Chamber\n\n\
         See [the door](#sec:midnight).\n",
    )
    .unwrap();
    dir
}

fn ok(dir: &Path, query: &str, target: &str) -> String {
    let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
        .env("QUARB_LOCALE", "C")
        .current_dir(dir)
        .args([query, target])
        .stdin(std::process::Stdio::null())
        .output()
        .unwrap();
    let err = String::from_utf8_lossy(&out.stderr).trim().to_string();
    assert!(out.status.success(), "{query}: {err}");
    assert_eq!(err, "", "{query}");
    String::from_utf8_lossy(&out.stdout)
        .trim()
        .replace('\n', " ")
}

#[test]
fn a_text_nodes_record_lists_what_it_answers() {
    let d = fixture("record");
    let text = |q: &str| ok(&d, q, "text:raven.md");
    // the document: its title under the canonical name, and what it
    // declares
    assert_eq!(
        text("^ | %"),
        r#"%(lemma = "The Raven"; lang = "en"; author = "Edgar Allan Poe")"#
    );
    // a section: its heading and its anchor
    assert_eq!(
        text("//section | %"),
        r#"%(lemma = "Midnight"; onym = "sec:midnight") %(lemma = "Chamber"; onym = "chamber")"#
    );
    // a paragraph holds none of its own; a reference its target
    assert_eq!(text("//paragraph[1] | %"), "%()");
    assert_eq!(text("//ref[1] | %"), r##"%(target = "#sec:midnight")"##);
    // the node as a record's item, and the names alone
    assert_eq!(
        text("//section[1] | .me | %($$.me)"),
        r#"%(lemma = "Midnight"; onym = "sec:midnight")"#
    );
    assert_eq!(text("//section[1] | :props"), "@(::lemma; ::onym)");
    // an alias is a spelling of the field, read by name as ever
    assert_eq!(
        text("//section[1] | %(::lemma; ::title)"),
        r#"%(lemma = "Midnight"; title = "Midnight")"#
    );
}

/// A bibliography entry lists its campi once, each under its key:
/// `author` is a spelling of its `auctor`, as `title` is of a
/// section's lemma, and is no field beside it.
#[test]
fn an_entry_lists_its_campi_under_their_keys() {
    let d = fixture("entry");
    std::fs::write(
        d.join("refs.md"),
        "# Sources\n\nThe frame is Austen [1]; the war is Homer [2].\n\n## References\n\n\
         1. Austen, Jane. Emma. London: John Murray, 1815.\n\
         2. Homer. The Iliad. Translated by Samuel Butler. London: Longmans, 1898.\n",
    )
    .unwrap();
    let entry = |q: &str| ok(&d, q, "text:refs.md?references=cmos");
    assert_eq!(
        entry("//bib[2] | %"),
        r#"%(onym = "2"; auctor = "Homer"; titulus = "The Iliad"; locus = "London"; officina = "Longmans"; annus = "1898")"#
    );
    assert_eq!(
        entry("//bib[2] | :props"),
        "@(::onym; ::auctor; ::titulus; ::locus; ::officina; ::annus)"
    );
    // the alias answers by name, as it did
    assert_eq!(
        entry("//bib[1] | %(::author; ::auctor)"),
        r#"%(author = "Austen, Jane"; auctor = "Austen, Jane")"#
    );
}

#[test]
fn the_corpus_readings_nodes_list_their_own() {
    let d = fixture("corpus");
    let corpus = |q: &str| ok(&d, q, "corpus:raven.md");
    assert_eq!(corpus("//sentence[2] | %"), "%(taxis = 2)");
    assert_eq!(
        corpus("//token[1] | %"),
        r#"%(lower = "midnight"; modern = "Midnight"; class = "word")"#
    );
}