use recursive::error::Error;
use recursive::permissions::{DecisionReason, PermissionMode, RuleBehavior, RuleSource};
#[test]
fn error_struct_variants_remain_constructible() {
let err = Error::Config {
message: "missing RECURSIVE_API_KEY".into(),
};
assert!(err.to_string().contains("config error"));
let err = Error::Internal {
context: "backend".into(),
message: "spawn failed".into(),
};
assert!(err.to_string().contains("internal error"));
let err = Error::Tool {
name: "Read".into(),
call_id: None,
message: "no such file".into(),
};
assert!(err.to_string().contains("tool error"));
}
#[test]
fn error_unit_variant_remains_constructible() {
let err = Error::Cancelled;
assert!(err.to_string().contains("cancelled"));
}
#[test]
fn error_permission_denied_carries_decision_reason() {
let err = Error::PermissionDenied {
name: "Bash".into(),
reason: DecisionReason::Mode(PermissionMode::DontAsk),
};
assert!(err.to_string().contains("permission denied"));
}
#[test]
fn error_matches_with_catch_all_arm() {
let err = Error::Config {
message: "x".into(),
};
let label = match &err {
Error::Config { .. } => "config",
Error::Internal { .. } => "internal",
_ => "other",
};
assert_eq!(label, "config");
let err = Error::Cancelled;
let label = match &err {
Error::Config { .. } => "config",
Error::Internal { .. } => "internal",
_ => "other",
};
assert_eq!(label, "other");
}
#[test]
fn decision_reason_construct_and_match_with_catch_all() {
let reason = DecisionReason::Rule {
source: RuleSource::User,
pattern: "Bash".into(),
};
let label = match reason {
DecisionReason::Rule { pattern, .. } => pattern,
DecisionReason::Mode(_) => "mode".to_string(),
DecisionReason::Hook { .. } => "hook".to_string(),
DecisionReason::SafetyCheck { .. } => "safety".to_string(),
_ => "other".to_string(),
};
assert_eq!(label, "Bash");
}
#[test]
fn rule_source_variants_construct_and_match_with_catch_all() {
let sources = [RuleSource::Session, RuleSource::Project, RuleSource::User];
for source in sources {
let label = match source {
RuleSource::Session => "session",
RuleSource::Project => "project",
RuleSource::User => "user",
_ => "unknown",
};
assert!(!label.is_empty(), "known RuleSource must match a named arm");
}
}
#[test]
fn rule_behavior_variants_construct_and_match_with_catch_all() {
let behaviors = [
RuleBehavior::Allow,
RuleBehavior::Deny,
RuleBehavior::Interactive,
];
for behavior in behaviors {
let label = match behavior {
RuleBehavior::Allow => "allow",
RuleBehavior::Deny => "deny",
RuleBehavior::Interactive => "interactive",
_ => "unknown",
};
assert!(
!label.is_empty(),
"known RuleBehavior must match a named arm"
);
}
}