mod common;
use apcore_cli::approval::{check_approval, ApprovalError};
use serde_json::json;
#[tokio::test]
async fn test_check_approval_auto_approve_skips_prompt() {
let module_def = json!({
"module_id": "math.add",
"annotations": {"requires_approval": true}
});
let result = check_approval(&module_def, true, None).await;
assert!(
result.is_ok(),
"expected Ok for auto_approve=true: {result:?}"
);
}
#[tokio::test]
async fn test_check_approval_no_tty_returns_error() {
use std::io::IsTerminal;
if std::io::stdin().is_terminal() {
eprintln!("Skipping non-TTY test (stdin is a TTY).");
return;
}
let module_def = json!({
"module_id": "math.add",
"annotations": {"requires_approval": true}
});
let result = check_approval(&module_def, false, None).await;
assert!(
matches!(result, Err(ApprovalError::NonInteractive { .. })),
"expected NonInteractive error, got {:?}",
result
);
}
#[tokio::test]
async fn test_check_approval_denied_returns_error() {
let module_def = json!({
"module_id": "math.add",
"annotations": {"requires_approval": true}
});
let result = apcore_cli::approval::check_approval_with_tty(&module_def, false, false).await;
assert!(
matches!(result, Err(ApprovalError::NonInteractive { .. })),
"expected NonInteractive error, got {:?}",
result
);
}
#[tokio::test]
async fn test_check_approval_timeout_returns_error() {
let err = ApprovalError::Timeout {
module_id: "math.add".to_string(),
seconds: 1,
};
assert!(err.to_string().contains("math.add"));
assert!(err.to_string().contains("1s"));
}
#[tokio::test]
async fn test_approval_timeout_error_display() {
let err = ApprovalError::Timeout {
module_id: "math.add".to_string(),
seconds: 30,
};
assert!(err.to_string().contains("math.add"));
assert!(err.to_string().contains("30"));
}