apexe 0.3.0

Outside-In CLI-to-Agent Bridge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
mod config_gen;

use std::path::PathBuf;

use clap::{Parser, Subcommand};
use tracing::info;

use crate::config::{load_config, ApexeConfig};

/// apexe -- Outside-In CLI-to-Agent Bridge.
///
/// Wraps CLI tools into governed apcore modules served via MCP/A2A.
#[derive(Debug, Parser)]
#[command(name = "apexe", version, about, long_about = None)]
pub struct Cli {
    /// Log level (trace, debug, info, warn, error). When omitted, the level
    /// resolves from RUST_LOG, then APEXE_LOG_LEVEL / config.yaml, then "info".
    #[arg(long, global = true)]
    pub log_level: Option<String>,

    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Scan CLI tools and generate apcore binding files.
    Scan(ScanArgs),
    /// Start MCP server for scanned CLI tools.
    Serve(ServeArgs),
    /// Start A2A agent server for scanned CLI tools.
    A2a(A2aArgs),
    /// List previously scanned CLI tools and their modules.
    List(ListArgs),
    /// Show or initialize apexe configuration.
    Config(ConfigArgs),
}

impl Cli {
    /// Resolve the log-level fallback used when `RUST_LOG` is unset: an explicit
    /// `--log-level` wins, otherwise the resolved `config_level` (which already
    /// folds in `APEXE_LOG_LEVEL` and `config.yaml`).
    pub fn effective_log_level(&self, config_level: &str) -> String {
        self.log_level
            .clone()
            .unwrap_or_else(|| config_level.to_string())
    }

    pub fn run(self) -> anyhow::Result<()> {
        let config = load_config(None, None)?;
        config.ensure_dirs()?;

        match self.command {
            Commands::Scan(args) => args.execute(&config),
            Commands::Serve(args) => args.execute(&config),
            Commands::A2a(args) => args.execute(&config),
            Commands::List(args) => args.execute(&config),
            Commands::Config(args) => args.execute(&config),
        }
    }
}

/// Scan CLI tools and generate apcore binding files.
///
/// TOOLS: One or more CLI tool names to scan (e.g., git docker ffmpeg).
#[derive(Debug, clap::Args)]
pub struct ScanArgs {
    /// CLI tool names to scan
    #[arg(required = true)]
    pub tools: Vec<String>,

    /// Output directory for binding files (default: ~/.apexe/modules/)
    #[arg(long)]
    pub output_dir: Option<PathBuf>,

    /// Maximum subcommand recursion depth (1-5)
    #[arg(long, default_value = "2", value_parser = clap::value_parser!(u32).range(1..=5))]
    pub depth: u32,

    /// Force re-scan, bypassing cache
    #[arg(long)]
    pub no_cache: bool,

    /// Output format for scan results
    #[arg(long, default_value = "table", value_parser = ["json", "yaml", "table"])]
    pub format: String,

    /// Also write a Claude Skill (`SKILL.md`) per module under
    /// `<DIR>/.claude/skills/<module_id>/SKILL.md`
    #[arg(long)]
    pub skills_dir: Option<PathBuf>,
}

impl ScanArgs {
    pub fn execute(self, config: &ApexeConfig) -> anyhow::Result<()> {
        let orchestrator = crate::scanner::ScanOrchestrator::new(config.clone());
        let results = orchestrator.scan(&self.tools, self.no_cache, self.depth)?;

        let output_dir = self
            .output_dir
            .clone()
            .unwrap_or_else(|| config.modules_dir.clone());

        let converter = crate::adapter::CliToolConverter::new();
        let modules = converter.convert_all(&results);

        // These are the command's deliverables. A failed write must surface as
        // a non-zero exit, not a swallowed warning — otherwise `apexe scan`
        // reports success with no bindings (or, worse, no ACL policy) written.
        self.write_bindings(&modules, &output_dir)?;
        self.write_acl(&modules, config)?;
        self.write_skills(&modules)?;
        self.print_results(&results)?;

        Ok(())
    }

    fn write_bindings(
        &self,
        modules: &[apcore_toolkit::ScannedModule],
        output_dir: &std::path::Path,
    ) -> anyhow::Result<()> {
        let yaml_output = crate::output::YamlOutput::new();
        let write_results = yaml_output
            .write(modules, output_dir, false)
            .map_err(|e| anyhow::anyhow!("Failed to write binding files: {e}"))?;
        for wr in &write_results {
            if let Some(ref path) = wr.path {
                info!(path, "Generated binding");
            }
        }
        Ok(())
    }

    fn write_acl(
        &self,
        modules: &[apcore_toolkit::ScannedModule],
        config: &ApexeConfig,
    ) -> anyhow::Result<()> {
        let acl_manager = crate::governance::AclManager::generate_default(modules);
        let acl_path = config.config_dir.join("acl.yaml");
        acl_manager
            .write_config(&acl_path)
            .map_err(|e| anyhow::anyhow!("Failed to write ACL: {e}"))?;
        Ok(())
    }

    fn write_skills(&self, modules: &[apcore_toolkit::ScannedModule]) -> anyhow::Result<()> {
        let Some(ref skills_dir) = self.skills_dir else {
            return Ok(());
        };
        let paths = crate::output::SkillOutput::new()
            .write(modules, skills_dir)
            .map_err(|e| anyhow::anyhow!("Failed to write skill files: {e}"))?;
        for path in &paths {
            info!(path = %path.display(), "Generated skill");
        }
        Ok(())
    }

    fn print_results(&self, results: &[crate::models::ScannedCLITool]) -> anyhow::Result<()> {
        for tool in results {
            match self.format.as_str() {
                "json" => println!("{}", serde_json::to_string_pretty(tool)?),
                "yaml" => println!("{}", serde_yaml::to_string(tool)?),
                _ => Self::print_tool_table(tool),
            }
        }
        Ok(())
    }

    fn print_tool_table(tool: &crate::models::ScannedCLITool) {
        println!(
            "Tool: {} ({})",
            tool.name,
            tool.version.as_deref().unwrap_or("unknown")
        );
        println!("  Binary: {}", tool.binary_path);
        println!("  Scan tier: {}", tool.scan_tier);
        println!("  Subcommands: {}", tool.subcommands.len());
        println!("  Global flags: {}", tool.global_flags.len());
        if tool.structured_output.supported {
            println!(
                "  Structured output: {} ({})",
                tool.structured_output.flag.as_deref().unwrap_or(""),
                tool.structured_output.format.as_deref().unwrap_or("")
            );
        }
        if !tool.warnings.is_empty() {
            println!("  Warnings: {}", tool.warnings.join(", "));
        }
        println!();
    }
}

/// Start MCP server for scanned CLI tools.
#[derive(Debug, clap::Args)]
pub struct ServeArgs {
    /// MCP transport type
    #[arg(long, default_value = "stdio", value_parser = ["stdio", "http", "sse"])]
    pub transport: String,

    /// Host for HTTP transports
    #[arg(long, default_value = "127.0.0.1")]
    pub host: String,

    /// Port for HTTP transports (1-65535)
    #[arg(long, default_value = "8000", value_parser = clap::value_parser!(u16).range(1..))]
    pub port: u16,

    /// Enable browser-based Tool Explorer UI (HTTP only)
    #[arg(long)]
    pub explorer: bool,

    /// Directory containing binding files
    #[arg(long)]
    pub modules_dir: Option<PathBuf>,

    /// MCP server name
    #[arg(long, default_value = "apexe")]
    pub name: String,

    /// Print integration config snippet (claude-desktop, cursor)
    #[arg(long)]
    pub show_config: Option<String>,

    /// Filter exposed tools by tags (comma-separated, AND logic)
    #[arg(long)]
    pub tags: Option<String>,

    /// Filter exposed tools by module ID prefix
    #[arg(long)]
    pub prefix: Option<String>,

    /// Path to ACL policy YAML file
    #[arg(long)]
    pub acl: Option<PathBuf>,

    /// Enable approval handler for destructive commands
    #[arg(long)]
    pub enable_approval: bool,

    /// Disable structured logging middleware
    #[arg(long)]
    pub no_logging: bool,

    /// Disable circuit breaker (short-circuits a hanging/broken tool)
    #[arg(long)]
    pub no_circuit_breaker: bool,

    /// Disable retry (only ever retries idempotent commands after a timeout)
    #[arg(long)]
    pub no_retry: bool,

    /// Enable /metrics (Prometheus) and /usage observability endpoints
    /// (HTTP/SSE transports only)
    #[arg(long)]
    pub metrics: bool,

    /// Skip input validation against tool schemas
    #[arg(long)]
    pub skip_validation: bool,
}

impl ServeArgs {
    pub fn execute(self, config: &ApexeConfig) -> anyhow::Result<()> {
        if let Some(ref format) = self.show_config {
            println!(
                "{}",
                config_gen::generate_config(
                    format,
                    &self.name,
                    &self.transport,
                    &self.host,
                    self.port,
                )
            );
            return Ok(());
        }

        let server = self.build_server(config)?;
        let opts = self.serve_options();
        server
            .serve_with_options(opts)
            .map_err(|e| anyhow::anyhow!("{e}"))
    }

    fn build_server(&self, config: &ApexeConfig) -> anyhow::Result<apcore_mcp::APCoreMCP> {
        let modules_dir = self
            .modules_dir
            .clone()
            .unwrap_or_else(|| config.modules_dir.clone());
        let mut builder = crate::mcp::McpServerBuilder::new()
            .name(&self.name)
            .transport(&self.transport)
            .host(&self.host)
            .port(self.port)
            .explorer(self.explorer)
            .modules_dir(modules_dir)
            .timeout_ms(config.default_timeout * 1000)
            .enable_logging(!self.no_logging)
            .enable_approval(self.enable_approval)
            .enable_circuit_breaker(!self.no_circuit_breaker)
            .enable_retry(!self.no_retry)
            .enable_metrics(self.metrics)
            .validate_inputs(!self.skip_validation)
            .audit_path(config.audit_log.clone());

        // Only load ACL when explicitly specified via --acl flag.
        // Without --acl, the server runs without access control (all tools allowed).
        if let Some(ref acl_path) = self.acl {
            builder = builder.acl_path(acl_path);
        }

        if let Some(ref tags_str) = self.tags {
            let tags: Vec<String> = tags_str.split(',').map(|s| s.trim().to_string()).collect();
            builder = builder.tags(tags);
        }
        if let Some(ref prefix) = self.prefix {
            builder = builder.prefix(prefix);
        }

        builder.build().map_err(|e| anyhow::anyhow!("{e}"))
    }

    fn serve_options(&self) -> apcore_mcp::ServeOptions {
        apcore_mcp::ServeOptions {
            explorer: apcore_mcp::ExplorerOptions {
                explorer: self.explorer,
                allow_execute: true,
                ..Default::default()
            },
            ..Default::default()
        }
    }
}

/// Start A2A agent server for scanned CLI tools.
#[derive(Debug, clap::Args)]
pub struct A2aArgs {
    /// Base URL to bind the A2A server to
    #[arg(long, default_value = "http://127.0.0.1:8000")]
    pub url: String,

    /// Directory containing binding files
    #[arg(long)]
    pub modules_dir: Option<PathBuf>,

    /// A2A agent name
    #[arg(long, default_value = "apexe")]
    pub name: String,

    /// Enable browser-based Explorer UI
    #[arg(long)]
    pub explorer: bool,

    /// Path to ACL policy YAML file
    #[arg(long)]
    pub acl: Option<PathBuf>,

    // Note: no `--enable-approval` flag on `a2a`. Unlike MCP, A2A has no
    // interactive elicitation transport, and there is no CLI way to supply an
    // approval store, so `A2aServerBuilder::serve` fails fast on it. Approval on
    // A2A is a library-only feature (construct the builder with an approval
    // store directly). See ExecutorOptions::approval_store.
    /// Disable structured logging middleware
    #[arg(long)]
    pub no_logging: bool,

    /// Disable circuit breaker (short-circuits a hanging/broken tool)
    #[arg(long)]
    pub no_circuit_breaker: bool,

    /// Disable retry (only ever retries idempotent commands after a timeout)
    #[arg(long)]
    pub no_retry: bool,

    /// Per-task execution timeout in seconds
    #[arg(long, default_value = "300")]
    pub execution_timeout: u64,

    /// Allowed CORS origin (repeatable)
    #[arg(long)]
    pub cors_origin: Vec<String>,
}

impl A2aArgs {
    pub fn execute(self, config: &ApexeConfig) -> anyhow::Result<()> {
        let server = self.build_server(config);
        let runtime = tokio::runtime::Runtime::new()?;
        runtime
            .block_on(server.serve())
            .map_err(|e| anyhow::anyhow!("{e}"))
    }

    fn build_server(&self, config: &ApexeConfig) -> crate::a2a::A2aServerBuilder {
        let modules_dir = self
            .modules_dir
            .clone()
            .unwrap_or_else(|| config.modules_dir.clone());
        let mut builder = crate::a2a::A2aServerBuilder::new()
            .name(&self.name)
            .url(&self.url)
            .explorer(self.explorer)
            .modules_dir(modules_dir)
            .timeout_ms(config.default_timeout * 1000)
            .enable_logging(!self.no_logging)
            .enable_circuit_breaker(!self.no_circuit_breaker)
            .enable_retry(!self.no_retry)
            .execution_timeout(self.execution_timeout)
            .cors_origins(self.cors_origin.clone())
            .audit_path(config.audit_log.clone());

        // Only load ACL when explicitly specified via --acl flag.
        // Without --acl, the server runs without access control (all tools allowed).
        if let Some(ref acl_path) = self.acl {
            builder = builder.acl_path(acl_path);
        }

        builder
    }
}

/// List previously scanned CLI tools and their modules.
#[derive(Debug, clap::Args)]
pub struct ListArgs {
    /// Output format
    #[arg(long, default_value = "table", value_parser = ["json", "table"])]
    pub format: String,

    /// Directory containing binding files
    #[arg(long)]
    pub modules_dir: Option<PathBuf>,
}

impl ListArgs {
    pub fn execute(self, config: &ApexeConfig) -> anyhow::Result<()> {
        let modules_dir = self.modules_dir.as_ref().unwrap_or(&config.modules_dir);

        let modules = self.load_modules(modules_dir)?;
        if modules.is_empty() {
            println!("No modules found. Run 'apexe scan <tool>' first.");
            return Ok(());
        }

        self.print_modules(&modules)?;
        Ok(())
    }

    fn load_modules(
        &self,
        dir: &std::path::Path,
    ) -> anyhow::Result<Vec<apcore_toolkit::ScannedModule>> {
        // An absent directory legitimately means "no modules yet". A load
        // FAILURE (e.g. a corrupt .binding.yaml) must surface, not be masked as
        // an empty list that misleadingly prints "No modules found".
        if !dir.exists() {
            return Ok(vec![]);
        }
        crate::output::load_modules_from_dir(dir).map_err(|e| anyhow::anyhow!(e))
    }

    fn print_modules(&self, modules: &[apcore_toolkit::ScannedModule]) -> anyhow::Result<()> {
        let mut sorted: Vec<_> = modules
            .iter()
            .map(|m| (m.module_id.as_str(), m.description.as_str()))
            .collect();
        sorted.sort_by(|a, b| a.0.cmp(b.0));

        match self.format.as_str() {
            "json" => {
                let json: Vec<serde_json::Value> = sorted
                    .iter()
                    .map(|(id, desc)| serde_json::json!({"module_id": id, "description": desc}))
                    .collect();
                println!("{}", serde_json::to_string_pretty(&json)?);
            }
            _ => {
                println!("{:<40} DESCRIPTION", "MODULE ID");
                println!("{:<40} {}", "\u{2500}".repeat(40), "\u{2500}".repeat(40));
                for (id, desc) in &sorted {
                    let truncated = if desc.chars().count() > 60 {
                        format!("{}...", desc.chars().take(57).collect::<String>())
                    } else {
                        desc.to_string()
                    };
                    println!("{:<40} {}", id, truncated);
                }
                println!("\n{} module(s) found.", sorted.len());
            }
        }
        Ok(())
    }
}

/// Show or initialize apexe configuration.
#[derive(Debug, clap::Args)]
pub struct ConfigArgs {
    /// Show current configuration
    #[arg(long)]
    pub show: bool,

    /// Initialize default config file
    #[arg(long)]
    pub init: bool,
}

impl ConfigArgs {
    pub fn execute(self, config: &ApexeConfig) -> anyhow::Result<()> {
        if self.show {
            let yaml = serde_yaml::to_string(config)?;
            println!("{yaml}");
        }
        if self.init {
            let config_path = config.config_dir.join("config.yaml");
            if !config_path.exists() {
                let default = ApexeConfig::default();
                let yaml = serde_yaml::to_string(&default)?;
                std::fs::write(&config_path, yaml)?;
                println!("Config written to {}", config_path.display());
            } else {
                println!("Config already exists at {}", config_path.display());
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_scan_subcommand() {
        let cli = Cli::try_parse_from(["apexe", "scan", "git"]).unwrap();
        assert!(matches!(cli.command, Commands::Scan(_)));
        if let Commands::Scan(args) = cli.command {
            assert_eq!(args.tools, vec!["git".to_string()]);
        }
    }

    #[test]
    fn test_parse_no_subcommand_fails() {
        let result = Cli::try_parse_from(["apexe"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_log_level_flag() {
        let cli = Cli::try_parse_from(["apexe", "--log-level", "debug", "scan", "git"]).unwrap();
        assert_eq!(cli.log_level.as_deref(), Some("debug"));
        // Explicit flag wins over the config-resolved level.
        assert_eq!(cli.effective_log_level("warn"), "debug");
    }

    #[test]
    fn test_parse_default_log_level() {
        let cli = Cli::try_parse_from(["apexe", "scan", "git"]).unwrap();
        assert_eq!(cli.log_level, None);
        // With no flag, the resolved config level is used (not a hardcoded "info").
        assert_eq!(cli.effective_log_level("debug"), "debug");
    }

    // ScanArgs validation tests
    #[test]
    fn test_scan_no_tools_fails() {
        let result = Cli::try_parse_from(["apexe", "scan"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_scan_depth_zero_fails() {
        let result = Cli::try_parse_from(["apexe", "scan", "git", "--depth", "0"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_scan_depth_six_fails() {
        let result = Cli::try_parse_from(["apexe", "scan", "git", "--depth", "6"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_scan_depth_three_succeeds() {
        let cli = Cli::try_parse_from(["apexe", "scan", "git", "--depth", "3"]).unwrap();
        if let Commands::Scan(args) = cli.command {
            assert_eq!(args.depth, 3);
        }
    }

    #[test]
    fn test_scan_format_xml_fails() {
        let result = Cli::try_parse_from(["apexe", "scan", "git", "--format", "xml"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_scan_format_json_succeeds() {
        let cli = Cli::try_parse_from(["apexe", "scan", "git", "--format", "json"]).unwrap();
        if let Commands::Scan(args) = cli.command {
            assert_eq!(args.format, "json");
        }
    }

    #[test]
    fn test_scan_multiple_tools() {
        let cli = Cli::try_parse_from(["apexe", "scan", "git", "docker"]).unwrap();
        if let Commands::Scan(args) = cli.command {
            assert_eq!(args.tools, vec!["git".to_string(), "docker".to_string()]);
        }
    }

    #[test]
    fn test_scan_default_depth() {
        let cli = Cli::try_parse_from(["apexe", "scan", "git"]).unwrap();
        if let Commands::Scan(args) = cli.command {
            assert_eq!(args.depth, 2);
        }
    }

    #[test]
    fn test_scan_default_format() {
        let cli = Cli::try_parse_from(["apexe", "scan", "git"]).unwrap();
        if let Commands::Scan(args) = cli.command {
            assert_eq!(args.format, "table");
        }
    }

    #[test]
    fn test_scan_skills_dir_default_none() {
        let cli = Cli::try_parse_from(["apexe", "scan", "git"]).unwrap();
        if let Commands::Scan(args) = cli.command {
            assert!(args.skills_dir.is_none());
        }
    }

    #[test]
    fn test_scan_skills_dir_flag() {
        let cli =
            Cli::try_parse_from(["apexe", "scan", "git", "--skills-dir", "/tmp/skills"]).unwrap();
        if let Commands::Scan(args) = cli.command {
            assert_eq!(args.skills_dir, Some(PathBuf::from("/tmp/skills")));
        }
    }

    // ServeArgs validation tests
    #[test]
    fn test_serve_defaults() {
        let cli = Cli::try_parse_from(["apexe", "serve"]).unwrap();
        if let Commands::Serve(args) = cli.command {
            assert_eq!(args.transport, "stdio");
            assert_eq!(args.host, "127.0.0.1");
            assert_eq!(args.port, 8000);
            assert!(!args.explorer);
        }
    }

    #[test]
    fn test_serve_invalid_transport_fails() {
        let result = Cli::try_parse_from(["apexe", "serve", "--transport", "invalid"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_serve_port_zero_fails() {
        let result = Cli::try_parse_from(["apexe", "serve", "--port", "0"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_serve_with_all_flags() {
        let cli = Cli::try_parse_from([
            "apexe",
            "serve",
            "--transport",
            "http",
            "--host",
            "0.0.0.0",
            "--port",
            "9000",
            "--explorer",
        ])
        .unwrap();
        if let Commands::Serve(args) = cli.command {
            assert_eq!(args.transport, "http");
            assert_eq!(args.host, "0.0.0.0");
            assert_eq!(args.port, 9000);
            assert!(args.explorer);
        }
    }

    #[test]
    fn test_serve_resilience_flags_default_enabled() {
        let cli = Cli::try_parse_from(["apexe", "serve"]).unwrap();
        if let Commands::Serve(args) = cli.command {
            assert!(!args.no_circuit_breaker);
            assert!(!args.no_retry);
        } else {
            panic!("expected Commands::Serve");
        }
    }

    #[test]
    fn test_serve_metrics_default_disabled() {
        let cli = Cli::try_parse_from(["apexe", "serve"]).unwrap();
        if let Commands::Serve(args) = cli.command {
            assert!(!args.metrics);
        } else {
            panic!("expected Commands::Serve");
        }
    }

    #[test]
    fn test_serve_metrics_flag() {
        let cli = Cli::try_parse_from(["apexe", "serve", "--metrics"]).unwrap();
        if let Commands::Serve(args) = cli.command {
            assert!(args.metrics);
        } else {
            panic!("expected Commands::Serve");
        }
    }

    #[test]
    fn test_serve_resilience_flags_can_be_disabled() {
        let cli =
            Cli::try_parse_from(["apexe", "serve", "--no-circuit-breaker", "--no-retry"]).unwrap();
        if let Commands::Serve(args) = cli.command {
            assert!(args.no_circuit_breaker);
            assert!(args.no_retry);
        } else {
            panic!("expected Commands::Serve");
        }
    }

    // A2aArgs validation tests
    #[test]
    fn test_a2a_rejects_enable_approval_flag() {
        // A2A has no elicitation and no CLI approval-store path, so
        // --enable-approval is not a valid a2a flag (it would only ever error
        // at serve time). It must be rejected at parse.
        let result = Cli::try_parse_from(["apexe", "a2a", "--enable-approval"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_a2a_defaults() {
        let cli = Cli::try_parse_from(["apexe", "a2a"]).unwrap();
        if let Commands::A2a(args) = cli.command {
            assert_eq!(args.url, "http://127.0.0.1:8000");
            assert_eq!(args.execution_timeout, 300);
            assert!(!args.explorer);
            assert!(args.cors_origin.is_empty());
        } else {
            panic!("expected Commands::A2a");
        }
    }

    #[test]
    fn test_a2a_with_flags() {
        let cli = Cli::try_parse_from([
            "apexe",
            "a2a",
            "--url",
            "http://0.0.0.0:9090",
            "--explorer",
            "--execution-timeout",
            "600",
            "--cors-origin",
            "https://example.com",
            "--cors-origin",
            "https://foo.example.com",
        ])
        .unwrap();
        if let Commands::A2a(args) = cli.command {
            assert_eq!(args.url, "http://0.0.0.0:9090");
            assert!(args.explorer);
            assert_eq!(args.execution_timeout, 600);
            assert_eq!(
                args.cors_origin,
                vec!["https://example.com", "https://foo.example.com"]
            );
        } else {
            panic!("expected Commands::A2a");
        }
    }

    #[test]
    fn test_a2a_resilience_flags_can_be_disabled() {
        let cli =
            Cli::try_parse_from(["apexe", "a2a", "--no-circuit-breaker", "--no-retry"]).unwrap();
        if let Commands::A2a(args) = cli.command {
            assert!(args.no_circuit_breaker);
            assert!(args.no_retry);
        } else {
            panic!("expected Commands::A2a");
        }
    }

    // ListArgs validation tests
    #[test]
    fn test_list_default_format() {
        let cli = Cli::try_parse_from(["apexe", "list"]).unwrap();
        if let Commands::List(args) = cli.command {
            assert_eq!(args.format, "table");
        }
    }

    #[test]
    fn test_list_format_json() {
        let cli = Cli::try_parse_from(["apexe", "list", "--format", "json"]).unwrap();
        if let Commands::List(args) = cli.command {
            assert_eq!(args.format, "json");
        }
    }

    #[test]
    fn test_list_format_xml_fails() {
        let result = Cli::try_parse_from(["apexe", "list", "--format", "xml"]);
        assert!(result.is_err());
    }

    // ConfigArgs tests
    #[test]
    fn test_config_show_flag() {
        let cli = Cli::try_parse_from(["apexe", "config", "--show"]).unwrap();
        if let Commands::Config(args) = cli.command {
            assert!(args.show);
            assert!(!args.init);
        }
    }

    #[test]
    fn test_config_init_flag() {
        let cli = Cli::try_parse_from(["apexe", "config", "--init"]).unwrap();
        if let Commands::Config(args) = cli.command {
            assert!(!args.show);
            assert!(args.init);
        }
    }

    #[test]
    fn test_config_no_flags_parses() {
        let cli = Cli::try_parse_from(["apexe", "config"]).unwrap();
        if let Commands::Config(args) = cli.command {
            assert!(!args.show);
            assert!(!args.init);
        }
    }

    // ConfigArgs execute tests
    #[test]
    fn test_config_no_flags_is_noop() {
        let config = ApexeConfig::default();
        let args = ConfigArgs {
            show: false,
            init: false,
        };
        let result = args.execute(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_config_show_outputs_valid_yaml() {
        let tmp = tempfile::TempDir::new().unwrap();
        let config = ApexeConfig {
            modules_dir: tmp.path().join("modules"),
            cache_dir: tmp.path().join("cache"),
            config_dir: tmp.path().to_path_buf(),
            audit_log: tmp.path().join("audit.jsonl"),
            log_level: "info".to_string(),
            default_timeout: 30,
            scan_depth: 2,
            json_output_preference: true,
            ..ApexeConfig::default()
        };

        // --show should serialize to valid YAML
        let yaml = serde_yaml::to_string(&config).unwrap();
        let deserialized: ApexeConfig = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(deserialized.log_level, "info");
        assert_eq!(deserialized.default_timeout, 30);
    }

    #[test]
    fn test_config_init_creates_file() {
        let tmp = tempfile::TempDir::new().unwrap();
        let config = ApexeConfig {
            modules_dir: tmp.path().join("modules"),
            cache_dir: tmp.path().join("cache"),
            config_dir: tmp.path().to_path_buf(),
            audit_log: tmp.path().join("audit.jsonl"),
            log_level: "info".to_string(),
            default_timeout: 30,
            scan_depth: 2,
            json_output_preference: true,
            ..ApexeConfig::default()
        };

        let args = ConfigArgs {
            show: false,
            init: true,
        };
        args.execute(&config).unwrap();

        let config_path = tmp.path().join("config.yaml");
        assert!(config_path.exists());

        // Verify the written file is valid YAML
        let contents = std::fs::read_to_string(&config_path).unwrap();
        let parsed: ApexeConfig = serde_yaml::from_str(&contents).unwrap();
        assert_eq!(parsed.log_level, "info");
    }

    #[test]
    fn test_config_init_does_not_overwrite() {
        let tmp = tempfile::TempDir::new().unwrap();
        let config_path = tmp.path().join("config.yaml");
        std::fs::write(&config_path, "existing content").unwrap();

        let config = ApexeConfig {
            modules_dir: tmp.path().join("modules"),
            cache_dir: tmp.path().join("cache"),
            config_dir: tmp.path().to_path_buf(),
            audit_log: tmp.path().join("audit.jsonl"),
            log_level: "info".to_string(),
            default_timeout: 30,
            scan_depth: 2,
            json_output_preference: true,
            ..ApexeConfig::default()
        };

        let args = ConfigArgs {
            show: false,
            init: true,
        };
        args.execute(&config).unwrap();

        let contents = std::fs::read_to_string(&config_path).unwrap();
        assert_eq!(contents, "existing content");
    }

    // ScanArgs execute error case test
    #[test]
    fn test_scan_execute_nonexistent_tool_errors() {
        let config = ApexeConfig::default();
        let args = ScanArgs {
            tools: vec!["nonexistent_tool_xyz_12345".to_string()],
            output_dir: None,
            depth: 2,
            no_cache: false,
            format: "table".to_string(),
            skills_dir: None,
        };
        let result = args.execute(&config);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("not found on PATH"),
            "Expected 'not found on PATH' in error, got: {err_msg}"
        );
    }

    #[test]
    fn test_scan_write_bindings_surfaces_write_failure() {
        // A binding-write failure must return Err (non-zero exit), not warn-and-continue.
        let tmp = tempfile::TempDir::new().unwrap();
        // Use an output path whose parent component is a file, so create_dir_all fails.
        let file_path = tmp.path().join("iamafile");
        std::fs::write(&file_path, "x").unwrap();
        let bad_output = file_path.join("nested");

        let args = ScanArgs {
            tools: vec!["echo".to_string()],
            output_dir: None,
            depth: 2,
            no_cache: false,
            format: "table".to_string(),
            skills_dir: None,
        };
        let modules = vec![apcore_toolkit::ScannedModule::new(
            "cli.echo".to_string(),
            "Echo".to_string(),
            serde_json::json!({"type": "object"}),
            serde_json::json!({"type": "object"}),
            vec!["cli".to_string()],
            "exec:///bin/echo".to_string(),
        )];

        let result = args.write_bindings(&modules, &bad_output);
        assert!(
            result.is_err(),
            "write_bindings must surface a write failure, not swallow it"
        );
    }

    #[test]
    fn test_list_load_modules_surfaces_corrupt_binding() {
        // A corrupt binding file must surface as an error, not a misleading empty list.
        let tmp = tempfile::TempDir::new().unwrap();
        std::fs::write(
            tmp.path().join("bad.binding.yaml"),
            "not: valid: binding: [[[",
        )
        .unwrap();

        let args = ListArgs {
            format: "table".to_string(),
            modules_dir: Some(tmp.path().to_path_buf()),
        };
        let result = args.load_modules(tmp.path());
        assert!(
            result.is_err(),
            "corrupt binding must surface, not collapse to an empty module list"
        );
    }
}