liminal-server 0.24.9

Standalone server for the liminal messaging bus
Documentation
use super::binding;
use super::test_support::{TestResult, session};
use crate::auth_lys::HotAction;
use crate::auth_lys::LysPassConsumer;

#[test]
fn the_verified_agent_cannot_claim_another_inbox_even_with_its_subject_grant() -> TestResult {
    let pass = session("a")?;
    assert_eq!(
        binding::check(Some(pass.as_ref()), HotAction::Subscribe, "agents.b.inbox")
            .err()
            .ok_or("foreign inbox admitted")?
            .to_string(),
        "pass holder a is not agent b"
    );
    binding::check(Some(pass.as_ref()), HotAction::Subscribe, "agents.a.inbox")?;
    binding::decide(
        Some(pass.as_ref()),
        "fixture-network",
        HotAction::Subscribe,
        "agents.a.inbox",
        super::test_support::now()? * 1000,
    )?;
    Ok(())
}

#[test]
fn no_agent_even_its_owner_can_publish_server_presence_or_state() -> TestResult {
    let pass = session("a")?;
    for leaf in ["presence", "state"] {
        let subject = format!("agents.a.{leaf}");
        assert_eq!(
            binding::check(Some(pass.as_ref()), HotAction::Publish, &subject)
                .err()
                .ok_or("server subject admitted")?
                .to_string(),
            format!("agent_subject_server_owned: {subject}")
        );
    }
    for leaf in ["stream", "plan"] {
        assert_eq!(
            binding::check(
                Some(pass.as_ref()),
                HotAction::Publish,
                &format!("agents.b.{leaf}")
            )
            .err()
            .ok_or("foreign subject admitted")?
            .to_string(),
            "pass holder a is not agent b"
        );
    }
    Ok(())
}

#[test]
fn a_verified_identity_does_not_replace_the_pass_subject_grant() -> TestResult {
    let pass = session("a")?;
    assert!(
        binding::decide(
            Some(pass.as_ref()),
            "fixture-network",
            HotAction::Publish,
            "agents.c.inbox",
            super::test_support::now()? * 1000
        )
        .is_err()
    );
    assert_eq!(
        binding::check(None, HotAction::Subscribe, "agents.a.inbox")
            .err()
            .ok_or("unverified inbox admitted")?
            .to_string(),
        "pass holder unverified is not agent a"
    );
    Ok(())
}

#[test]
fn an_agents_creation_counts_against_its_verified_responsible_person() -> TestResult {
    let (keys, token) = super::test_support::signed_pass("creator-agent")?;
    let adapter = crate::auth_lys::lys_pass_adapter::LysPassAdapter::from_keys_json(
        &keys,
        "https://agent.fixture".into(),
    )?;
    let verified =
        adapter.verify_offline(token.as_bytes(), "liminal", super::test_support::now()?)?;
    let act = super::records::AgentAct::prepare(
        "builder".into(),
        super::records::AgentEvent::Create(super::records::AgentSpec {
            harness: super::records::AgentHarness::Cdx,
            purpose: "Build one service.".into(),
            service: None,
        }),
        verified.verified(),
        1,
    )?;
    assert_eq!(act.holder, "creator-agent");
    assert_eq!(act.owner, "fixture-owner");
    Ok(())
}