supercode-harness 0.4.21

The optional native Supercode agent and tool harness
Documentation
//! ONT-2: the orchestration records' wire form is the orchestration's canonical JSON. The
//! proof is a real load: the codec reads the fixture home as a Hermes home,
//! the orchestration goes out as JSON, and JSON comes back as the same `Orchestration`.

use std::path::PathBuf;

use supercode_harness::ontology::Trigger;
use supercode_harness::orchestration::Orchestration;
use supercode_interchange::orchestration::codec::{canonical_json, load_home, Flavor};

fn repo() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..")
}

#[test]
fn hermes_fixture_home_deserializes_into_orchestration() {
    let home = repo().join("crates/harness/tests/fixtures/hermes_home");
    let loaded = load_home(&home, Flavor::Hermes).expect("the fixture home loads");
    let json = canonical_json(&serde_json::to_value(&loaded.orchestration).unwrap());
    let orchestration: Orchestration =
        serde_json::from_str(&json).expect("the canonical JSON is the orchestration's wire form");
    let names: Vec<&String> = orchestration.profiles.keys().collect();
    assert_eq!(names, vec!["coder", "default", "ops"]);
    let default = &orchestration.profiles["default"];
    assert_eq!(default.bindings.len(), 2);
    assert_eq!(orchestration.profiles["coder"].bindings.len(), 1);
    assert!(
        default.jobs.contains_key("coder-standup")
            || !orchestration.profiles["coder"].jobs.is_empty()
    );
    assert!(default.channels["telegram"]
        .credentials
        .contains_key("token"));
    // a Hermes session row names its source; the trigger is read from it
    let cron = default
        .bindings
        .values()
        .find(|b| b.key.platform.as_deref() == Some("cron"))
        .expect("the fixture's cron fire binding");
    assert_eq!(cron.trigger, Trigger::Cron);
    assert_eq!(
        cron.recurrence.as_ref().map(|r| r.job_id.as_str()),
        Some("job42")
    );
    // and the orchestration re-serializes to JSON that reads back equal
    let again: Orchestration =
        serde_json::from_str(&serde_json::to_string(&orchestration).unwrap()).unwrap();
    assert_eq!(again, orchestration);
}

#[test]
fn schema_names_every_record() {
    let schema = supercode_harness::orchestration::schema();
    let text = serde_json::to_string(&schema).unwrap();
    for record in [
        "Profile",
        "Binding",
        "Job",
        "Fire",
        "Obligation",
        "ChannelConfig",
        "Route",
        "Access",
        "WebhookSubscription",
        "SecretRef",
    ] {
        assert!(
            text.contains(&format!("\"{record}\"")),
            "{record} is a definition of the orchestration schema"
        );
    }
    for record in supercode_harness::orchestration::RECORDS {
        assert!(
            supercode_harness::orchestration::schema_for(record).is_some(),
            "{record}"
        );
    }
}