aion-server 0.31.0

Aion workflow server library: HTTP, gRPC, WebSocket, and worker endpoints. Run it with the `aion` binary from the aion-cli crate.
Documentation
use super::{HarnessQueueError, harness_queues};

type TestResult = Result<(), Box<dyn std::error::Error>>;

/// The document these tests read and cut about.
///
/// A FIXTURE, and deliberately so: the server ships no document of its own, and
/// what this reader must be held against is a document carrying the shape it
/// keys on — a `harness` section with every required setting, one bodyless
/// `agent` action, and one action with a declared body. Every mutilation below
/// is cut from THIS text, so a refusal and its control differ in exactly the
/// edit under test. It is the same fixture `provision_tests` deploys.
const FIXTURE_DOCUMENT: &str = include_str!("fixtures/harness_document.awl");

/// The queue the fixture's `worker` block declares.
const FIXTURE_QUEUE: &str = "demo_agent";

/// The opening of the harness section, as the fixture writes it.
const HARNESS_BLOCK: &str = "  harness\n";

/// The fixture's `harness` section, verbatim, as the tests below cut it about.
fn fixture_harness() -> Result<(usize, usize), Box<dyn std::error::Error>> {
    let start = FIXTURE_DOCUMENT
        .find(HARNESS_BLOCK)
        .ok_or("the fixture must carry a harness section")?;
    let end = FIXTURE_DOCUMENT[start..]
        .find("\n  action ")
        .map(|offset| start.saturating_add(offset).saturating_add(1))
        .ok_or("the harness section must be followed by an action")?;
    Ok((start, end))
}

/// 🔴 THE NEGATIVE CONTROL. Auto-provision keys on the `harness` section, not
/// on deploys. The SAME document with its section removed declares no queue —
/// nothing to mint, and no error either.
#[test]
fn a_document_with_no_harness_section_declares_no_queue() -> TestResult {
    let (start, end) = fixture_harness()?;
    let mut stripped = String::from(&FIXTURE_DOCUMENT[..start]);
    stripped.push_str(&FIXTURE_DOCUMENT[end..]);
    assert!(
        harness_queues(&stripped)?.is_empty(),
        "a document with no harness section must mint nothing"
    );
    Ok(())
}

/// The control for the control: the UNMODIFIED document does declare a queue,
/// and its launch compiles — so the empty answer above is the section's
/// absence rather than a reader that never finds anything.
#[test]
fn a_declaring_document_states_its_queue_and_its_launch_compiles() -> TestResult {
    let queues = harness_queues(FIXTURE_DOCUMENT)?;
    let queue = queues.first().ok_or("the fixture's queue must be read")?;
    assert_eq!(queues.len(), 1);
    assert_eq!(queue.task_queue, FIXTURE_QUEUE);
    assert!(queue.harness.concurrency > 0);
    Ok(())
}

/// An incomplete section is a REFUSAL naming its queue, not a silent skip: a
/// record minted from it would replay an argv the worker refuses on every
/// restart until the crash-loop budget parked it.
#[test]
fn an_incomplete_section_refuses_and_names_its_queue() -> TestResult {
    let (start, end) = fixture_harness()?;
    let section = &FIXTURE_DOCUMENT[start..end];
    let concurrency = section
        .lines()
        .find(|line| line.trim_start().starts_with("concurrency "))
        .ok_or("the fixture's section must declare a concurrency")?;
    let maimed = FIXTURE_DOCUMENT.replace(&format!("{concurrency}\n"), "");
    match harness_queues(&maimed) {
        Err(HarnessQueueError::Harness {
            task_queue,
            message,
        }) => {
            assert_eq!(task_queue, FIXTURE_QUEUE);
            assert!(message.contains("concurrency"), "{message}");
            Ok(())
        }
        other => Err(format!("an incomplete section must refuse: {other:?}").into()),
    }
}

/// A queue whose every action carries a declared body has nothing an agent
/// could be dispatched — the server runs those from the deployed contract — so
/// no worker is minted, and the refusal names the actions that shut it out.
#[test]
fn a_queue_of_only_bodied_actions_has_nothing_to_serve() -> TestResult {
    let agent_action = "  action demo_round(prompt: String) -> Reply\n    agent\n";
    assert!(
        FIXTURE_DOCUMENT.contains(agent_action),
        "the fixture must declare its agent action in the expected shape"
    );
    let bodyless = FIXTURE_DOCUMENT.replace(agent_action, "");
    match harness_queues(&bodyless) {
        Err(HarnessQueueError::NoServiceableAction {
            task_queue,
            actions,
        }) => {
            assert_eq!(task_queue, FIXTURE_QUEUE);
            assert!(actions.contains("demo_provision"), "{actions}");
            Ok(())
        }
        other => Err(format!("an all-bodied queue must refuse: {other:?}").into()),
    }
}

/// Source that does not parse is reported as such, not as "declares nothing".
/// Those two answers lead an operator to opposite acts, and the archived
/// document is provenance — a package can load while carrying source that no
/// longer parses.
#[test]
fn unparseable_source_is_a_parse_refusal_not_an_empty_answer() -> TestResult {
    match harness_queues("this is not an AWL document") {
        Err(HarnessQueueError::Parse { .. }) => Ok(()),
        other => Err(format!("unparseable source must refuse: {other:?}").into()),
    }
}