botx-api-framework 0.1.8

Фреймворк для реализации ботов под платформу eXpress
Documentation
use std::{str::FromStr, collections::HashMap};

use botx_api::bot::models::*;
use serde_json::json;

use crate::{
    contexts::*,
    tests::shared::*,
    extensions::anthill_di::*
};

#[tokio::test]
async fn process_smartapp_event_handler_ok() {
    let bot_context = BotXApiFrameworkContext::new();

    bot_context.di_container()
        .register_smartapp_event_handler::<SmartappEventHandlerFirst>()
        .await.unwrap();

    bot_context.di_container()
        .register_smartapp_event_handler::<SmartappEventHandlerSecond>()
        .await.unwrap();

    let result = bot_context.process_command(CommandRequest::<serde_json::Value, serde_json::Value> {
            sync_id: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(),
            source_sync_id: None,
            command: Command::SmartappEvent(SmartappEventCommand {
                data: SmartappEventCommandData {
                    request_ref: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(),
                    smartapp_id: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(),
                    smartapp_api_version: 1,
                    data: serde_json::value::to_value(SmartappEventHandlerFirstData {
                        some_data_text: "test".to_string()
                    }).unwrap(),
                    opts: serde_json::value::to_value(SmartappEventHandlerOptions {
                        some_text: "test 2".to_string(),
                        some_id: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap()
                    }).unwrap(),
                },
                metadata: json!({}),
                command_type: CommandType::System,
            }),
            attachments: vec![],
            from: From {
                user_huid: None,
                group_chat_id: None,
                chat_type: None,
                ad_login: None,
                ad_domain: None,
                username: None,
                is_admin: None,
                is_creator: None,
                manufacturer: None,
                device: None,
                device_software: None,
                device_meta: None,
                platform: None,
                platform_package_id: None,
                app_version: None,
                locale: None,
                host: None,
            },
            async_files: vec![],
            bot_id: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(),
            proto_version: 4,
            entities: vec![],
        })
        .await;

    let result = result.unwrap();

    assert_eq!(
        result.result,
        format!("{} - {} - {} - {}", "event precessed 1", "test", uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(), "test 2").to_string()
    );

    let result = bot_context.process_command(CommandRequest::<serde_json::Value, serde_json::Value> {
        sync_id: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(),
        source_sync_id: None,
        command: Command::SmartappEvent(SmartappEventCommand {
            data: SmartappEventCommandData {
                request_ref: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(),
                smartapp_id: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(),
                smartapp_api_version: 1,
                data: serde_json::value::to_value(SmartappEventHandlerSecondData {
                    url: "example.com".to_string(),
                    method: Method::Post,
                    headers: HashMap::from_iter([
                        ("test key".to_string(), "test value".to_string())
                    ]),
                    body: "test body".to_string(),
                }).unwrap(),
                opts: serde_json::value::to_value(SmartappEventHandlerOptions {
                    some_text: "test 2".to_string(),
                    some_id: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap()
                }).unwrap(),
            },
            metadata: json!({}),
            command_type: CommandType::System,
        }),
        attachments: vec![],
        from: From {
            user_huid: None,
            group_chat_id: None,
            chat_type: None,
            ad_login: None,
            ad_domain: None,
            username: None,
            is_admin: None,
            is_creator: None,
            manufacturer: None,
            device: None,
            device_software: None,
            device_meta: None,
            platform: None,
            platform_package_id: None,
            app_version: None,
            locale: None,
            host: None,
        },
        async_files: vec![],
        bot_id: uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(),
        proto_version: 4,
        entities: vec![],
    })
    .await;

    let result = result.unwrap();

    assert_eq!(
        result.result,
        format!("{} - {} - {} - {} - {}", "event precessed 2", "example.com", "test body", uuid::Uuid::from_str("a465f0f3-1354-491c-8f11-f400164295cb").unwrap(), "test 2").to_string()
    );
}