surjection-core 1.1.0

Core library of Surjection: MCP servers, skills and secret references for AI coding clients
Documentation
use super::*;
use crate::apply::journal::JOURNAL_SCHEMA;
use crate::apply::ledger::LEDGER_SCHEMA;
use crate::apply::unmanaged::SEEN_SCHEMA;
use crate::layering::trust::TRUST_SCHEMA;
use crate::store::merge::MERGE_STATE_SCHEMA;

fn kind<'r>(registry: &'r Registry, name: &str) -> &'r Kind {
    registry.kind(name).expect("kind exists")
}

/// Each kind's current version is the constant its module reads and
/// writes: bumping one without a migration step (or the reverse) fails here.
#[test]
fn builtin_matches_module_constants() {
    let registry = Registry::builtin();
    let expected = [
        ("store", SCHEMA_VERSION),
        ("machine", SCHEMA_VERSION),
        ("local-settings", SCHEMA_VERSION),
        ("remotes", SCHEMA_VERSION),
        ("remote-status", SCHEMA_VERSION),
        ("scan-rules", i64::from(crate::scan::rules::SCHEMA)),
        ("ledger", i64::from(LEDGER_SCHEMA)),
        ("trust", i64::from(TRUST_SCHEMA)),
        ("unmanaged-seen", i64::from(SEEN_SCHEMA)),
        ("merge-state", i64::from(MERGE_STATE_SCHEMA)),
        ("journal", i64::from(JOURNAL_SCHEMA)),
        ("eject", i64::from(crate::eject::EJECT_SCHEMA)),
    ];
    assert_eq!(registry.kinds().len(), expected.len());
    for (name, version) in expected {
        assert_eq!(kind(&registry, name).current(), version, "{name}");
    }
}

#[test]
fn kinds_match_their_paths() {
    let registry = Registry::builtin();
    let of = |area, path: &str| registry.kind_of(area, path).map(|kind| kind.name);
    assert_eq!(of(Area::Store, "loadout.toml"), Some("store"));
    assert_eq!(of(Area::Store, "servers/github.toml"), Some("store"));
    assert_eq!(
        of(Area::Store, "projects/p1/project.local.toml"),
        Some("store")
    );
    assert_eq!(of(Area::Store, "machines/01J.applied.toml"), Some("store"));
    assert_eq!(of(Area::Store, "skills/demo/SKILL.md"), None);
    assert_eq!(of(Area::Store, "ledger.json"), None);
    assert_eq!(of(Area::State, "ledger.json"), Some("ledger"));
    assert_eq!(of(Area::State, "machine.toml"), Some("machine"));
    assert_eq!(of(Area::State, "journal/01J.json"), Some("journal"));
    assert_eq!(of(Area::State, "applies/01J.json"), Some("journal"));
    assert_eq!(of(Area::State, "applies/01J.json.tmp"), None);
    assert_eq!(
        of(Area::State, "merge/MERGE_STATE.json"),
        Some("merge-state")
    );
    assert_eq!(of(Area::State, "ledger.key"), None);
    assert_eq!(of(Area::State, "surjection.lock"), None);
}

fn add_marker(_: &str, doc: &mut Doc) -> Result<(), CoreError> {
    match doc {
        Doc::Toml(doc) => {
            doc.insert("migrated", toml_edit::value(true));
        }
        Doc::Json(value) => {
            value["migrated"] = serde_json::Value::Bool(true);
        }
    }
    Ok(())
}

#[test]
fn upgrade_keeps_bom_crlf_and_comments() {
    let registry = Registry::builtin().with_step(
        "store",
        Step {
            rewrite: add_marker,
        },
    );
    let store = kind(&registry, "store");
    assert_eq!(store.current(), 2);
    let text = "\u{feff}# kept\r\nschema = 1 # the version\r\nid = \"a\"\r\n";
    let out = upgrade(store, "servers/a.toml", text.as_bytes(), 1).unwrap();
    assert_eq!(
        out,
        "\u{feff}# kept\r\nschema = 2 # the version\r\nid = \"a\"\r\nmigrated = true\r\n"
    );
    assert_eq!(read_version(Format::Toml, &out), Some(2));
}

#[test]
fn upgrade_json_sets_the_version() {
    let registry = Registry::builtin().with_step(
        "trust",
        Step {
            rewrite: add_marker,
        },
    );
    let trust = kind(&registry, "trust");
    let out = upgrade(
        trust,
        "trust.json",
        b"{\"schema\": 1, \"entries\": []}\n",
        1,
    )
    .unwrap();
    let value: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(value["schema"], 2);
    assert_eq!(value["migrated"], true);
    assert!(out.ends_with("}\n"));
}

#[test]
fn a_missing_step_is_an_error_not_a_silent_skip() {
    let registry = Registry::builtin();
    let err = upgrade(kind(&registry, "store"), "loadout.toml", b"schema = 0\n", 0).unwrap_err();
    assert_eq!(err.code(), ErrorCode::UnsupportedSchema);
}

#[test]
fn incoming_files_newer_than_this_build_are_refused() {
    let files = [
        ("skills/demo/SKILL.md", b"schema = 9\n".as_slice()),
        ("servers/a.toml", b"schema = 1\n".as_slice()),
    ];
    check_store_files(files, "The remote \"backup\"").unwrap();
    let files = [("servers/a.toml", b"schema = 2\nid = \"a\"\n".as_slice())];
    let err = check_store_files(files, "The remote \"backup\"").unwrap_err();
    assert_eq!(err.code(), ErrorCode::UnsupportedSchema);
    assert_eq!(
        err.message(),
        "The remote \"backup\" holds data written by a newer Surjection (schema 2; this version \
         reads up to 1). Update Surjection."
    );
}