autoagents_core/
tool.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct ToolCallResult {
6    pub tool_name: String,
7    pub success: bool,
8    pub arguments: Value,
9    pub result: Value,
10}
11
12#[cfg(test)]
13mod tests {
14    use super::*;
15    use serde_json::json;
16
17    #[test]
18    fn test_tool_call_result_creation() {
19        let result = ToolCallResult {
20            tool_name: "test_tool".to_string(),
21            success: true,
22            arguments: json!({"param": "value"}),
23            result: json!({"output": "success"}),
24        };
25
26        assert_eq!(result.tool_name, "test_tool");
27        assert!(result.success);
28        assert_eq!(result.arguments, json!({"param": "value"}));
29        assert_eq!(result.result, json!({"output": "success"}));
30    }
31
32    #[test]
33    fn test_tool_call_result_serialization() {
34        let result = ToolCallResult {
35            tool_name: "serialize_tool".to_string(),
36            success: false,
37            arguments: json!({"input": "test"}),
38            result: json!({"error": "failed"}),
39        };
40
41        let serialized = serde_json::to_string(&result).unwrap();
42        let deserialized: ToolCallResult = serde_json::from_str(&serialized).unwrap();
43
44        assert_eq!(deserialized.tool_name, "serialize_tool");
45        assert!(!deserialized.success);
46        assert_eq!(deserialized.arguments, json!({"input": "test"}));
47        assert_eq!(deserialized.result, json!({"error": "failed"}));
48    }
49
50    #[test]
51    fn test_tool_call_result_clone() {
52        let result = ToolCallResult {
53            tool_name: "clone_tool".to_string(),
54            success: true,
55            arguments: json!({"data": [1, 2, 3]}),
56            result: json!({"processed": [2, 4, 6]}),
57        };
58
59        let cloned = result.clone();
60        assert_eq!(result.tool_name, cloned.tool_name);
61        assert_eq!(result.success, cloned.success);
62        assert_eq!(result.arguments, cloned.arguments);
63        assert_eq!(result.result, cloned.result);
64    }
65
66    #[test]
67    fn test_tool_call_result_debug() {
68        let result = ToolCallResult {
69            tool_name: "debug_tool".to_string(),
70            success: true,
71            arguments: json!({}),
72            result: json!(null),
73        };
74
75        let debug_str = format!("{result:?}");
76        assert!(debug_str.contains("ToolCallResult"));
77        assert!(debug_str.contains("debug_tool"));
78    }
79
80    #[test]
81    fn test_tool_call_result_with_null_values() {
82        let result = ToolCallResult {
83            tool_name: "null_tool".to_string(),
84            success: false,
85            arguments: json!(null),
86            result: json!(null),
87        };
88
89        let serialized = serde_json::to_string(&result).unwrap();
90        let deserialized: ToolCallResult = serde_json::from_str(&serialized).unwrap();
91
92        assert_eq!(deserialized.tool_name, "null_tool");
93        assert!(!deserialized.success);
94        assert_eq!(deserialized.arguments, json!(null));
95        assert_eq!(deserialized.result, json!(null));
96    }
97
98    #[test]
99    fn test_tool_call_result_with_complex_data() {
100        let complex_args = json!({
101            "nested": {
102                "array": [1, 2, {"key": "value"}],
103                "string": "test",
104                "number": 42.5
105            }
106        });
107
108        let complex_result = json!({
109            "status": "completed",
110            "data": {
111                "items": ["a", "b", "c"],
112                "count": 3
113            }
114        });
115
116        let result = ToolCallResult {
117            tool_name: "complex_tool".to_string(),
118            success: true,
119            arguments: complex_args.clone(),
120            result: complex_result.clone(),
121        };
122
123        let serialized = serde_json::to_string(&result).unwrap();
124        let deserialized: ToolCallResult = serde_json::from_str(&serialized).unwrap();
125
126        assert_eq!(deserialized.arguments, complex_args);
127        assert_eq!(deserialized.result, complex_result);
128    }
129
130    #[test]
131    fn test_tool_call_result_empty_tool_name() {
132        let result = ToolCallResult {
133            tool_name: String::new(),
134            success: true,
135            arguments: json!({}),
136            result: json!({}),
137        };
138
139        assert!(result.tool_name.is_empty());
140        assert!(result.success);
141    }
142
143    #[test]
144    fn test_tool_call_result_large_data() {
145        let large_string = "x".repeat(10000);
146        let result = ToolCallResult {
147            tool_name: "large_tool".to_string(),
148            success: true,
149            arguments: json!({"large_param": large_string}),
150            result: json!({"processed": true}),
151        };
152
153        let serialized = serde_json::to_string(&result).unwrap();
154        let deserialized: ToolCallResult = serde_json::from_str(&serialized).unwrap();
155
156        assert_eq!(deserialized.tool_name, "large_tool");
157        assert!(deserialized.success);
158        assert!(
159            deserialized.arguments["large_param"]
160                .as_str()
161                .unwrap()
162                .len()
163                == 10000
164        );
165    }
166
167    #[test]
168    fn test_tool_call_result_equality() {
169        let result1 = ToolCallResult {
170            tool_name: "equal_tool".to_string(),
171            success: true,
172            arguments: json!({"param": "value"}),
173            result: json!({"output": "result"}),
174        };
175
176        let result2 = ToolCallResult {
177            tool_name: "equal_tool".to_string(),
178            success: true,
179            arguments: json!({"param": "value"}),
180            result: json!({"output": "result"}),
181        };
182
183        let result3 = ToolCallResult {
184            tool_name: "different_tool".to_string(),
185            success: true,
186            arguments: json!({"param": "value"}),
187            result: json!({"output": "result"}),
188        };
189
190        // Test equality through serialization since ToolCallResult doesn't implement PartialEq
191        let serialized1 = serde_json::to_string(&result1).unwrap();
192        let serialized2 = serde_json::to_string(&result2).unwrap();
193        let serialized3 = serde_json::to_string(&result3).unwrap();
194
195        assert_eq!(serialized1, serialized2);
196        assert_ne!(serialized1, serialized3);
197    }
198
199    #[test]
200    fn test_tool_call_result_with_unicode() {
201        let result = ToolCallResult {
202            tool_name: "unicode_tool".to_string(),
203            success: true,
204            arguments: json!({"message": "Hello δΈ–η•Œ! 🌍"}),
205            result: json!({"response": "Processed: Hello δΈ–η•Œ! 🌍"}),
206        };
207
208        let serialized = serde_json::to_string(&result).unwrap();
209        let deserialized: ToolCallResult = serde_json::from_str(&serialized).unwrap();
210
211        assert_eq!(deserialized.arguments["message"], "Hello δΈ–η•Œ! 🌍");
212        assert_eq!(deserialized.result["response"], "Processed: Hello δΈ–η•Œ! 🌍");
213    }
214
215    #[test]
216    fn test_tool_call_result_with_arrays() {
217        let result = ToolCallResult {
218            tool_name: "array_tool".to_string(),
219            success: true,
220            arguments: json!({"numbers": [1, 2, 3, 4, 5]}),
221            result: json!({"sum": 15, "count": 5}),
222        };
223
224        let serialized = serde_json::to_string(&result).unwrap();
225        let deserialized: ToolCallResult = serde_json::from_str(&serialized).unwrap();
226
227        assert_eq!(deserialized.arguments["numbers"], json!([1, 2, 3, 4, 5]));
228        assert_eq!(deserialized.result["sum"], 15);
229        assert_eq!(deserialized.result["count"], 5);
230    }
231
232    #[test]
233    fn test_tool_call_result_boolean_values() {
234        let result = ToolCallResult {
235            tool_name: "bool_tool".to_string(),
236            success: false,
237            arguments: json!({"enabled": true, "debug": false}),
238            result: json!({"valid": false, "error": true}),
239        };
240
241        let serialized = serde_json::to_string(&result).unwrap();
242        let deserialized: ToolCallResult = serde_json::from_str(&serialized).unwrap();
243
244        assert!(!deserialized.success);
245        assert_eq!(deserialized.arguments["enabled"], true);
246        assert_eq!(deserialized.arguments["debug"], false);
247        assert_eq!(deserialized.result["valid"], false);
248        assert_eq!(deserialized.result["error"], true);
249    }
250}