liminal-server 0.25.4

Standalone server for the liminal messaging bus
Documentation
use super::{Crossing, LINK_CROSSING_REFUSED_CODE, publish_crossing};
use crate::link::LinkLife;
use crate::link_handshake::MachineIdentity;
use crate::server::connection::state::{ConnectionProcessState, FrameAction};
use crate::server::connection::supervisor::ConnectionSupervisor;
use liminal::protocol::{CausalContext, Frame, MessageEnvelope, SchemaId};

fn refused_import(subject: &str) -> Result<(FrameAction, LinkLife), crate::ServerError> {
    let supervisor = ConnectionSupervisor::new()?;
    let life = LinkLife::new(MachineIdentity::new([7; 32]));
    let crossing = Crossing {
        envelope: MessageEnvelope::new(
            SchemaId::new([0; 32]),
            CausalContext::independent(),
            b"{}".to_vec(),
        ),
        position: 1,
        link_id: "named-link".to_owned(),
        life: life.clone(),
    };
    let action = publish_crossing(
        0,
        &supervisor.runtime_for_test(),
        &ConnectionProcessState::default(),
        17,
        subject,
        &crossing,
    );
    supervisor.shutdown();
    Ok((action, life))
}

fn named_refusal(action: FrameAction, subject: &str) -> Result<(), crate::ServerError> {
    let FrameAction::Respond(Frame::PublishError {
        stream_id,
        reason_code,
        message,
        ..
    }) = action
    else {
        return Err(crate::ServerError::ListenerAccept {
            message: format!("import did not answer with PublishError: {action:?}"),
        });
    };
    assert_eq!(stream_id, 17);
    assert_eq!(
        reason_code, LINK_CROSSING_REFUSED_CODE,
        "named import refusal must settle the subject, not fault the bridge: {message:?}"
    );
    assert!(message.is_some_and(|reason| reason.contains(subject)));
    Ok(())
}

#[test]
fn reserved_namespace_import_answers_with_a_subject_refusal() -> Result<(), crate::ServerError> {
    let (action, life) = refused_import("liminal.foreign")?;
    named_refusal(action, "liminal.foreign")?;
    assert_eq!(
        life.reason()
            .map_err(|message| crate::ServerError::ListenerAccept { message })?,
        None
    );
    Ok(())
}

#[test]
fn unplaced_import_answers_with_a_subject_refusal() -> Result<(), crate::ServerError> {
    let (action, life) = refused_import("orders.absent")?;
    named_refusal(action, "orders.absent")?;
    assert_eq!(
        life.reason()
            .map_err(|message| crate::ServerError::ListenerAccept { message })?,
        None
    );
    Ok(())
}