use std::sync::{Arc, Mutex};
use serde_json::json;
use super::*;
use crate::agent_loop::GoalSlots;
#[test]
fn set_goal_schema_has_required_fields() {
let tool = SetGoalTool::new(Arc::new(Mutex::new(GoalSlots::new())));
let schema = tool.schema();
let required: Vec<&str> = schema["function"]["parameters"]["required"]
.as_array()
.expect("required array")
.iter()
.filter_map(|v| v.as_str())
.collect();
assert_eq!(required, vec!["slot", "text"]);
assert_eq!(schema["function"]["name"], SET_GOAL_TOOL_NAME);
}
#[test]
fn clear_goal_schema_has_required_fields() {
let tool = ClearGoalTool::new(Arc::new(Mutex::new(GoalSlots::new())));
let schema = tool.schema();
let required: Vec<&str> = schema["function"]["parameters"]["required"]
.as_array()
.expect("required array")
.iter()
.filter_map(|v| v.as_str())
.collect();
assert_eq!(required, vec!["slot"]);
assert_eq!(schema["function"]["name"], CLEAR_GOAL_TOOL_NAME);
}
#[tokio::test]
async fn set_goal_valid_args_writes_slot() {
let goals = Arc::new(Mutex::new(GoalSlots::new()));
let tool = SetGoalTool::new(Arc::clone(&goals));
let result = tool
.execute(json!({"slot": 2, "text": "ship the release"}))
.await;
assert!(!result.is_error(), "expected success: {}", result.content());
let slot = goals
.lock()
.expect("lock")
.get(2)
.cloned()
.expect("slot 2 occupied");
assert_eq!(slot.text, "ship the release");
assert_eq!(slot.source, crate::agent_loop::GoalSource::Model);
}
#[tokio::test]
async fn set_goal_out_of_range_slot_is_recoverable_error() {
let tool = SetGoalTool::new(Arc::new(Mutex::new(GoalSlots::new())));
for bad_slot in [0, 6, 100] {
let result = tool.execute(json!({"slot": bad_slot, "text": "x"})).await;
assert!(result.is_error(), "slot {bad_slot} must be rejected");
assert!(!result.is_fatal(), "must be recoverable, not fatal");
assert!(result.content().contains("out of range"));
}
}
#[tokio::test]
async fn set_goal_malformed_args_is_recoverable_error() {
let tool = SetGoalTool::new(Arc::new(Mutex::new(GoalSlots::new())));
let result = tool.execute(json!({"slot": 1})).await;
assert!(result.is_error());
assert!(!result.is_fatal());
assert!(
result
.content()
.contains("did not match the expected shape")
);
}
#[tokio::test]
async fn clear_goal_valid_args_clears_slot() {
let goals = Arc::new(Mutex::new(GoalSlots::new()));
goals
.lock()
.expect("lock")
.set(3, "temp", crate::agent_loop::GoalSource::Model)
.expect("valid slot");
let tool = ClearGoalTool::new(Arc::clone(&goals));
let result = tool.execute(json!({"slot": 3})).await;
assert!(!result.is_error(), "expected success: {}", result.content());
assert!(goals.lock().expect("lock").get(3).is_none());
}
#[tokio::test]
async fn clear_goal_out_of_range_slot_is_recoverable_error() {
let tool = ClearGoalTool::new(Arc::new(Mutex::new(GoalSlots::new())));
for bad_slot in [0, 6] {
let result = tool.execute(json!({"slot": bad_slot})).await;
assert!(result.is_error(), "slot {bad_slot} must be rejected");
assert!(!result.is_fatal());
assert!(result.content().contains("out of range"));
}
}