forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_conversions.py
More file actions
2040 lines (1756 loc) · 66 KB
/
test_conversions.py
File metadata and controls
2040 lines (1756 loc) · 66 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
import base64
import pytest
from google.protobuf.json_format import ParseDict
import json
from a2a.compat.v0_3 import types as types_v03
from a2a.compat.v0_3.conversions import (
to_compat_agent_capabilities,
to_compat_agent_card,
to_compat_agent_card_signature,
to_compat_agent_extension,
to_compat_agent_interface,
to_compat_agent_provider,
to_compat_agent_skill,
to_compat_artifact,
to_compat_authentication_info,
to_compat_cancel_task_request,
to_compat_create_task_push_notification_config_request,
to_compat_delete_task_push_notification_config_request,
to_compat_get_extended_agent_card_request,
to_compat_get_task_push_notification_config_request,
to_compat_get_task_request,
to_compat_list_task_push_notification_config_request,
to_compat_list_task_push_notification_config_response,
to_compat_message,
to_compat_oauth_flows,
to_compat_part,
to_compat_push_notification_config,
to_compat_security_requirement,
to_compat_security_scheme,
to_compat_send_message_configuration,
to_compat_send_message_request,
to_compat_send_message_response,
to_compat_stream_response,
to_compat_subscribe_to_task_request,
to_compat_task,
to_compat_task_artifact_update_event,
to_compat_task_push_notification_config,
to_compat_task_status,
to_compat_task_status_update_event,
to_core_agent_capabilities,
to_core_agent_card,
to_core_agent_card_signature,
to_core_agent_extension,
to_core_agent_interface,
to_core_agent_provider,
to_core_agent_skill,
to_core_artifact,
to_core_authentication_info,
to_core_cancel_task_request,
to_core_create_task_push_notification_config_request,
to_core_delete_task_push_notification_config_request,
to_core_get_extended_agent_card_request,
to_core_get_task_push_notification_config_request,
to_core_get_task_request,
to_core_list_task_push_notification_config_request,
to_core_list_task_push_notification_config_response,
to_core_message,
to_core_oauth_flows,
to_core_part,
to_core_push_notification_config,
to_core_security_requirement,
to_core_security_scheme,
to_core_send_message_configuration,
to_core_send_message_request,
to_core_send_message_response,
to_core_stream_response,
to_core_subscribe_to_task_request,
to_core_task,
to_core_task_artifact_update_event,
to_core_task_push_notification_config,
to_core_task_status,
to_core_task_status_update_event,
)
from a2a.compat.v0_3.model_conversions import (
core_to_compat_task_model,
compat_task_model_to_core,
core_to_compat_push_notification_config_model,
compat_push_notification_config_model_to_core,
)
from a2a.server.models import PushNotificationConfigModel, TaskModel
from cryptography.fernet import Fernet
from a2a.types import a2a_pb2 as pb2_v10
from a2a.utils.errors import VersionNotSupportedError
def test_text_part_conversion():
v03_part = types_v03.Part(
root=types_v03.TextPart(text='Hello, World!', metadata={'test': 'val'})
)
v10_expected = pb2_v10.Part(text='Hello, World!')
v10_expected.metadata.update({'test': 'val'})
v10_part = to_core_part(v03_part)
assert v10_part == v10_expected
v03_restored = to_compat_part(v10_part)
assert v03_restored == v03_part
def test_data_part_conversion():
data = {'key': 'val', 'nested': {'a': 1}}
v03_part = types_v03.Part(root=types_v03.DataPart(data=data))
v10_expected = pb2_v10.Part()
ParseDict(data, v10_expected.data.struct_value)
v10_part = to_core_part(v03_part)
assert v10_part == v10_expected
v03_restored = to_compat_part(v10_part)
assert v03_restored == v03_part
def test_data_part_conversion_primitive():
primitive_cases = [
'Primitive String',
42,
3.14,
True,
False,
['a', 'b', 'c'],
[1, 2, 3],
None,
]
for val in primitive_cases:
v10_expected = pb2_v10.Part()
ParseDict(val, v10_expected.data)
# Test v10 -> v03
v03_part = to_compat_part(v10_expected)
assert isinstance(v03_part.root, types_v03.DataPart)
assert v03_part.root.data == {'value': val}
assert v03_part.root.metadata['data_part_compat'] is True
# Test v03 -> v10
v10_restored = to_core_part(v03_part)
assert v10_restored == v10_expected
def test_file_part_uri_conversion():
v03_file = types_v03.FileWithUri(
uri='http://example.com/file', mime_type='text/plain', name='file.txt'
)
v03_part = types_v03.Part(root=types_v03.FilePart(file=v03_file))
v10_expected = pb2_v10.Part(
url='http://example.com/file',
media_type='text/plain',
filename='file.txt',
)
v10_part = to_core_part(v03_part)
assert v10_part == v10_expected
v03_restored = to_compat_part(v10_part)
assert v03_restored == v03_part
def test_file_part_bytes_conversion():
content = b'hello world'
b64 = base64.b64encode(content).decode('utf-8')
v03_file = types_v03.FileWithBytes(
bytes=b64, mime_type='application/octet-stream', name='file.bin'
)
v03_part = types_v03.Part(root=types_v03.FilePart(file=v03_file))
v10_expected = pb2_v10.Part(
raw=content, media_type='application/octet-stream', filename='file.bin'
)
v10_part = to_core_part(v03_part)
assert v10_part == v10_expected
v03_restored = to_compat_part(v10_part)
assert v03_restored == v03_part
def test_message_conversion():
v03_msg = types_v03.Message(
message_id='m1',
role=types_v03.Role.user,
context_id='c1',
task_id='t1',
reference_task_ids=['rt1'],
metadata={'k': 'v'},
extensions=['ext1'],
parts=[types_v03.Part(root=types_v03.TextPart(text='hi'))],
)
v10_expected = pb2_v10.Message(
message_id='m1',
role=pb2_v10.Role.ROLE_USER,
context_id='c1',
task_id='t1',
reference_task_ids=['rt1'],
extensions=['ext1'],
parts=[pb2_v10.Part(text='hi')],
)
ParseDict({'k': 'v'}, v10_expected.metadata)
v10_msg = to_core_message(v03_msg)
assert v10_msg == v10_expected
v03_restored = to_compat_message(v10_msg)
assert v03_restored == v03_msg
def test_message_conversion_minimal():
v03_msg = types_v03.Message(
message_id='m1',
role=types_v03.Role.agent,
parts=[types_v03.Part(root=types_v03.TextPart(text='hi'))],
)
v10_expected = pb2_v10.Message(
message_id='m1',
role=pb2_v10.Role.ROLE_AGENT,
parts=[pb2_v10.Part(text='hi')],
)
v10_msg = to_core_message(v03_msg)
assert v10_msg == v10_expected
v03_restored = to_compat_message(v10_msg)
# v03 expects None for missing fields, conversions.py handles this correctly
assert v03_restored == v03_msg
def test_task_status_conversion():
now_v03 = '2023-01-01T12:00:00Z'
v03_msg = types_v03.Message(
message_id='m1',
role=types_v03.Role.agent,
parts=[types_v03.Part(root=types_v03.TextPart(text='status'))],
)
v03_status = types_v03.TaskStatus(
state=types_v03.TaskState.working, message=v03_msg, timestamp=now_v03
)
v10_expected = pb2_v10.TaskStatus(
state=pb2_v10.TaskState.TASK_STATE_WORKING,
message=pb2_v10.Message(
message_id='m1',
role=pb2_v10.Role.ROLE_AGENT,
parts=[pb2_v10.Part(text='status')],
),
)
v10_expected.timestamp.FromJsonString(now_v03)
v10_status = to_core_task_status(v03_status)
assert v10_status == v10_expected
v03_restored = to_compat_task_status(v10_status)
assert v03_restored == v03_status
def test_task_status_conversion_special_states():
# input-required
s1 = types_v03.TaskStatus(state=types_v03.TaskState.input_required)
assert (
to_core_task_status(s1).state
== pb2_v10.TaskState.TASK_STATE_INPUT_REQUIRED
)
assert to_compat_task_status(to_core_task_status(s1)).state == s1.state
# auth-required
s2 = types_v03.TaskStatus(state=types_v03.TaskState.auth_required)
assert (
to_core_task_status(s2).state
== pb2_v10.TaskState.TASK_STATE_AUTH_REQUIRED
)
assert to_compat_task_status(to_core_task_status(s2)).state == s2.state
# unknown
s3 = types_v03.TaskStatus(state=types_v03.TaskState.unknown)
assert (
to_core_task_status(s3).state
== pb2_v10.TaskState.TASK_STATE_UNSPECIFIED
)
assert to_compat_task_status(to_core_task_status(s3)).state == s3.state
def test_task_conversion():
v03_msg = types_v03.Message(
message_id='m1',
role=types_v03.Role.user,
parts=[types_v03.Part(root=types_v03.TextPart(text='hi'))],
)
v03_status = types_v03.TaskStatus(state=types_v03.TaskState.submitted)
v03_art = types_v03.Artifact(
artifact_id='a1',
parts=[types_v03.Part(root=types_v03.TextPart(text='data'))],
)
v03_task = types_v03.Task(
id='t1',
context_id='c1',
status=v03_status,
history=[v03_msg],
artifacts=[v03_art],
metadata={'m': 'v'},
)
v10_expected = pb2_v10.Task(
id='t1',
context_id='c1',
status=pb2_v10.TaskStatus(state=pb2_v10.TaskState.TASK_STATE_SUBMITTED),
history=[
pb2_v10.Message(
message_id='m1',
role=pb2_v10.Role.ROLE_USER,
parts=[pb2_v10.Part(text='hi')],
)
],
artifacts=[
pb2_v10.Artifact(
artifact_id='a1', parts=[pb2_v10.Part(text='data')]
)
],
)
ParseDict({'m': 'v'}, v10_expected.metadata)
v10_task = to_core_task(v03_task)
assert v10_task == v10_expected
v03_restored = to_compat_task(v10_task)
# v03 restored artifacts will have None for name/desc/etc
v03_expected_restored = types_v03.Task(
id='t1',
context_id='c1',
status=v03_status,
history=[v03_msg],
artifacts=[
types_v03.Artifact(
artifact_id='a1',
parts=[types_v03.Part(root=types_v03.TextPart(text='data'))],
name=None,
description=None,
metadata=None,
extensions=None,
)
],
metadata={'m': 'v'},
)
assert v03_restored == v03_expected_restored
def test_task_conversion_minimal():
# Test v10 to v03 minimal
v10_min = pb2_v10.Task(id='tm', context_id='cm')
v03_expected_restored = types_v03.Task(
id='tm',
context_id='cm',
status=types_v03.TaskStatus(state=types_v03.TaskState.unknown),
)
v03_min_restored = to_compat_task(v10_min)
assert v03_min_restored == v03_expected_restored
def test_authentication_info_conversion():
v03_auth = types_v03.PushNotificationAuthenticationInfo(
schemes=['Bearer'], credentials='token123'
)
v10_expected = pb2_v10.AuthenticationInfo(
scheme='Bearer', credentials='token123'
)
v10_auth = to_core_authentication_info(v03_auth)
assert v10_auth == v10_expected
v03_restored = to_compat_authentication_info(v10_auth)
assert v03_restored == v03_auth
def test_authentication_info_conversion_minimal():
v03_auth = types_v03.PushNotificationAuthenticationInfo(schemes=[])
v10_expected = pb2_v10.AuthenticationInfo()
v10_auth = to_core_authentication_info(v03_auth)
assert v10_auth == v10_expected
v03_restored = to_compat_authentication_info(v10_auth)
v03_expected_restored = types_v03.PushNotificationAuthenticationInfo(
schemes=[], credentials=None
)
assert v03_restored == v03_expected_restored
def test_push_notification_config_conversion():
v03_auth = types_v03.PushNotificationAuthenticationInfo(schemes=['Basic'])
v03_config = types_v03.PushNotificationConfig(
id='c1',
url='http://test.com',
token='tok', # noqa: S106
authentication=v03_auth,
)
v10_expected = pb2_v10.TaskPushNotificationConfig(
id='c1',
url='http://test.com',
token='tok', # noqa: S106
authentication=pb2_v10.AuthenticationInfo(scheme='Basic'),
)
v10_config = to_core_push_notification_config(v03_config)
assert v10_config == v10_expected
v03_restored = to_compat_push_notification_config(v10_config)
assert v03_restored == v03_config
def test_push_notification_config_conversion_minimal():
v03_config = types_v03.PushNotificationConfig(url='http://test.com')
v10_expected = pb2_v10.TaskPushNotificationConfig(url='http://test.com')
v10_config = to_core_push_notification_config(v03_config)
assert v10_config == v10_expected
v03_restored = to_compat_push_notification_config(v10_config)
v03_expected_restored = types_v03.PushNotificationConfig(
url='http://test.com', id=None, token=None, authentication=None
)
assert v03_restored == v03_expected_restored
def test_send_message_configuration_conversion():
v03_auth = types_v03.PushNotificationAuthenticationInfo(schemes=['Basic'])
v03_push = types_v03.PushNotificationConfig(
url='http://test', authentication=v03_auth
)
v03_config = types_v03.MessageSendConfiguration(
accepted_output_modes=['text/plain', 'application/json'],
history_length=10,
blocking=True,
push_notification_config=v03_push,
)
v10_expected = pb2_v10.SendMessageConfiguration(
accepted_output_modes=['text/plain', 'application/json'],
history_length=10,
task_push_notification_config=pb2_v10.TaskPushNotificationConfig(
url='http://test',
authentication=pb2_v10.AuthenticationInfo(scheme='Basic'),
),
)
v10_config = to_core_send_message_configuration(v03_config)
assert v10_config == v10_expected
v03_restored = to_compat_send_message_configuration(v10_config)
assert v03_restored == v03_config
def test_send_message_configuration_conversion_minimal():
v03_config = types_v03.MessageSendConfiguration()
v10_expected = pb2_v10.SendMessageConfiguration()
v10_config = to_core_send_message_configuration(v03_config)
assert v10_config == v10_expected
v03_restored = to_compat_send_message_configuration(v10_config)
v03_expected_restored = types_v03.MessageSendConfiguration(
accepted_output_modes=None,
history_length=None,
blocking=True,
push_notification_config=None,
)
assert v03_restored == v03_expected_restored
def test_artifact_conversion_full():
v03_artifact = types_v03.Artifact(
artifact_id='a1',
name='Test Art',
description='A test artifact',
parts=[types_v03.Part(root=types_v03.TextPart(text='data'))],
metadata={'k': 'v'},
extensions=['ext1'],
)
v10_expected = pb2_v10.Artifact(
artifact_id='a1',
name='Test Art',
description='A test artifact',
parts=[pb2_v10.Part(text='data')],
extensions=['ext1'],
)
ParseDict({'k': 'v'}, v10_expected.metadata)
v10_art = to_core_artifact(v03_artifact)
assert v10_art == v10_expected
v03_restored = to_compat_artifact(v10_art)
assert v03_restored == v03_artifact
def test_artifact_conversion_minimal():
v03_artifact = types_v03.Artifact(
artifact_id='a1',
parts=[types_v03.Part(root=types_v03.TextPart(text='data'))],
)
v10_expected = pb2_v10.Artifact(
artifact_id='a1', parts=[pb2_v10.Part(text='data')]
)
v10_art = to_core_artifact(v03_artifact)
assert v10_art == v10_expected
v03_restored = to_compat_artifact(v10_art)
v03_expected_restored = types_v03.Artifact(
artifact_id='a1',
parts=[types_v03.Part(root=types_v03.TextPart(text='data'))],
name=None,
description=None,
metadata=None,
extensions=None,
)
assert v03_restored == v03_expected_restored
def test_task_status_update_event_conversion():
v03_status = types_v03.TaskStatus(state=types_v03.TaskState.completed)
v03_event = types_v03.TaskStatusUpdateEvent(
task_id='t1',
context_id='c1',
status=v03_status,
metadata={'m': 'v'},
final=True,
)
v10_expected = pb2_v10.TaskStatusUpdateEvent(
task_id='t1',
context_id='c1',
status=pb2_v10.TaskStatus(state=pb2_v10.TaskState.TASK_STATE_COMPLETED),
)
ParseDict({'m': 'v'}, v10_expected.metadata)
v10_event = to_core_task_status_update_event(v03_event)
assert v10_event == v10_expected
v03_restored = to_compat_task_status_update_event(v10_event)
v03_expected_restored = types_v03.TaskStatusUpdateEvent(
task_id='t1',
context_id='c1',
status=v03_status,
metadata={'m': 'v'},
final=True, # final is computed based on status.state
)
assert v03_restored == v03_expected_restored
def test_task_status_update_event_conversion_terminal_states():
# Test all terminal states result in final=True
terminal_states = [
(
pb2_v10.TaskState.TASK_STATE_COMPLETED,
types_v03.TaskState.completed,
),
(pb2_v10.TaskState.TASK_STATE_CANCELED, types_v03.TaskState.canceled),
(pb2_v10.TaskState.TASK_STATE_FAILED, types_v03.TaskState.failed),
(pb2_v10.TaskState.TASK_STATE_REJECTED, types_v03.TaskState.rejected),
]
for core_st, compat_st in terminal_states:
v10_event = pb2_v10.TaskStatusUpdateEvent(
status=pb2_v10.TaskStatus(state=core_st)
)
v03_restored = to_compat_task_status_update_event(v10_event)
assert v03_restored.final is True
assert v03_restored.status.state == compat_st
# Test non-terminal states result in final=False
non_terminal_states = [
(
pb2_v10.TaskState.TASK_STATE_SUBMITTED,
types_v03.TaskState.submitted,
),
(pb2_v10.TaskState.TASK_STATE_WORKING, types_v03.TaskState.working),
(
pb2_v10.TaskState.TASK_STATE_INPUT_REQUIRED,
types_v03.TaskState.input_required,
),
(
pb2_v10.TaskState.TASK_STATE_AUTH_REQUIRED,
types_v03.TaskState.auth_required,
),
(
pb2_v10.TaskState.TASK_STATE_UNSPECIFIED,
types_v03.TaskState.unknown,
),
]
for core_st, compat_st in non_terminal_states:
v10_event = pb2_v10.TaskStatusUpdateEvent(
status=pb2_v10.TaskStatus(state=core_st)
)
v03_restored = to_compat_task_status_update_event(v10_event)
assert v03_restored.final is False
assert v03_restored.status.state == compat_st
def test_task_status_update_event_conversion_minimal():
# v03 status is required but might be constructed empty internally
v10_event = pb2_v10.TaskStatusUpdateEvent(task_id='t1', context_id='c1')
v03_restored = to_compat_task_status_update_event(v10_event)
v03_expected = types_v03.TaskStatusUpdateEvent(
task_id='t1',
context_id='c1',
status=types_v03.TaskStatus(state=types_v03.TaskState.unknown),
final=False,
)
assert v03_restored == v03_expected
def test_task_artifact_update_event_conversion():
v03_art = types_v03.Artifact(
artifact_id='a1',
parts=[types_v03.Part(root=types_v03.TextPart(text='d'))],
)
v03_event = types_v03.TaskArtifactUpdateEvent(
task_id='t1',
context_id='c1',
artifact=v03_art,
append=True,
last_chunk=False,
metadata={'k': 'v'},
)
v10_expected = pb2_v10.TaskArtifactUpdateEvent(
task_id='t1',
context_id='c1',
artifact=pb2_v10.Artifact(
artifact_id='a1', parts=[pb2_v10.Part(text='d')]
),
append=True,
last_chunk=False,
)
ParseDict({'k': 'v'}, v10_expected.metadata)
v10_event = to_core_task_artifact_update_event(v03_event)
assert v10_event == v10_expected
v03_restored = to_compat_task_artifact_update_event(v10_event)
assert v03_restored == v03_event
def test_task_artifact_update_event_conversion_minimal():
v03_art = types_v03.Artifact(
artifact_id='a1',
parts=[types_v03.Part(root=types_v03.TextPart(text='d'))],
)
v03_event = types_v03.TaskArtifactUpdateEvent(
task_id='t1', context_id='c1', artifact=v03_art
)
v10_expected = pb2_v10.TaskArtifactUpdateEvent(
task_id='t1',
context_id='c1',
artifact=pb2_v10.Artifact(
artifact_id='a1', parts=[pb2_v10.Part(text='d')]
),
)
v10_event = to_core_task_artifact_update_event(v03_event)
assert v10_event == v10_expected
v03_restored = to_compat_task_artifact_update_event(v10_event)
v03_expected_restored = types_v03.TaskArtifactUpdateEvent(
task_id='t1',
context_id='c1',
artifact=v03_art,
append=False, # primitive bools default to False
last_chunk=False,
metadata=None,
)
assert v03_restored == v03_expected_restored
def test_security_requirement_conversion():
v03_req = {'oauth': ['read', 'write'], 'apikey': []}
v10_expected = pb2_v10.SecurityRequirement()
sl_oauth = pb2_v10.StringList()
sl_oauth.list.extend(['read', 'write'])
sl_apikey = pb2_v10.StringList()
v10_expected.schemes['oauth'].CopyFrom(sl_oauth)
v10_expected.schemes['apikey'].CopyFrom(sl_apikey)
v10_req = to_core_security_requirement(v03_req)
assert v10_req == v10_expected
v03_restored = to_compat_security_requirement(v10_req)
assert v03_restored == v03_req
def test_oauth_flows_conversion_auth_code():
v03_flows = types_v03.OAuthFlows(
authorization_code=types_v03.AuthorizationCodeOAuthFlow(
authorization_url='http://auth',
token_url='http://token', # noqa: S106
scopes={'a': 'b'},
refresh_url='ref1',
)
)
v10_expected = pb2_v10.OAuthFlows(
authorization_code=pb2_v10.AuthorizationCodeOAuthFlow(
authorization_url='http://auth',
token_url='http://token', # noqa: S106
scopes={'a': 'b'},
refresh_url='ref1',
)
)
v10_flows = to_core_oauth_flows(v03_flows)
assert v10_flows == v10_expected
v03_restored = to_compat_oauth_flows(v10_flows)
assert v03_restored == v03_flows
def test_oauth_flows_conversion_client_credentials():
v03_flows = types_v03.OAuthFlows(
client_credentials=types_v03.ClientCredentialsOAuthFlow(
token_url='http://token2', # noqa: S106
scopes={'c': 'd'},
refresh_url='ref2',
)
)
v10_expected = pb2_v10.OAuthFlows(
client_credentials=pb2_v10.ClientCredentialsOAuthFlow(
token_url='http://token2', # noqa: S106
scopes={'c': 'd'},
refresh_url='ref2',
)
)
v10_flows = to_core_oauth_flows(v03_flows)
assert v10_flows == v10_expected
v03_restored = to_compat_oauth_flows(v10_flows)
assert v03_restored == v03_flows
def test_oauth_flows_conversion_implicit():
v03_flows = types_v03.OAuthFlows(
implicit=types_v03.ImplicitOAuthFlow(
authorization_url='http://auth2',
scopes={'e': 'f'},
refresh_url='ref3',
)
)
v10_expected = pb2_v10.OAuthFlows(
implicit=pb2_v10.ImplicitOAuthFlow(
authorization_url='http://auth2',
scopes={'e': 'f'},
refresh_url='ref3',
)
)
v10_flows = to_core_oauth_flows(v03_flows)
assert v10_flows == v10_expected
v03_restored = to_compat_oauth_flows(v10_flows)
assert v03_restored == v03_flows
def test_oauth_flows_conversion_password():
v03_flows = types_v03.OAuthFlows(
password=types_v03.PasswordOAuthFlow(
token_url='http://token3', # noqa: S106
scopes={'g': 'h'},
refresh_url='ref4',
)
)
v10_expected = pb2_v10.OAuthFlows(
password=pb2_v10.PasswordOAuthFlow(
token_url='http://token3', # noqa: S106
scopes={'g': 'h'},
refresh_url='ref4',
)
)
v10_flows = to_core_oauth_flows(v03_flows)
assert v10_flows == v10_expected
v03_restored = to_compat_oauth_flows(v10_flows)
assert v03_restored == v03_flows
def test_security_scheme_apikey():
v03_scheme = types_v03.SecurityScheme(
root=types_v03.APIKeySecurityScheme(
in_=types_v03.In.header, name='X-API-KEY', description='desc'
)
)
v10_expected = pb2_v10.SecurityScheme(
api_key_security_scheme=pb2_v10.APIKeySecurityScheme(
location='header', name='X-API-KEY', description='desc'
)
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_security_scheme_http_auth():
v03_scheme = types_v03.SecurityScheme(
root=types_v03.HTTPAuthSecurityScheme(
scheme='Bearer', bearer_format='JWT', description='desc'
)
)
v10_expected = pb2_v10.SecurityScheme(
http_auth_security_scheme=pb2_v10.HTTPAuthSecurityScheme(
scheme='Bearer', bearer_format='JWT', description='desc'
)
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_security_scheme_oauth2():
v03_flows = types_v03.OAuthFlows(
authorization_code=types_v03.AuthorizationCodeOAuthFlow(
authorization_url='u',
token_url='t', # noqa: S106
scopes={},
)
)
v03_scheme = types_v03.SecurityScheme(
root=types_v03.OAuth2SecurityScheme(
flows=v03_flows, oauth2_metadata_url='url', description='desc'
)
)
v10_expected = pb2_v10.SecurityScheme(
oauth2_security_scheme=pb2_v10.OAuth2SecurityScheme(
flows=pb2_v10.OAuthFlows(
authorization_code=pb2_v10.AuthorizationCodeOAuthFlow(
authorization_url='u',
token_url='t', # noqa: S106
)
),
oauth2_metadata_url='url',
description='desc',
)
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_security_scheme_oidc():
v03_scheme = types_v03.SecurityScheme(
root=types_v03.OpenIdConnectSecurityScheme(
open_id_connect_url='url', description='desc'
)
)
v10_expected = pb2_v10.SecurityScheme(
open_id_connect_security_scheme=pb2_v10.OpenIdConnectSecurityScheme(
open_id_connect_url='url', description='desc'
)
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_security_scheme_mtls():
v03_scheme = types_v03.SecurityScheme(
root=types_v03.MutualTLSSecurityScheme(description='desc')
)
v10_expected = pb2_v10.SecurityScheme(
mtls_security_scheme=pb2_v10.MutualTlsSecurityScheme(description='desc')
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_oauth_flows_conversion_minimal():
v03_flows = types_v03.OAuthFlows(
authorization_code=types_v03.AuthorizationCodeOAuthFlow(
authorization_url='http://auth',
token_url='http://token', # noqa: S106
scopes={'a': 'b'},
) # no refresh_url
)
v10_expected = pb2_v10.OAuthFlows(
authorization_code=pb2_v10.AuthorizationCodeOAuthFlow(
authorization_url='http://auth',
token_url='http://token', # noqa: S106
scopes={'a': 'b'},
)
)
v10_flows = to_core_oauth_flows(v03_flows)
assert v10_flows == v10_expected
v03_restored = to_compat_oauth_flows(v10_flows)
assert v03_restored == v03_flows
def test_security_scheme_minimal():
v03_scheme = types_v03.SecurityScheme(
root=types_v03.APIKeySecurityScheme(
in_=types_v03.In.header,
name='X-API-KEY', # no description
)
)
v10_expected = pb2_v10.SecurityScheme(
api_key_security_scheme=pb2_v10.APIKeySecurityScheme(
location='header', name='X-API-KEY'
)
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_security_scheme_http_auth_minimal():
v03_scheme = types_v03.SecurityScheme(
root=types_v03.HTTPAuthSecurityScheme(
scheme='Bearer' # no bearer_format, no description
)
)
v10_expected = pb2_v10.SecurityScheme(
http_auth_security_scheme=pb2_v10.HTTPAuthSecurityScheme(
scheme='Bearer'
)
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_security_scheme_oauth2_minimal():
v03_flows = types_v03.OAuthFlows(
implicit=types_v03.ImplicitOAuthFlow(authorization_url='u', scopes={})
)
v03_scheme = types_v03.SecurityScheme(
root=types_v03.OAuth2SecurityScheme(
flows=v03_flows # no oauth2_metadata_url, no description
)
)
v10_expected = pb2_v10.SecurityScheme(
oauth2_security_scheme=pb2_v10.OAuth2SecurityScheme(
flows=pb2_v10.OAuthFlows(
implicit=pb2_v10.ImplicitOAuthFlow(authorization_url='u')
)
)
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_security_scheme_oidc_minimal():
v03_scheme = types_v03.SecurityScheme(
root=types_v03.OpenIdConnectSecurityScheme(
open_id_connect_url='url' # no description
)
)
v10_expected = pb2_v10.SecurityScheme(
open_id_connect_security_scheme=pb2_v10.OpenIdConnectSecurityScheme(
open_id_connect_url='url'
)
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
def test_security_scheme_mtls_minimal():
v03_scheme = types_v03.SecurityScheme(
root=types_v03.MutualTLSSecurityScheme()
)
v10_expected = pb2_v10.SecurityScheme(
mtls_security_scheme=pb2_v10.MutualTlsSecurityScheme()
)
v10_scheme = to_core_security_scheme(v03_scheme)
assert v10_scheme == v10_expected
v03_restored = to_compat_security_scheme(v10_scheme)
assert v03_restored == v03_scheme
v10_scheme = pb2_v10.SecurityScheme()
with pytest.raises(ValueError, match='Unknown security scheme type'):
to_compat_security_scheme(v10_scheme)
def test_agent_interface_conversion():
v03_int = types_v03.AgentInterface(url='http', transport='JSONRPC')
v10_expected = pb2_v10.AgentInterface(
url='http', protocol_binding='JSONRPC', protocol_version='0.3'
)
v10_int = to_core_agent_interface(v03_int)
assert v10_int == v10_expected
v03_restored = to_compat_agent_interface(v10_int)
assert v03_restored == v03_int
def test_agent_provider_conversion():