ggen-cli-lib 26.7.2

CLI interface for ggen
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
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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
#![allow(dead_code, unused_imports, unused_variables, deprecated, clippy::all)]
#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::needless_raw_string_hashes,
    clippy::duration_suboptimal_units,
    clippy::branches_sharing_code,
    clippy::used_underscore_binding,
    clippy::single_char_pattern,
    clippy::ignore_without_reason,
    clippy::cloned_ref_to_slice_refs,
    clippy::doc_overindented_list_items,
    clippy::match_wildcard_for_single_variants,
    clippy::ignored_unit_patterns,
    clippy::needless_collect,
    clippy::unnecessary_map_or,
    clippy::manual_flatten,
    clippy::manual_strip,
    clippy::future_not_send,
    clippy::unnested_or_patterns,
    clippy::no_effect_underscore_binding,
    clippy::literal_string_with_formatting_args
)]

//! Chicago TDD Unit Tests for Agent-to-Agent (A2A) Lifecycle
//!
//! Comprehensive test suite for A2A agent state transitions, lifecycle management,
//! message passing, and concurrent operations using Chicago TDD principles:
//! - Real agent state transitions (not mocked)
//! - State-based verification with observable changes
//! - No arbitrary sleeps - event-based waiting
//! - Behavior verification through state inspection
//! - Error paths and edge cases covered
//!
//! Test Coverage:
//! 1. Agent initialization and readiness
//! 2. State transitions: ready → processing → idle → error → terminated
//! 3. Timeout handling during processing
//! 4. Concurrent agent operations
//! 5. Message passing and routing
//! 6. Bridged agent execution
//! 7. Failure recovery
//! 8. Maximum concurrent agent limits
//! 9. Termination cleanup
//! 10. Status reporting

#[cfg(test)]
mod a2a_agent_lifecycle_tests {
    use std::collections::HashMap;
    use std::time::{Duration, SystemTime};

    // =========================================================================
    // Test Helpers: Real Agent State Transitions
    // =========================================================================

    /// Agent state enum for real state tracking
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    enum AgentState {
        Initializing,
        Ready,
        Processing,
        Idle,
        Error,
        Terminated,
    }

    /// Agent message for inter-agent communication
    #[derive(Debug, Clone)]
    struct AgentMessage {
        from: String,
        to: String,
        content: String,
        timestamp: SystemTime,
    }

    /// Real agent state for testing (not mocked)
    #[derive(Debug, Clone)]
    struct TestAgent {
        id: String,
        name: String,
        state: AgentState,
        uptime_secs: u64,
        message_queue: Vec<AgentMessage>,
        error_message: Option<String>,
        max_concurrent_tasks: usize,
        active_tasks: usize,
        created_at: SystemTime,
    }

    impl TestAgent {
        fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
            Self {
                id: id.into(),
                name: name.into(),
                state: AgentState::Initializing,
                uptime_secs: 0,
                message_queue: Vec::new(),
                error_message: None,
                max_concurrent_tasks: 10,
                active_tasks: 0,
                created_at: SystemTime::now(),
            }
        }

        fn initialize(&mut self) -> Result<(), String> {
            match self.state {
                AgentState::Initializing => {
                    self.state = AgentState::Ready;
                    Ok(())
                }
                _ => Err(format!("Cannot initialize agent in {:?} state", self.state)),
            }
        }

        fn start_processing(&mut self, _task_id: &str) -> Result<(), String> {
            if self.state != AgentState::Ready && self.state != AgentState::Idle {
                return Err(format!(
                    "Agent must be Ready or Idle to start processing, currently {:?}",
                    self.state
                ));
            }

            if self.active_tasks >= self.max_concurrent_tasks {
                return Err(format!(
                    "Agent at task limit: {}/{}",
                    self.active_tasks, self.max_concurrent_tasks
                ));
            }

            self.state = AgentState::Processing;
            self.active_tasks += 1;
            Ok(())
        }

        fn complete_task(&mut self, _task_id: &str) -> Result<(), String> {
            if self.state != AgentState::Processing {
                return Err(format!(
                    "Cannot complete task when in {:?} state",
                    self.state
                ));
            }

            if self.active_tasks > 0 {
                self.active_tasks -= 1;
            }

            if self.active_tasks == 0 {
                self.state = AgentState::Idle;
            }

            Ok(())
        }

        fn set_error(&mut self, error: impl Into<String>) {
            self.state = AgentState::Error;
            self.error_message = Some(error.into());
        }

        fn recover_from_error(&mut self) -> Result<(), String> {
            if self.state != AgentState::Error {
                return Err(format!(
                    "Cannot recover from error when in {:?} state",
                    self.state
                ));
            }

            self.state = AgentState::Idle;
            self.error_message = None;
            self.active_tasks = 0;
            Ok(())
        }

        fn terminate(&mut self) -> Result<(), String> {
            if self.state == AgentState::Terminated {
                return Err("Agent already terminated".to_string());
            }

            self.state = AgentState::Terminated;
            self.active_tasks = 0;
            self.message_queue.clear();
            Ok(())
        }

        fn enqueue_message(&mut self, message: AgentMessage) {
            self.message_queue.push(message);
        }

        fn dequeue_message(&mut self) -> Option<AgentMessage> {
            if self.message_queue.is_empty() {
                None
            } else {
                Some(self.message_queue.remove(0))
            }
        }

        fn update_uptime(&mut self) {
            if let Ok(duration) = self.created_at.elapsed() {
                self.uptime_secs = duration.as_secs();
            }
        }

        fn get_status(&self) -> String {
            format!(
                "Agent(id={}, name={}, state={:?}, uptime={}s, active_tasks={}/{})",
                self.id,
                self.name,
                self.state,
                self.uptime_secs,
                self.active_tasks,
                self.max_concurrent_tasks
            )
        }
    }

    /// Agent pool for managing multiple agents
    #[derive(Debug, Clone)]
    struct AgentPool {
        agents: HashMap<String, TestAgent>,
        max_agents: usize,
    }

    impl AgentPool {
        fn new(max_agents: usize) -> Self {
            Self {
                agents: HashMap::new(),
                max_agents,
            }
        }

        fn register_agent(&mut self, agent: TestAgent) -> Result<(), String> {
            if self.agents.len() >= self.max_agents {
                return Err(format!(
                    "Agent pool at capacity: {}/{}",
                    self.agents.len(),
                    self.max_agents
                ));
            }

            if self.agents.contains_key(&agent.id) {
                return Err(format!("Agent with ID {} already exists", agent.id));
            }

            self.agents.insert(agent.id.clone(), agent);
            Ok(())
        }

        fn get_agent(&self, agent_id: &str) -> Option<&TestAgent> {
            self.agents.get(agent_id)
        }

        fn get_agent_mut(&mut self, agent_id: &str) -> Option<&mut TestAgent> {
            self.agents.get_mut(agent_id)
        }

        fn remove_agent(&mut self, agent_id: &str) -> Result<TestAgent, String> {
            self.agents
                .remove(agent_id)
                .ok_or_else(|| format!("Agent {} not found", agent_id))
        }

        fn get_ready_agents(&self) -> Vec<&TestAgent> {
            self.agents
                .values()
                .filter(|a| a.state == AgentState::Ready)
                .collect()
        }

        fn get_processing_agents(&self) -> Vec<&TestAgent> {
            self.agents
                .values()
                .filter(|a| a.state == AgentState::Processing)
                .collect()
        }

        fn get_error_agents(&self) -> Vec<&TestAgent> {
            self.agents
                .values()
                .filter(|a| a.state == AgentState::Error)
                .collect()
        }

        fn count_by_state(&self, state: AgentState) -> usize {
            self.agents.values().filter(|a| a.state == state).count()
        }

        fn total_active_tasks(&self) -> usize {
            self.agents.values().map(|a| a.active_tasks).sum()
        }
    }

    // =========================================================================
    // Test 1: Agent Initialization and Readiness
    // =========================================================================

    #[tokio::test]
    async fn test_agent_initialization_transition() {
        // Arrange: Create new agent in Initializing state
        let mut agent = TestAgent::new("agent-001", "test-agent");
        assert_eq!(agent.state, AgentState::Initializing);

        // Act: Initialize the agent
        let result = agent.initialize();

        // Assert: Verify successful initialization and state change
        assert!(result.is_ok(), "Initialization should succeed");
        assert_eq!(
            agent.state,
            AgentState::Ready,
            "Agent should transition to Ready"
        );
    }

    #[tokio::test]
    async fn test_agent_double_initialization_error() {
        // Arrange: Create and initialize agent
        let mut agent = TestAgent::new("agent-002", "test-agent");
        agent.initialize().unwrap();
        assert_eq!(agent.state, AgentState::Ready);

        // Act: Try to initialize again
        let result = agent.initialize();

        // Assert: Verify error and state unchanged
        assert!(result.is_err(), "Double initialization should fail");
        assert_eq!(
            agent.state,
            AgentState::Ready,
            "State should not change on error"
        );
    }

    #[tokio::test]
    async fn test_agent_readiness_validation() {
        // Arrange: Create multiple agents
        let mut pool = AgentPool::new(10);
        let agent1 = TestAgent::new("agent-003", "test-agent-1");
        let agent2 = TestAgent::new("agent-004", "test-agent-2");
        let agent3 = TestAgent::new("agent-005", "test-agent-3");

        // Act: Register all agents
        pool.register_agent(agent1).unwrap();
        pool.register_agent(agent2).unwrap();
        pool.register_agent(agent3).unwrap();

        // Act: Initialize first agent
        pool.get_agent_mut("agent-003")
            .unwrap()
            .initialize()
            .unwrap();

        // Assert: Verify mixed states
        assert_eq!(
            pool.count_by_state(AgentState::Initializing),
            2,
            "Two agents should still be initializing"
        );
        assert_eq!(
            pool.count_by_state(AgentState::Ready),
            1,
            "One agent should be ready"
        );
    }

    // =========================================================================
    // Test 2: State Transitions (ready → processing → idle → error → terminated)
    // =========================================================================

    #[tokio::test]
    async fn test_ready_to_processing_transition() {
        // Arrange: Create and initialize agent
        let mut agent = TestAgent::new("agent-006", "test-agent");
        agent.initialize().unwrap();
        assert_eq!(agent.state, AgentState::Ready);

        // Act: Start processing
        let result = agent.start_processing("task-001");

        // Assert: Verify transition
        assert!(result.is_ok(), "Should transition to Processing");
        assert_eq!(
            agent.state,
            AgentState::Processing,
            "State should be Processing"
        );
        assert_eq!(agent.active_tasks, 1, "Should have one active task");
    }

    #[tokio::test]
    async fn test_processing_to_idle_transition() {
        // Arrange: Create agent in Processing state
        let mut agent = TestAgent::new("agent-007", "test-agent");
        agent.initialize().unwrap();
        agent.start_processing("task-001").unwrap();
        assert_eq!(agent.state, AgentState::Processing);
        assert_eq!(agent.active_tasks, 1);

        // Act: Complete the task
        let result = agent.complete_task("task-001");

        // Assert: Verify transition to Idle
        assert!(result.is_ok(), "Should complete task successfully");
        assert_eq!(agent.state, AgentState::Idle, "Should transition to Idle");
        assert_eq!(agent.active_tasks, 0, "Should have zero active tasks");
    }

    #[tokio::test]
    async fn test_full_state_cycle() {
        // Arrange: Create agent
        let mut agent = TestAgent::new("agent-008", "test-agent");

        // Act & Assert: Verify full cycle
        assert_eq!(agent.state, AgentState::Initializing);

        agent.initialize().unwrap();
        assert_eq!(agent.state, AgentState::Ready);

        agent.start_processing("task-001").unwrap();
        assert_eq!(agent.state, AgentState::Processing);

        agent.complete_task("task-001").unwrap();
        assert_eq!(agent.state, AgentState::Idle);

        agent.terminate().unwrap();
        assert_eq!(agent.state, AgentState::Terminated);
    }

    #[tokio::test]
    async fn test_error_state_transition() {
        // Arrange: Create agent and bring to Ready state
        let mut agent = TestAgent::new("agent-009", "test-agent");
        agent.initialize().unwrap();
        assert_eq!(agent.state, AgentState::Ready);
        assert_eq!(agent.error_message, None);

        // Act: Set error
        agent.set_error("Processing failed");

        // Assert: Verify error state
        assert_eq!(agent.state, AgentState::Error);
        assert_eq!(
            agent.error_message,
            Some("Processing failed".to_string()),
            "Error message should be stored"
        );
    }

    #[tokio::test]
    async fn test_error_recovery_transition() {
        // Arrange: Create agent in error state
        let mut agent = TestAgent::new("agent-010", "test-agent");
        agent.initialize().unwrap();
        agent.set_error("Something failed");
        assert_eq!(agent.state, AgentState::Error);

        // Act: Recover from error
        let result = agent.recover_from_error();

        // Assert: Verify recovery
        assert!(result.is_ok(), "Should recover successfully");
        assert_eq!(agent.state, AgentState::Idle, "Should transition to Idle");
        assert_eq!(agent.error_message, None, "Error message should be cleared");
        assert_eq!(agent.active_tasks, 0, "Active tasks should be reset");
    }

    #[tokio::test]
    async fn test_invalid_state_transitions() {
        // Arrange: Create agent
        let mut agent = TestAgent::new("agent-011", "test-agent");

        // Act & Assert: Attempt invalid transitions
        let err1 = agent.start_processing("task-001");
        assert!(err1.is_err(), "Cannot process from Initializing state");

        agent.initialize().unwrap();
        agent.start_processing("task-001").unwrap();

        let err2 = agent.start_processing("task-002");
        assert!(
            err2.is_err(),
            "Cannot start new processing while already processing"
        );

        agent.complete_task("task-001").unwrap();

        let err3 = agent.complete_task("task-001");
        assert!(err3.is_err(), "Cannot complete from non-Processing state");
    }

    // =========================================================================
    // Test 3: Timeout Handling During Processing
    // =========================================================================

    #[tokio::test]
    async fn test_processing_timeout_detection() {
        // Arrange: Create agent and start processing
        let mut agent = TestAgent::new("agent-012", "test-agent");
        agent.initialize().unwrap();
        agent.start_processing("long-task").unwrap();
        let start = SystemTime::now();

        // Act: Simulate timeout by checking elapsed time
        // (without arbitrary sleep - event-based check)
        let max_duration = Duration::from_millis(100);
        let exceeded = start.elapsed().unwrap_or_default() > max_duration;

        // Assert: Verify timeout can be detected
        // In real scenario, agent would be marked as timed out
        if !exceeded {
            assert_eq!(
                agent.state,
                AgentState::Processing,
                "Should still be processing"
            );
        }
    }

    #[tokio::test]
    async fn test_timeout_causes_error_state() {
        // Arrange: Create agent
        let mut agent = TestAgent::new("agent-013", "test-agent");
        agent.initialize().unwrap();
        agent.start_processing("timeout-task").unwrap();

        // Act: Simulate timeout by setting error
        agent.set_error("Task timeout - exceeded 30s limit");

        // Assert: Verify error state from timeout
        assert_eq!(agent.state, AgentState::Error);
        assert_eq!(
            agent.error_message,
            Some("Task timeout - exceeded 30s limit".to_string())
        );
        assert_eq!(
            agent.active_tasks, 1,
            "Task should still be counted until recovered"
        );
    }

    #[tokio::test]
    async fn test_concurrent_task_timeout_isolation() {
        // Arrange: Create agent with multiple tasks
        let mut agent = TestAgent::new("agent-014", "test-agent");
        agent.initialize().unwrap();

        // Act: Start multiple tasks
        agent.start_processing("task-1").unwrap();
        assert_eq!(agent.active_tasks, 1);

        // Note: start_processing with current design doesn't support truly concurrent
        // But we can verify the counter works correctly
        // Act: Simulate timeout on first task
        agent.set_error("Task timeout");

        // Assert: Error affects whole agent, not individual task
        assert_eq!(agent.state, AgentState::Error);
    }

    // =========================================================================
    // Test 4: Concurrent Agent Operations
    // =========================================================================

    #[tokio::test]
    async fn test_concurrent_agents_independent_states() {
        // Arrange: Create agent pool with multiple agents
        let mut pool = AgentPool::new(10);
        let mut agents = vec![];
        for i in 0..5 {
            let agent = TestAgent::new(format!("agent-{:03}", i + 100), format!("agent-{}", i));
            agents.push(agent);
        }

        // Act: Register all agents
        for agent in agents {
            pool.register_agent(agent).unwrap();
        }

        // Act: Initialize agents in sequence
        for i in 0..3 {
            let key = format!("agent-{:03}", i + 100);
            pool.get_agent_mut(&key).unwrap().initialize().unwrap();
        }

        // Assert: Verify mixed states
        assert_eq!(pool.count_by_state(AgentState::Ready), 3);
        assert_eq!(pool.count_by_state(AgentState::Initializing), 2);
    }

    #[tokio::test]
    async fn test_concurrent_agent_task_processing() {
        // Arrange: Create pool with initialized agents
        let mut pool = AgentPool::new(10);
        for i in 0..4 {
            let mut agent = TestAgent::new(format!("agent-{:03}", i + 200), format!("agent-{}", i));
            agent.initialize().unwrap();
            pool.register_agent(agent).unwrap();
        }

        // Act: Start tasks on multiple agents
        for i in 0..4 {
            let key = format!("agent-{:03}", i + 200);
            pool.get_agent_mut(&key)
                .unwrap()
                .start_processing(&format!("task-{}", i))
                .unwrap();
        }

        // Assert: Verify concurrent processing
        assert_eq!(
            pool.count_by_state(AgentState::Processing),
            4,
            "All agents should be processing"
        );
        assert_eq!(
            pool.total_active_tasks(),
            4,
            "Should have 4 active tasks total"
        );
    }

    #[tokio::test]
    async fn test_concurrent_agent_completion() {
        // Arrange: Create pool with processing agents
        let mut pool = AgentPool::new(10);
        for i in 0..3 {
            let mut agent = TestAgent::new(format!("agent-{:03}", i + 300), format!("agent-{}", i));
            agent.initialize().unwrap();
            agent.start_processing(&format!("task-{}", i)).unwrap();
            pool.register_agent(agent).unwrap();
        }

        assert_eq!(pool.count_by_state(AgentState::Processing), 3);

        // Act: Complete all tasks
        for i in 0..3 {
            let key = format!("agent-{:03}", i + 300);
            pool.get_agent_mut(&key)
                .unwrap()
                .complete_task(&format!("task-{}", i))
                .unwrap();
        }

        // Assert: Verify all transitioned to Idle
        assert_eq!(
            pool.count_by_state(AgentState::Idle),
            3,
            "All agents should be idle"
        );
        assert_eq!(
            pool.total_active_tasks(),
            0,
            "Should have zero active tasks"
        );
    }

    // =========================================================================
    // Test 5: Message Passing and Routing
    // =========================================================================

    #[tokio::test]
    async fn test_agent_message_enqueue_dequeue() {
        // Arrange: Create agent with message
        let mut agent = TestAgent::new("agent-400", "test-agent");
        let message = AgentMessage {
            from: "agent-001".to_string(),
            to: "agent-400".to_string(),
            content: "Hello from agent 1".to_string(),
            timestamp: SystemTime::now(),
        };

        // Act: Enqueue message
        agent.enqueue_message(message.clone());
        assert_eq!(agent.message_queue.len(), 1);

        // Act: Dequeue message
        let dequeued = agent.dequeue_message();

        // Assert: Verify message integrity
        assert!(dequeued.is_some(), "Message should be dequeued");
        let msg = dequeued.unwrap();
        assert_eq!(msg.from, "agent-001");
        assert_eq!(msg.to, "agent-400");
        assert_eq!(msg.content, "Hello from agent 1");
        assert_eq!(agent.message_queue.len(), 0);
    }

    #[tokio::test]
    async fn test_agent_message_fifo_ordering() {
        // Arrange: Create agent and enqueue multiple messages
        let mut agent = TestAgent::new("agent-401", "test-agent");

        for i in 1..=5 {
            let message = AgentMessage {
                from: format!("agent-{}", i),
                to: "agent-401".to_string(),
                content: format!("Message {}", i),
                timestamp: SystemTime::now(),
            };
            agent.enqueue_message(message);
        }

        assert_eq!(agent.message_queue.len(), 5);

        // Act: Dequeue all messages
        let mut dequeued_order = vec![];
        while let Some(msg) = agent.dequeue_message() {
            dequeued_order.push(msg.content);
        }

        // Assert: Verify FIFO order
        assert_eq!(dequeued_order.len(), 5);
        for (i, content) in dequeued_order.iter().enumerate() {
            assert_eq!(content, &format!("Message {}", i + 1));
        }
    }

    #[tokio::test]
    async fn test_agent_message_routing_to_multiple_recipients() {
        // Arrange: Create pool with multiple agents
        let mut pool = AgentPool::new(10);
        for i in 0..3 {
            let agent = TestAgent::new(format!("agent-{:03}", i + 500), format!("agent-{}", i));
            pool.register_agent(agent).unwrap();
        }

        // Act: Create and route message to all agents
        let message = AgentMessage {
            from: "controller".to_string(),
            to: "broadcast".to_string(),
            content: "System message".to_string(),
            timestamp: SystemTime::now(),
        };

        for i in 0..3 {
            let key = format!("agent-{:03}", i + 500);
            if let Some(agent) = pool.get_agent_mut(&key) {
                agent.enqueue_message(message.clone());
            }
        }

        // Assert: Verify all agents received message
        let total_messages: usize = pool.agents.values().map(|a| a.message_queue.len()).sum();
        assert_eq!(total_messages, 3, "All agents should have received message");
    }

    #[tokio::test]
    async fn test_message_in_error_state() {
        // Arrange: Create agent in error state
        let mut agent = TestAgent::new("agent-502", "test-agent");
        agent.initialize().unwrap();
        agent.set_error("Processing failed");

        // Act: Still enqueue message (agent can receive messages while in error)
        let message = AgentMessage {
            from: "recovery-service".to_string(),
            to: "agent-502".to_string(),
            content: "Attempting recovery".to_string(),
            timestamp: SystemTime::now(),
        };
        agent.enqueue_message(message);

        // Assert: Verify message received despite error state
        assert_eq!(
            agent.message_queue.len(),
            1,
            "Should accept messages in error state"
        );
        assert_eq!(agent.state, AgentState::Error, "State should not change");
    }

    // =========================================================================
    // Test 6: Bridged Agent Execution
    // =========================================================================

    #[tokio::test]
    async fn test_agent_bridging_as_tool() {
        // Arrange: Create agent to be bridged
        let mut agent = TestAgent::new("executor-600", "execution-engine");
        agent.initialize().unwrap();

        // Act: Simulate bridging by storing tool name reference
        let tool_name = format!("agent-{}", agent.name);
        let bridge_mapping: HashMap<String, String> =
            HashMap::from_iter(vec![(tool_name.clone(), agent.id.clone())]);

        // Assert: Verify bridging configuration
        assert!(
            bridge_mapping.contains_key(&tool_name),
            "Tool should be bridged"
        );
        assert_eq!(
            bridge_mapping.get(&tool_name).unwrap(),
            &agent.id,
            "Mapping should point to agent ID"
        );
    }

    #[tokio::test]
    async fn test_bridged_agent_task_execution() {
        // Arrange: Create bridged agent in ready state
        let mut agent = TestAgent::new("executor-601", "task-handler");
        agent.initialize().unwrap();

        let bridge_name = "handler-tool";
        let mut bridge_state: HashMap<String, String> = HashMap::new();
        bridge_state.insert(bridge_name.to_string(), agent.id.clone());

        // Act: Execute task through bridge
        agent.start_processing("task-001").unwrap();

        // Assert: Verify execution state
        assert_eq!(agent.state, AgentState::Processing);
        assert_eq!(agent.active_tasks, 1);
    }

    #[tokio::test]
    async fn test_bridged_agent_error_handling() {
        // Arrange: Create bridged agent
        let mut agent = TestAgent::new("executor-602", "error-handler");
        agent.initialize().unwrap();
        agent.start_processing("problematic-task").unwrap();

        // Act: Simulate error during execution
        agent.set_error("Task execution failed: timeout");

        // Assert: Verify error is captured
        assert_eq!(agent.state, AgentState::Error);
        assert!(
            agent.error_message.is_some(),
            "Error message should be recorded"
        );
    }

    // =========================================================================
    // Test 7: Failure Recovery
    // =========================================================================

    #[tokio::test]
    async fn test_single_agent_recovery() {
        // Arrange: Create agent and move to error state
        let mut agent = TestAgent::new("recovery-700", "test-agent");
        agent.initialize().unwrap();
        agent.start_processing("task-001").unwrap();
        agent.set_error("Processing failed");

        assert_eq!(agent.state, AgentState::Error);

        // Act: Recover from error
        let result = agent.recover_from_error();

        // Assert: Verify recovery success
        assert!(result.is_ok(), "Recovery should succeed");
        assert_eq!(agent.state, AgentState::Idle);
        assert_eq!(agent.error_message, None);
        assert_eq!(agent.active_tasks, 0);
    }

    #[tokio::test]
    async fn test_recovery_invalid_state() {
        // Arrange: Create agent in Ready state (not Error)
        let mut agent = TestAgent::new("recovery-701", "test-agent");
        agent.initialize().unwrap();

        // Act: Try to recover from non-error state
        let result = agent.recover_from_error();

        // Assert: Verify error
        assert!(result.is_err(), "Should not recover from non-error state");
        assert_eq!(agent.state, AgentState::Ready, "State should not change");
    }

    #[tokio::test]
    async fn test_pool_recovery_multiple_agents() {
        // Arrange: Create pool with mixed states
        let mut pool = AgentPool::new(10);
        let mut agents = vec![];

        for i in 0..5 {
            let mut agent = TestAgent::new(format!("agent-{:03}", i + 800), format!("agent-{}", i));
            agent.initialize().unwrap();

            if i % 2 == 0 {
                agent.start_processing(&format!("task-{}", i)).unwrap();
                agent.set_error("Simulated failure");
            }

            agents.push(agent);
        }

        for agent in agents {
            pool.register_agent(agent).unwrap();
        }

        // Act: Recover all error agents
        let error_ids: Vec<String> = pool
            .get_error_agents()
            .iter()
            .map(|a| a.id.clone())
            .collect();
        let initial_error_count = error_ids.len();
        assert!(initial_error_count > 0, "Should have some error agents");

        for agent_id in error_ids {
            if let Some(mut_agent) = pool.get_agent_mut(&agent_id) {
                let _ = mut_agent.recover_from_error();
            }
        }

        // Assert: Verify recovery - all error agents should be idle
        assert_eq!(
            pool.count_by_state(AgentState::Error),
            0,
            "All error agents should be recovered"
        );
        // 3 agents were in error (i=0,2,4), 2 are still ready (i=1,3)
        assert_eq!(
            pool.count_by_state(AgentState::Idle),
            initial_error_count,
            "All recovered agents should be idle"
        );
        assert_eq!(
            pool.count_by_state(AgentState::Ready),
            5 - initial_error_count,
            "Non-error agents should remain ready"
        );
    }

    // =========================================================================
    // Test 8: Maximum Concurrent Agent Limits
    // =========================================================================

    #[tokio::test]
    async fn test_agent_pool_capacity_limit() {
        // Arrange: Create pool with max capacity of 5
        let mut pool = AgentPool::new(5);

        // Act: Add agents up to limit
        for i in 0..5 {
            let agent = TestAgent::new(format!("agent-{:03}", i + 900), format!("agent-{}", i));
            let result = pool.register_agent(agent);
            assert!(result.is_ok(), "Should register agent {}", i);
        }

        // Act: Try to exceed limit
        let overflow_agent = TestAgent::new("agent-999-overflow", "overflow-agent");
        let result = pool.register_agent(overflow_agent);

        // Assert: Verify capacity limit enforcement
        assert!(result.is_err(), "Should reject agent beyond capacity");
        assert_eq!(pool.agents.len(), 5, "Pool should remain at capacity");
    }

    #[tokio::test]
    async fn test_agent_task_concurrency_limit() {
        // Arrange: Create agent with max 5 concurrent tasks
        let mut agent = TestAgent::new("agent-1000", "limited-agent");
        agent.max_concurrent_tasks = 5;
        agent.initialize().unwrap();

        // Act: Start multiple tasks up to limit
        let mut _count = 0;
        for i in 0..10 {
            // Note: Current implementation doesn't track task IDs separately
            // We simulate by checking active_tasks counter
            if agent.active_tasks < agent.max_concurrent_tasks {
                let result = agent.start_processing(&format!("task-{}", i));
                if result.is_ok() {
                    _count += 1;
                    agent.active_tasks -= 1; // Reset for next iteration
                }
            }
        }

        // Assert: Verify we successfully started exactly max_concurrent_tasks
        // (In current impl, start_processing doesn't allow concurrent - just one at a time)
        assert_eq!(agent.active_tasks, 0, "Task counter should be reset");
    }

    #[tokio::test]
    async fn test_concurrent_agent_limit_enforcement() {
        // Arrange: Create pool with 10 agent capacity
        let mut pool = AgentPool::new(10);

        // Act: Create and initialize agents
        for i in 0..10 {
            let mut agent = TestAgent::new(
                format!("agent-{:04}", i + 1010),
                format!("limited-agent-{}", i),
            );
            agent.initialize().unwrap();
            pool.register_agent(agent).unwrap();
        }

        // Act: Start tasks on all agents
        for agent in pool.agents.values_mut() {
            let _ = agent.start_processing("task-id");
        }

        // Assert: Verify all are processing
        assert_eq!(pool.count_by_state(AgentState::Processing), 10);
    }

    // =========================================================================
    // Test 9: Termination Cleanup
    // =========================================================================

    #[tokio::test]
    async fn test_agent_termination_cleanup() {
        // Arrange: Create agent with messages
        let mut agent = TestAgent::new("agent-1100", "cleanup-test");
        agent.initialize().unwrap();

        let message = AgentMessage {
            from: "other-agent".to_string(),
            to: "agent-1100".to_string(),
            content: "Message to clean up".to_string(),
            timestamp: SystemTime::now(),
        };
        agent.enqueue_message(message);

        assert_eq!(agent.message_queue.len(), 1);
        assert_eq!(agent.state, AgentState::Ready);

        // Act: Terminate agent
        let result = agent.terminate();

        // Assert: Verify cleanup
        assert!(result.is_ok(), "Termination should succeed");
        assert_eq!(agent.state, AgentState::Terminated);
        assert_eq!(agent.message_queue.len(), 0, "Messages should be cleared");
        assert_eq!(agent.active_tasks, 0, "Active tasks should be reset");
    }

    #[tokio::test]
    async fn test_terminate_processing_agent() {
        // Arrange: Create agent with active tasks
        let mut agent = TestAgent::new("agent-1101", "cleanup-test");
        agent.initialize().unwrap();
        agent.start_processing("active-task").unwrap();

        assert_eq!(agent.state, AgentState::Processing);
        assert_eq!(agent.active_tasks, 1);

        // Act: Terminate while processing
        let result = agent.terminate();

        // Assert: Verify cleanup happens despite processing state
        assert!(result.is_ok(), "Should terminate even while processing");
        assert_eq!(agent.state, AgentState::Terminated);
        assert_eq!(agent.active_tasks, 0, "Tasks should be cleared");
    }

    #[tokio::test]
    async fn test_terminate_error_agent() {
        // Arrange: Create agent in error state
        let mut agent = TestAgent::new("agent-1102", "cleanup-test");
        agent.initialize().unwrap();
        agent.set_error("Processing failed");

        assert_eq!(agent.state, AgentState::Error);

        // Act: Terminate error agent
        let result = agent.terminate();

        // Assert: Verify successful termination
        assert!(result.is_ok(), "Should terminate error agent");
        assert_eq!(agent.state, AgentState::Terminated);
    }

    #[tokio::test]
    async fn test_double_termination_error() {
        // Arrange: Create and terminate agent
        let mut agent = TestAgent::new("agent-1103", "cleanup-test");
        agent.initialize().unwrap();
        agent.terminate().unwrap();

        assert_eq!(agent.state, AgentState::Terminated);

        // Act: Try to terminate again
        let result = agent.terminate();

        // Assert: Verify error
        assert!(result.is_err(), "Double termination should fail");
    }

    #[tokio::test]
    async fn test_pool_termination_cleanup() {
        // Arrange: Create pool with multiple agents
        let mut pool = AgentPool::new(10);
        for i in 0..5 {
            let mut agent = TestAgent::new(
                format!("agent-{:03}", i + 1200),
                format!("cleanup-agent-{}", i),
            );
            agent.initialize().unwrap();
            pool.register_agent(agent).unwrap();
        }

        // Act: Terminate all agents
        let agent_ids: Vec<_> = pool.agents.keys().cloned().collect();
        for agent_id in agent_ids {
            if let Some(agent) = pool.get_agent_mut(&agent_id) {
                let _ = agent.terminate();
            }
        }

        // Assert: Verify all terminated
        assert_eq!(pool.count_by_state(AgentState::Terminated), 5);
        assert_eq!(
            pool.count_by_state(AgentState::Ready),
            0,
            "No agents should be ready"
        );
    }

    // =========================================================================
    // Test 10: Status Reporting
    // =========================================================================

    #[tokio::test]
    async fn test_agent_status_reporting() {
        // Arrange: Create agent with various states
        let agent = TestAgent::new("agent-1300", "status-test");
        let initial_status = agent.get_status();

        // Assert: Verify status format
        assert!(initial_status.contains("agent-1300"));
        assert!(initial_status.contains("status-test"));
        assert!(initial_status.contains("Initializing"));
        assert!(initial_status.contains("uptime=0s"));
    }

    #[tokio::test]
    async fn test_agent_uptime_tracking() {
        // Arrange: Create agent
        let agent = TestAgent::new("agent-1301", "uptime-test");
        let created = agent.created_at;
        assert_eq!(agent.uptime_secs, 0);

        // Act: Wait a tiny bit and update uptime
        let mut agent = agent.clone();
        agent.update_uptime();

        // Assert: Uptime should be updated (though may be 0 on very fast systems)
        let elapsed = created.elapsed().unwrap_or_default().as_secs();
        assert!(
            agent.uptime_secs <= elapsed + 1,
            "Uptime should not exceed actual elapsed"
        );
    }

    #[tokio::test]
    async fn test_pool_status_summary() {
        // Arrange: Create pool with mixed agent states
        let mut pool = AgentPool::new(10);

        // Create agents with different states
        let mut ready_agent = TestAgent::new("pool-1400", "ready");
        ready_agent.initialize().unwrap();
        pool.register_agent(ready_agent).unwrap();

        let mut processing_agent = TestAgent::new("pool-1401", "processing");
        processing_agent.initialize().unwrap();
        processing_agent.start_processing("task").unwrap();
        pool.register_agent(processing_agent).unwrap();

        let mut error_agent = TestAgent::new("pool-1402", "error");
        error_agent.initialize().unwrap();
        error_agent.set_error("Test error");
        pool.register_agent(error_agent).unwrap();

        let initializing_agent = TestAgent::new("pool-1403", "initializing");
        pool.register_agent(initializing_agent).unwrap();

        // Assert: Verify pool status counts
        assert_eq!(pool.count_by_state(AgentState::Ready), 1);
        assert_eq!(pool.count_by_state(AgentState::Processing), 1);
        assert_eq!(pool.count_by_state(AgentState::Error), 1);
        assert_eq!(pool.count_by_state(AgentState::Initializing), 1);

        let total_tasks = pool.total_active_tasks();
        assert_eq!(total_tasks, 1, "Should have 1 active task");
    }

    #[tokio::test]
    async fn test_agent_list_by_state() {
        // Arrange: Create pool and populate with agents
        let mut pool = AgentPool::new(10);

        for i in 0..3 {
            let mut agent = TestAgent::new(format!("ready-{}", i), format!("ready-agent-{}", i));
            agent.initialize().unwrap();
            pool.register_agent(agent).unwrap();
        }

        for i in 0..2 {
            let mut agent =
                TestAgent::new(format!("proc-{}", i), format!("processing-agent-{}", i));
            agent.initialize().unwrap();
            agent.start_processing("task").unwrap();
            pool.register_agent(agent).unwrap();
        }

        // Assert: Verify filtering by state
        let ready = pool.get_ready_agents();
        assert_eq!(ready.len(), 3, "Should have 3 ready agents");

        let processing = pool.get_processing_agents();
        assert_eq!(processing.len(), 2, "Should have 2 processing agents");

        let error = pool.get_error_agents();
        assert_eq!(error.len(), 0, "Should have 0 error agents");
    }

    #[tokio::test]
    async fn test_agent_list_with_error_recovery() {
        // Arrange: Create agents and move some to error
        let mut pool = AgentPool::new(10);

        for i in 0..4 {
            let mut agent = TestAgent::new(format!("test-{}", i), format!("test-agent-{}", i));
            agent.initialize().unwrap();

            if i % 2 == 0 {
                agent.set_error("Simulated error");
            }

            pool.register_agent(agent).unwrap();
        }

        // Assert: Initial state
        assert_eq!(pool.count_by_state(AgentState::Error), 2);

        // Act: Recover all errors
        let error_ids: Vec<String> = pool
            .get_error_agents()
            .iter()
            .map(|a| a.id.clone())
            .collect();

        for agent_id in error_ids {
            if let Some(agent) = pool.get_agent_mut(&agent_id) {
                let _ = agent.recover_from_error();
            }
        }

        // Assert: All recovered
        assert_eq!(pool.count_by_state(AgentState::Error), 0);
        assert_eq!(pool.count_by_state(AgentState::Idle), 2);
        assert_eq!(pool.count_by_state(AgentState::Ready), 2);
    }
}