use hope_agents::policy::Condition;
use hope_agents::{
Action, Agent, AgentConfig, Goal, GoalStatus, Observation, Policy, Rule, SimpleAgent,
};
#[test]
fn test_agent_stats_serialization() {
let mut agent = SimpleAgent::new("test_agent");
for i in 0..5 {
agent.observe(Observation::sensor("temp", i as f64 * 10.0));
}
let stats = agent.stats();
let json = serde_json::to_string(stats).unwrap();
assert!(json.contains("observations_received"));
assert!(json.contains("5"));
}
#[test]
fn test_agent_config_serialization() {
let config = AgentConfig::default();
let json = serde_json::to_string(&config).unwrap();
let restored: AgentConfig = serde_json::from_str(&json).unwrap();
assert_eq!(config.name, restored.name);
}
#[test]
fn test_agent_config_iot_mode_serialization() {
let config = AgentConfig::iot_mode();
let json = serde_json::to_string(&config).unwrap();
let restored: AgentConfig = serde_json::from_str(&json).unwrap();
assert_eq!(config.name, restored.name);
}
#[test]
fn test_observation_serialization() {
let obs = Observation::sensor("temperature", 25.5);
let json = serde_json::to_string(&obs).unwrap();
let restored: Observation = serde_json::from_str(&json).unwrap();
assert_eq!(obs.value.as_f64(), restored.value.as_f64());
}
#[test]
fn test_observation_event_serialization() {
let obs = Observation::event("button_pressed");
let json = serde_json::to_string(&obs).unwrap();
let restored: Observation = serde_json::from_str(&json).unwrap();
assert_eq!(format!("{:?}", obs.value), format!("{:?}", restored.value));
}
#[test]
fn test_action_serialization() {
let action = Action::alert("Test alert message");
let json = serde_json::to_string(&action).unwrap();
let restored: Action = serde_json::from_str(&json).unwrap();
assert_eq!(action.action_type, restored.action_type);
}
#[test]
fn test_action_with_params_serialization() {
let action = Action::store("key", "value");
let json = serde_json::to_string(&action).unwrap();
let restored: Action = serde_json::from_str(&json).unwrap();
assert_eq!(action.params.len(), restored.params.len());
}
#[test]
fn test_goal_serialization() {
let goal = Goal::maintain("temperature", 20.0..25.0);
let json = serde_json::to_string(&goal).unwrap();
let restored: Goal = serde_json::from_str(&json).unwrap();
assert_eq!(goal.name, restored.name);
assert_eq!(goal.status, restored.status);
}
#[test]
fn test_goal_with_status_serialization() {
let mut goal = Goal::achieve("target", 100);
goal.status = GoalStatus::Active;
let json = serde_json::to_string(&goal).unwrap();
let restored: Goal = serde_json::from_str(&json).unwrap();
assert_eq!(restored.status, GoalStatus::Active);
}
#[test]
fn test_goal_completed_serialization() {
let mut goal = Goal::maximize("efficiency");
goal.status = GoalStatus::Achieved;
goal.set_progress(1.0);
let json = serde_json::to_string(&goal).unwrap();
let restored: Goal = serde_json::from_str(&json).unwrap();
assert_eq!(restored.status, GoalStatus::Achieved);
assert_eq!(restored.progress, 1.0);
}
#[test]
fn test_policy_serialization() {
let mut policy = Policy::new("test_policy");
policy.add_rule(Rule::new(
"high_temp",
Condition::above("temperature", 30.0),
Action::alert("High temperature!"),
));
let json = serde_json::to_string(&policy).unwrap();
let restored: Policy = serde_json::from_str(&json).unwrap();
assert_eq!(policy.rule_count(), restored.rule_count());
}
#[test]
fn test_policy_with_multiple_rules_serialization() {
let mut policy = Policy::new("complex_policy");
policy.add_rule(Rule::new(
"rule1",
Condition::above("temp", 30.0),
Action::alert("Hot!"),
));
policy.add_rule(Rule::new(
"rule2",
Condition::below("temp", 10.0),
Action::alert("Cold!"),
));
let json = serde_json::to_string(&policy).unwrap();
let restored: Policy = serde_json::from_str(&json).unwrap();
assert_eq!(restored.rule_count(), 2);
}
#[test]
fn test_rule_serialization() {
let rule = Rule::new(
"test_rule",
Condition::above("sensor", 50.0),
Action::alert("Threshold exceeded"),
);
let json = serde_json::to_string(&rule).unwrap();
let restored: Rule = serde_json::from_str(&json).unwrap();
assert_eq!(rule.name, restored.name);
}
#[test]
fn test_complete_agent_state_persistence() {
let mut agent = SimpleAgent::with_config("persistent_agent", AgentConfig::default());
agent.add_rule(Rule::new(
"high_temp",
Condition::above("temperature", 30.0),
Action::alert("High temperature!"),
));
let mut goal = Goal::maintain("temperature", 20.0..25.0);
goal.activate();
agent.add_goal(goal);
for i in 0..10 {
agent.observe(Observation::sensor("temperature", 20.0 + i as f64));
}
let stats = agent.stats();
let stats_json = serde_json::to_string(stats).unwrap();
let restored_stats: hope_agents::agent::AgentStats = serde_json::from_str(&stats_json).unwrap();
assert_eq!(restored_stats.observations_received, 10);
}
#[test]
fn test_observation_buffer_persistence() {
let mut agent = SimpleAgent::new("buffer_test");
for i in 0..5 {
agent.observe(Observation::sensor("temp", i as f64 * 10.0));
}
let recent = agent.recent_observations(5);
assert_eq!(recent.len(), 5);
let observations: Vec<_> = recent.iter().map(|o| (*o).clone()).collect();
let json = serde_json::to_string(&observations).unwrap();
let restored: Vec<Observation> = serde_json::from_str(&json).unwrap();
assert_eq!(restored.len(), 5);
}
#[test]
fn test_goals_batch_serialization() {
let goals = vec![
Goal::maintain("temperature", 20.0..25.0),
Goal::achieve("target", 100),
Goal::maximize("efficiency"),
];
let json = serde_json::to_string(&goals).unwrap();
let restored: Vec<Goal> = serde_json::from_str(&json).unwrap();
assert_eq!(restored.len(), 3);
}
#[test]
fn test_json_pretty_format() {
let goal = Goal::maintain("temperature", 20.0..25.0);
let pretty_json = serde_json::to_string_pretty(&goal).unwrap();
assert!(pretty_json.contains('\n'));
let restored: Goal = serde_json::from_str(&pretty_json).unwrap();
assert_eq!(goal.name, restored.name);
}