Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

5.7 KB
Raw Blame
use super::*;
use crate::core::TypeSchema;
use serde_json::json;

#[tokio::test]
async fn channel_test_handler_requires_module_before_provider_field_validation() {
    let mut config = Config::default();
    config.modules.enabled = false;
    let error = test_with_config(
        &config,
        TestParams {
            channel: " telegram ".into(),
            auth_mode: ChannelAuthMode::BotToken,
            credentials: Value::Null,
        },
    )
    .await
    .unwrap_err();
    assert!(error.contains("module unavailable"), "{error}");
}

#[tokio::test]
async fn rich_channel_send_requires_module_before_normalization_or_backend_delivery() {
    let mut config = Config::default();
    config.modules.enabled = false;
    let error = send_with_config(
        &config,
        SendMessageParams {
            channel: " telegram ".into(),
            message: json!({"text":"fixture-content", "thread_ts":"fixture-thread"}),
        },
    )
    .await
    .unwrap_err();
    assert!(error.contains("module unavailable"), "{error}");
    assert!(!error.contains("fixture-content"));
    assert!(!error.contains("fixture-thread"));
}

#[test]
fn schema_handler_parity() {
    let schemas = all_controller_schemas();
    let controllers = all_registered_controllers();
    assert_eq!(
        schemas.len(),
        controllers.len(),
        "schema count must match controller count"
    );

    for (s, c) in schemas.iter().zip(controllers.iter()) {
        assert_eq!(s.namespace, c.schema.namespace);
        assert_eq!(s.function, c.schema.function);
    }
}

#[test]
fn deserialize_connect_params() {
    let params: ConnectParams = serde_json::from_value(json!({
        "channel": "telegram",
        "authMode": "bot_token"
    }))
    .unwrap();
    assert_eq!(params.channel, "telegram");
    assert_eq!(params.auth_mode, ChannelAuthMode::BotToken);
    assert!(params.credentials.is_none());
}

#[test]
fn deserialize_disconnect_params() {
    let params: DisconnectParams = serde_json::from_value(json!({
        "channel": "discord",
        "authMode": "bot_token"
    }))
    .unwrap();
    assert_eq!(params.channel, "discord");
    assert!(!params.clear_memory);
}

#[test]
fn deserialize_disconnect_params_accepts_clear_memory() {
    let params: DisconnectParams = serde_json::from_value(json!({
        "channel": "discord",
        "authMode": "bot_token",
        "clearMemory": true
    }))
    .unwrap();
    assert!(params.clear_memory);
}

#[test]
fn deserialize_status_params_empty() {
    let params: StatusParams = serde_json::from_value(json!({})).unwrap();
    assert!(params.channel.is_none());
}

#[test]
fn deserialize_status_params_with_channel() {
    let params: StatusParams = serde_json::from_value(json!({"channel": "telegram"})).unwrap();
    assert_eq!(params.channel.as_deref(), Some("telegram"));
}

#[test]
fn deserialize_send_message_params() {
    let params: SendMessageParams = serde_json::from_value(json!({
        "channel": "telegram",
        "message": {"text": "hello"}
    }))
    .unwrap();
    assert_eq!(params.channel, "telegram");
}

#[test]
fn connect_logs_preserve_legacy_envelope_cases() {
    assert!(connect_logs("discord", ChannelAuthMode::OAuth, true).is_empty());
    assert_eq!(
        connect_logs("imessage", ChannelAuthMode::ManagedDm, false),
        vec!["stored imessage channel config (local-only)".to_string()]
    );
    assert_eq!(
        connect_logs("telegram", ChannelAuthMode::BotToken, false),
        vec!["stored credentials for channel:telegram:bot_token".to_string()]
    );
}

#[test]
fn raw_or_typed_prefers_raw_payload() {
    let raw = json!({"legacy": true});
    let typed = json!({"typed": true});
    assert_eq!(raw_or_typed(Some(raw.clone()), &typed).unwrap(), raw);
    assert_eq!(raw_or_typed(None, &typed).unwrap(), typed);
}

#[test]
fn schema_adapter_preserves_optional_field_type() {
    let schema = schemas("list_threads");
    let active = schema
        .inputs
        .iter()
        .find(|field| field.name == "active")
        .expect("active input");
    assert!(!active.required);
    assert!(matches!(
        &active.ty,
        TypeSchema::Option(inner) if matches!(inner.as_ref(), TypeSchema::Bool)
    ));
}

#[test]
fn hosted_link_functions_are_not_served_by_the_core() {
    // `channels.telegram_login_*` / `channels.discord_link_*` need a TinyHumans
    // account and are registered by `openhuman-tinyhumans`; the core must not
    // register them too (the extension would collide) nor advertise them.
    let names: Vec<&str> = all_controller_schemas()
        .iter()
        .map(|s| s.function)
        .collect();
    let registered: Vec<&str> = all_registered_controllers()
        .iter()
        .map(|c| c.schema.function)
        .collect();
    for f in HOSTED_CHANNEL_FUNCTIONS {
        assert!(!names.contains(f), "core schema still lists `{f}`");
        assert!(!registered.contains(f), "core still registers `{f}`");
    }
    assert!(names.contains(&"send_message"));
}

#[tokio::test]
async fn channel_connect_handler_requires_module_before_provider_field_validation() {
    let directory = tempfile::tempdir().unwrap();
    let mut config = Config::default();
    config.modules.enabled = false;
    config.workspace_dir = directory.path().join("workspace");
    config.config_path = directory.path().join("config.toml");
    let error = connect_with_config(
        &config,
        ConnectParams {
            channel: " telegram ".into(),
            auth_mode: ChannelAuthMode::BotToken,
            credentials: Some(Value::Null),
        },
    )
    .await
    .unwrap_err();
    assert!(error.contains("module unavailable"), "{error}");
    assert!(!config.config_path.exists());
    assert!(!config.workspace_dir.exists());
}