dynamic-cli 0.7.0

A framework for building configurable CLI and REPL applications from YAML/JSON configuration files
Documentation
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
//! Command handler trait and related types
//!
//! This module defines the core trait that all command implementations must implement.
//! The trait is designed to be object-safe, meaning it can be used as a trait object
//! (`&dyn CommandHandler`), which is critical for dynamic command registration.
//!
//! # Design Principles
//!
//! ## Object Safety
//!
//! The `CommandHandler` trait is intentionally kept simple and object-safe:
//! - No generic methods (would prevent trait object usage)
//! - No associated types with type parameters
//! - All methods use concrete types or trait objects
//!
//! This allows the registry to store handlers as `Box<dyn CommandHandler>`,
//! enabling dynamic command registration at runtime.
//!
//! ## Simple Type Signatures
//!
//! Arguments are passed as [`crate::parser::ParsedArgs`] rather than generic
//! types. This design choice:
//! - Maintains object safety
//! - Represents both scalar and repeatable-option values (DD-024)
//! - Delegates type parsing to the parser module
//!
//! ## Thread Safety
//!
//! All handlers must be `Send + Sync` to support:
//! - Shared access across threads
//! - Potential async execution in the future
//! - Safe usage in multi-threaded contexts
//!
//! # Example
//!
//! ```
//! use dynamic_cli::executor::{CommandHandler, ParsedArgs};
//! use dynamic_cli::context::ExecutionContext;
//! use dynamic_cli::Result;
//!
//! // Define a simple command handler
//! struct HelloCommand;
//!
//! impl CommandHandler for HelloCommand {
//!     fn execute(
//!         &self,
//!         _context: &mut dyn ExecutionContext,
//!         args: &ParsedArgs,
//!     ) -> Result<()> {
//!         let name = args.get_scalar("name").unwrap_or("World");
//!         println!("Hello, {}!", name);
//!         Ok(())
//!     }
//! }
//! ```

use crate::context::ExecutionContext;
use crate::error::Result;
use crate::parser::ParsedArgs;
use async_trait::async_trait;

/// Trait for command implementations
///
/// Each command in the CLI/REPL application must implement this trait.
/// The trait is designed to be object-safe, allowing commands to be
/// stored and invoked dynamically through trait objects.
///
/// # Object Safety
///
/// This trait is intentionally object-safe (can be used as `dyn CommandHandler`).
/// **Do not add methods with generic type parameters**, as this would break
/// object safety and prevent dynamic dispatch.
///
/// # Thread Safety
///
/// Implementations must be `Send + Sync` to allow:
/// - Sharing command handlers across threads
/// - Safe concurrent access to the command registry
/// - Future async execution support
///
/// # Execution Flow
///
/// 1. Parser converts user input to [`crate::parser::ParsedArgs`]
/// 2. Validator checks argument constraints
/// 3. `validate()` is called for custom validation (optional)
/// 4. `execute()` is called with validated arguments
///
/// # Example
///
/// ```
/// use dynamic_cli::error::ExecutionError;
/// use dynamic_cli::executor::{CommandHandler, ParsedArgs};
/// use dynamic_cli::context::ExecutionContext;
/// use dynamic_cli::Result;
///
/// struct GreetCommand;
///
/// impl CommandHandler for GreetCommand {
///     fn execute(
///         &self,
///         _context: &mut dyn ExecutionContext,
///         args: &ParsedArgs,
///     ) -> Result<()> {
///         let name = args.get_scalar("name")
///             .ok_or_else(|| {
///                 ExecutionError::CommandFailed(
///                     anyhow::anyhow!("Missing 'name' argument")
///              )
///          })?;
///         
///         let greeting = if let Some(formal) = args.get_scalar("formal") {
///             if formal == "true" {
///                 format!("Good day, {}.", name)
///             } else {
///                 format!("Hi, {}!", name)
///             }
///         } else {
///             format!("Hello, {}!", name)
///         };
///         
///         println!("{}", greeting);
///         Ok(())
///     }
///     
///     fn validate(&self, args: &ParsedArgs) -> Result<()> {
///         // Custom validation: name must not be empty
///         if let Some(name) = args.get_scalar("name") {
///             if name.trim().is_empty() {
///                 return Err(ExecutionError::CommandFailed(
///                         anyhow::anyhow!("Name cannot be empty")
///                 ).into());
///             }
///         }
///         Ok(())
///     }
/// }
/// ```
pub trait CommandHandler: Send + Sync {
    /// Execute the command with the given context and arguments
    ///
    /// This is the main entry point for command execution. It receives:
    /// - A mutable reference to the execution context (for shared state)
    /// - A map of argument names to their string values
    ///
    /// # Arguments
    ///
    /// * `context` - Mutable execution context for sharing state between commands.
    ///   Use `downcast_ref` or `downcast_mut` from the `context` module
    ///   to access your specific context type.
    ///
    /// * `args` - Parsed and validated arguments as name-value pairs.
    ///   All values are strings; type conversion should be done
    ///   within the handler if needed.
    ///
    /// # Returns
    ///
    /// - `Ok(())` if execution succeeds
    /// - `Err(DynamicCliError)` if execution fails
    ///
    /// # Errors
    ///
    /// Implementations should return errors for:
    /// - Invalid argument values (caught by validate, but can be rechecked)
    /// - Execution failures (I/O errors, computation errors, etc.)
    /// - Invalid context state
    ///
    /// Use `ExecutionError::CommandFailed` to wrap application-specific errors:
    /// ```ignore
    /// Err(ExecutionError::CommandFailed(anyhow::anyhow!("Details")).into())
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// # use dynamic_cli::error::ExecutionError;
    /// # use dynamic_cli::executor::{CommandHandler, ParsedArgs};
    /// # use dynamic_cli::context::ExecutionContext;
    /// # use dynamic_cli::Result;
    /// #
    /// struct FileCommand;
    ///
    /// impl CommandHandler for FileCommand {
    ///     fn execute(
    ///         &self,
    ///         _context: &mut dyn ExecutionContext,
    ///         args: &ParsedArgs,
    ///     ) -> Result<()> {
    ///         let path = args.get_scalar("path")
    ///             .ok_or_else(|| {
    ///                ExecutionError::CommandFailed(
    ///                       anyhow::anyhow!("Missing path argument")
    ///             )
    ///          })?;
    ///         
    ///         // Perform the actual work
    ///         let content = std::fs::read_to_string(path)
    ///             .map_err(|e| {
    ///                ExecutionError::CommandFailed(anyhow::anyhow!("Failed to read file: {}", e))
    ///         })?;
    ///         
    ///         println!("File contains {} bytes", content.len());
    ///         Ok(())
    ///     }
    /// }
    /// ```
    fn execute(&self, context: &mut dyn ExecutionContext, args: &ParsedArgs) -> Result<()>;

    /// Optional custom validation for arguments
    ///
    /// This method is called after the standard validation (type checking,
    /// required arguments, etc.) but before execution. It allows commands
    /// to implement custom validation logic.
    ///
    /// # Default Implementation
    ///
    /// The default implementation accepts all arguments (returns `Ok(())`).
    /// Override this method only if you need custom validation.
    ///
    /// # Arguments
    ///
    /// * `args` - The arguments to validate
    ///
    /// # Returns
    ///
    /// - `Ok(())` if validation succeeds
    /// - `Err(DynamicCliError)` if validation fails
    ///
    /// # Example
    ///
    /// ```
    /// # use dynamic_cli::executor::{CommandHandler, ParsedArgs};
    /// # use dynamic_cli::context::ExecutionContext;
    /// # use dynamic_cli::error::ExecutionError;
    /// # use dynamic_cli::Result;
    /// #
    /// struct RangeCommand;
    ///
    /// impl CommandHandler for RangeCommand {
    ///     fn execute(
    ///         &self,
    ///         _context: &mut dyn ExecutionContext,
    ///         args: &ParsedArgs,
    ///     ) -> Result<()> {
    ///         // Execution logic here
    ///         Ok(())
    ///     }
    ///     
    ///     fn validate(&self, args: &ParsedArgs) -> Result<()> {
    ///         // Custom validation: ensure min < max
    ///         if let (Some(min), Some(max)) = (args.get_scalar("min"), args.get_scalar("max")) {
    ///             let min_val: f64 = min.parse()
    ///                 .map_err(|_| {
    ///                     ExecutionError::CommandFailed(anyhow::anyhow!("Invalid min value"))
    ///             })?;
    ///             let max_val: f64 = max.parse()
    ///                 .map_err(|_| {ExecutionError::CommandFailed(anyhow::anyhow!("Invalid max value"))})?;
    ///             
    ///             if min_val >= max_val {
    ///                 return Err(ExecutionError::CommandFailed(anyhow::anyhow!("min must be less than max")).into());
    ///             }
    ///         }
    ///         Ok(())
    ///     }
    /// }
    /// ```
    fn validate(&self, _args: &ParsedArgs) -> Result<()> {
        Ok(())
    }
}

/// Async counterpart of [`CommandHandler`].
///
/// Additive to `CommandHandler` (see DD-022) — it does not replace it.
/// Implementations use this trait when their command body needs to perform
/// async I/O (network calls, streaming, etc.). The signatures deliberately
/// mirror `CommandHandler` exactly, `execute`/`validate` aside from the
/// `async` keyword, so that migrating a handler from sync to async is a
/// mechanical change.
///
/// # Object Safety
///
/// Made `dyn`-compatible via `#[async_trait]` (which desugars `async fn` to
/// a boxed, pinned future under the hood). Stored as `Box<dyn
/// AsyncCommandHandler>` in the registry, exactly like `CommandHandler` is
/// stored as `Box<dyn CommandHandler>`.
///
/// # Thread Safety
///
/// Same constraint as `CommandHandler`: `Send + Sync` is required so the
/// handler can be shared across the registry and, transitively, across
/// threads if the application needs it.
///
/// # Why a separate trait instead of an async `CommandHandler`?
///
/// Existing sync `CommandHandler` implementations (including downstream
/// consumers) must keep compiling unchanged. See DD-022 for the full
/// rationale, including why `tokio` is not a dependency of `dynamic-cli`
/// itself and why driving the returned future via
/// `futures::executor::block_on` at the dispatch site is safe.
///
/// # Example
///
/// ```
/// use async_trait::async_trait;
/// use dynamic_cli::executor::{AsyncCommandHandler, ParsedArgs};
/// use dynamic_cli::context::ExecutionContext;
/// use dynamic_cli::error::ExecutionError;
/// use dynamic_cli::Result;
///
/// struct FetchCommand;
///
/// #[async_trait]
/// impl AsyncCommandHandler for FetchCommand {
///     async fn execute(
///         &self,
///         _context: &mut dyn ExecutionContext,
///         args: &ParsedArgs,
///     ) -> Result<()> {
///         let url = args.get_scalar("url").ok_or_else(|| {
///             ExecutionError::CommandFailed(anyhow::anyhow!("Missing 'url' argument"))
///         })?;
///         // Real implementations would `.await` an async HTTP call here.
///         println!("Fetching {url}...");
///         Ok(())
///     }
///
///     async fn validate(&self, args: &ParsedArgs) -> Result<()> {
///         if args.get_scalar("url").is_none() {
///             return Err(ExecutionError::CommandFailed(anyhow::anyhow!("url is required")).into());
///         }
///         Ok(())
///     }
/// }
/// ```
#[async_trait]
pub trait AsyncCommandHandler: Send + Sync {
    /// Async equivalent of [`CommandHandler::execute`]. Same contract:
    /// receives the mutable execution context and the parsed arguments,
    /// returns `Ok(())` on success or a `DynamicCliError` on failure.
    async fn execute(&self, context: &mut dyn ExecutionContext, args: &ParsedArgs) -> Result<()>;

    /// Async equivalent of [`CommandHandler::validate`]. Same contract and
    /// same default (accepts all arguments) — override only for custom
    /// validation logic.
    async fn validate(&self, _args: &ParsedArgs) -> Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::ExecutionError;
    use std::any::Any;
    use std::collections::HashMap;
    use std::sync::Arc;

    /// Test helper: build a scalar-only `ParsedArgs` from `[(key, value), ...]`.
    fn scalar_args<const N: usize>(pairs: [(&str, &str); N]) -> ParsedArgs {
        let map: HashMap<String, String> = pairs
            .into_iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();
        ParsedArgs::from_scalars(map)
    }

    // ============================================================================
    // TEST FIXTURES
    // ============================================================================

    /// Simple test context for unit tests
    #[derive(Default)]
    struct TestContext {
        state: String,
    }

    impl ExecutionContext for TestContext {
        fn as_any(&self) -> &dyn Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
    }

    /// Simple command that prints to context
    struct HelloCommand;

    impl CommandHandler for HelloCommand {
        fn execute(&self, context: &mut dyn ExecutionContext, args: &ParsedArgs) -> Result<()> {
            let ctx = crate::context::downcast_mut::<TestContext>(context).ok_or_else(|| {
                ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type"))
            })?;

            let name = args.get_scalar("name").unwrap_or("World");
            ctx.state = format!("Hello, {}!", name);
            Ok(())
        }
    }

    /// Command with custom validation
    struct ValidatedCommand;

    impl CommandHandler for ValidatedCommand {
        fn execute(&self, _context: &mut dyn ExecutionContext, _args: &ParsedArgs) -> Result<()> {
            Ok(())
        }

        fn validate(&self, args: &ParsedArgs) -> Result<()> {
            // Require "count" argument to be present and > 0
            if let Some(count) = args.get_scalar("count") {
                let count_val: i32 = count.parse().map_err(|_| {
                    ExecutionError::CommandFailed(anyhow::anyhow!("count must be an integer"))
                })?;

                if count_val <= 0 {
                    return Err(ExecutionError::CommandFailed(anyhow::anyhow!(
                        "count must be positive"
                    ))
                    .into());
                }
            } else {
                return Err(
                    ExecutionError::CommandFailed(anyhow::anyhow!("count is required")).into(),
                );
            }
            Ok(())
        }
    }

    /// Command that fails during execution
    struct FailingCommand;

    impl CommandHandler for FailingCommand {
        fn execute(&self, _context: &mut dyn ExecutionContext, _args: &ParsedArgs) -> Result<()> {
            Err(ExecutionError::CommandFailed(anyhow::anyhow!("Simulated failure")).into())
        }
    }

    /// Command that modifies context
    struct StatefulCommand;

    impl CommandHandler for StatefulCommand {
        fn execute(&self, context: &mut dyn ExecutionContext, args: &ParsedArgs) -> Result<()> {
            let ctx = crate::context::downcast_mut::<TestContext>(context).ok_or_else(|| {
                ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type"))
            })?;

            let value = args.get_scalar("value").unwrap_or("default");
            ctx.state.push_str(value);
            Ok(())
        }
    }

    // ============================================================================
    // BASIC FUNCTIONALITY TESTS
    // ============================================================================

    #[test]
    fn test_basic_execution() {
        let handler = HelloCommand;
        let mut context = TestContext::default();
        let args = scalar_args([("name", "Rust")]);

        let result = handler.execute(&mut context, &args);

        assert!(result.is_ok());
        assert_eq!(context.state, "Hello, Rust!");
    }

    #[test]
    fn test_execution_without_args() {
        let handler = HelloCommand;
        let mut context = TestContext::default();
        let args = ParsedArgs::from_scalars(HashMap::new());

        let result = handler.execute(&mut context, &args);

        assert!(result.is_ok());
        assert_eq!(context.state, "Hello, World!");
    }

    #[test]
    fn test_execution_with_empty_name() {
        let handler = HelloCommand;
        let mut context = TestContext::default();
        let args = scalar_args([("name", "")]);

        let result = handler.execute(&mut context, &args);

        assert!(result.is_ok());
        assert_eq!(context.state, "Hello, !");
    }

    // ============================================================================
    // VALIDATION TESTS
    // ============================================================================

    #[test]
    fn test_default_validation_accepts_all() {
        let handler = HelloCommand;
        let args = scalar_args([("random", "value")]);

        let result = handler.validate(&args);

        assert!(result.is_ok());
    }

    #[test]
    fn test_custom_validation_success() {
        let handler = ValidatedCommand;
        let args = scalar_args([("count", "5")]);

        let result = handler.validate(&args);

        assert!(result.is_ok());
    }

    #[test]
    fn test_custom_validation_missing_arg() {
        let handler = ValidatedCommand;
        let args = ParsedArgs::from_scalars(HashMap::new());

        let result = handler.validate(&args);

        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("required"));
    }

    #[test]
    fn test_custom_validation_invalid_value() {
        let handler = ValidatedCommand;
        let args = scalar_args([("count", "0")]);

        let result = handler.validate(&args);

        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("positive"));
    }

    #[test]
    fn test_custom_validation_non_integer() {
        let handler = ValidatedCommand;
        let args = scalar_args([("count", "abc")]);

        let result = handler.validate(&args);

        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("integer"));
    }

    // ============================================================================
    // ERROR HANDLING TESTS
    // ============================================================================

    #[test]
    fn test_execution_failure() {
        let handler = FailingCommand;
        let mut context = TestContext::default();
        let args = ParsedArgs::from_scalars(HashMap::new());

        let result = handler.execute(&mut context, &args);

        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("Simulated failure"));
    }

    #[test]
    fn test_context_downcast_failure() {
        // Use a different context type to trigger downcast failure
        struct WrongContext;

        impl ExecutionContext for WrongContext {
            fn as_any(&self) -> &dyn Any {
                self
            }

            fn as_any_mut(&mut self) -> &mut dyn Any {
                self
            }
        }

        let handler = HelloCommand;
        let mut wrong_context = WrongContext;
        let args = ParsedArgs::from_scalars(HashMap::new());

        let result = handler.execute(&mut wrong_context, &args);

        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("Wrong context type"));
    }

    // ============================================================================
    // STATE MODIFICATION TESTS
    // ============================================================================

    #[test]
    fn test_context_state_modification() {
        let handler = StatefulCommand;
        let mut context = TestContext {
            state: "initial".to_string(),
        };
        let args = scalar_args([("value", "_modified")]);

        let result = handler.execute(&mut context, &args);

        assert!(result.is_ok());
        assert_eq!(context.state, "initial_modified");
    }

    #[test]
    fn test_multiple_executions_preserve_state() {
        let handler = StatefulCommand;
        let mut context = TestContext::default();

        // First execution
        let args1 = scalar_args([("value", "first")]);
        handler.execute(&mut context, &args1).unwrap();
        assert_eq!(context.state, "first");

        // Second execution
        let args2 = scalar_args([("value", "_second")]);
        handler.execute(&mut context, &args2).unwrap();
        assert_eq!(context.state, "first_second");
    }

    // ============================================================================
    // TRAIT OBJECT TESTS
    // ============================================================================

    #[test]
    fn test_trait_object_usage() {
        // Verify that CommandHandler can be used as a trait object
        let handler: Box<dyn CommandHandler> = Box::new(HelloCommand);
        let mut context = TestContext::default();
        let args = scalar_args([("name", "TraitObject")]);

        let result = handler.execute(&mut context, &args);

        assert!(result.is_ok());
        assert_eq!(context.state, "Hello, TraitObject!");
    }

    #[test]
    fn test_multiple_trait_objects() {
        // Store multiple handlers as trait objects
        let handlers: Vec<Box<dyn CommandHandler>> =
            vec![Box::new(HelloCommand), Box::new(StatefulCommand)];

        let mut context = TestContext::default();

        // Execute first handler
        let args1 = scalar_args([("name", "First")]);
        handlers[0].execute(&mut context, &args1).unwrap();
        assert_eq!(context.state, "Hello, First!");

        // Execute second handler
        context.state.clear();
        let args2 = scalar_args([("value", "Second")]);
        handlers[1].execute(&mut context, &args2).unwrap();
        assert_eq!(context.state, "Second");
    }

    // ============================================================================
    // THREAD SAFETY TESTS
    // ============================================================================

    #[test]
    fn test_send_sync_requirement() {
        // This test verifies that CommandHandler is Send + Sync
        // by using it in a multi-threaded context
        let handler: Arc<dyn CommandHandler> = Arc::new(HelloCommand);

        // Clone the Arc to simulate sharing across threads
        let handler_clone = handler.clone();

        // This compilation test ensures Send + Sync are satisfied
        let _ = std::thread::spawn(move || {
            let _h = handler_clone;
        });
    }

    #[test]
    fn test_concurrent_validation() {
        // Test that validation can be called from multiple threads
        let handler = Arc::new(ValidatedCommand);
        let handler_clone = handler.clone();

        let handle = std::thread::spawn(move || {
            let args = scalar_args([("count", "10")]);
            handler_clone.validate(&args)
        });

        let args = scalar_args([("count", "5")]);
        let result1 = handler.validate(&args);

        let result2 = handle.join().unwrap();

        assert!(result1.is_ok());
        assert!(result2.is_ok());
    }

    // ============================================================================
    // EDGE CASES
    // ============================================================================

    #[test]
    fn test_empty_args() {
        let handler = StatefulCommand;
        let mut context = TestContext::default();
        let args = ParsedArgs::from_scalars(HashMap::new());

        // Should use default value
        let result = handler.execute(&mut context, &args);

        assert!(result.is_ok());
        assert_eq!(context.state, "default");
    }

    #[test]
    fn test_args_with_special_characters() {
        let handler = HelloCommand;
        let mut context = TestContext::default();
        let args = scalar_args([("name", "Hello, 世界! 🌍")]);

        let result = handler.execute(&mut context, &args);

        assert!(result.is_ok());
        assert_eq!(context.state, "Hello, Hello, 世界! 🌍!");
    }

    #[test]
    fn test_very_long_argument() {
        let handler = HelloCommand;
        let mut context = TestContext::default();
        let long_name = "x".repeat(10000);
        let args = scalar_args([("name", long_name.as_str())]);

        let result = handler.execute(&mut context, &args);

        assert!(result.is_ok());
        assert!(context.state.contains(&long_name));
    }

    // ============================================================================
    // SHARED STATE TESTS
    // ============================================================================

    #[test]
    fn test_shared_mutable_context() {
        // Test that context can be safely modified by multiple commands
        let handler1 = StatefulCommand;
        let handler2 = StatefulCommand;
        let mut context = TestContext::default();

        let args1 = scalar_args([("value", "A")]);
        handler1.execute(&mut context, &args1).unwrap();

        let args2 = scalar_args([("value", "B")]);
        handler2.execute(&mut context, &args2).unwrap();

        assert_eq!(context.state, "AB");
    }

    // Test to ensure the trait is indeed object-safe at compile time
    #[test]
    fn test_object_safety_compile_time() {
        // This function signature requires CommandHandler to be object-safe
        fn _accepts_trait_object(_: &dyn CommandHandler) {}

        // If this compiles, the trait is object-safe
        let handler = HelloCommand;
        _accepts_trait_object(&handler);
    }

    // Test that demonstrates why we can't have generic methods
    // (This is a documentation test, not an actual test that runs)
    /// ```compile_fail
    /// use dynamic_cli::executor::CommandHandler;
    ///
    /// trait BrokenHandler: CommandHandler {
    ///     fn generic_method<T>(&self, value: T);
    /// }
    ///
    /// // This would fail because trait objects can't have generic methods
    /// fn use_as_trait_object(handler: &dyn BrokenHandler) {
    ///     // Cannot call generic_method on trait object
    /// }
    /// ```
    #[allow(dead_code)]
    fn test_no_generic_methods_documentation() {}

    // ============================================================================
    // AsyncCommandHandler TESTS (DD-022)
    // ============================================================================

    /// Async command that writes to the test context, mirroring `HelloCommand`.
    struct AsyncHelloCommand;

    #[async_trait]
    impl AsyncCommandHandler for AsyncHelloCommand {
        async fn execute(
            &self,
            context: &mut dyn ExecutionContext,
            args: &ParsedArgs,
        ) -> Result<()> {
            let ctx = crate::context::downcast_mut::<TestContext>(context).ok_or_else(|| {
                ExecutionError::CommandFailed(anyhow::anyhow!("Wrong context type"))
            })?;
            let name = args.get_scalar("name").unwrap_or("World");
            ctx.state = format!("Hello, {}!", name);
            Ok(())
        }
    }

    /// Async command with custom validation, mirroring `ValidatedCommand`.
    struct AsyncValidatedCommand;

    #[async_trait]
    impl AsyncCommandHandler for AsyncValidatedCommand {
        async fn execute(
            &self,
            _context: &mut dyn ExecutionContext,
            _args: &ParsedArgs,
        ) -> Result<()> {
            Ok(())
        }

        async fn validate(&self, args: &ParsedArgs) -> Result<()> {
            if args.get_scalar("count").is_none() {
                return Err(
                    ExecutionError::CommandFailed(anyhow::anyhow!("count is required")).into(),
                );
            }
            Ok(())
        }
    }

    /// Async command that fails during execution, mirroring `FailingCommand`.
    struct AsyncFailingCommand;

    #[async_trait]
    impl AsyncCommandHandler for AsyncFailingCommand {
        async fn execute(
            &self,
            _context: &mut dyn ExecutionContext,
            _args: &ParsedArgs,
        ) -> Result<()> {
            Err(ExecutionError::CommandFailed(anyhow::anyhow!("Simulated async failure")).into())
        }
    }

    #[test]
    fn test_async_basic_execution() {
        let handler = AsyncHelloCommand;
        let mut context = TestContext::default();
        let args = scalar_args([("name", "Rust")]);

        let result = futures::executor::block_on(handler.execute(&mut context, &args));

        assert!(result.is_ok());
        assert_eq!(context.state, "Hello, Rust!");
    }

    #[test]
    fn test_async_default_validation_accepts_all() {
        let handler = AsyncHelloCommand;
        let args = scalar_args([("random", "value")]);

        let result = futures::executor::block_on(handler.validate(&args));

        assert!(result.is_ok());
    }

    #[test]
    fn test_async_custom_validation_missing_arg() {
        let handler = AsyncValidatedCommand;
        let args = ParsedArgs::from_scalars(HashMap::new());

        let result = futures::executor::block_on(handler.validate(&args));

        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("required"));
    }

    #[test]
    fn test_async_custom_validation_success() {
        let handler = AsyncValidatedCommand;
        let args = scalar_args([("count", "5")]);

        let result = futures::executor::block_on(handler.validate(&args));

        assert!(result.is_ok());
    }

    #[test]
    fn test_async_execution_failure() {
        let handler = AsyncFailingCommand;
        let mut context = TestContext::default();
        let args = ParsedArgs::from_scalars(HashMap::new());

        let result = futures::executor::block_on(handler.execute(&mut context, &args));

        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("Simulated async failure"));
    }

    #[test]
    fn test_async_trait_object_usage() {
        // Verify that AsyncCommandHandler can be used as a trait object —
        // the core object-safety guarantee DD-022 depends on.
        let handler: Box<dyn AsyncCommandHandler> = Box::new(AsyncHelloCommand);
        let mut context = TestContext::default();
        let args = scalar_args([("name", "TraitObject")]);

        let result = futures::executor::block_on(handler.execute(&mut context, &args));

        assert!(result.is_ok());
        assert_eq!(context.state, "Hello, TraitObject!");
    }

    #[test]
    fn test_async_send_sync_requirement() {
        // Verifies AsyncCommandHandler is Send + Sync by sharing it across
        // threads via Arc — same pattern as test_send_sync_requirement above.
        let handler: Arc<dyn AsyncCommandHandler> = Arc::new(AsyncHelloCommand);
        let handler_clone = handler.clone();

        let _ = std::thread::spawn(move || {
            let _h = handler_clone;
        });
    }

    #[test]
    fn test_async_object_safety_compile_time() {
        // If this compiles, AsyncCommandHandler is dyn-compatible.
        fn _accepts_trait_object(_: &dyn AsyncCommandHandler) {}
        _accepts_trait_object(&AsyncHelloCommand);
    }
}