sherd 0.6.0

two-axis federation of SPEC.md and source over a dir DAG, for small-context local models
Documentation
use super::*;

#[test]
fn a_node_ceiling_comes_from_the_file_not_a_constant() {
    // Reads a file this repo has and the published crate excludes.
    crate::testrepo::dogfood(|| {
        let root = Path::new(env!("CARGO_MANIFEST_DIR"));
        // .context-limits names src/tdd; the value is read, not assumed.
        let tdd = ceiling_for(root, &root.join("src/tdd")).unwrap();
        assert!(
            tdd > tokens::DEFAULT_NODE,
            "src/tdd is listed and should not fall back to the default: {tdd}"
        );
        // A new node under src inherits src's ceiling -- prefix matching, so
        // adding a node does not silently drop it to the global default.
        assert_eq!(
            ceiling_for(root, &root.join("src/nope")).unwrap(),
            ceiling_for(root, &root.join("src")).unwrap()
        );
        // A path sharing no listed prefix falls back, which is NOT "no limit".
        assert_eq!(
            ceiling_for(root, &root.join("docs")).unwrap(),
            tokens::DEFAULT_NODE
        );
    });
}

#[test]
fn verdict_reports_direction_and_distance() {
    assert_eq!(verdict(100, 500), Verdict::Fits { slack: 400 });
    assert_eq!(verdict(900, 500), Verdict::Over { by: 400 });
}

#[test]
fn the_root_ceiling_comes_from_its_spec_row_not_the_default() {
    // Reads a file this repo has and the published crate excludes.
    crate::testrepo::dogfood(|| {
        // `.context-limits` names the root `SPEC.md`, while a node is
        // addressed as `.` -- so the lookup has to bridge those two spellings
        // or the root silently falls to DEFAULT_NODE and reads as 5x over
        // (`.:V104`: absence must never read as a verdict).
        let root = Path::new(env!("CARGO_MANIFEST_DIR"));
        let c = ceiling_for(root, root).unwrap();
        assert!(
            c > tokens::DEFAULT_NODE,
            "root must resolve to its SPEC.md row, got the default: {c}"
        );
    });
}

#[test]
fn depth_selects_something_or_it_is_a_flag_that_lies() {
    // V105, and the test that would have caught B8 the day `Depth` was
    // introduced: same input, two settings, DIFFERENT output. `Rule` was
    // consulted nowhere, so the two branches agreed for the project's
    // whole life while §I advertised a choice.
    let root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let rule = pack(root, root, Depth::Rule).unwrap();
    let all = pack(root, root, Depth::All).unwrap();
    assert!(
        rule.cost.tokens < all.cost.tokens,
        "rule {} must be cheaper than all {}",
        rule.cost.tokens,
        all.cost.tokens
    );
    assert!(all.text.contains("## \u{a7}B"), "all keeps the archive");
    assert!(
        !rule.text.contains("## \u{a7}B"),
        "rule drops §B -- history is not a rule"
    );
    assert!(
        !rule.text.contains("## \u{a7}R"),
        "rule drops §R -- a measurement is not a rule"
    );
    assert!(
        rule.text.contains("## \u{a7}V"),
        "rule keeps §V -- that is the point of it"
    );
}

#[test]
fn every_node_resolves_to_a_real_ceiling() {
    // Reads a file this repo has and the published crate excludes.
    crate::testrepo::dogfood(|| {
        // V104's second half, made mechanical: absence must never read as
        // permission. DEFAULT_NODE is 2,000 -- a NODE budget, impossible as a
        // CHAIN ceiling -- so a node landing on it means no row covers it,
        // directly or by prefix, and it would be gated against a number
        // nobody chose.
        let root = Path::new(env!("CARGO_MANIFEST_DIR"));
        for node in fed::discover(root) {
            let c = ceiling_for(root, &node).unwrap();
            assert_ne!(
                c,
                tokens::DEFAULT_NODE,
                "{} fell back to the node default -- give it a row in \
                 .context-limits, or a prefix that covers it",
                node.display()
            );
        }
    });
}

#[test]
fn a_ceiling_is_compared_at_the_boundary_not_near_it() {
    // T10 turns this comparison into an exit code, so off-by-one here is
    // a gate that fires on compliant nodes or misses drifting ones.
    assert_eq!(verdict(500, 500), Verdict::Fits { slack: 0 });
    assert_eq!(verdict(501, 500), Verdict::Over { by: 1 });
}

#[test]
fn chain_of_repo_root_is_at_least_the_root_spec() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR"));
    assert!(
        !fed::chain(root, root).is_empty(),
        "root SPEC.md must exist (V5)"
    );
}

/// `V5`: a node that is not there is an error, never its ancestors' pack
/// under its name. `pack(root, "nowhere")` returned the ROOT chain, so
/// `sherd lens nowhere` printed the root's rules as that node's (`B2`).
#[test]
fn a_dir_with_no_spec_is_an_error_not_its_ancestors_pack() -> Result<(), String>
{
    let r = crate::testrepo::TestRepo::new("lens-nowhere")?;
    let err = pack(r.path(), &r.path().join("nowhere"), Depth::Rule)
        .err()
        .ok_or("a missing node was packed")?;
    assert!(err.to_string().contains("nowhere"), "{err}");
    assert!(
        pack(r.path(), r.path(), Depth::Rule).is_ok(),
        "the root packs"
    );
    Ok(())
}

/// `own_cost` is the node's OWN spec at the depth asked, never the chain. A
/// split reads it to say which spec in a chain carries the tokens; were it
/// the chain, every child would report its ancestors' rows as its own.
#[test]
fn own_is_the_node_spec_alone_not_its_chain() -> Result<(), String> {
    let r = crate::testrepo::TestRepo::new("lens-own")?;
    let child = "# SPEC\n\n## \u{a7}G GOAL\n\nthe child\n\n## \u{a7}V INVARIANTS\n\nV1: a child rule\n";
    r.write("SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\nthe root, long enough to cost a few tokens more than its child does\n")?;
    r.write("a/SPEC.md", child)?;
    let a = r.path().join("a");
    let p = pack(r.path(), &a, Depth::Rule).map_err(|e| e.to_string())?;
    let own = own_cost(&a, Depth::Rule).map_err(|e| e.to_string())?;
    assert_eq!(p.chain.len(), 2, "root and child");
    assert_eq!(
        own.tokens,
        tokens::count(&crate::spec::rule_depth(child)).tokens,
        "own is the child's spec at rule depth"
    );
    assert!(own.tokens < p.cost.tokens, "the chain also pays the root");
    assert!(
        own_cost(&r.path().join("nowhere"), Depth::Rule).is_err(),
        "no node is an error, never a zero cost"
    );
    Ok(())
}

/// V18: a task row in the root and in `a` is in neither's descendant's pack;
/// the target's own row is. Every depth but `All` drops them -- `Why` adds
/// rationale, never the plan of the nodes above. `All` keeps the whole
/// picture.
#[test]
fn ancestors_task_rows_stay_out_of_the_pack() -> Result<(), String> {
    let r = crate::testrepo::TestRepo::new("lens-own-tasks")?;
    let spec = |t: &str, v: &str| {
        format!(
            "# SPEC\n\n## \u{a7}G GOAL\n\ng\n\n## \u{a7}V INVARIANTS\n\nV1: {v}\n\n## \u{a7}T TASKS\n\nid|status|task|cites\n{t}|.|work|V1\n"
        )
    };
    r.write("SPEC.md", &spec("T1", "root-rule"))?;
    r.write("a/SPEC.md", &spec("T2", "a-rule"))?;
    r.write("a/b/SPEC.md", &spec("T3", "b-rule"))?;
    let b = r.path().join("a/b");
    for depth in [Depth::Rule, Depth::Why] {
        let p = pack(r.path(), &b, depth).map_err(|e| e.to_string())?;
        assert!(
            !p.text.contains("T1|") && !p.text.contains("T2|"),
            "{depth:?}: {}",
            p.text
        );
        assert!(
            p.text.contains("T3|"),
            "{depth:?}: the target keeps its rows"
        );
        assert!(
            p.text.contains("V1: root-rule") && p.text.contains("V1: a-rule"),
            "{depth:?}: ancestors keep their rules: {}",
            p.text
        );
    }
    let all = pack(r.path(), &b, Depth::All).map_err(|e| e.to_string())?;
    assert!(all.text.contains("T1|") && all.text.contains("T2|"));
    Ok(())
}