//! MCP agent tools — start, stop, wait, list, and inspect agents via MCP.

mod gc;
mod inspection;
mod lifecycle;
mod worktree;

pub(crate) use gc::tool_gc;
pub(crate) use inspection::{
    tool_get_agent_config, tool_get_agent_output, tool_get_agent_remaining_turns_and_budget,
};
pub(crate) use lifecycle::{
    tool_list_agents, tool_start_agent, tool_stop_agent, tool_wait_for_agent,
};
pub(crate) use worktree::{
    tool_cleanup_worktrees, tool_create_worktree, tool_get_editor_command, tool_list_worktrees,
    tool_remove_worktree,
};

#[cfg(test)]
mod tests {
    use serde_json::json;

    #[test]
    fn wait_for_agent_tool_in_list() {
        use super::super::handle_tools_list;
        let resp = handle_tools_list(Some(json!(1)));
        let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
        let wait_tool = tools.iter().find(|t| t["name"] == "wait_for_agent");
        assert!(
            wait_tool.is_some(),
            "wait_for_agent missing from tools list"
        );
        let t = wait_tool.unwrap();
        assert!(
            t["description"].as_str().unwrap().contains("block")
                || t["description"].as_str().unwrap().contains("Block")
        );
        let required = t["inputSchema"]["required"].as_array().unwrap();
        let req_names: Vec<&str> = required.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(req_names.contains(&"story_id"));
        assert!(req_names.contains(&"agent_name"));
    }

    #[test]
    fn tool_get_agent_remaining_turns_and_budget_in_tools_list() {
        use super::super::handle_tools_list;
        let resp = handle_tools_list(Some(json!(1)));
        let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
        let tool = tools
            .iter()
            .find(|t| t["name"] == "get_agent_remaining_turns_and_budget");
        assert!(
            tool.is_some(),
            "get_agent_remaining_turns_and_budget missing from tools list"
        );
        let t = tool.unwrap();
        let required = t["inputSchema"]["required"].as_array().unwrap();
        let req_names: Vec<&str> = required.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(req_names.contains(&"story_id"));
        assert!(req_names.contains(&"agent_name"));
    }
}
