qua 0.75.0

Structure-aware query CLI for the Quarb engine
//! The measures that walk the lightest way (spec: The graph library;
//! the pages of `graph:distance`, `graph:route`, `graph:closeness`):
//! "Dijkstra's algorithm under a weight, ... in the direction the step
//! names: `->*` follows the edges forward from the seeds, `<-*`
//! backward". Two faults lang's review of the recipes found on
//! 2026-11-28, present in 0.74.0: the weights of parallel edges were
//! added, where the lightest is the edge of a way; and a reverse step
//! was read as the forward one.

use std::path::PathBuf;

/// Four towns on a road, A to B to C to D, with two edges on the
/// first leg, of 3 and 5 kilometres.
fn road(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("quarb-graph-ways-{}-{tag}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("nodes.csv"), "id\nA\nB\nC\nD\n").unwrap();
    std::fs::write(
        dir.join("edges.csv"),
        "a,b,km,h\nA,B,3,1\nA,B,5,2\nB,C,4,1\nC,D,1,1\n",
    )
    .unwrap();
    std::fs::write(
        dir.join("r.model"),
        "mount nodes: nodes.csv;\nmount edges: edges.csv;\n\
         ref /edges/row::a --> /nodes/row[::id = $];\n\
         ref /edges/row::b --> /nodes/row[::id = $];\n\
         edge /edges/row: ::a -- ::b as e weights(::km<_km>; ::h<_h>);\n",
    )
    .unwrap();
    dir
}

/// The answer's lines, one a town in the order A, B, C, D.
fn towns(dir: &std::path::Path, query: &str) -> Vec<String> {
    let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
        .env("QUARB_LOCALE", "C")
        .current_dir(dir)
        .args(["--model", "r.model", query])
        .stdin(std::process::Stdio::null())
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "{query}: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8_lossy(&out.stdout)
        .lines()
        .map(str::to_string)
        .collect()
}

const FROM_A: &str = "--from=/nodes/row[::id = \"A\"]";
const FROM_D: &str = "--from=/nodes/row[::id = \"D\"]";

#[test]
fn a_way_takes_the_lightest_of_parallel_edges() {
    let d = road("lightest");
    // the walk's own total to B, by the lighter edge
    assert_eq!(
        towns(
            &d,
            "/nodes/row[::id = \"A\"](->*){*_km}[::id = \"B\"]? | $total"
        ),
        ["3_km"]
    );
    // and the library's: 3, not the two edges' 8
    assert_eq!(
        towns(
            &d,
            &format!("/nodes/row @| graph:distance(->*; {FROM_A} --weight=km)")
        ),
        ["0_km", "3_km", "7_km", "8_km"]
    );
    // in steps the two edges were one edge already
    assert_eq!(
        towns(&d, &format!("/nodes/row @| graph:distance(->*; {FROM_A})")),
        ["0", "1", "2", "3"]
    );
    // closeness reads those distances
    assert_eq!(
        towns(
            &d,
            "/nodes/row @| graph:closeness(->*; --weight=km) | round(4)"
        ),
        ["0.1667", "0.1481", "0.3333", "0.0"]
    );
    // the table of the edges keeps both, as it did
    assert_eq!(towns(&d, "/nodes/row @| edges(->*) | count"), ["4"]);
}

#[test]
fn a_reverse_step_is_walked_backward() {
    let d = road("backward");
    // forward from D nothing is reached; backward the whole road
    assert_eq!(
        towns(&d, &format!("/nodes/row @| graph:distance(->*; {FROM_D})")),
        ["null", "null", "null", "0"]
    );
    assert_eq!(
        towns(&d, &format!("/nodes/row @| graph:distance(<-*; {FROM_D})")),
        ["3", "2", "1", "0"]
    );
    // either way, as it was
    assert_eq!(
        towns(&d, &format!("/nodes/row @| graph:distance(--*; {FROM_D})")),
        ["3", "2", "1", "0"]
    );
    // the route from the seed, town by town against the edges
    assert_eq!(
        towns(
            &d,
            &format!("/nodes/row @| graph:route(<-*; {FROM_D}) | ($_ $| ::id | join(\" \"))")
        ),
        ["D C B A", "D C B", "D C", "D"]
    );
    // closeness is how near a town is to those it reaches: the
    // mirror of the forward one
    assert_eq!(
        towns(&d, "/nodes/row @| graph:closeness(->*)"),
        ["0.5", "0.4444444444444444", "0.3333333333333333", "0.0"]
    );
    assert_eq!(
        towns(&d, "/nodes/row @| graph:closeness(<-*)"),
        ["0.0", "0.3333333333333333", "0.4444444444444444", "0.5"]
    );
}

#[test]
fn what_reads_the_edges_as_the_table_prints_them_is_the_same_either_way() {
    let d = road("same");
    // betweenness is one number with every edge turned
    for step in ["->*", "<-*"] {
        assert_eq!(
            towns(&d, &format!("/nodes/row @| graph:betweenness({step})")),
            ["0.0", "2.0", "2.0", "0.0"],
            "{step}"
        );
    }
    // a rank reads the graph `edges` prints for the operand, and the
    // incoming edges are printed with their stored ends
    let forward = towns(&d, "/nodes/row @| graph:pagerank(->*) | round(4)");
    assert_eq!(forward, ["0.1162", "0.2149", "0.2988", "0.3701"]);
    assert_eq!(
        towns(&d, "/nodes/row @| graph:pagerank(<-*) | round(4)"),
        forward
    );
    assert_eq!(towns(&d, "/nodes/row @| edges(<-*) | count"), ["4"]);
}