//! Integration tests for built-in action nodes

use mecha10_behavior_runtime::{register_builtin_actions, NodeRegistry};
use serde_json::json;

#[tokio::test]
async fn test_wander_node_creation() {
    let mut registry = NodeRegistry::new();
    register_builtin_actions(&mut registry);

    let config = json!({
        "topic": "/motor/cmd_vel",
        "linear_speed": 0.5,
        "angular_speed": 0.3,
        "change_interval_secs": 2.0
    });

    let node = registry.create("wander", config);
    assert!(node.is_ok(), "WanderNode should be created successfully");
}

#[tokio::test]
async fn test_move_node_creation() {
    let mut registry = NodeRegistry::new();
    register_builtin_actions(&mut registry);

    let config = json!({
        "topic": "/motor/cmd_vel",
        "linear": 1.0,
        "angular": 0.0,
        "duration_secs": 3.0
    });

    let node = registry.create("move", config);
    assert!(node.is_ok(), "MoveNode should be created successfully");
}

#[tokio::test]
async fn test_timer_node_creation() {
    let mut registry = NodeRegistry::new();
    register_builtin_actions(&mut registry);

    let config = json!({
        "duration_secs": 5.0
    });

    let node = registry.create("timer", config);
    assert!(node.is_ok(), "TimerNode should be created successfully");
}

#[tokio::test]
async fn test_sensor_check_node_creation() {
    let mut registry = NodeRegistry::new();
    register_builtin_actions(&mut registry);

    let config = json!({
        "topic": "/sensors/distance",
        "field": "value",
        "operator": "less_than",
        "threshold": 0.5
    });

    let node = registry.create("sensor_check", config);
    assert!(node.is_ok(), "SensorCheckNode should be created successfully");
}

#[tokio::test]
async fn test_all_builtin_nodes_registered() {
    let mut registry = NodeRegistry::new();
    register_builtin_actions(&mut registry);

    // Check all built-in nodes are registered
    assert!(registry.has_node("wander"), "wander should be registered");
    assert!(registry.has_node("move"), "move should be registered");
    assert!(registry.has_node("timer"), "timer should be registered");
    assert!(registry.has_node("sensor_check"), "sensor_check should be registered");
}

#[tokio::test]
async fn test_unknown_node_type_fails() {
    let registry = NodeRegistry::new();
    // Don't register any nodes

    let result = registry.create("unknown_type", json!({}));
    assert!(result.is_err(), "Creating unknown node type should fail");
}
