use std::sync::Arc;
use std::time::{Duration, Instant};
use serde_json::{Value, json};
use super::{BASH_TOOL_NAME, BashTool};
use crate::tools::registry::ToolRegistry;
use crate::tools::traits::ToolExecutor;
#[test]
fn name_matches_constant() {
let tool = BashTool::default_config();
assert_eq!(tool.name(), BASH_TOOL_NAME);
assert_eq!(BASH_TOOL_NAME, "bash");
}
#[tokio::test]
async fn fast_command_exit_zero() {
let tool = BashTool::default_config();
let result = tool.execute(json!({"command": "echo hello"})).await;
assert!(
!result.is_error(),
"echo should succeed: {}",
result.content()
);
let content = result.content();
assert!(content.contains("hello"), "stdout must contain 'hello'");
assert!(content.contains("exit_code: 0"), "exit code must be 0");
}
#[tokio::test]
async fn nonzero_exit_is_recoverable() {
let tool = BashTool::default_config();
let result = tool.execute(json!({"command": "false"})).await;
assert!(
!result.is_error(),
"non-zero exit must be recoverable ToolResult::ok, not ToolResult::Error"
);
assert!(
result.content().contains("exit_code: 1"),
"exit code 1 must appear in content, got: {}",
result.content()
);
}
#[tokio::test]
async fn custom_exit_code_is_captured() {
let tool = BashTool::default_config();
let result = tool.execute(json!({"command": "sh -c 'exit 42'"})).await;
assert!(!result.is_error());
assert!(
result.content().contains("exit_code: 42"),
"exit_code 42 must appear, got: {}",
result.content()
);
}
#[cfg(unix)]
#[tokio::test]
async fn timeout_kills_child() {
let tool = BashTool::new(None, Duration::from_secs(120));
let start = Instant::now();
let result = tool
.execute(json!({"command": "sleep 30", "timeout_secs": 1}))
.await;
let elapsed = start.elapsed();
assert!(
result.is_error(),
"timed-out command must return an error variant"
);
assert!(
result.content().contains("timed out"),
"error must mention timeout, got: {}",
result.content()
);
assert!(
elapsed < Duration::from_secs(10),
"timeout must fire in under 10 s, took {}ms",
elapsed.as_millis()
);
}
#[tokio::test]
async fn cwd_is_honored() {
let tmp = tempfile::tempdir().expect("tempdir");
let canonical = tmp.path().canonicalize().expect("canonicalize");
let tool = BashTool::new(Some(canonical.clone()), Duration::from_secs(10));
let result = tool.execute(json!({"command": "pwd"})).await;
assert!(
!result.is_error(),
"pwd should succeed: {}",
result.content()
);
let content = result.content();
assert!(
content.contains(canonical.to_string_lossy().as_ref()),
"pwd output must contain the tempdir path, got: {content}"
);
}
#[tokio::test]
async fn stdout_truncation() {
let tool = BashTool::new(None, Duration::from_secs(30));
let result = tool
.execute(json!({"command": "yes | head -c 110000"}))
.await;
assert!(
!result.is_error(),
"yes|head should succeed: {}",
result.content()
);
let content = result.content();
assert!(
content.contains("truncated"),
"output must include truncation notice, got length={}",
content.len()
);
}
#[tokio::test]
async fn missing_command_returns_error() {
let tool = BashTool::default_config();
let result = tool.execute(json!({})).await;
assert!(result.is_error());
assert!(
result.content().contains("missing required 'command'"),
"got: {}",
result.content()
);
}
#[tokio::test]
async fn empty_command_returns_error() {
let tool = BashTool::default_config();
let result = tool.execute(json!({"command": " "})).await;
assert!(result.is_error());
assert!(
result.content().contains("must not be empty"),
"got: {}",
result.content()
);
}
#[tokio::test]
async fn registry_registration_and_dispatch() {
let mut reg = ToolRegistry::new();
reg.register(Arc::new(BashTool::default_config()));
assert!(reg.contains("bash"), "registry must know about 'bash'");
assert_eq!(reg.schemas().len(), 1, "exactly one schema");
let schema_val = reg.schemas().into_iter().next().expect("schema");
let fn_name = schema_val
.pointer("/function/name")
.and_then(Value::as_str)
.expect("function.name");
assert_eq!(fn_name, "bash");
let result = reg
.dispatch("bash", json!({"command": "echo registry-ok"}))
.await;
assert!(
!result.is_error(),
"dispatch must succeed: {}",
result.content()
);
assert!(result.content().contains("registry-ok"));
}
#[test]
fn restricted_tiers_includes_readonly_and_analytics() {
use crate::tools::traits::ServiceTier;
let tool = BashTool::default_config();
let tiers = tool.restricted_tiers();
assert!(
tiers.contains(&ServiceTier::ReadOnly),
"ReadOnly must be restricted"
);
assert!(
tiers.contains(&ServiceTier::Analytics),
"Analytics must be restricted"
);
}