-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_integration_tool.py
More file actions
1112 lines (948 loc) · 38.8 KB
/
Copy pathtest_integration_tool.py
File metadata and controls
1112 lines (948 loc) · 38.8 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
"""Tests for integration_tool.py module."""
from unittest.mock import AsyncMock, MagicMock, patch
import httpx
import pytest
from pydantic import BaseModel
from uipath.agent.models.agent import (
AgentIntegrationToolParameter,
AgentIntegrationToolProperties,
AgentIntegrationToolResourceConfig,
AgentToolArgumentArgumentProperties,
AgentToolStaticArgumentProperties,
)
from uipath.platform.connections import ActivityParameterLocationInfo, Connection
from uipath.platform.errors import EnrichedException
from uipath.runtime.errors import UiPathErrorCategory
from uipath_langchain.agent.exceptions import (
AgentRuntimeError,
AgentRuntimeErrorCode,
AgentStartupError,
)
from uipath_langchain.agent.tools.integration_tool import (
_is_param_name_to_jsonpath,
_param_name_to_segments,
convert_integration_parameters_to_argument_properties,
convert_to_activity_metadata,
create_integration_tool,
remove_asterisk_from_properties,
strip_enums_from_schema,
)
from uipath_langchain.agent.tools.structured_tool_with_argument_properties import (
StructuredToolWithArgumentProperties,
)
class TestConvertToIntegrationServiceMetadata:
"""Test cases for convert_to_activity_metadata function."""
@pytest.fixture
def common_connection(self):
"""Common connection object used by all tests."""
return Connection(
id="test-connection-id", name="Test Connection", element_instance_id=12345
)
@pytest.fixture
def base_properties_factory(self, common_connection):
"""Factory for creating base properties with common connection."""
def _create_properties(
method="POST",
tool_path="/api/test",
object_name="test_object",
tool_display_name="Test Tool",
tool_description="Test tool description",
parameters=None,
):
return AgentIntegrationToolProperties(
method=method,
tool_path=tool_path,
object_name=object_name,
tool_display_name=tool_display_name,
tool_description=tool_description,
connection=common_connection,
parameters=parameters or [],
)
return _create_properties
@pytest.fixture
def resource_factory(self, base_properties_factory):
"""Factory for creating resource config with reusable properties."""
def _create_resource(
name="test_tool",
description="Test tool",
properties=None,
**properties_kwargs,
):
if properties is None:
properties = base_properties_factory(**properties_kwargs)
return AgentIntegrationToolResourceConfig(
name=name,
description=description,
properties=properties,
input_schema={},
)
return _create_resource
def test_basic_conversion(self, resource_factory):
"""Test basic conversion with minimal parameters."""
param = AgentIntegrationToolParameter(
name="test_param", type="string", field_location="body"
)
resource = resource_factory(parameters=[param])
result = convert_to_activity_metadata(resource)
assert result.object_path == "/api/test"
assert result.method_name == "POST"
assert result.content_type == "application/json"
assert isinstance(result.parameter_location_info, ActivityParameterLocationInfo)
def test_getbyid_method_normalization(self, resource_factory):
"""Test that GETBYID method is normalized to GET."""
resource = resource_factory(method="GETBYID")
result = convert_to_activity_metadata(resource)
assert result.method_name == "GET"
def test_jsonpath_parameter_handling_nested_field(self, resource_factory):
"""Test handling of jsonpath parameter names with nested fields should extract top-level field only."""
param = AgentIntegrationToolParameter(
name="metadata.field.test", type="string", field_location="body"
)
resource = resource_factory(
name="create_tool",
description="Create tool",
tool_path="/api/create",
object_name="create_object",
tool_display_name="Create Tool",
tool_description="Create tool description",
parameters=[param],
)
result = convert_to_activity_metadata(resource)
# DESIRED BEHAVIOR: Should extract only the top-level field "metadata"
assert "metadata" in result.parameter_location_info.body_fields
assert len(result.parameter_location_info.body_fields) == 1
@pytest.mark.parametrize(
"param_name,expected_field",
[
("attachments[*]", "attachments"),
("attachments[0]", "attachments"),
("attachments[1]", "attachments"),
("attachments[10]", "attachments"),
("attachments[*][*]", "attachments"),
("attachments[*][*][*]", "attachments"),
("attachments[*][0][*]", "attachments"),
("attachments[*].property", "attachments"),
],
)
def test_jsonpath_parameter_handling_array_notation(
self, resource_factory, param_name, expected_field
):
"""Test handling of jsonpath parameter names with array notation should extract top-level field only."""
param = AgentIntegrationToolParameter(
name=param_name, type="string", field_location="body"
)
resource = resource_factory(
name="create_tool",
description="Create tool",
tool_path="/api/create",
object_name="create_object",
tool_display_name="Create Tool",
tool_description="Create tool description",
parameters=[param],
)
result = convert_to_activity_metadata(resource)
# DESIRED BEHAVIOR: Should extract only the top-level field
assert expected_field in result.parameter_location_info.body_fields
assert param_name not in result.parameter_location_info.body_fields
assert len(result.parameter_location_info.body_fields) == 1
def test_jsonpath_parameter_handling_multiple_nested_same_root(
self, resource_factory
):
"""Test that multiple parameters with same root field are consolidated into one top-level field."""
params = [
AgentIntegrationToolParameter(
name="metadata.field1", type="string", field_location="body"
),
AgentIntegrationToolParameter(
name="metadata.field2", type="string", field_location="body"
),
AgentIntegrationToolParameter(
name="metadata.nested.field", type="string", field_location="body"
),
]
resource = resource_factory(
name="create_tool",
description="Create tool",
tool_path="/api/create",
object_name="create_object",
tool_display_name="Create Tool",
tool_description="Create tool description",
parameters=params,
)
result = convert_to_activity_metadata(resource)
# DESIRED BEHAVIOR: Should have only "metadata" once in body_fields
assert "metadata" in result.parameter_location_info.body_fields
assert len(result.parameter_location_info.body_fields) == 1
# These should NOT be present
assert "metadata.field1" not in result.parameter_location_info.body_fields
assert "metadata.field2" not in result.parameter_location_info.body_fields
assert "metadata.nested.field" not in result.parameter_location_info.body_fields
def test_json_body_section_from_body_structure(self, resource_factory):
"""Test that jsonBodySection is extracted from body_structure."""
param = AgentIntegrationToolParameter(
name="prompt", type="string", field_location="body"
)
resource = resource_factory(parameters=[param])
resource.properties.body_structure = {
"contentType": "multipart",
"jsonBodySection": "RagRequest",
}
result = convert_to_activity_metadata(resource)
assert result.content_type == "multipart/form-data"
assert result.json_body_section == "RagRequest"
def test_json_body_section_none_when_not_specified(self, resource_factory):
"""Test that json_body_section is None when bodyStructure has no jsonBodySection."""
param = AgentIntegrationToolParameter(
name="prompt", type="string", field_location="body"
)
resource = resource_factory(parameters=[param])
resource.properties.body_structure = {"contentType": "multipart"}
result = convert_to_activity_metadata(resource)
assert result.content_type == "multipart/form-data"
assert result.json_body_section is None
def test_json_body_section_none_when_no_body_structure(self, resource_factory):
"""Test that json_body_section is None when body_structure is None."""
param = AgentIntegrationToolParameter(
name="prompt", type="string", field_location="body"
)
resource = resource_factory(parameters=[param])
result = convert_to_activity_metadata(resource)
assert result.content_type == "application/json"
assert result.json_body_section is None
def test_parameter_location_mapping_simple_fields(self, resource_factory):
"""Test parameter mapping for simple field names across different locations."""
params = [
AgentIntegrationToolParameter(
name="id", type="string", field_location="path"
),
AgentIntegrationToolParameter(
name="search", type="string", field_location="query"
),
AgentIntegrationToolParameter(
name="authorization", type="string", field_location="header"
),
AgentIntegrationToolParameter(
name="user", type="string", field_location="body"
),
]
resource = resource_factory(
name="update_user_tool",
description="Update user tool",
tool_path="/api/users/{id}",
object_name="user_object",
tool_display_name="Update User Tool",
tool_description="Update user tool description",
parameters=params,
)
result = convert_to_activity_metadata(resource)
# Simple field names should be added as-is for non-body locations
assert "id" in result.parameter_location_info.path_params
assert len(result.parameter_location_info.path_params) == 1
assert "search" in result.parameter_location_info.query_params
assert len(result.parameter_location_info.query_params) == 1
assert "authorization" in result.parameter_location_info.header_params
assert len(result.parameter_location_info.header_params) == 1
assert "user" in result.parameter_location_info.body_fields
assert len(result.parameter_location_info.body_fields) == 1
class TestConvertIntegrationParametersToArgumentProperties:
"""Test cases for convert_integration_parameters_to_argument_properties function."""
def test_static_parameter_converted(self):
"""Static fieldVariant converts to AgentToolStaticArgumentProperties."""
params = [
AgentIntegrationToolParameter(
name="api_key",
type="string",
value="my-secret-key",
field_location="header",
field_variant="static",
),
]
result = convert_integration_parameters_to_argument_properties(params)
assert "$['api_key']" in result
prop = result["$['api_key']"]
assert isinstance(prop, AgentToolStaticArgumentProperties)
assert prop.is_sensitive is False
assert prop.value == "my-secret-key"
def test_argument_parameter_converted(self):
"""Argument fieldVariant converts to AgentToolArgumentArgumentProperties with argument_path extracted from {{...}}."""
params = [
AgentIntegrationToolParameter(
name="user_id",
type="string",
value="{{userId}}",
field_location="path",
field_variant="argument",
),
]
result = convert_integration_parameters_to_argument_properties(params)
assert "$['user_id']" in result
prop = result["$['user_id']"]
assert isinstance(prop, AgentToolArgumentArgumentProperties)
assert prop.is_sensitive is False
assert prop.argument_path == "userId"
def test_mixed_parameters(self):
"""Both static and argument parameters are converted correctly."""
params = [
AgentIntegrationToolParameter(
name="base_url",
type="string",
value="https://api.example.com",
field_location="body",
field_variant="static",
),
AgentIntegrationToolParameter(
name="token",
type="string",
value="{{authToken}}",
field_location="header",
field_variant="argument",
),
]
result = convert_integration_parameters_to_argument_properties(params)
assert len(result) == 2
assert "$['base_url']" in result
static_prop = result["$['base_url']"]
assert isinstance(static_prop, AgentToolStaticArgumentProperties)
assert static_prop.value == "https://api.example.com"
assert "$['token']" in result
arg_prop = result["$['token']"]
assert isinstance(arg_prop, AgentToolArgumentArgumentProperties)
assert arg_prop.argument_path == "authToken"
def test_parameter_without_field_variant_skipped(self):
"""Parameters with no fieldVariant are skipped."""
params = [
AgentIntegrationToolParameter(
name="search_query",
type="string",
value="test",
field_location="query",
# field_variant is None by default
),
AgentIntegrationToolParameter(
name="api_key",
type="string",
value="key-123",
field_location="header",
field_variant="static",
),
]
result = convert_integration_parameters_to_argument_properties(params)
assert len(result) == 1
assert "$['search_query']" not in result
assert "$['api_key']" in result
def test_dynamic_parameter_skipped(self):
"""Parameters with no fieldVariant are skipped."""
params = [
AgentIntegrationToolParameter(
name="search_query",
type="string",
value="test",
field_location="query",
field_variant="dynamic",
),
]
result = convert_integration_parameters_to_argument_properties(params)
assert "$['search_query']" not in result
def test_empty_parameters(self):
"""Empty list returns empty dict."""
result = convert_integration_parameters_to_argument_properties([])
assert result == {}
assert isinstance(result, dict)
def test_nested_static_parameter_has_bracket_notation_key(self):
"""Nested static param produces bracket-notation JSONPath key."""
params = [
AgentIntegrationToolParameter(
name="attachment.title",
type="string",
value="Custom title",
field_location="body",
field_variant="static",
),
]
result = convert_integration_parameters_to_argument_properties(params)
assert "$['attachment']['title']" in result
prop = result["$['attachment']['title']"]
assert isinstance(prop, AgentToolStaticArgumentProperties)
assert prop.value == "Custom title"
def test_nested_argument_parameter_has_bracket_notation_key(self):
"""Nested argument param produces bracket-notation JSONPath key."""
params = [
AgentIntegrationToolParameter(
name="attachment.title_link",
type="string",
value="{{opportunityID}}",
field_location="body",
field_variant="argument",
),
]
result = convert_integration_parameters_to_argument_properties(params)
assert "$['attachment']['title_link']" in result
prop = result["$['attachment']['title_link']"]
assert isinstance(prop, AgentToolArgumentArgumentProperties)
assert prop.argument_path == "opportunityID"
def test_array_notation_parameter_has_bracket_notation_key(self):
"""Array-notation param produces bracket-notation JSONPath key with wildcard."""
params = [
AgentIntegrationToolParameter(
name="attachments[*].text",
type="string",
value="fixed text",
field_location="body",
field_variant="static",
),
]
result = convert_integration_parameters_to_argument_properties(params)
assert "$['attachments'][*]['text']" in result
prop = result["$['attachments'][*]['text']"]
assert isinstance(prop, AgentToolStaticArgumentProperties)
assert prop.is_sensitive is False
assert prop.value == "fixed text"
def test_argument_parameter_with_invalid_template_raises(self):
"""Malformed template raises AgentStartupError."""
params = [
AgentIntegrationToolParameter(
name="bad_param",
type="string",
value="not_a_template",
field_location="body",
field_variant="argument",
),
]
with pytest.raises(AgentStartupError):
convert_integration_parameters_to_argument_properties(params)
@pytest.mark.parametrize(
"invalid_value",
[
"{missing_closing",
"missing_opening}",
"{{missing_closing}",
"{missing_opening}}",
"no_braces_at_all",
],
)
def test_argument_parameter_with_malformed_braces_raises(self, invalid_value):
"""Various malformed brace patterns raise AgentStartupError."""
params = [
AgentIntegrationToolParameter(
name="test_param",
type="string",
value=invalid_value,
field_location="body",
field_variant="argument",
),
]
with pytest.raises(AgentStartupError):
convert_integration_parameters_to_argument_properties(params)
class TestStripEnumsFromSchema:
"""Test cases for strip_enums_from_schema function."""
def test_strips_enum_with_single_template_value(self):
"""An enum containing only a single template value is removed entirely."""
schema = {
"type": "object",
"properties": {
"opportunityID": {
"type": "string",
"enum": ["{{opportunityID}}"],
}
},
}
parameters = [
AgentIntegrationToolParameter(
name="opportunityID",
type="string",
value="{{opportunityID}}",
field_location="body",
field_variant="argument",
),
]
result = strip_enums_from_schema(schema, parameters)
assert "enum" not in result["properties"]["opportunityID"]
def test_strips_static_variant_enum(self):
"""Static-variant param's enum is stripped — StaticArgsHandler handles enforcement separately."""
schema = {
"type": "object",
"properties": {
"title": {
"type": "string",
"enum": ["Custom title"],
},
},
}
parameters = [
AgentIntegrationToolParameter(
name="title",
type="string",
value="Custom title",
field_location="body",
field_variant="static",
),
]
result = strip_enums_from_schema(schema, parameters)
assert "enum" not in result["properties"]["title"]
def test_handles_schema_without_properties(self):
"""A schema with no properties key is returned unchanged."""
schema = {"type": "object"}
result = strip_enums_from_schema(schema, [])
assert result == {"type": "object"}
def test_does_not_mutate_original_schema(self):
"""The original schema object must not be modified."""
schema = {
"type": "object",
"properties": {
"id": {
"type": "string",
"enum": ["{{id}}"],
}
},
}
parameters = [
AgentIntegrationToolParameter(
name="id",
type="string",
value="{{id}}",
field_location="body",
field_variant="argument",
),
]
id_field = schema["properties"]["id"] # type: ignore[index]
original_enum = id_field["enum"][:]
strip_enums_from_schema(schema, parameters)
assert id_field["enum"] == original_enum
def test_strips_enum_on_nested_fields(self):
"""Enums are stripped from nested fields for all parameter variants."""
schema = {
"type": "object",
"properties": {
"attachment": {
"type": "object",
"properties": {
"title_link": {
"type": "string",
"enum": ["{{opportunityID}}"],
},
"title": {
"type": "string",
"enum": ["Custom title"],
},
},
},
},
}
parameters = [
AgentIntegrationToolParameter(
name="attachment.title_link",
type="string",
value="{{opportunityID}}",
field_location="body",
field_variant="argument",
),
AgentIntegrationToolParameter(
name="attachment.title",
type="string",
value="Custom title",
field_location="body",
field_variant="static",
),
]
result = strip_enums_from_schema(schema, parameters)
assert (
"enum" not in result["properties"]["attachment"]["properties"]["title_link"]
)
assert "enum" not in result["properties"]["attachment"]["properties"]["title"]
def test_strips_enum_on_array_nested_field(self):
"""Argument-variant param inside an array is correctly navigated and stripped."""
schema = {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["{{status}}", "active"],
},
},
},
},
},
}
parameters = [
AgentIntegrationToolParameter(
name="items[*].status",
type="string",
value="{{status}}",
field_location="body",
field_variant="argument",
),
]
result = strip_enums_from_schema(schema, parameters)
assert (
"enum" not in result["properties"]["items"]["items"]["properties"]["status"]
)
def test_handles_ref_resolution(self):
"""Argument-variant param with $ref in schema path is resolved and enum stripped."""
schema = {
"type": "object",
"properties": {
"config": {
"$ref": "#/definitions/Config",
},
},
"definitions": {
"Config": {
"type": "object",
"properties": {
"mode": {
"type": "string",
"enum": ["{{mode}}", "auto"],
},
},
},
},
}
parameters = [
AgentIntegrationToolParameter(
name="config.mode",
type="string",
value="{{mode}}",
field_location="body",
field_variant="argument",
),
]
result = strip_enums_from_schema(schema, parameters)
# The $ref is inlined and modified on the inlined copy
config_props = result["properties"]["config"]["properties"]
assert "enum" not in config_props["mode"]
def test_skips_argument_param_when_schema_path_not_found(self):
"""If the schema path for an argument param doesn't exist, skip silently."""
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
},
}
parameters = [
AgentIntegrationToolParameter(
name="nonexistent.field",
type="string",
value="{{val}}",
field_location="body",
field_variant="argument",
),
]
result = strip_enums_from_schema(schema, parameters)
assert result == schema
class TestCreateIntegrationToolWithArgumentProperties:
"""Test cases for create_integration_tool with argument_properties support."""
@pytest.fixture
def common_connection(self):
"""Common connection object used by all tests."""
return Connection(
id="test-connection-id", name="Test Connection", element_instance_id=12345
)
@pytest.fixture
def base_properties_factory(self, common_connection):
"""Factory for creating base properties with common connection."""
def _create_properties(
method="POST",
tool_path="/api/test",
object_name="test_object",
tool_display_name="Test Tool",
tool_description="Test tool description",
parameters=None,
):
return AgentIntegrationToolProperties(
method=method,
tool_path=tool_path,
object_name=object_name,
tool_display_name=tool_display_name,
tool_description=tool_description,
connection=common_connection,
parameters=parameters or [],
)
return _create_properties
@pytest.fixture
def resource_factory(self, base_properties_factory):
"""Factory for creating resource config with reusable properties."""
def _create_resource(
name="test_tool",
description="Test tool",
input_schema=None,
output_schema=None,
properties=None,
**properties_kwargs,
):
if properties is None:
properties = base_properties_factory(**properties_kwargs)
return AgentIntegrationToolResourceConfig(
name=name,
description=description,
properties=properties,
input_schema=input_schema
or {"type": "object", "properties": {"query": {"type": "string"}}},
output_schema=output_schema,
)
return _create_resource
@patch("uipath_langchain.agent.tools.integration_tool.UiPath")
def test_tool_has_argument_properties_for_static_param(
self, mock_uipath_cls, resource_factory
):
"""Tool created with a static parameter has argument_properties entry."""
mock_uipath_cls.return_value = MagicMock()
params = [
AgentIntegrationToolParameter(
name="api_key",
type="string",
value="secret-key-123",
field_location="header",
field_variant="static",
),
]
resource = resource_factory(parameters=params)
tool = create_integration_tool(resource)
assert isinstance(tool, StructuredToolWithArgumentProperties)
assert "$['api_key']" in tool.argument_properties
prop = tool.argument_properties["$['api_key']"]
assert isinstance(prop, AgentToolStaticArgumentProperties)
assert prop.value == "secret-key-123"
@patch("uipath_langchain.agent.tools.integration_tool.UiPath")
def test_tool_strips_template_enum_from_schema(
self, mock_uipath_cls, resource_factory
):
"""Template enum values are stripped from the tool's args_schema for argument-variant params."""
mock_uipath_cls.return_value = MagicMock()
input_schema = {
"type": "object",
"properties": {
"opportunityID": {
"type": "string",
"enum": ["{{opportunityID}}"],
},
"name": {
"type": "string",
},
},
}
params = [
AgentIntegrationToolParameter(
name="opportunityID",
type="string",
value="{{opportunityID}}",
field_location="body",
field_variant="argument",
),
]
resource = resource_factory(input_schema=input_schema, parameters=params)
tool = create_integration_tool(resource)
assert isinstance(tool.args_schema, type) and issubclass(
tool.args_schema, BaseModel
)
schema = tool.args_schema.model_json_schema()
opp_field = schema["properties"]["opportunityID"]
assert "enum" not in opp_field
for def_value in schema.get("$defs", {}).values():
assert "{{opportunityID}}" not in def_value.get("enum", [])
@patch("uipath_langchain.agent.tools.integration_tool.UiPath")
def test_tool_with_no_static_params_has_empty_argument_properties(
self, mock_uipath_cls, resource_factory
):
"""Tool with parameters that have no fieldVariant gets empty argument_properties."""
mock_uipath_cls.return_value = MagicMock()
params = [
AgentIntegrationToolParameter(
name="search_query",
type="string",
field_location="query",
# field_variant is None by default
),
]
resource = resource_factory(parameters=params)
tool = create_integration_tool(resource)
assert isinstance(tool, StructuredToolWithArgumentProperties)
assert tool.argument_properties == {}
class TestParseIsParamName:
"""Test cases for _parse_is_param_name helper."""
def test_simple_field(self):
assert _param_name_to_segments("channel") == ["channel"]
def test_nested_field(self):
assert _param_name_to_segments("attachment.title") == ["attachment", "title"]
def test_deeply_nested_field(self):
assert _param_name_to_segments("metadata.event_payload.id") == [
"metadata",
"event_payload",
"id",
]
def test_array_notation(self):
assert _param_name_to_segments("attachments[*]") == ["attachments", "*"]
def test_array_with_nested_field(self):
assert _param_name_to_segments("attachments[*].text") == [
"attachments",
"*",
"text",
]
def test_deeply_nested_with_multiple_arrays(self):
assert _param_name_to_segments("attachments[*].actions[*].confirm.text") == [
"attachments",
"*",
"actions",
"*",
"confirm",
"text",
]
class TestIsParamNameToJsonpath:
"""Test cases for _is_param_name_to_jsonpath helper."""
def test_simple_field(self):
assert _is_param_name_to_jsonpath("channel") == "$['channel']"
def test_nested_field(self):
assert (
_is_param_name_to_jsonpath("attachment.title") == "$['attachment']['title']"
)
def test_deeply_nested_field(self):
assert _is_param_name_to_jsonpath("metadata.event_payload.id") == (
"$['metadata']['event_payload']['id']"
)
def test_array_notation(self):
assert _is_param_name_to_jsonpath("attachments[*]") == "$['attachments'][*]"
def test_array_with_nested_field(self):
assert _is_param_name_to_jsonpath("attachments[*].text") == (
"$['attachments'][*]['text']"
)
def test_deeply_nested_with_multiple_arrays(self):
assert (
_is_param_name_to_jsonpath("attachments[*].actions[*].confirm.text")
== "$['attachments'][*]['actions'][*]['confirm']['text']"
)
def test_escapes_single_quotes(self):
assert _is_param_name_to_jsonpath("it's_field") == "$['it\\'s_field']"
def test_escapes_backslashes(self):
assert _is_param_name_to_jsonpath("path\\to") == "$['path\\\\to']"
class TestIntegrationToolErrorHandling:
"""Test error handling for integration tool HTTP failures."""
@pytest.fixture
def common_connection(self):
return Connection(
id="test-connection-id", name="Test Connection", element_instance_id=12345
)
@pytest.fixture
def resource(self, common_connection):
return AgentIntegrationToolResourceConfig(
name="test_tool",
description="Test tool",
properties=AgentIntegrationToolProperties(
method="POST",
tool_path="/api/test",
object_name="test_object",
tool_display_name="Test Tool",
tool_description="Test tool description",
connection=common_connection,
parameters=[],
),
input_schema={
"type": "object",
"properties": {"query": {"type": "string"}},
},
)
@pytest.mark.asyncio
@patch("uipath_langchain.agent.tools.integration_tool.UiPath")
async def test_400_raises_agent_runtime_error_with_user_category(
self, mock_uipath_cls, resource, make_enriched_exception
):
mock_sdk = MagicMock()