use tracing::instrument;
use crate::error::OxidizedError;
use crate::oxidized::{OxidizedBackend, OxidizedClient};
use super::ToolResult;
#[instrument(skip(backend))]
pub async fn reload_sources(backend: &OxidizedClient) -> Result<ToolResult, OxidizedError> {
match backend.reload_sources().await {
Ok(()) => {
tracing::info!("Oxidized sources reloaded successfully");
Ok(ToolResult::success(
"",
"Oxidized sources reloaded. New devices are now available in the inventory.",
))
}
Err(e) => Err(e),
}
}
#[cfg(test)]
mod tests {
use super::*;
const EXPECTED_MESSAGE: &str =
"Oxidized sources reloaded. New devices are now available in the inventory.";
#[test]
fn test_success_message_exact_format() {
let result = ToolResult::success("", EXPECTED_MESSAGE);
assert!(result.success);
assert!(
result.node.is_empty(),
"reload_sources has no specific node"
);
assert_eq!(result.message, EXPECTED_MESSAGE);
}
#[test]
fn test_success_node_is_empty() {
let result = ToolResult::success("", EXPECTED_MESSAGE);
assert!(result.node.is_empty());
assert_eq!(result.node.len(), 0);
}
#[test]
fn test_tool_result_json_serialization() {
let result = ToolResult::success("", EXPECTED_MESSAGE);
let json = serde_json::to_string(&result).expect("Should serialize to JSON");
assert!(json.contains("\"success\":true"));
assert!(
json.contains("\"node\":\"\""),
"Node should be empty string"
);
assert!(json.contains("\"message\":"));
let value: serde_json::Value = serde_json::from_str(&json).expect("Should parse as JSON");
assert_eq!(value["success"], true);
assert_eq!(value["node"], "");
assert_eq!(value["message"], EXPECTED_MESSAGE);
}
#[test]
fn test_tool_result_json_contains_all_fields() {
let result = ToolResult::success("", EXPECTED_MESSAGE);
let json = serde_json::to_string(&result).expect("Should serialize to JSON");
let value: serde_json::Value = serde_json::from_str(&json).expect("Should parse as JSON");
assert!(
value.get("success").is_some(),
"Should have 'success' field"
);
assert!(value.get("node").is_some(), "Should have 'node' field");
assert!(
value.get("message").is_some(),
"Should have 'message' field"
);
}
}