Cotton code
90 lines · 5.1 KB · RustRaw
/// RULE: Engine behavior comes from definition keys, never seeding provenance; changing who supplied a definition must not change which records dictation or the people engine reads.#[test]fn voice_and_people_engines_never_read_definition_provenance() {    let manifest = std::env::var("CARGO_MANIFEST_DIR").expect("Cargo supplies the manifest directory at run time");    let root = std::path::Path::new(&manifest).parent().unwrap();    let mut paths = vec![root.join("cotton-ffi/src/phone/voice_words.rs")];    for directory in ["cotton-core/src/records", "cotton-tools/src/mcp/handlers/tables"] {        paths.extend(std::fs::read_dir(root.join(directory)).unwrap().map(|entry| entry.unwrap().path())            .filter(|path| path.extension().is_some_and(|extension| extension == "rs")                && !path.file_name().unwrap().to_string_lossy().ends_with("_tests.rs")                && path.file_name().unwrap() != "definition_writes.rs"));    }    for path in paths {        let source = std::fs::read_to_string(&path).unwrap();        for line in source.split("#[cfg(test)]").next().unwrap().lines().filter(|line| !line.trim_start().starts_with("//")) {            for key in ["builtin", "shipped"] {                let field = line.contains(&format!(".{key}"));                let accessor = ["int", "text", "get", "get_mut", "remove"].iter()                    .any(|method| line.contains(&format!("{method}(\"{key}\"")))                    || line.contains(&format!("[\"{key}\"]"))                    || (line.contains(".columns(") && line.contains(&format!("\"{key}\"")));                let words: Vec<_> = line.split(|c: char| !c.is_ascii_alphanumeric() && c != '_').collect();                let sql = words.contains(&key) && words.iter().any(|word| matches!(*word, "SELECT" | "WHERE" | "AND"));                assert!(!field && !accessor && !sql, "{} reads provenance: {line}", path.display());            }        }    }}/// RULE: Native storage and verbs require this build's engine binding, never a provenance claim; an authored data-only table can reuse an id without acquiring another table's records.#[test]fn engine_access_ignores_provenance_and_refuses_invented_bindings() {    use cotton_core::{definitions::builtin_table_definition, records};    for id in ["person", "photo"] {        let mut definition = builtin_table_definition(id).unwrap().clone();        assert!(definition.has_engine());        let bin = records::receipt::lands_in_bin(&definition);        definition.builtin = false;        assert!(definition.has_engine());        assert_eq!(records::receipt::lands_in_bin(&definition), bin);        definition.engine = None;        assert!(!definition.has_engine());        definition.builtin = true;        assert!(!definition.has_engine());        definition.engine = Some("invented".into());        assert!(!definition.has_engine());    }}/// RULE: Provenance constrains engines only at the import/write boundary; reading an already validated native definition follows its binding, and arbitrary SQL never becomes trusted through a provenance claim.#[test]fn imports_check_origin_and_native_storage_ignores_served_sql() {    use cotton_core::definitions::builtin_table_definition;    use cotton_store::schema::package_tables::PackageIndex;    let mut definition = builtin_table_definition("todo").unwrap().clone();    assert!(definition.validate_import().is_ok());    definition.builtin = false;    assert!(definition.validate().is_ok());    assert!(definition.validate_import().is_err());    definition.builtin = true;    definition.tables.as_mut().unwrap()[0].indexes.push(PackageIndex::Legacy(        "CREATE INDEX injected ON cotton_devices (id)".into()));    // Native storage comes from the compiled binding, so storage text from    // another build is ignored rather than validated or executed.    assert!(definition.validate().is_ok());    assert!(definition.validate_import().is_ok());    let value = serde_json::to_value(&definition).unwrap();    assert!(cotton_store::schema::package_shape::declared(&value).unwrap().is_none());    // A data-only definition has no compiled binding to wall off its SQL.    definition.engine = None;    definition.builtin = false;    assert!(definition.validate().is_err());    assert!(definition.validate_import().is_err());}/// RULE: A package's physical home follows its compiled engine binding or declared data shape; provenance cannot hide a data-only table or redirect a native definition into another table.#[test]fn storage_discovery_never_uses_provenance() {    use cotton_core::definitions::builtin_table_definition;    use cotton_store::schema::package_shape::declared;    let mut native = serde_json::to_value(builtin_table_definition("todo").unwrap()).unwrap();    let mut made = serde_json::json!({"id":"synthetic_lesson","engine":null,"columns":[{"key":"name","type":"text"}]});    for origin in [true, false] {        native["builtin"] = origin.into();        made["builtin"] = origin.into();        assert!(declared(&native).unwrap().is_none());        let tables = declared(&made).unwrap().unwrap();        assert_eq!(tables[0].table.name, "cotton_synthetic_lesson");    }}