Skip to main content

alopex_cli/batch/
mod.rs

1pub mod detector;
2pub mod exit_code;
3
4#[allow(unused_imports)]
5pub use detector::{BatchMode, BatchModeSource};
6#[allow(unused_imports)]
7pub use exit_code::{ExitCode, ExitCodeCollector};
8
9#[cfg(test)]
10mod tests {
11    use super::*;
12    use clap::Parser;
13
14    use crate::cli::Cli;
15
16    fn parse_cli(args: &[&str]) -> Cli {
17        Cli::try_parse_from(args).expect("cli parse")
18    }
19
20    #[test]
21    fn detect_batch_mode_sources() {
22        let base = parse_cli(&["alopex", "kv", "list"]);
23        let explicit_cli = parse_cli(&["alopex", "--batch", "kv", "list"]);
24
25        let explicit = BatchMode::detect_with(&explicit_cli, true, Some("batch"));
26        let env = BatchMode::detect_with(&base, true, Some("batch"));
27        let pipe = BatchMode::detect_with(&base, false, None);
28        let default = BatchMode::detect_with(&base, true, None);
29
30        assert_eq!(explicit.source, BatchModeSource::Explicit);
31        assert_eq!(env.source, BatchModeSource::Environment);
32        assert_eq!(pipe.source, BatchModeSource::PipeDetected);
33        assert_eq!(default.source, BatchModeSource::Default);
34    }
35
36    #[test]
37    fn should_prompt_requires_destructive_action() {
38        let cli = parse_cli(&["alopex", "kv", "list"]);
39        let mode = BatchMode {
40            is_batch: false,
41            is_tty: true,
42            source: BatchModeSource::Default,
43        };
44
45        assert!(mode.should_prompt(&cli, true));
46        assert!(!mode.should_prompt(&cli, false));
47    }
48
49    #[test]
50    fn exit_code_collector_reports_warning_on_mixed_results() {
51        let mut collector = ExitCodeCollector::new();
52        collector.record_success();
53        collector.record_error();
54
55        assert_eq!(collector.finalize(), ExitCode::Warning);
56    }
57}