// ---
// tags: optica, rust
// crystal-type: source
// crystal-domain: comp
// ---
//! Tests for the subgraphs config loader and the walk of nested
//! `subgraphs.toml` files. Kept beside the module to hold it under the
//! 500-line limit of the quality rules.

use super::*;
use std::fs;
use tempfile::TempDir;

/// A workspace: `root_dir/<name>` checkouts, a materialized config that
/// lists the top-level ones, and nested files where the test puts them.
struct Workspace {
    dir: TempDir,
}

impl Workspace {
    fn new() -> Self {
        Workspace { dir: TempDir::new().unwrap() }
    }
    fn root(&self) -> PathBuf {
        self.dir.path().canonicalize().unwrap()
    }
    fn repo(&self, name: &str) -> PathBuf {
        let p = self.root().join(name);
        fs::create_dir_all(&p).unwrap();
        fs::write(p.join("README.md"), format!("# {name}\n")).unwrap();
        p
    }
    fn nested(&self, repo: &str, toml: &str) {
        fs::write(self.root().join(repo).join(NESTED_FILE), toml).unwrap();
    }
    fn materialized(&self, public_only: bool, tops: &[(&str, &str)]) -> PathBuf {
        let mut s = String::new();
        if public_only {
            s.push_str("public_only = true\n");
        }
        for (name, vis) in tops {
            s.push_str(&format!(
                "subgraphs\nname = \"{name}\"\npath = {:?}\nvisibility = \"{vis}\"\nmenu = true\n",
                self.root().join(name)
            ));
        }
        let p = self.root().join("materialized.toml");
        fs::write(&p, s).unwrap();
        p
    }
}

fn names(decls: &[SubgraphDecl]) -> Vec<String> {
    decls.iter().map(|d| d.name.clone()).collect()
}

#[test]
fn nested_children_mount_under_their_parent_as_sibling_checkouts() {
    let ws = Workspace::new();
    ws.repo("cyberia");
    ws.repo("cyberia-my");
    ws.repo("cve");
    ws.nested(
        "cyberia",
        "subgraph\nname = \"cyberia-my\"\nvisibility = \"public\"\n\nsubgraph\nname = \"cve\"\n",
    );
    let cfg = ws.materialized(false, &[("cyberia", "public")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(names(&decls), vec!["cyberia", "cyberia-my", "cve"]);
    let my = &decls[1];
    assert_eq!(my.mount, "cyberia/cyberia-my");
    assert_eq!(my.repo_path, ws.root().join("cyberia-my"));
    assert!(!my.is_private);
    assert!(!my.menu, "nested subgraphs stay out of the menu by default");
    assert_eq!(my.menu_order, None);
    assert_eq!(decls[2].mount, "cyberia/cve");
}

#[test]
fn a_tree_goes_as_deep_as_its_files_and_accepts_both_table_names() {
    let ws = Workspace::new();
    ws.repo("a");
    ws.repo("b");
    ws.repo("c");
    ws.nested("a", "subgraph\nname = \"b\"\n");
    ws.nested("b", "subgraphs\nname = \"c\"\n");
    let cfg = ws.materialized(false, &[("a", "public")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(names(&decls), vec!["a", "b", "c"]);
    assert_eq!(decls[2].mount, "a/b/c");
}

#[test]
fn an_explicit_parent_in_the_same_file_nests_deeper() {
    let ws = Workspace::new();
    ws.repo("a");
    ws.repo("b");
    ws.repo("c");
    ws.nested(
        "a",
        "subgraph\nname = \"b\"\n\nsubgraph\nname = \"c\"\nparent = \"b\"\n",
    );
    let cfg = ws.materialized(false, &[("a", "public")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(decls[2].mount, "a/b/c");
}

#[test]
fn explicit_mount_and_path_win() {
    let ws = Workspace::new();
    ws.repo("a");
    let inner = ws.root().join("a").join("docs").join("inner");
    fs::create_dir_all(&inner).unwrap();
    ws.repo("elsewhere");
    ws.nested(
        "a",
        "subgraph\nname = \"inner\"\npath = \"docs/inner\"\nmount = \"a/deep/inner\"\n\nsubgraph\nname = \"far\"\npath = \"../elsewhere\"\n",
    );
    let cfg = ws.materialized(false, &[("a", "public")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(decls[1].mount, "a/deep/inner");
    assert_eq!(decls[1].repo_path, inner.canonicalize().unwrap());
    assert_eq!(decls[2].name, "far");
    assert_eq!(decls[2].repo_path, ws.root().join("elsewhere"));
    assert_eq!(decls[2].mount, "a/far");
}

#[test]
fn privacy_is_inherited_and_public_only_drops_private_and_local_only() {
    let ws = Workspace::new();
    ws.repo("pub");
    ws.repo("secret");
    ws.repo("local");
    ws.repo("open");
    ws.repo("priv");
    ws.repo("child-of-priv");
    ws.nested(
        "pub",
        "subgraph\nname = \"secret\"\nvisibility = \"private\"\n\nsubgraph\nname = \"local\"\nlocal-only = true\n\nsubgraph\nname = \"open\"\n",
    );
    ws.nested("priv", "subgraph\nname = \"child-of-priv\"\nvisibility = \"public\"\n");

    let cfg = ws.materialized(false, &[("pub", "public"), ("priv", "private")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(
        names(&decls),
        vec!["pub", "priv", "secret", "local", "open", "child-of-priv"]
    );
    let by = |n: &str| decls.iter().find(|d| d.name == n).unwrap();
    assert!(by("secret").is_private);
    assert!(!by("local").is_private);
    assert!(by("child-of-priv").is_private, "a private parent makes its children private");
    let private = load_private_names(&cfg);
    assert!(private.contains("secret") && private.contains("priv") && private.contains("child-of-priv"));
    assert!(!private.contains("open"));

    // A public build: the private root goes too, with the child under it.
    let cfg = ws.materialized(true, &[("pub", "public"), ("priv", "private")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(names(&decls), vec!["pub", "open"]);
    assert!(load_private_names(&cfg).is_empty());
}

#[test]
fn archived_orphan_self_missing_duplicate_and_cycles_are_skipped() {
    let ws = Workspace::new();
    ws.repo("a");
    ws.repo("b");
    ws.repo("dup");
    ws.nested(
        "a",
        concat!(
            "subgraph\nname = \"a\"\n",                        // the census line for itself
            "subgraph\nname = \"old\"\narchived = true\n",     // archived: no checkout needed
            "subgraph\nname = \"gone\"\norphan = true\n",
            "subgraph\nname = \"never-cloned\"\n",             // no checkout
            "subgraph\nname = \"b\"\n",
            "subgraph\nname = \"dup\"\n",
            "subgraph\nname = \"dup\"\n",                      // declared twice
            "subgraph\nname = \"also-a\"\npath = \"../a\"\n", // the same checkout again
        ),
    );
    ws.nested("b", "subgraph\nname = \"a\"\n"); // b claims a: a cycle
    let cfg = ws.materialized(false, &[("a", "public")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(names(&decls), vec!["a", "b", "dup"]);
}

#[test]
fn a_broken_nested_file_is_a_warning_not_a_failure() {
    let ws = Workspace::new();
    ws.repo("a");
    ws.nested("a", "[[subgraph]\nname = ");
    let cfg = ws.materialized(false, &[("a", "public")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(names(&decls), vec!["a"]);
}

#[test]
fn root_entries_are_unchanged_without_nested_files() {
    let ws = Workspace::new();
    ws.repo("solo");
    let cfg = ws.materialized(false, &[("solo", "private")]);
    let decls = load(&cfg).unwrap();
    assert_eq!(decls.len(), 1);
    assert_eq!(decls[0].mount, "solo");
    assert!(decls[0].is_private);
    assert!(decls[0].menu);
}

#[test]
fn the_walk_stops_at_max_depth_however_the_mounts_read() {
    // Every child sets a flat mount of its own, so nothing in the mount
    // string says how deep its subgraph sits: the depth is the walk's.
    let ws = Workspace::new();
    let deepest = MAX_DEPTH + 2;
    for i in 0..=deepest {
        ws.repo(&format!("r{i}"));
    }
    for i in 0..deepest {
        ws.nested(
            &format!("r{i}"),
            &format!("subgraph\nname = \"r{n}\"\nmount = \"r{n}\"\n", n = i + 1),
        );
    }
    let cfg = ws.materialized(false, &[("r0", "public")]);
    let decls = load(&cfg).unwrap();
    let expected: Vec<String> = (0..=MAX_DEPTH).map(|i| format!("r{i}")).collect();
    assert_eq!(names(&decls), expected, "r{} sits at depth {} and is skipped", MAX_DEPTH + 1, MAX_DEPTH + 1);
}

#[test]
fn an_explicit_parent_cannot_reach_past_max_depth() {
    // The depth is the parent's, not the declaring file's: the file of a
    // subgraph at depth 7 may name r8 as the parent of one more entry, and
    // that one sits deeper than the limit and is refused.
    let ws = Workspace::new();
    for i in 0..=MAX_DEPTH {
        ws.repo(&format!("r{i}"));
    }
    ws.repo("late");
    for i in 0..MAX_DEPTH {
        ws.nested(&format!("r{i}"), &format!("subgraph\nname = \"r{}\"\n", i + 1));
    }
    ws.nested(
        &format!("r{}", MAX_DEPTH - 1),
        &format!(
            "subgraph\nname = \"r{d}\"\n\nsubgraph\nname = \"late\"\nparent = \"r{d}\"\n",
            d = MAX_DEPTH
        ),
    );
    let cfg = ws.materialized(false, &[("r0", "public")]);
    let decls = load(&cfg).unwrap();
    let loaded = names(&decls);
    assert!(loaded.contains(&format!("r{MAX_DEPTH}")), "the chain reaches depth {MAX_DEPTH}");
    assert!(!loaded.contains(&"late".to_string()));
}

Homonyms

soft3/crate/src/genesis/tests.rs
cyb/honeycrisp/aruminium/src/tests.rs
neural/trident/src/deploy/tests.rs
neural/trident/src/neural/tests.rs
soft3/crate/src/wallet/tests.rs
warriors/trisha/rs/lower/tests.rs
soft3/lens/cli/src/tests.rs
soft3/radio/iroh-blobs/src/tests.rs
soft3/zheng/rs/src/machine/tests.rs
soft3/zheng/rs/src/envelope/tests.rs
neural/trident/src/lsp/util/tests.rs
soft3/cybernode/rs/src/genesis/tests.rs
soft3/zheng/rs/src/accumulate/tests.rs
soft3/radio/quinn/quinn-udp/tests/tests.rs
neural/trident/src/syntax/grammar/tests.rs
cyb/wysm/crates/wasmi/src/tests.rs
neural/trident/src/verify/smt/tests.rs
neural/trident/src/lsp/semantic/tests.rs
soft3/cybernode/rs/src/wallet/tests.rs
soft3/radio/cyber-bao/src/tree/tests.rs
neural/trident/src/config/resolve/tests.rs
neural/trident/src/syntax/lexer/tests.rs
neural/trident/src/syntax/format/tests.rs
neural/trident/src/verify/solve/tests.rs
soft3/zheng/rs/src/air/tests.rs
soft3/crate/src/chain/pool/tests.rs
neural/trident/src/verify/report/tests.rs
cyb/wysm/crates/cli/src/tests.rs
neural/trident/src/config/scaffold/tests.rs
neural/trident/src/package/hash/tests.rs
neural/trident/src/package/store/tests.rs
cyb/honeycrisp/unimem/src/block/tests.rs
cyb/honeycrisp/aruminium/src/render/tests.rs
soft3/crate/src/chain/economy/tests.rs
soft3/radio/quinn/quinn/src/tests.rs
warriors/trisha/rs/cost/stack_verifier/tests.rs
neural/trident/src/package/registry/tests.rs
neural/trident/src/verify/equiv/tests.rs
neural/trident/src/verify/sym/tests.rs
neural/trident/src/package/manifest/tests.rs
soft3/radio/cyber-bao/src/group/tests.rs
cyb/wysm/crates/collections/src/tests.rs
neural/trident/src/verify/synthesize/tests.rs
warriors/trisha/rs/lower/legalize/tests.rs
soft3/zheng/rs/src/recursion/circuit/tests.rs
warriors/erga/rs/rtable-bench/src/probe/tests.rs
neural/trident/src/neural/data/tir_graph/tests.rs
warriors/erga/rs/mine-bench/src/owner/tests.rs
neural/trident/src/ir/tir/stack/tests.rs
soft3/zheng/rs/src/recursion/wrap/tests.rs
cyb/wysm/crates/core/src/memory/tests.rs
soft3/zheng/rs/src/execution/tagged/tests.rs
soft3/zheng/rs/src/ccs/universal/tests.rs
cyb/wysm/crates/core/src/table/tests.rs
soft3/zheng/rs/src/recursion/whir/tests.rs
cyb/wysm/crates/wasmi/src/instance/tests.rs
warriors/erga/rs/blake-bench/src/batch/tests.rs
warriors/erga/rs/mine-bench/src/texture_ffi/tests.rs
cyb/evy/forks/bevy_ecs/src/bundle/tests.rs
soft3/cybernode/rs/src/chain/pool/tests.rs
neural/trident/src/ir/tir/optimize/tests.rs
soft3/zheng/rs/src/execution/veil/tests.rs
warriors/erga/rs/rtable-bench/src/table/tests.rs
soft3/zheng/rs/src/execution/zk/tests.rs
soft3/cybernode/rs/src/chain/economy/tests.rs
soft3/crate/src/chain/net/kernel/tests.rs
soft3/radio/quinn/quinn-proto/src/range_set/tests.rs
soft3/bbg/rs/src/storage/application/transfer/tests.rs
cyb/wysm/crates/wasmi/src/engine/limits/tests.rs
soft3/zheng/rs/src/execution/disclosed/evaluation/tests.rs
soft3/cybernode/rs/src/chain/net/kernel/tests.rs
cyb/evy/forks/naga/src/front/wgsl/tests.rs
cyb/wysm/crates/wasmi/src/module/instantiate/tests.rs
soft3/zheng/rs/src/execution/veil/hiding/tests.rs
soft3/zheng/rs/src/execution/disclosed/stream/tests.rs
warriors/erga/rs/mine-bench/src/cli/timed/tests.rs
soft3/zheng/rs/src/execution/disclosed/memory/tests.rs

Graph