multicast-workflows 2.0.0

Durable Code Mode workflows attached to multicast CLIs as first-party commands
Documentation
//! Startup checks performed when workflows attach to a CLI.
#![cfg(all(feature = "native", not(target_arch = "wasm32")))]

use multicast::cli::Cli;
use multicast::command::{CommandContext, CommandDef, CommandHandler};
use multicast::output::CommandResult;
use multicast_workflows::Workflows;
use serde_json::{Value, json};

struct Echo;

#[async_trait::async_trait]
impl CommandHandler for Echo {
    async fn run(&self, _context: CommandContext) -> CommandResult {
        CommandResult::Ok {
            data: Value::Null,
            cta: None,
            exit_code: None,
        }
    }
}

fn cli() -> Cli {
    Cli::create("tool").command("echo", CommandDef::build("echo", Echo).done())
}

fn empty_input() -> Value {
    json!({"type": "object", "properties": {}, "additionalProperties": false})
}

fn attach(workflows: Workflows) -> Result<Cli, String> {
    workflows
        .store_path("/nonexistent/never-opened.sqlite")
        .attach(cli())
}

#[test]
fn accepts_a_workflow_that_calls_existing_commands() {
    let attached = attach(Workflows::new().shipped(
        "twice",
        "Echo twice",
        empty_input(),
        "async () => { await tool.echo({}); return tool['echo']({}); }",
    ));
    assert!(attached.is_ok(), "{:?}", attached.err());
}

#[test]
fn rejects_a_call_to_a_missing_command() {
    for code in [
        "async () => tool.missing({})",
        "async () => tool[\"missing\"]({})",
    ] {
        let error = attach(Workflows::new().shipped("broken", "Broken", empty_input(), code))
            .err()
            .expect("missing command accepted");
        assert!(error.contains("tool.missing"), "{error}");
    }
}

#[test]
fn rejects_a_name_that_collides_with_a_command() {
    for name in ["echo", "workflows"] {
        let error =
            attach(Workflows::new().shipped(name, "Collides", empty_input(), "async () => null"))
                .err()
                .expect("collision accepted");
        assert!(error.contains("collides"), "{error}");
    }
}

#[test]
fn rejects_a_workflow_shipped_twice() {
    let workflow =
        |workflows: Workflows| workflows.shipped("same", "Same", empty_input(), "async () => null");
    let error = attach(workflow(workflow(Workflows::new())))
        .err()
        .expect("duplicate accepted");
    assert!(error.contains("shipped twice"), "{error}");
}

#[test]
fn rejects_unsupported_or_non_object_schemas() {
    for schema in [
        json!({"type": "string"}),
        json!({"type": "object", "patternProperties": {"x": {}}}),
    ] {
        assert!(
            attach(Workflows::new().shipped("odd", "Odd", schema.clone(), "async () => null"))
                .is_err(),
            "{schema}"
        );
    }
}