-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtest_agent_card_builder.py
More file actions
1174 lines (943 loc) · 36.6 KB
/
Copy pathtest_agent_card_builder.py
File metadata and controls
1174 lines (943 loc) · 36.6 KB
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
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import Mock
from unittest.mock import patch
from a2a.types import AgentCapabilities
from a2a.types import AgentCard
from a2a.types import AgentProvider
from a2a.types import AgentSkill
from a2a.types import SecurityScheme
from google.adk.a2a.utils.agent_card_builder import _build_agent_description
from google.adk.a2a.utils.agent_card_builder import _build_llm_agent_description_with_instructions
from google.adk.a2a.utils.agent_card_builder import _build_loop_description
from google.adk.a2a.utils.agent_card_builder import _build_orchestration_skill
from google.adk.a2a.utils.agent_card_builder import _build_parallel_description
from google.adk.a2a.utils.agent_card_builder import _build_sequential_description
from google.adk.a2a.utils.agent_card_builder import _convert_example_tool_examples
from google.adk.a2a.utils.agent_card_builder import _extract_examples_from_instruction
from google.adk.a2a.utils.agent_card_builder import _extract_inputs_from_examples
from google.adk.a2a.utils.agent_card_builder import _get_agent_skill_name
from google.adk.a2a.utils.agent_card_builder import _get_agent_type
from google.adk.a2a.utils.agent_card_builder import _get_default_description
from google.adk.a2a.utils.agent_card_builder import _get_input_modes
from google.adk.a2a.utils.agent_card_builder import _get_output_modes
from google.adk.a2a.utils.agent_card_builder import _get_workflow_description
from google.adk.a2a.utils.agent_card_builder import _replace_pronouns
from google.adk.a2a.utils.agent_card_builder import AgentCardBuilder
from google.adk.agents.base_agent import BaseAgent
from google.adk.agents.llm_agent import LlmAgent
from google.adk.agents.loop_agent import LoopAgent
from google.adk.agents.parallel_agent import ParallelAgent
from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.examples import Example
from google.adk.tools.example_tool import ExampleTool
import pytest
class TestAgentCardBuilder:
"""Test suite for AgentCardBuilder class."""
def test_init_with_valid_agent(self):
"""Test successful initialization with valid agent."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.name = "test_agent"
# Act
builder = AgentCardBuilder(agent=mock_agent)
# Assert
assert builder._agent == mock_agent
assert builder._rpc_url == "http://localhost:80/a2a"
assert isinstance(builder._capabilities, AgentCapabilities)
assert builder._doc_url is None
assert builder._provider is None
assert builder._security_schemes is None
assert builder._agent_version == "0.0.1"
def test_init_with_custom_parameters(self):
"""Test initialization with custom parameters."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.name = "test_agent"
mock_capabilities = Mock(spec=AgentCapabilities)
mock_provider = Mock(spec=AgentProvider)
mock_security_schemes = {"test": Mock(spec=SecurityScheme)}
# Act
builder = AgentCardBuilder(
agent=mock_agent,
rpc_url="https://example.com/a2a",
capabilities=mock_capabilities,
doc_url="https://docs.example.com",
provider=mock_provider,
agent_version="1.2.3",
security_schemes=mock_security_schemes,
)
# Assert
assert builder._agent == mock_agent
assert builder._rpc_url == "https://example.com/a2a"
assert builder._capabilities == mock_capabilities
assert builder._doc_url == "https://docs.example.com"
assert builder._provider == mock_provider
assert builder._security_schemes == mock_security_schemes
assert builder._agent_version == "1.2.3"
def test_init_with_none_agent(self):
"""Test initialization with None agent raises ValueError."""
# Act & Assert
with pytest.raises(ValueError, match="Agent cannot be None or empty."):
AgentCardBuilder(agent=None)
def test_init_with_empty_agent(self):
"""Test initialization with empty agent raises ValueError."""
# Arrange
mock_agent = None
# Act & Assert
with pytest.raises(ValueError, match="Agent cannot be None or empty."):
AgentCardBuilder(agent=mock_agent)
@patch("google.adk.a2a.utils.agent_card_builder._build_primary_skills")
@patch("google.adk.a2a.utils.agent_card_builder._build_sub_agent_skills")
async def test_build_success(
self, mock_build_sub_skills, mock_build_primary_skills
):
"""Test successful agent card building."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.name = "test_agent"
mock_agent.description = "Test agent description"
mock_primary_skill = Mock(spec=AgentSkill)
mock_sub_skill = Mock(spec=AgentSkill)
mock_build_primary_skills.return_value = [mock_primary_skill]
mock_build_sub_skills.return_value = [mock_sub_skill]
builder = AgentCardBuilder(agent=mock_agent)
# Act
result = await builder.build()
# Assert
assert isinstance(result, AgentCard)
assert result.name == "test_agent"
assert result.description == "Test agent description"
assert result.documentation_url is None
assert result.url == "http://localhost:80/a2a"
assert result.version == "0.0.1"
assert result.skills == [mock_primary_skill, mock_sub_skill]
assert result.default_input_modes == ["text/plain"]
assert result.default_output_modes == ["text/plain"]
assert result.supports_authenticated_extended_card is False
assert result.provider is None
assert result.security_schemes is None
@patch("google.adk.a2a.utils.agent_card_builder._build_primary_skills")
@patch("google.adk.a2a.utils.agent_card_builder._build_sub_agent_skills")
async def test_build_with_custom_parameters(
self, mock_build_sub_skills, mock_build_primary_skills
):
"""Test agent card building with custom parameters."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.name = "test_agent"
mock_agent.description = None # Should use default description
mock_primary_skill = Mock(spec=AgentSkill)
mock_sub_skill = Mock(spec=AgentSkill)
mock_build_primary_skills.return_value = [mock_primary_skill]
mock_build_sub_skills.return_value = [mock_sub_skill]
mock_provider = Mock(spec=AgentProvider)
mock_security_schemes = {"test": Mock(spec=SecurityScheme)}
builder = AgentCardBuilder(
agent=mock_agent,
rpc_url="https://example.com/a2a/",
doc_url="https://docs.example.com",
provider=mock_provider,
agent_version="2.0.0",
security_schemes=mock_security_schemes,
)
# Act
result = await builder.build()
# Assert
assert result.name == "test_agent"
assert result.description == "An ADK Agent" # Default description
# The source code uses doc_url parameter but AgentCard expects documentation_url
# Since the source code doesn't map doc_url to documentation_url, it will be None
assert result.documentation_url is None
assert (
result.url == "https://example.com/a2a"
) # Should strip trailing slash
assert result.version == "2.0.0"
assert result.provider == mock_provider
assert result.security_schemes == mock_security_schemes
@patch("google.adk.a2a.utils.agent_card_builder._build_primary_skills")
@patch("google.adk.a2a.utils.agent_card_builder._build_sub_agent_skills")
async def test_build_raises_runtime_error_on_failure(
self, mock_build_sub_skills, mock_build_primary_skills
):
"""Test that build raises RuntimeError when underlying functions fail."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.name = "test_agent"
mock_build_primary_skills.side_effect = Exception("Test error")
builder = AgentCardBuilder(agent=mock_agent)
# Act & Assert
with pytest.raises(
RuntimeError,
match="Failed to build agent card for test_agent: Test error",
):
await builder.build()
class TestHelperFunctions:
"""Test suite for helper functions."""
def test_get_agent_type_llm_agent(self):
"""Test _get_agent_type for LlmAgent."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
# Act
result = _get_agent_type(mock_agent)
# Assert
assert result == "llm"
def test_get_agent_type_sequential_agent(self):
"""Test _get_agent_type for SequentialAgent."""
# Arrange
mock_agent = Mock(spec=SequentialAgent)
# Act
result = _get_agent_type(mock_agent)
# Assert
assert result == "sequential_workflow"
def test_get_agent_type_parallel_agent(self):
"""Test _get_agent_type for ParallelAgent."""
# Arrange
mock_agent = Mock(spec=ParallelAgent)
# Act
result = _get_agent_type(mock_agent)
# Assert
assert result == "parallel_workflow"
def test_get_agent_type_loop_agent(self):
"""Test _get_agent_type for LoopAgent."""
# Arrange
mock_agent = Mock(spec=LoopAgent)
# Act
result = _get_agent_type(mock_agent)
# Assert
assert result == "loop_workflow"
def test_get_agent_type_custom_agent(self):
"""Test _get_agent_type for custom agent."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
# Act
result = _get_agent_type(mock_agent)
# Assert
assert result == "custom_agent"
def test_get_agent_skill_name_llm_agent(self):
"""Test _get_agent_skill_name for LlmAgent."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
# Act
result = _get_agent_skill_name(mock_agent)
# Assert
assert result == "model"
def test_get_agent_skill_name_workflow_agents(self):
"""Test _get_agent_skill_name for workflow agents."""
# Arrange
mock_sequential = Mock(spec=SequentialAgent)
mock_parallel = Mock(spec=ParallelAgent)
mock_loop = Mock(spec=LoopAgent)
# Act & Assert
assert _get_agent_skill_name(mock_sequential) == "workflow"
assert _get_agent_skill_name(mock_parallel) == "workflow"
assert _get_agent_skill_name(mock_loop) == "workflow"
def test_get_agent_skill_name_custom_agent(self):
"""Test _get_agent_skill_name for custom agent."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
# Act
result = _get_agent_skill_name(mock_agent)
# Assert
assert result == "custom"
def test_replace_pronouns_basic(self):
"""Test _replace_pronouns with basic pronoun replacement."""
# Arrange
text = "You should do your work and it will be yours."
# Act
result = _replace_pronouns(text)
# Assert
assert result == "I should do my work and it will be mine."
def test_replace_pronouns_case_insensitive(self):
"""Test _replace_pronouns with case-insensitive matching."""
# Arrange
text = "YOU should do YOUR work and it will be YOURS."
# Act
result = _replace_pronouns(text)
# Assert
assert result == "I should do my work and it will be mine."
def test_replace_pronouns_mixed_case(self):
"""Test _replace_pronouns with mixed case."""
# Arrange
text = "You should do Your work and it will be Yours."
# Act
result = _replace_pronouns(text)
# Assert
assert result == "I should do my work and it will be mine."
def test_replace_pronouns_no_pronouns(self):
"""Test _replace_pronouns with no pronouns."""
# Arrange
text = "This is a test message without pronouns."
# Act
result = _replace_pronouns(text)
# Assert
assert result == text
def test_replace_pronouns_partial_matches(self):
"""Test _replace_pronouns with partial matches that shouldn't be replaced."""
# Arrange
text = "youth, yourself, yourname"
# Act
result = _replace_pronouns(text)
# Assert
assert result == "youth, yourself, yourname" # No changes
def test_replace_pronouns_phrases(self):
"""Test _replace_pronouns with phrases that should be replaced."""
# Arrange
text = "You are a helpful chatbot"
# Act
result = _replace_pronouns(text)
# Assert
assert result == "I am a helpful chatbot"
def test_get_default_description_llm_agent(self):
"""Test _get_default_description for LlmAgent."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
# Act
result = _get_default_description(mock_agent)
# Assert
assert result == "An LLM-based agent"
def test_get_default_description_sequential_agent(self):
"""Test _get_default_description for SequentialAgent."""
# Arrange
mock_agent = Mock(spec=SequentialAgent)
# Act
result = _get_default_description(mock_agent)
# Assert
assert result == "A sequential workflow agent"
def test_get_default_description_parallel_agent(self):
"""Test _get_default_description for ParallelAgent."""
# Arrange
mock_agent = Mock(spec=ParallelAgent)
# Act
result = _get_default_description(mock_agent)
# Assert
assert result == "A parallel workflow agent"
def test_get_default_description_loop_agent(self):
"""Test _get_default_description for LoopAgent."""
# Arrange
mock_agent = Mock(spec=LoopAgent)
# Act
result = _get_default_description(mock_agent)
# Assert
assert result == "A loop workflow agent"
def test_get_default_description_custom_agent(self):
"""Test _get_default_description for custom agent."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
# Act
result = _get_default_description(mock_agent)
# Assert
assert result == "A custom agent"
def test_get_input_modes_llm_agent(self):
"""Test _get_input_modes for LlmAgent."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
# Act
result = _get_input_modes(mock_agent)
# Assert
assert result is None # Currently returns None for all cases
def test_get_input_modes_non_llm_agent(self):
"""Test _get_input_modes for non-LlmAgent."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
# Act
result = _get_input_modes(mock_agent)
# Assert
assert result is None
def test_get_output_modes_llm_agent_with_config(self):
"""Test _get_output_modes for LlmAgent with response_modalities."""
# Arrange
mock_config = Mock()
mock_config.response_modalities = ["text/plain", "application/json"]
mock_agent = Mock(spec=LlmAgent)
mock_agent.generate_content_config = mock_config
# Act
result = _get_output_modes(mock_agent)
# Assert
assert result == ["text/plain", "application/json"]
def test_get_output_modes_llm_agent_without_config(self):
"""Test _get_output_modes for LlmAgent without config."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
mock_agent.generate_content_config = None
# Act
result = _get_output_modes(mock_agent)
# Assert
assert result is None
def test_get_output_modes_llm_agent_without_response_modalities(self):
"""Test _get_output_modes for LlmAgent without response_modalities."""
# Arrange
mock_config = Mock()
del mock_config.response_modalities
mock_agent = Mock(spec=LlmAgent)
mock_agent.generate_content_config = mock_config
# Act
result = _get_output_modes(mock_agent)
# Assert
assert result is None
def test_get_output_modes_non_llm_agent(self):
"""Test _get_output_modes for non-LlmAgent."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
# Act
result = _get_output_modes(mock_agent)
# Assert
assert result is None
class TestDescriptionBuildingFunctions:
"""Test suite for description building functions."""
def test_build_agent_description_with_description(self):
"""Test _build_agent_description with agent description."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.description = "Test agent description"
mock_agent.sub_agents = []
# Act
result = _build_agent_description(mock_agent)
# Assert
assert result == "Test agent description"
def test_build_agent_description_without_description(self):
"""Test _build_agent_description without agent description."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.description = None
mock_agent.sub_agents = []
# Act
result = _build_agent_description(mock_agent)
# Assert
assert result == "A custom agent" # Default description
def test_build_llm_agent_description_with_instructions(self):
"""Test _build_llm_agent_description_with_instructions with all components."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
mock_agent.description = "Test agent"
mock_agent.instruction = "You should help users."
mock_agent.global_instruction = "Your role is to assist."
# Act
result = _build_llm_agent_description_with_instructions(mock_agent)
# Assert
assert result == "Test agent I should help users. my role is to assist."
def test_build_llm_agent_description_without_instructions(self):
"""Test _build_llm_agent_description_with_instructions without instructions."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
mock_agent.description = "Test agent"
mock_agent.instruction = None
mock_agent.global_instruction = None
# Act
result = _build_llm_agent_description_with_instructions(mock_agent)
# Assert
assert result == "Test agent"
def test_build_llm_agent_description_without_description(self):
"""Test _build_llm_agent_description_with_instructions without description."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
mock_agent.description = None
mock_agent.instruction = "You should help users."
mock_agent.global_instruction = None
# Act
result = _build_llm_agent_description_with_instructions(mock_agent)
# Assert
assert result == "I should help users."
def test_build_llm_agent_description_empty_all(self):
"""Test _build_llm_agent_description_with_instructions with all empty."""
# Arrange
mock_agent = Mock(spec=LlmAgent)
mock_agent.description = None
mock_agent.instruction = None
mock_agent.global_instruction = None
# Act
result = _build_llm_agent_description_with_instructions(mock_agent)
# Assert
assert result == "An LLM-based agent" # Default description
def test_get_workflow_description_sequential_agent(self):
"""Test _get_workflow_description for SequentialAgent."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = "First agent"
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = "Second agent"
mock_agent = Mock(spec=SequentialAgent)
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2]
# Act
result = _get_workflow_description(mock_agent)
# Assert
assert result is not None
assert (
result
== "First, this agent will First agent Finally, this agent will Second"
" agent."
)
def test_get_workflow_description_parallel_agent(self):
"""Test _get_workflow_description for ParallelAgent."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = "First agent"
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = "Second agent"
mock_agent = Mock(spec=ParallelAgent)
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2]
# Act
result = _get_workflow_description(mock_agent)
# Assert
assert result is not None
assert (
result == "This agent will First agent and Second agent simultaneously."
)
def test_get_workflow_description_loop_agent(self):
"""Test _get_workflow_description for LoopAgent."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = "First agent"
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = "Second agent"
mock_agent = Mock(spec=LoopAgent)
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2]
mock_agent.max_iterations = 5
# Act
result = _get_workflow_description(mock_agent)
# Assert
assert (
result
== "This agent will First agent and Second agent in a loop (max 5"
" iterations)."
)
def test_get_workflow_description_loop_agent_unlimited(self):
"""Test _get_workflow_description for LoopAgent with unlimited iterations."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = "First agent"
mock_agent = Mock(spec=LoopAgent)
mock_agent.sub_agents = [mock_sub_agent1]
mock_agent.max_iterations = None
# Act
result = _get_workflow_description(mock_agent)
# Assert
assert (
result
== "This agent will First agent in a loop (max unlimited iterations)."
)
def test_get_workflow_description_no_sub_agents(self):
"""Test _get_workflow_description for agent without sub-agents."""
# Arrange
mock_agent = Mock(spec=SequentialAgent)
mock_agent.sub_agents = []
# Act
result = _get_workflow_description(mock_agent)
# Assert
assert result is None
def test_get_workflow_description_custom_agent(self):
"""Test _get_workflow_description for custom agent."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.sub_agents = [Mock(spec=BaseAgent)]
# Act
result = _get_workflow_description(mock_agent)
# Assert
assert result is None
def test_build_sequential_description_single_agent(self):
"""Test _build_sequential_description with single sub-agent."""
# Arrange
mock_sub_agent = Mock(spec=BaseAgent)
mock_sub_agent.name = "agent1"
mock_sub_agent.description = "First agent"
mock_agent = Mock(spec=SequentialAgent)
mock_agent.sub_agents = [mock_sub_agent]
# Act
result = _build_sequential_description(mock_agent)
# Assert
assert result == "First, this agent will First agent."
def test_build_sequential_description_multiple_agents(self):
"""Test _build_sequential_description with multiple sub-agents."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = "First agent"
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = "Second agent"
mock_sub_agent3 = Mock(spec=BaseAgent)
mock_sub_agent3.name = "agent3"
mock_sub_agent3.description = "Third agent"
mock_agent = Mock(spec=SequentialAgent)
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2, mock_sub_agent3]
# Act
result = _build_sequential_description(mock_agent)
# Assert
assert (
result
== "First, this agent will First agent Then, this agent will Second"
" agent Finally, this agent will Third agent."
)
def test_build_sequential_description_without_descriptions(self):
"""Test _build_sequential_description with sub-agents without descriptions."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = None
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = None
mock_agent = Mock(spec=SequentialAgent)
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2]
# Act
result = _build_sequential_description(mock_agent)
# Assert
assert (
result
== "First, this agent will execute the agent1 agent Finally, this agent"
" will execute the agent2 agent."
)
def test_build_parallel_description_single_agent(self):
"""Test _build_parallel_description with single sub-agent."""
# Arrange
mock_sub_agent = Mock(spec=BaseAgent)
mock_sub_agent.name = "agent1"
mock_sub_agent.description = "First agent"
mock_agent = Mock(spec=ParallelAgent)
mock_agent.sub_agents = [mock_sub_agent]
# Act
result = _build_parallel_description(mock_agent)
# Assert
assert result == "This agent will First agent simultaneously."
def test_build_parallel_description_multiple_agents(self):
"""Test _build_parallel_description with multiple sub-agents."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = "First agent"
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = "Second agent"
mock_sub_agent3 = Mock(spec=BaseAgent)
mock_sub_agent3.name = "agent3"
mock_sub_agent3.description = "Third agent"
mock_agent = Mock(spec=ParallelAgent)
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2, mock_sub_agent3]
# Act
result = _build_parallel_description(mock_agent)
# Assert
assert (
result
== "This agent will First agent , Second agent and Third agent"
" simultaneously."
)
def test_build_loop_description_single_agent(self):
"""Test _build_loop_description with single sub-agent."""
# Arrange
mock_sub_agent = Mock(spec=BaseAgent)
mock_sub_agent.name = "agent1"
mock_sub_agent.description = "First agent"
mock_agent = Mock(spec=LoopAgent)
mock_agent.sub_agents = [mock_sub_agent]
mock_agent.max_iterations = 3
# Act
result = _build_loop_description(mock_agent)
# Assert
assert result == "This agent will First agent in a loop (max 3 iterations)."
def test_build_loop_description_multiple_agents(self):
"""Test _build_loop_description with multiple sub-agents."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = "First agent"
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = "Second agent"
mock_agent = Mock(spec=LoopAgent)
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2]
mock_agent.max_iterations = 10
# Act
result = _build_loop_description(mock_agent)
# Assert
assert (
result
== "This agent will First agent and Second agent in a loop (max 10"
" iterations)."
)
def test_build_orchestration_skill_with_sub_agents(self):
"""Test _build_orchestration_skill with sub-agents."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = "First agent description"
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = "Second agent description"
mock_agent = Mock(spec=BaseAgent)
mock_agent.name = "main_agent"
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2]
# Act
result = _build_orchestration_skill(mock_agent, "sequential_workflow")
# Assert
assert result is not None
assert result.id == "main_agent-sub-agents"
assert result.name == "sub-agents"
assert (
result.description
== "Orchestrates: agent1: First agent description; agent2: Second agent"
" description"
)
assert result.tags == ["sequential_workflow", "orchestration"]
def test_build_orchestration_skill_without_descriptions(self):
"""Test _build_orchestration_skill with sub-agents without descriptions."""
# Arrange
mock_sub_agent1 = Mock(spec=BaseAgent)
mock_sub_agent1.name = "agent1"
mock_sub_agent1.description = None
mock_sub_agent2 = Mock(spec=BaseAgent)
mock_sub_agent2.name = "agent2"
mock_sub_agent2.description = None
mock_agent = Mock(spec=BaseAgent)
mock_agent.name = "main_agent"
mock_agent.sub_agents = [mock_sub_agent1, mock_sub_agent2]
# Act
result = _build_orchestration_skill(mock_agent, "parallel_workflow")
# Assert
assert result is not None
assert (
result.description
== "Orchestrates: agent1: No description; agent2: No description"
)
def test_build_orchestration_skill_no_sub_agents(self):
"""Test _build_orchestration_skill with no sub-agents."""
# Arrange
mock_agent = Mock(spec=BaseAgent)
mock_agent.sub_agents = []
# Act
result = _build_orchestration_skill(mock_agent, "custom_agent")
# Assert
assert result is None
class TestExampleExtractionFunctions:
"""Test suite for example extraction functions."""
def test_convert_example_tool_examples_with_model_dump(self):
"""Test _convert_example_tool_examples with examples that have model_dump."""
# Arrange
mock_input = Mock()
mock_input.model_dump.return_value = {"text": "test input"}
mock_output1 = Mock()
mock_output1.model_dump.return_value = {"text": "test output 1"}
mock_output2 = Mock()
mock_output2.model_dump.return_value = {"text": "test output 2"}
mock_example = Mock()
mock_example.input = mock_input
mock_example.output = [mock_output1, mock_output2]
mock_tool = Mock(spec=ExampleTool)
mock_tool.examples = [mock_example]
# Act
result = _convert_example_tool_examples(mock_tool)
# Assert
assert len(result) == 1
assert result[0]["input"] == {"text": "test input"}
assert result[0]["output"] == [
{"text": "test output 1"},
{"text": "test output 2"},
]
def test_convert_example_tool_examples_without_model_dump(self):
"""Test _convert_example_tool_examples with examples without model_dump."""
# Arrange
mock_input = {"text": "test input"}
mock_output1 = {"text": "test output 1"}
mock_output2 = {"text": "test output 2"}
mock_example = Mock()
mock_example.input = mock_input
mock_example.output = [mock_output1, mock_output2]
mock_tool = Mock(spec=ExampleTool)
mock_tool.examples = [mock_example]
# Act
result = _convert_example_tool_examples(mock_tool)
# Assert
assert len(result) == 1
assert result[0]["input"] == {"text": "test input"}
assert result[0]["output"] == [
{"text": "test output 1"},
{"text": "test output 2"},
]
def test_convert_example_tool_examples_multiple_examples(self):
"""Test _convert_example_tool_examples with multiple examples."""
# Arrange
mock_example1 = Mock()
mock_example1.input = {"text": "input 1"}
mock_example1.output = [{"text": "output 1"}]
mock_example2 = Mock()
mock_example2.input = {"text": "input 2"}
mock_example2.output = [{"text": "output 2"}]
mock_tool = Mock(spec=ExampleTool)
mock_tool.examples = [mock_example1, mock_example2]
# Act
result = _convert_example_tool_examples(mock_tool)
# Assert
assert len(result) == 2
assert result[0]["input"] == {"text": "input 1"}
assert result[0]["output"] == [{"text": "output 1"}]
assert result[1]["input"] == {"text": "input 2"}
assert result[1]["output"] == [{"text": "output 2"}]
def test_convert_example_tool_examples_empty_list(self):
"""Test _convert_example_tool_examples with empty examples list."""
# Arrange
mock_tool = Mock(spec=ExampleTool)
mock_tool.examples = []
# Act