use hypen_engine::dispatch::action::{Action, ActionDispatcher};
use serde_json::json;
use std::sync::{Arc, Mutex};
#[test]
fn test_action_new() {
let action = Action::new("signIn");
assert_eq!(action.name, "signIn");
assert!(action.payload.is_none());
assert!(action.sender.is_none());
}
#[test]
fn test_action_with_payload() {
let action = Action::new("updateUser").with_payload(json!({
"userId": 123,
"name": "Alice"
}));
assert_eq!(action.name, "updateUser");
assert!(action.payload.is_some());
let payload = action.payload.as_ref().unwrap();
assert_eq!(payload["userId"], 123);
assert_eq!(payload["name"], "Alice");
}
#[test]
fn test_action_with_sender() {
let action = Action::new("notify").with_sender("UserModule");
assert_eq!(action.name, "notify");
assert!(action.sender.is_some());
assert_eq!(action.sender.unwrap(), "UserModule");
}
#[test]
fn test_dispatcher_register_handler() {
let mut dispatcher = ActionDispatcher::new();
dispatcher.on("increment", |_action| {
});
assert!(dispatcher.has_handler("increment"));
}
#[test]
fn test_dispatcher_multiple_handlers() {
let mut dispatcher = ActionDispatcher::new();
dispatcher.on("action1", |_| {});
dispatcher.on("action2", |_| {});
dispatcher.on("action3", |_| {});
assert!(dispatcher.has_handler("action1"));
assert!(dispatcher.has_handler("action2"));
assert!(dispatcher.has_handler("action3"));
}
#[test]
fn test_dispatcher_remove_handler() {
let mut dispatcher = ActionDispatcher::new();
dispatcher.on("test", |_| {});
assert!(dispatcher.has_handler("test"));
dispatcher.remove("test");
assert!(!dispatcher.has_handler("test"));
}
#[test]
fn test_dispatcher_clear_all_handlers() {
let mut dispatcher = ActionDispatcher::new();
dispatcher.on("action1", |_| {});
dispatcher.on("action2", |_| {});
dispatcher.on("action3", |_| {});
dispatcher.clear();
assert!(!dispatcher.has_handler("action1"));
assert!(!dispatcher.has_handler("action2"));
assert!(!dispatcher.has_handler("action3"));
}
#[test]
fn test_dispatch_with_payload_to_handler() {
let mut dispatcher = ActionDispatcher::new();
let captured_payload = Arc::new(Mutex::new(None));
let captured_clone = Arc::clone(&captured_payload);
dispatcher.on("updateCount", move |action| {
*captured_clone.lock().unwrap() = action.payload.clone();
});
let action = Action::new("updateCount").with_payload(json!({"count": 42}));
dispatcher.dispatch(&action).unwrap();
let payload = captured_payload.lock().unwrap();
assert!(payload.is_some());
assert_eq!(payload.as_ref().unwrap()["count"], 42);
}
#[test]
fn test_dispatch_to_missing_handler_returns_error() {
let dispatcher = ActionDispatcher::new();
let action = Action::new("nonexistent");
let result = dispatcher.dispatch(&action);
assert!(result.is_err());
match result.unwrap_err() {
hypen_engine::EngineError::ActionNotFound(name) => {
assert_eq!(name, "nonexistent");
}
other => panic!("Expected ActionNotFound, got: {:?}", other),
}
}
#[test]
fn test_handler_replacement_on_duplicate_registration() {
let mut dispatcher = ActionDispatcher::new();
let first_called = Arc::new(Mutex::new(false));
let second_called = Arc::new(Mutex::new(false));
let first_clone = Arc::clone(&first_called);
dispatcher.on("test", move |_| {
*first_clone.lock().unwrap() = true;
});
let second_clone = Arc::clone(&second_called);
dispatcher.on("test", move |_| {
*second_clone.lock().unwrap() = true;
});
let action = Action::new("test");
dispatcher.dispatch(&action).unwrap();
assert!(!(*first_called.lock().unwrap()));
assert!(*second_called.lock().unwrap());
}
#[test]
fn test_dispatcher_default() {
let dispatcher = ActionDispatcher::default();
assert!(!dispatcher.has_handler("anything"));
}
#[test]
fn test_action_builder_chain() {
let action = Action::new("complexAction")
.with_payload(json!({"data": "test"}))
.with_sender("TestModule");
assert_eq!(action.name, "complexAction");
assert_eq!(action.payload.unwrap()["data"], "test");
assert_eq!(action.sender.unwrap(), "TestModule");
}
#[test]
fn test_action_serialization() {
let action = Action::new("testAction")
.with_payload(json!({"value": 100}))
.with_sender("Source");
let json = serde_json::to_value(&action).unwrap();
assert_eq!(json["name"], "testAction");
assert_eq!(json["payload"]["value"], 100);
assert_eq!(json["sender"], "Source");
}
#[test]
fn test_action_deserialization() {
let json = json!({
"name": "deserializedAction",
"payload": {"count": 5},
"sender": "Client"
});
let action: Action = serde_json::from_value(json).unwrap();
assert_eq!(action.name, "deserializedAction");
assert_eq!(action.payload.unwrap()["count"], 5);
assert_eq!(action.sender.unwrap(), "Client");
}
#[test]
fn test_remove_nonexistent_handler_no_panic() {
let mut dispatcher = ActionDispatcher::new();
dispatcher.remove("nonexistent");
assert!(!dispatcher.has_handler("nonexistent"));
}
#[test]
fn test_dispatch_multiple_times_to_same_handler() {
let mut dispatcher = ActionDispatcher::new();
let call_count = Arc::new(Mutex::new(0));
let call_count_clone = Arc::clone(&call_count);
dispatcher.on("increment", move |_| {
*call_count_clone.lock().unwrap() += 1;
});
let action = Action::new("increment");
dispatcher.dispatch(&action).unwrap();
dispatcher.dispatch(&action).unwrap();
dispatcher.dispatch(&action).unwrap();
assert_eq!(*call_count.lock().unwrap(), 3);
}