whyno-cli 0.5.0

Linux permission debugger
use super::*;
use crate::error::CliError;

// --- flag validation ---

#[test]
fn validate_flags_json_and_explain_conflict() {
    let cli = Cli {
        subject: Some("nginx".to_string()),
        operation: Some("read".to_string()),
        path: Some("/tmp".into()),
        json: true,
        explain: true,
        no_color: false,
        self_test: false,
        with_cap: vec![],
        new_mode: None,
        new_uid: None,
        new_gid: None,
        xattr_key: None,
        command: None,
    };
    assert_eq!(
        validate_flags(&cli).unwrap_err(),
        CliError::ConflictingFlags
    );
}

#[test]
fn validate_flags_json_only_ok() {
    let cli = Cli {
        subject: Some("nginx".to_string()),
        operation: Some("read".to_string()),
        path: Some("/tmp".into()),
        json: true,
        explain: false,
        no_color: false,
        self_test: false,
        with_cap: vec![],
        new_mode: None,
        new_uid: None,
        new_gid: None,
        xattr_key: None,
        command: None,
    };
    assert!(validate_flags(&cli).is_ok());
}

#[test]
fn validate_flags_explain_only_ok() {
    let cli = Cli {
        subject: Some("nginx".to_string()),
        operation: Some("read".to_string()),
        path: Some("/tmp".into()),
        json: false,
        explain: true,
        no_color: false,
        self_test: false,
        with_cap: vec![],
        new_mode: None,
        new_uid: None,
        new_gid: None,
        xattr_key: None,
        command: None,
    };
    assert!(validate_flags(&cli).is_ok());
}

#[test]
fn validate_flags_neither_ok() {
    let cli = Cli {
        subject: Some("nginx".to_string()),
        operation: Some("read".to_string()),
        path: Some("/tmp".into()),
        json: false,
        explain: false,
        no_color: false,
        self_test: false,
        with_cap: vec![],
        new_mode: None,
        new_uid: None,
        new_gid: None,
        xattr_key: None,
        command: None,
    };
    assert!(validate_flags(&cli).is_ok());
}

// --- clap parsing integration ---

#[test]
fn clap_parse_check_mode() {
    let cli = Cli::try_parse_from(["whyno", "nginx", "read", "/tmp"]).unwrap();
    assert_eq!(cli.subject.as_deref(), Some("nginx"));
    assert_eq!(cli.operation.as_deref(), Some("read"));
    assert_eq!(cli.path.as_deref(), Some(std::path::Path::new("/tmp")));
    assert!(cli.command.is_none());
}

#[test]
fn clap_parse_check_mode_with_json() {
    let cli = Cli::try_parse_from(["whyno", "nginx", "read", "/tmp", "--json"]).unwrap();
    assert!(cli.json);
    assert!(!cli.explain);
}

#[test]
fn clap_parse_check_mode_with_explain() {
    let cli = Cli::try_parse_from(["whyno", "nginx", "read", "/tmp", "--explain"]).unwrap();
    assert!(!cli.json);
    assert!(cli.explain);
}

#[test]
fn clap_parse_caps_install() {
    let cli = Cli::try_parse_from(["whyno", "caps", "install"]).unwrap();
    assert_eq!(
        cli.command,
        Some(Commands::Caps {
            action: CapsAction::Install,
        })
    );
}

#[test]
fn clap_parse_caps_uninstall() {
    let cli = Cli::try_parse_from(["whyno", "caps", "uninstall"]).unwrap();
    assert_eq!(
        cli.command,
        Some(Commands::Caps {
            action: CapsAction::Uninstall,
        })
    );
}

#[test]
fn clap_parse_caps_check() {
    let cli = Cli::try_parse_from(["whyno", "caps", "check"]).unwrap();
    assert_eq!(
        cli.command,
        Some(Commands::Caps {
            action: CapsAction::Check,
        })
    );
}

#[test]
fn clap_parse_schema_subcommand() {
    let cli = Cli::try_parse_from(["whyno", "schema"]).unwrap();
    assert_eq!(cli.command, Some(Commands::Schema));
}

#[test]
fn clap_parse_no_color_long_flag() {
    let cli = Cli::try_parse_from(["whyno", "nginx", "read", "/tmp", "--no-color"]).unwrap();
    assert!(cli.no_color);
}

#[test]
fn clap_parse_no_color_env_alias() {
    // --no-color is the canonical form; NO_COLOR env is the standard.
    // clap derives --no-color from field name `no_color`.
    let cli = Cli::try_parse_from(["whyno", "nginx", "read", "/tmp", "--no-color"]).unwrap();
    assert!(cli.no_color);
}