use crate::brain::agent::service::AgentService;
use crate::config::Config;
use crate::db::Database;
use crate::services::ServiceContext;
use crate::tests::agent_service_mocks::MockProviderWithTools;
use crate::utils::approval::policy_auto_approves;
use std::sync::Arc;
#[test]
fn both_auto_policies_grant_execution() {
assert!(policy_auto_approves("auto-always"));
assert!(policy_auto_approves("auto-session"));
}
#[test]
fn gating_policies_do_not_grant_execution() {
assert!(!policy_auto_approves("ask"));
assert!(!policy_auto_approves("manual"));
assert!(!policy_auto_approves(""));
}
#[test]
fn an_unknown_policy_never_grants_execution() {
assert!(!policy_auto_approves("auto"));
assert!(!policy_auto_approves("Auto-Always"));
assert!(!policy_auto_approves("yolo"));
}
async fn service_with_policy(policy: &str) -> AgentService {
let db = Database::connect_in_memory().await.expect("in-memory db");
db.run_migrations().await.expect("migrations");
let provider = Arc::new(MockProviderWithTools::new());
let mut config = Config::default();
config.agent.approval_policy = policy.to_string();
AgentService::new(provider, ServiceContext::new(db.pool().clone()), &config).await
}
#[tokio::test]
async fn auto_always_in_config_reaches_the_service_without_a_flag() {
let service = service_with_policy("auto-always").await;
assert!(
service.auto_approves_tools(),
"a configured auto-always must grant execution with no flag and no callback"
);
}
#[tokio::test]
async fn a_gating_policy_still_withholds_the_grant() {
let service = service_with_policy("ask").await;
assert!(!service.auto_approves_tools());
}
#[tokio::test]
async fn an_explicit_grant_widens_a_gating_policy() {
let service = service_with_policy("ask")
.await
.with_auto_approve_tools(true);
assert!(service.auto_approves_tools());
}
#[tokio::test]
async fn a_flagless_caller_does_not_revoke_the_policy() {
let service = service_with_policy("auto-always")
.await
.with_auto_approve_tools(false);
assert!(
service.auto_approves_tools(),
"an absent CLI flag means 'nothing to add', not 'revoke the policy'"
);
}