Cotton code
52 lines · 2.9 KB · RustRaw
use super::*;use crate::mcp::protocol::{grants, listed_to, TOOLS};fn scopes(words: &[&str]) -> Vec<String> {    words.iter().map(|w| w.to_string()).collect()}/// RULE: A lane's token is offered and admitted every read tool and no write but its session log, whatever else its row carries — a lane outside the owner's login reads the owner's decisions and procedures and cannot change them (architect, 15 Oct 2026; the Work lane row it once wrote left Cotton, 27 Oct 2026).#[test]fn a_lane_token_reads_everything_and_writes_only_its_session_log() {    for words in [&["read", "lane:synthetic"][..], &["read", "write", "machines", "lane:synthetic"][..]] {        let lane = scopes(words);        for tool in TOOLS.iter() {            let door_tool = tool.name == "log_session" && tool.scope == McpScope::Write;            if tool.scope == McpScope::Read {                assert!(grants(&lane, tool.scope), "{} is a read the lane is refused", tool.name);                assert!(listed_to(&lane, tool, true), "{} is a read not offered to a lane", tool.name);                assert!(!may_write(&lane, tool), "{} gets a writer", tool.name);                continue;            }            assert!(!grants(&lane, tool.scope), "{} is granted to a lane", tool.name);            assert_eq!(listed_to(&lane, tool, true), door_tool, "{} listing", tool.name);            assert_eq!(may_write(&lane, tool), door_tool, "{} writer", tool.name);            match door(&lane, tool) {                Ok(open) => assert!(door_tool && open, "{} is a write a lane is not refused", tool.name),                Err(refusal) => {                    assert!(!door_tool, "{} is refused to a lane", tool.name);                    assert!(refusal.to_string().contains("writes only its session log"), "{refusal}");                }            }        }    }    for brief in ["brief", "get_project", "get_record", "recall"] {        let tool = TOOLS.iter().find(|t| t.name == brief).expect("the catalogue holds it");        assert_eq!(tool.scope, McpScope::Read, "{brief} must stay a read for lanes to reach it");    }    // Not a lane: the door says nothing and the scope check decides.    let create = TOOLS.iter().find(|t| t.name == "create_record").expect("catalogued");    assert_eq!(door(&scopes(&["read", "write"]), create).ok(), Some(false));}/// RULE: A lane word names one lane in the launcher's alphabet, so no scope word can smuggle a pattern, a path or a second lane.#[test]fn a_lane_word_names_one_well_formed_lane() {    assert_eq!(lane_scope("lanekeychain").as_deref(), Some("lane:lanekeychain"));    for bad in ["", "a b", "a/b", "*", "a:b", &"x".repeat(61)] {        assert!(lane_scope(bad).is_none(), "{bad:?}");        assert!(!is_lane_scope(&format!("{LANE_SCOPE_PREFIX}{bad}")), "{bad:?}");    }    assert_eq!(lane_of(&scopes(&["read", "lane:a b", "lane:ok"])), Some("ok"));    assert_eq!(lane_of(&scopes(&["read", "write"])), None);}