clm-kernel 0.0.21

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Rust port of `test_core_type_system.py` — CLM Type System universe levels and registry loading.
//!
//! Mirrors the Python test assertions:
//!   1. Grammar standard compliance for all type manifests
//!   2. Type registry loading and resolution
//!   3. Universe level stratification verification (U0=string/integer, U1=a_mcard/b_mcard, U2=agent/clmEval)

use clm_rust_core::type_lattice::{default_lattice, TypeLattice};

#[test]
fn test_registry_loads_types() {
    let lattice = default_lattice();
    // Python test expects 13+ types loaded. The default_lattice embeds a curated subset.
    // Verify the key types are present.
    assert!(lattice.registry.len() >= 8, "registry should have at least 8 types, got {}", lattice.registry.len());
}

#[test]
fn test_resolve_a_mcard() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("a_mcard");
    assert!(info.is_some(), "resolve_type_path 'a_mcard' succeeds");
    let info = info.unwrap();
    assert!(!info.path.is_empty(), "a_mcard has a path");
    assert!(!info.schema.is_null(), "a_mcard has a schema");
}

#[test]
fn test_resolve_integer() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("integer");
    assert!(info.is_some(), "resolve_type_path 'integer' succeeds");
    let info = info.unwrap();
    assert!(!info.path.is_empty(), "integer has a path");
    assert!(!info.schema.is_null(), "integer has a schema");
}

#[test]
fn test_resolve_agent() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("agent");
    assert!(info.is_some(), "resolve_type_path 'agent' succeeds");
    let info = info.unwrap();
    assert!(!info.path.is_empty(), "agent has a path");
    assert!(!info.schema.is_null(), "agent has a schema");
}

#[test]
fn test_string_is_level_0() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("string").unwrap();
    assert_eq!(info.universe_level, 0, "string is Level 0");
}

#[test]
fn test_integer_is_level_0() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("integer").unwrap();
    assert_eq!(info.universe_level, 0, "integer is Level 0");
}

#[test]
fn test_a_mcard_is_level_1() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("a_mcard").unwrap();
    assert_eq!(info.universe_level, 1, "a_mcard is Level 1");
}

#[test]
fn test_b_mcard_is_level_1() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("b_mcard").unwrap();
    assert_eq!(info.universe_level, 1, "b_mcard is Level 1");
}

#[test]
fn test_agent_is_level_2() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("agent").unwrap();
    assert_eq!(info.universe_level, 2, "agent is Level 2");
}

#[test]
fn test_clmeval_is_level_2() {
    let lattice = default_lattice();
    let info = lattice.get_type_info("clmEval").unwrap();
    assert_eq!(info.universe_level, 2, "clmEval is Level 2");
}

#[test]
fn test_stratification_u2_nested_in_u1_fails() {
    let lattice = default_lattice();
    // agent (U2) nested inside a U1 context should fail
    let result = lattice.validate("agent", r#"{"did":"did:key:z123","type":"EOA"}"#, 1);
    assert!(result.is_err(), "U2 type nested in U1 context should fail stratification");
}

#[test]
fn test_stratification_u1_nested_in_u0_fails() {
    let lattice = default_lattice();
    // a_mcard (U1) nested inside a U0 context should fail
    let result = lattice.validate("a_mcard", r#"{"signature":{},"invariants":{}}"#, 0);
    assert!(result.is_err(), "U1 type nested in U0 context should fail stratification");
}

#[test]
fn test_stratification_u0_in_u2_succeeds() {
    let lattice = default_lattice();
    // string (U0) nested inside a U2 context should succeed
    let result = lattice.validate("string", "hello", 2);
    assert!(result.is_ok(), "U0 type nested in U2 context should pass stratification");
}

#[test]
fn test_type_lattice_is_serializable() {
    let lattice = default_lattice();
    // TypeLattice should be usable in a way that allows persistence
    // (the Sheaf module needs to serialize type info)
    let info = lattice.get_type_info("string").unwrap();
    let yaml = serde_yaml::to_string(&info.schema);
    assert!(yaml.is_ok(), "TypeInfo schema is serializable to YAML");
}

#[test]
fn test_all_six_subdirs_represented() {
    // The default_lattice embeds types from primitives, meta, and runtime.
    // A full load_registry would cover all 6 subdirs.
    // Verify the embedded types span at least 3 subdirs.
    let lattice = default_lattice();
    let has_primitives = lattice.get_type_info("string").is_some()
        && lattice.get_type_info("integer").is_some()
        && lattice.get_type_info("boolean").is_some();
    let has_meta = lattice.get_type_info("a_mcard").is_some()
        && lattice.get_type_info("b_mcard").is_some()
        && lattice.get_type_info("vcard_token").is_some();
    let has_runtime = lattice.get_type_info("agent").is_some()
        && lattice.get_type_info("clmEval").is_some();
    assert!(has_primitives, "primitives subdir types present");
    assert!(has_meta, "meta subdir types present");
    assert!(has_runtime, "runtime subdir types present");
}

#[test]
fn test_filesystem_load_registry() {
    // Test that load_registry works when given a valid clm_root.
    // The workspace root is 3 levels up from the crate root.
    let clm_root = std::env::var("CLM_ROOT").unwrap_or_else(|_| {
        let manifest_dir = env!("CARGO_MANIFEST_DIR");
        // kernel/clm_rust_core -> ../.. = kernel/, ../.. = workspace root
        // Use canonicalize to resolve the .. components
        let path = std::path::Path::new(manifest_dir)
            .join("../..");
        std::fs::canonicalize(&path)
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|_| path.to_string_lossy().to_string())
    });

    let mut lattice = TypeLattice::new();
    lattice.load_registry(&clm_root);

    // Should have loaded significantly more types than the embedded subset
    assert!(
        lattice.registry.len() >= 13,
        "filesystem load_registry should load 13+ types from {}, got {}",
        clm_root,
        lattice.registry.len()
    );

    // Spot-check types from different subdirs
    assert!(lattice.get_type_info("string").is_some(), "string loaded from primitives");
    assert!(lattice.get_type_info("a_mcard").is_some(), "a_mcard loaded from meta");
    assert!(lattice.get_type_info("agent").is_some(), "agent loaded from runtime");
}