qua 0.75.0

Structure-aware query CLI for the Quarb engine
//! A reduction that meets no thread inside a body keeps the capsa of
//! the thread the body started from (the user's ruling of
//! 2026-11-26). The reduced thread holds what every thread shared,
//! and with no thread there was nothing to take that from: its capsa
//! was empty, and a read after it of a regula the body's own thread
//! held was UNBOUND_REGULA by the data, for the one row whose path
//! reached nothing. (Until 0.74 that read was null in silence, and
//! the row's figure with it.) At the top of a query there is no such
//! thread, and nothing changes.

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

fn fixture(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("quarb-emptyred-{}-{tag}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    // the Odyssey carries no tag "c"
    std::fs::write(
        dir.join("b.json"),
        r#"{"books":[{"title":"Emma","pages":500,"tags":["a","c"]},
                    {"title":"Iliad","pages":200,"tags":["c"]},
                    {"title":"Odyssey","pages":300,"tags":["a"]}]}"#,
    )
    .unwrap();
    dir
}

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

fn ok(dir: &Path, query: &str) -> String {
    let (out, err, ran) = qua(dir, query);
    assert!(ran, "{query}: {err}");
    assert_eq!(err, "", "{query}");
    out
}

#[test]
fn a_reduction_of_no_thread_keeps_the_bodys_capsa() {
    let d = fixture("body");
    // in a record field's parentheses: the third book's path reaches
    // nothing, and its figure is 0.0
    assert_eq!(
        ok(
            &d,
            r#"/books/* | .n(::pages) | %(::title; r = (/tags/*[:: = "c"] @| count | $_ * 100 div $.n | round(2)))"#
        ),
        r#"%(title = "Emma"; r = 0.2) %(title = "Iliad"; r = 0.5) %(title = "Odyssey"; r = 0.0)"#
    );
    // in a subcontext's body
    assert_eq!(
        ok(
            &d,
            r#"/books/* | .n(::pages) | .r(/tags/*[:: = "c"] @| count | $_ * 100 div $.n) | $.r"#
        ),
        "0.2 0.5 0.0"
    );
    // a read inside the reducing command itself, and the capsa as a
    // record after it
    assert_eq!(
        ok(
            &d,
            r#"/books/*[3] | .n(::pages) | %(r = (/tags/*[:: = "c"] @| %(k = count; p = $.n)))"#
        ),
        "%(r = %(k = 0; p = 300))"
    );
    assert_eq!(
        ok(
            &d,
            r#"/books/*[3] | .n(::pages) | %(s = (/tags/*[:: = "c"] @| count | %.))"#
        ),
        "%(s = %(n = 300))"
    );
    // a reduction that meets threads is what it was: the shared
    // regulae, the starting thread's among them
    assert_eq!(
        ok(
            &d,
            r#"/books/*[1] | .n(::pages) | %(s = (/tags/* @| count | %.))"#
        ),
        "%(s = %(n = 500))"
    );
}

#[test]
fn at_the_top_of_a_query_nothing_changes() {
    let d = fixture("top");
    // no body, no thread it started from: the read is refused, and a
    // default answers for it
    let (out, err, ran) = qua(
        &d,
        r#"/books/* | .n(::pages) | /tags/*[:: = "zz"] @| count | $.n"#,
    );
    assert!(!ran, "{out}");
    assert!(
        err.starts_with(
            "error UNBOUND_REGULA ($.n): refused: the thread holds no regula '.n' where '$.n' is \
             read; it holds none"
        ),
        "{err}"
    );
    assert_eq!(
        ok(
            &d,
            r#"/books/*[::pages > 9999] | .n(::pages) @| count | ($.n ?? "none")"#
        ),
        "none"
    );
}