Cotton code
122 lines · 6.7 KB · RustRaw
//! RULE (build 4's walk, 22 Oct 2026): every act a kit definition declares//! in `commands` is reachable from the page — named in the page's acts as//! the core resolves them (`resolved_parts`: main, more and the one `away`),//! or in any other `acts` list the definition draws, or an act the app//! places by its own key (`sheet`: a field card's sheet; `list`: the list's//! head; `form`: the form's keep; a field edit, on its field's tap map; the//! table's `on_open`; another act's `undo`, offered on its receipt) — or it//! stands in [`UNLISTED`] with the reason it is not drawn. Without this, an//! act can be defined, gated and tested and still never appear: This//! Device's Add / Remove sample records were in `commands` but in no act//! list, and the walk found no way to press them.//!//! An act that puts the record away or destroys it is reachable only as the//! page's one `away` (coordinator, 2 Nov 2026): the shell draws that slot//! alone, so a second such act is drawn nowhere. The rule that counted every//! such act as drawn hid seven acts the page had lost.use cotton_core::definitions::TableDefinition;use serde_json::Value;use std::collections::BTreeSet;use std::path::PathBuf;/// `(table, command, why it is in no act list)`. May only shrink.const UNLISTED: &[(&str, &str, &str)] = &[    ("device", "open_devices", "in no act list and drawn by nothing today (22 Oct 2026); whether This Device lists it is the coordinator's call"),    // Drawn nowhere once the `away` rule stopped counting every put-away act    // as drawn (2 Nov 2026): the device page's one away is Erase (`erase_here`,    // this device's own row alone, since the sync card took Sign in, Sign out,    // Sync now and Erase). Where each goes (or whether it goes) is with the    // coordinator (fixer report).    ("device", "serve_from", "a Mac's act the shell's device page places nowhere; placement is the coordinator's call"),    ("device", "remove", "destroys another approved device's access, and the page's one away is Erase, which only this device's row offers; placement is the coordinator's call"),    ("device", "erase", "destroys, and the page's one away is Erase, which only this device's row offers; placement is the coordinator's call"),    ("device", "become_runner", "a Mac's act the shell's device page places nowhere; placement is the coordinator's call"),    ("device", "remove_samples", "the owner's Remove samples (22 Oct 2026) puts away, and the page's one away is Erase; placement is the coordinator's call"),];fn kits() -> PathBuf {    // Read at run time: a compile-time `env!` bakes the building machine's path    // into the binary (scripts/__tests__/manifest-dir-at-run-time.test.ts).    PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").expect("cargo sets the manifest directory")).join("kits")}/// Every table definition file the kit's tools carry — each table a personal/// kit tool names, its one definition in `kits/personal/tables` — and the/// shell's.fn table_files() -> Vec<PathBuf> {    let mut files = Vec::new();    let tools = std::fs::read_dir(kits().join("personal/tools")).expect("the kit's tools").map(|tool| tool.expect("an entry").path());    for tool in tools {        let Ok(manifest) = std::fs::read_to_string(tool.join("manifest.json")) else { continue };        let manifest: Value = serde_json::from_str(&manifest).expect("a manifest");        for table in manifest["tables"].as_array().into_iter().flatten().filter_map(Value::as_str) {            let path = kits().join("personal/tables").join(format!("{table}.json"));            if !files.contains(&path) {                files.push(path);            }        }    }    for entry in std::fs::read_dir(kits().join("shell/tables")).expect("the shell's tables") {        let path = entry.expect("an entry").path();        if path.extension().is_some_and(|ext| ext == "json") {            files.push(path);        }    }    files.sort();    assert!(!files.is_empty(), "the kit's table files were found");    files}/// Every string held anywhere under a key named `acts`.fn listed(value: &Value, under_acts: bool, out: &mut BTreeSet<String>) {    match value {        Value::String(id) if under_acts => {            out.insert(id.clone());        }        Value::Array(items) => items.iter().for_each(|item| listed(item, under_acts, out)),        Value::Object(map) => {            for (name, item) in map {                listed(item, under_acts || name == "acts", out);            }        }        _ => {}    }}#[test]fn every_defined_act_is_in_an_act_list_or_says_why_not() {    let mut missing = Vec::new();    let mut used = BTreeSet::new();    for path in table_files() {        let text = std::fs::read_to_string(&path).unwrap();        let file: Value = serde_json::from_str(&text).expect("a kit table parses");        let Some(table) = file.get("id").and_then(Value::as_str) else { continue };        let Some(commands) = file.get("commands").and_then(Value::as_array) else { continue };        let definition = TableDefinition::from_json(&text).unwrap_or_else(|error| panic!("{}: {error}", path.display()));        let mut reachable = BTreeSet::new();        listed(&file, false, &mut reachable);        // The page's acts as the core deals them, a page that ranks none included.        let acts = definition.resolved_parts().acts;        reachable.extend(acts.main.into_iter().chain(acts.more).chain(acts.away));        reachable.extend(file.get("on_open").and_then(Value::as_str).map(str::to_string));        reachable.extend(commands.iter().filter_map(|command| command.get("undo").and_then(Value::as_str)).map(str::to_string));        for command in commands {            let id = command["id"].as_str().expect("a command has an id");            let placed = ["sheet", "list", "form"].iter().any(|key| command.get(*key).is_some_and(|value| !value.is_null()))                || definition.command(id).is_some_and(|command| command.edits_field(&definition));            if reachable.contains(id) || placed {                continue;            }            match UNLISTED.iter().find(|(t, c, _)| *t == table && *c == id) {                Some(entry) => {                    used.insert((entry.0, entry.1));                }                None => missing.push(format!("{table}.{id} ({})", path.display())),            }        }    }    assert!(missing.is_empty(), "acts defined but in no act list — add each to the page's acts, or to UNLISTED with why:\n{}", missing.join("\n"));    let stale: Vec<_> = UNLISTED.iter().filter(|(t, c, _)| !used.contains(&(*t, *c))).map(|(t, c, _)| format!("{t}.{c}")).collect();    assert!(stale.is_empty(), "UNLISTED names acts that are now listed or gone; take them off: {stale:?}");}