-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_context_tool.py
More file actions
1223 lines (1023 loc) · 48.8 KB
/
test_context_tool.py
File metadata and controls
1223 lines (1023 loc) · 48.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 context_tool.py module."""
import os
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from langchain_core.documents import Document
from uipath.agent.models.agent import (
AgentContextOutputColumn,
AgentContextQuerySetting,
AgentContextResourceConfig,
AgentContextRetrievalMode,
AgentContextSettings,
AgentContextValueSetting,
)
from uipath.platform.context_grounding import (
CitationMode,
DeepRagContent,
)
from uipath.platform.errors import EnrichedException
from uipath.runtime.errors import UiPathErrorCategory
from uipath_langchain.agent.exceptions import (
AgentRuntimeError,
AgentRuntimeErrorCode,
AgentStartupError,
AgentStartupErrorCode,
)
from uipath_langchain.agent.tools.context_tool import (
_normalize_folder_prefix,
build_glob_pattern,
create_context_tool,
handle_batch_transform,
handle_deep_rag,
handle_semantic_search,
)
from uipath_langchain.agent.tools.structured_tool_with_argument_properties import (
StructuredToolWithArgumentProperties,
)
from uipath_langchain.agent.tools.structured_tool_with_output_type import (
StructuredToolWithOutputType,
)
def _make_context_resource(
name="test_tool",
description="Test tool",
index_name="test-index",
folder_path="/test/folder",
query_value=None,
query_variant="static",
citation_mode_value=None,
retrieval_mode=AgentContextRetrievalMode.SEMANTIC,
folder_path_prefix=None,
context_type="index",
**kwargs,
):
"""Helper to create an AgentContextResourceConfig."""
return AgentContextResourceConfig(
name=name,
description=description,
resource_type="context",
context_type=context_type,
index_name=index_name,
folder_path=folder_path,
settings=AgentContextSettings(
result_count=1,
retrieval_mode=retrieval_mode,
query=AgentContextQuerySetting(
value=query_value,
description="some description",
variant=query_variant,
),
citation_mode=citation_mode_value,
folder_path_prefix=folder_path_prefix,
),
is_enabled=True,
**kwargs,
)
class TestHandleDeepRag:
"""Test cases for handle_deep_rag function."""
@pytest.fixture
def base_resource_config(self):
"""Fixture for base resource configuration."""
return _make_context_resource
def test_successful_deep_rag_creation(self, base_resource_config):
"""Test successful creation of Deep RAG tool with all required fields."""
resource = base_resource_config(
citation_mode_value=AgentContextValueSetting(value="Inline"),
query_value="some query",
)
result = handle_deep_rag("test_deep_rag", resource)
assert isinstance(result, StructuredToolWithArgumentProperties)
assert result.name == "test_deep_rag"
assert result.description == "Test tool"
assert hasattr(result.args_schema, "model_json_schema")
assert result.args_schema.model_json_schema()["properties"] == {}
assert issubclass(result.output_type, DeepRagContent)
schema = result.output_type.model_json_schema()
assert "deepRagId" in schema["properties"]
assert schema["properties"]["deepRagId"]["type"] == "string"
def test_deep_rag_has_tool_wrapper(self, base_resource_config):
"""Test that Deep RAG tool has a tool wrapper for static args resolution."""
resource = base_resource_config(
citation_mode_value=AgentContextValueSetting(value="Inline"),
query_value="some query",
)
result = handle_deep_rag("test_deep_rag", resource)
assert result.awrapper is not None
def test_deep_rag_with_folder_path_prefix_from_settings(self, base_resource_config):
"""Test that folder_path_prefix with argument variant is resolved in wrapper, not via argument_properties."""
resource = base_resource_config(
citation_mode_value=AgentContextValueSetting(value="Inline"),
query_value="some query",
folder_path_prefix=AgentContextQuerySetting(
value="{deepRagFolderPrefix}", variant="argument"
),
)
result = handle_deep_rag("test_deep_rag", resource)
assert isinstance(result, StructuredToolWithArgumentProperties)
# folder_path_prefix is resolved directly in the wrapper from state,
# not via argument_properties or args_schema
assert "folder_path_prefix" not in result.argument_properties
assert isinstance(result.args_schema, type)
def test_missing_static_query_value_raises_error(self, base_resource_config):
"""Test that missing query.value for static variant raises AgentStartupError."""
resource = base_resource_config(query_variant="static", query_value=None)
with pytest.raises(AgentStartupError) as exc_info:
handle_deep_rag("test_deep_rag", resource)
assert exc_info.value.error_info.code == AgentStartupError.full_code(
AgentStartupErrorCode.INVALID_TOOL_CONFIG
)
def test_missing_query_variant_raises_error(self, base_resource_config):
"""Test that missing query.variant raises AgentStartupError."""
resource = base_resource_config(query_value="some query")
resource.settings.query.variant = None
with pytest.raises(AgentStartupError) as exc_info:
handle_deep_rag("test_deep_rag", resource)
assert exc_info.value.error_info.code == AgentStartupError.full_code(
AgentStartupErrorCode.INVALID_TOOL_CONFIG
)
def test_missing_citation_mode_raises_error(self, base_resource_config):
"""Test that missing citation_mode raises AgentStartupError."""
resource = base_resource_config(
query_value="some query", citation_mode_value=None
)
resource.settings.citation_mode = None
with pytest.raises(AgentStartupError) as exc_info:
handle_deep_rag("test_deep_rag", resource)
assert exc_info.value.error_info.code == AgentStartupError.full_code(
AgentStartupErrorCode.INVALID_TOOL_CONFIG
)
@pytest.mark.parametrize(
"citation_mode_value,expected_enum",
[
(AgentContextValueSetting(value="Inline"), CitationMode.INLINE),
(AgentContextValueSetting(value="Skip"), CitationMode.SKIP),
],
)
def test_citation_mode_conversion(
self, base_resource_config, citation_mode_value, expected_enum
):
"""Test that citation mode is correctly converted to CitationMode enum."""
resource = base_resource_config(
query_value="some query", citation_mode_value=citation_mode_value
)
result = handle_deep_rag("test_deep_rag", resource)
assert isinstance(result, StructuredToolWithArgumentProperties)
def test_tool_name_preserved(self, base_resource_config):
"""Test that the sanitized tool name is correctly applied."""
resource = base_resource_config(
name="My Deep RAG Tool",
citation_mode_value=AgentContextValueSetting(value="Inline"),
query_value="some query",
)
result = handle_deep_rag("my_deep_rag_tool", resource)
assert result.name == "my_deep_rag_tool"
def test_tool_description_preserved(self, base_resource_config):
"""Test that the tool description is correctly preserved."""
custom_description = "Custom description for Deep RAG retrieval"
resource = base_resource_config(
description=custom_description,
citation_mode_value=AgentContextValueSetting(value="Inline"),
query_value="some query",
)
result = handle_deep_rag("test_tool", resource)
assert result.description == custom_description
@pytest.mark.asyncio
async def test_tool_with_different_citation_modes(self, base_resource_config):
"""Test tool creation and invocation with different citation modes."""
for mode_value, expected_mode in [
("Inline", CitationMode.INLINE),
("Skip", CitationMode.SKIP),
]:
resource = base_resource_config(
query_value="test query",
citation_mode_value=AgentContextValueSetting(value=mode_value),
)
tool = handle_deep_rag("test_tool", resource)
with patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt:
mock_interrupt.return_value = {"mocked": "response"}
assert tool.coroutine is not None
await tool.coroutine()
call_args = mock_interrupt.call_args[0][0]
assert call_args.citation_mode == expected_mode
@pytest.mark.asyncio
async def test_unique_task_names_on_multiple_invocations(
self, base_resource_config
):
"""Test that each tool invocation generates a unique task name."""
resource = base_resource_config(
query_value="test query",
citation_mode_value=AgentContextValueSetting(value="Inline"),
)
tool = handle_deep_rag("test_tool", resource)
task_names = []
with patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt:
mock_interrupt.return_value = {"mocked": "response"}
# Invoke the tool multiple times
assert tool.coroutine is not None
for _ in range(3):
await tool.coroutine()
call_args = mock_interrupt.call_args[0][0]
task_names.append(call_args.name)
# Verify all task names are unique
assert len(task_names) == len(set(task_names))
# Verify all have task- prefix
assert all(name.startswith("task-") for name in task_names)
def test_dynamic_query_deep_rag_creation(self, base_resource_config):
"""Test successful creation of Deep RAG tool with dynamic query."""
resource = base_resource_config(
query_variant="dynamic",
query_value=None,
citation_mode_value=AgentContextValueSetting(value="Inline"),
)
result = handle_deep_rag("test_deep_rag", resource)
assert isinstance(result, StructuredToolWithArgumentProperties)
assert result.name == "test_deep_rag"
assert result.description == "Test tool"
assert result.args_schema is not None # Dynamic has input schema
assert issubclass(result.output_type, DeepRagContent)
def test_dynamic_query_deep_rag_has_query_parameter(self, base_resource_config):
"""Test that dynamic Deep RAG tool has query parameter in schema."""
resource = base_resource_config(
query_variant="dynamic",
query_value=None,
citation_mode_value=AgentContextValueSetting(value="Inline"),
)
result = handle_deep_rag("test_deep_rag", resource)
# Check that the input schema has a query field
assert result.args_schema is not None
assert hasattr(result.args_schema, "model_json_schema")
schema = result.args_schema.model_json_schema()
assert "properties" in schema
assert "query" in schema["properties"]
assert schema["properties"]["query"]["type"] == "string"
@pytest.mark.asyncio
async def test_dynamic_query_uses_provided_query(self, base_resource_config):
"""Test that dynamic query variant uses the query parameter provided at runtime."""
resource = base_resource_config(
query_variant="dynamic",
query_value=None,
citation_mode_value=AgentContextValueSetting(value="Inline"),
)
tool = handle_deep_rag("test_tool", resource)
with patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt:
mock_interrupt.return_value = {"mocked": "response"}
assert tool.coroutine is not None
await tool.coroutine(query="runtime provided query")
call_args = mock_interrupt.call_args[0][0]
assert call_args.prompt == "runtime provided query"
@pytest.mark.asyncio
@patch.dict(os.environ, {"UIPATH_FOLDER_PATH": "/Shared/TestFolder"})
async def test_deep_rag_uses_execution_folder_path(self, base_resource_config):
"""Test that CreateDeepRag receives index_folder_path from the execution environment."""
resource = base_resource_config(
query_variant="static",
query_value="test query",
citation_mode_value=AgentContextValueSetting(value="Inline"),
)
tool = handle_deep_rag("test_tool", resource)
with patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt:
mock_interrupt.return_value = {"mocked": "response"}
assert tool.coroutine is not None
await tool.coroutine()
deep_rag_arg = mock_interrupt.call_args[0][0]
assert deep_rag_arg.index_folder_path == "/Shared/TestFolder"
class TestCreateContextTool:
"""Test cases for create_context_tool function."""
@pytest.fixture
def semantic_search_config(self):
"""Fixture for semantic search configuration."""
return _make_context_resource(
name="test_semantic_search",
description="Test semantic search",
retrieval_mode=AgentContextRetrievalMode.SEMANTIC,
query_variant="dynamic",
)
@pytest.fixture
def deep_rag_config(self):
"""Fixture for deep RAG configuration."""
return _make_context_resource(
name="test_deep_rag",
description="Test Deep RAG",
retrieval_mode=AgentContextRetrievalMode.DEEP_RAG,
query_value="test query",
query_variant="static",
citation_mode_value=AgentContextValueSetting(value="Inline"),
)
def test_create_semantic_search_tool(self, semantic_search_config):
"""Test that semantic search retrieval mode creates semantic search tool."""
result = create_context_tool(semantic_search_config)
assert isinstance(result, StructuredToolWithOutputType)
assert result.name == "test_semantic_search"
assert result.args_schema is not None # Semantic search has input schema
def test_create_deep_rag_tool(self, deep_rag_config):
"""Test that deep_rag retrieval mode creates Deep RAG tool."""
result = create_context_tool(deep_rag_config)
assert isinstance(result, StructuredToolWithArgumentProperties)
assert result.name == "test_deep_rag"
assert hasattr(result.args_schema, "model_json_schema")
assert result.args_schema.model_json_schema()["properties"] == {}
assert issubclass(result.output_type, DeepRagContent)
def test_case_insensitive_retrieval_mode(self, deep_rag_config):
"""Test that retrieval mode matching is case-insensitive."""
# Test with uppercase
deep_rag_config.settings.retrieval_mode = "DEEPRAG"
result = create_context_tool(deep_rag_config)
assert isinstance(result, StructuredToolWithArgumentProperties)
# Test with mixed case
deep_rag_config.settings.retrieval_mode = "deeprag"
result = create_context_tool(deep_rag_config)
assert isinstance(result, StructuredToolWithArgumentProperties)
class TestHandleSemanticSearch:
"""Test cases for handle_semantic_search function."""
@pytest.fixture
def semantic_config(self):
"""Fixture for semantic search configuration."""
return _make_context_resource(
name="semantic_tool",
description="Semantic search tool",
retrieval_mode=AgentContextRetrievalMode.SEMANTIC,
query_variant="dynamic",
)
def test_semantic_search_tool_creation(self, semantic_config):
"""Test successful creation of semantic search tool."""
result = handle_semantic_search("semantic_tool", semantic_config)
assert isinstance(result, StructuredToolWithOutputType)
assert result.name == "semantic_tool"
assert result.description == "Semantic search tool"
assert result.args_schema is not None
def test_semantic_search_has_query_parameter(self, semantic_config):
"""Test that semantic search tool has query parameter in schema."""
result = handle_semantic_search("semantic_tool", semantic_config)
# Check that the input schema has a query field
assert result.args_schema is not None
assert hasattr(result.args_schema, "model_json_schema")
schema = result.args_schema.model_json_schema()
assert "properties" in schema
assert "query" in schema["properties"]
assert schema["properties"]["query"]["type"] == "string"
@pytest.mark.asyncio
async def test_semantic_search_returns_documents(self, semantic_config):
"""Test that semantic search tool returns documents."""
tool = handle_semantic_search("semantic_tool", semantic_config)
# Mock the retriever
mock_documents = [
Document(page_content="Test content 1", metadata={"source": "doc1"}),
Document(page_content="Test content 2", metadata={"source": "doc2"}),
]
with patch(
"uipath_langchain.agent.tools.context_tool.ContextGroundingRetriever"
) as mock_retriever_class:
mock_retriever = AsyncMock()
mock_retriever.ainvoke.return_value = mock_documents
mock_retriever_class.return_value = mock_retriever
# Recreate the tool with mocked retriever
tool = handle_semantic_search("semantic_tool", semantic_config)
assert tool.coroutine is not None
result = await tool.coroutine(query="test query")
assert "documents" in result
assert len(result["documents"]) == 2
assert result["documents"][0]["page_content"] == "Test content 1"
def test_static_query_semantic_search_creation(self):
"""Test successful creation of semantic search tool with static query."""
resource = _make_context_resource(
name="semantic_tool",
description="Semantic search tool",
retrieval_mode=AgentContextRetrievalMode.SEMANTIC,
query_value="predefined static query",
query_variant="static",
)
result = handle_semantic_search("semantic_tool", resource)
assert isinstance(result, StructuredToolWithOutputType)
assert result.name == "semantic_tool"
assert result.description == "Semantic search tool"
assert hasattr(result.args_schema, "model_json_schema")
assert result.args_schema.model_json_schema()["properties"] == {}
@pytest.mark.asyncio
async def test_static_query_uses_predefined_query(self):
"""Test that static query variant uses the predefined query value."""
resource = _make_context_resource(
name="semantic_tool",
description="Semantic search tool",
retrieval_mode=AgentContextRetrievalMode.SEMANTIC,
query_value="predefined static query",
query_variant="static",
)
mock_documents = [
Document(page_content="Test content", metadata={"source": "doc1"}),
]
with patch(
"uipath_langchain.agent.tools.context_tool.ContextGroundingRetriever"
) as mock_retriever_class:
mock_retriever = AsyncMock()
mock_retriever.ainvoke.return_value = mock_documents
mock_retriever_class.return_value = mock_retriever
tool = handle_semantic_search("semantic_tool", resource)
assert tool.coroutine is not None
result = await tool.coroutine()
# Verify the retriever was called with the static query value
mock_retriever.ainvoke.assert_called_once_with("predefined static query")
assert "documents" in result
assert len(result["documents"]) == 1
@pytest.mark.asyncio
@patch.dict(os.environ, {"UIPATH_FOLDER_PATH": "/Shared/TestFolder"})
async def test_semantic_search_uses_execution_folder_path(self, semantic_config):
"""Test that ContextGroundingRetriever receives folder_path from the execution environment."""
with patch(
"uipath_langchain.agent.tools.context_tool.ContextGroundingRetriever"
) as mock_retriever_class:
mock_retriever = AsyncMock()
mock_retriever.ainvoke.return_value = []
mock_retriever_class.return_value = mock_retriever
tool = handle_semantic_search("semantic_tool", semantic_config)
assert tool.coroutine is not None
await tool.coroutine(query="test query")
call_kwargs = mock_retriever_class.call_args[1]
assert call_kwargs["folder_path"] == "/Shared/TestFolder"
@pytest.mark.asyncio
async def test_semantic_search_enables_system_index_fallback_when_not_studio_project(
self,
semantic_config,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.delenv("UIPATH_PROJECT_ID", raising=False)
with patch(
"uipath_langchain.agent.tools.context_tool.ContextGroundingRetriever"
) as mock_retriever_class:
mock_retriever = AsyncMock()
mock_retriever.ainvoke.return_value = []
mock_retriever_class.return_value = mock_retriever
tool = handle_semantic_search("semantic_tool", semantic_config)
assert tool.coroutine is not None
await tool.coroutine(query="test query")
call_kwargs = mock_retriever_class.call_args.kwargs
assert call_kwargs["include_system_indexes"] is True
@pytest.mark.asyncio
async def test_semantic_search_disables_system_index_fallback_when_studio_project(
self,
semantic_config,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("UIPATH_PROJECT_ID", "some-project-id")
with patch(
"uipath_langchain.agent.tools.context_tool.ContextGroundingRetriever"
) as mock_retriever_class:
mock_retriever = AsyncMock()
mock_retriever.ainvoke.return_value = []
mock_retriever_class.return_value = mock_retriever
tool = handle_semantic_search("semantic_tool", semantic_config)
assert tool.coroutine is not None
await tool.coroutine(query="test query")
call_kwargs = mock_retriever_class.call_args.kwargs
assert call_kwargs["include_system_indexes"] is False
class TestHandleBatchTransform:
"""Test cases for handle_batch_transform function."""
@pytest.fixture
def batch_transform_config(self):
"""Fixture for batch transform configuration with static query."""
return AgentContextResourceConfig(
name="batch_transform_tool",
description="Batch transform tool",
resource_type="context",
index_name="test-index",
folder_path="/test/folder",
settings=AgentContextSettings(
result_count=5,
retrieval_mode=AgentContextRetrievalMode.BATCH_TRANSFORM,
query=AgentContextQuerySetting(
value="transform this data",
description="Static query for batch transform",
variant="static",
),
web_search_grounding=AgentContextValueSetting(value="enabled"),
output_columns=[
AgentContextOutputColumn(
name="output_col1", description="First output column"
),
AgentContextOutputColumn(
name="output_col2", description="Second output column"
),
],
),
is_enabled=True,
)
def test_static_query_batch_transform_creation(self, batch_transform_config):
"""Test successful creation of batch transform tool with static query."""
result = handle_batch_transform("batch_transform_tool", batch_transform_config)
assert isinstance(result, StructuredToolWithArgumentProperties)
assert result.name == "batch_transform_tool"
assert result.description == "Batch transform tool"
assert result.args_schema is not None # Has destination_path parameter
# Output model is built from the job-attachment schema so that the
# job_attachment_wrapper can locate and register the attachment.
output_schema = result.output_type.model_json_schema()
assert "result" in output_schema.get("properties", {})
def test_static_query_batch_transform_has_destination_path_only(
self, batch_transform_config
):
"""Test that static batch transform only has destination_path in schema."""
result = handle_batch_transform("batch_transform_tool", batch_transform_config)
assert result.args_schema is not None
assert hasattr(result.args_schema, "model_json_schema")
schema = result.args_schema.model_json_schema()
assert "properties" in schema
assert "destination_path" in schema["properties"]
assert "query" not in schema["properties"] # No query for static
def test_dynamic_query_batch_transform_creation(self):
"""Test successful creation of batch transform tool with dynamic query."""
resource = AgentContextResourceConfig(
name="batch_transform_tool",
description="Batch transform tool",
resource_type="context",
index_name="test-index",
folder_path="/test/folder",
settings=AgentContextSettings(
result_count=5,
retrieval_mode=AgentContextRetrievalMode.BATCH_TRANSFORM,
query=AgentContextQuerySetting(
value=None,
description="Dynamic query for batch transform",
variant="dynamic",
),
web_search_grounding=AgentContextValueSetting(value="enabled"),
output_columns=[
AgentContextOutputColumn(
name="output_col1", description="First output column"
),
],
),
is_enabled=True,
)
result = handle_batch_transform("batch_transform_tool", resource)
assert isinstance(result, StructuredToolWithArgumentProperties)
assert result.name == "batch_transform_tool"
assert result.args_schema is not None
output_schema = result.output_type.model_json_schema()
assert "result" in output_schema.get("properties", {})
def test_dynamic_query_batch_transform_has_both_parameters(self):
"""Test that dynamic batch transform has both query and destination_path."""
resource = AgentContextResourceConfig(
name="batch_transform_tool",
description="Batch transform tool",
resource_type="context",
index_name="test-index",
folder_path="/test/folder",
settings=AgentContextSettings(
result_count=5,
retrieval_mode=AgentContextRetrievalMode.BATCH_TRANSFORM,
query=AgentContextQuerySetting(
value=None,
description="Dynamic query for batch transform",
variant="dynamic",
),
web_search_grounding=AgentContextValueSetting(value="enabled"),
output_columns=[
AgentContextOutputColumn(
name="output_col1", description="First output column"
),
],
),
is_enabled=True,
)
result = handle_batch_transform("batch_transform_tool", resource)
assert result.args_schema is not None
assert hasattr(result.args_schema, "model_json_schema")
schema = result.args_schema.model_json_schema()
assert "properties" in schema
assert "query" in schema["properties"]
assert "destination_path" in schema["properties"]
def test_batch_transform_with_folder_path_prefix_from_settings(self):
"""Test that batch transform builds argument_properties from settings."""
resource = AgentContextResourceConfig(
name="batch_transform_tool",
description="Batch transform tool",
resource_type="context",
index_name="test-index",
folder_path="/test/folder",
settings=AgentContextSettings(
result_count=5,
retrieval_mode=AgentContextRetrievalMode.BATCH_TRANSFORM,
query=AgentContextQuerySetting(
value="transform query",
description="Static query",
variant="static",
),
web_search_grounding=AgentContextValueSetting(value="enabled"),
output_columns=[
AgentContextOutputColumn(
name="output_col1", description="First output column"
),
],
folder_path_prefix=AgentContextQuerySetting(
value="{batchFolderPrefix}", variant="argument"
),
),
is_enabled=True,
)
result = handle_batch_transform("batch_transform_tool", resource)
assert isinstance(result, StructuredToolWithArgumentProperties)
# folder_path_prefix is resolved directly in the wrapper from state,
# not via argument_properties or args_schema
assert "folder_path_prefix" not in result.argument_properties
assert isinstance(result.args_schema, type)
@pytest.mark.asyncio
async def test_static_query_batch_transform_uses_predefined_query(
self, batch_transform_config
):
"""Test that static query variant uses the predefined query value."""
tool = handle_batch_transform("batch_transform_tool", batch_transform_config)
mock_uipath = AsyncMock()
mock_uipath.jobs.create_attachment_async = AsyncMock(return_value="att-id-1")
with (
patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt,
patch(
"uipath_langchain.agent.tools.context_tool.UiPath",
return_value=mock_uipath,
),
):
mock_interrupt.return_value = {"mocked": "response"}
assert tool.coroutine is not None
await tool.coroutine(destination_path="/output/result.csv")
call_args = mock_interrupt.call_args[0][0]
assert call_args.prompt == "transform this data"
assert call_args.destination_path == "/output/result.csv"
@pytest.mark.asyncio
async def test_dynamic_query_batch_transform_uses_provided_query(self):
"""Test that dynamic query variant uses the query parameter provided at runtime."""
resource = AgentContextResourceConfig(
name="batch_transform_tool",
description="Batch transform tool",
resource_type="context",
index_name="test-index",
folder_path="/test/folder",
settings=AgentContextSettings(
result_count=5,
retrieval_mode=AgentContextRetrievalMode.BATCH_TRANSFORM,
query=AgentContextQuerySetting(
value=None,
description="Dynamic query for batch transform",
variant="dynamic",
),
web_search_grounding=AgentContextValueSetting(value="enabled"),
output_columns=[
AgentContextOutputColumn(
name="output_col1", description="First output column"
),
],
),
is_enabled=True,
)
tool = handle_batch_transform("batch_transform_tool", resource)
mock_uipath = AsyncMock()
mock_uipath.jobs.create_attachment_async = AsyncMock(return_value="att-id-2")
with (
patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt,
patch(
"uipath_langchain.agent.tools.context_tool.UiPath",
return_value=mock_uipath,
),
):
mock_interrupt.return_value = {"mocked": "response"}
assert tool.coroutine is not None
await tool.coroutine(
query="runtime provided query", destination_path="/output/result.csv"
)
call_args = mock_interrupt.call_args[0][0]
assert call_args.prompt == "runtime provided query"
assert call_args.destination_path == "/output/result.csv"
@pytest.mark.asyncio
async def test_static_query_batch_transform_uses_default_destination_path(
self, batch_transform_config
):
"""Test that static batch transform uses default destination_path when not provided."""
tool = handle_batch_transform("batch_transform_tool", batch_transform_config)
mock_uipath = AsyncMock()
mock_uipath.jobs.create_attachment_async = AsyncMock(return_value="att-id-3")
with (
patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt,
patch(
"uipath_langchain.agent.tools.context_tool.UiPath",
return_value=mock_uipath,
),
):
mock_interrupt.return_value = {"mocked": "response"}
assert tool.coroutine is not None
# Call without providing destination_path
await tool.coroutine()
call_args = mock_interrupt.call_args[0][0]
assert call_args.prompt == "transform this data"
assert call_args.destination_path == "output.csv"
@pytest.mark.asyncio
async def test_dynamic_query_batch_transform_uses_default_destination_path(self):
"""Test that dynamic batch transform uses default destination_path when not provided."""
resource = AgentContextResourceConfig(
name="batch_transform_tool",
description="Batch transform tool",
resource_type="context",
index_name="test-index",
folder_path="/test/folder",
settings=AgentContextSettings(
result_count=5,
retrieval_mode=AgentContextRetrievalMode.BATCH_TRANSFORM,
query=AgentContextQuerySetting(
value=None,
description="Dynamic query for batch transform",
variant="dynamic",
),
web_search_grounding=AgentContextValueSetting(value="enabled"),
output_columns=[
AgentContextOutputColumn(
name="output_col1", description="First output column"
),
],
),
is_enabled=True,
)
tool = handle_batch_transform("batch_transform_tool", resource)
mock_uipath = AsyncMock()
mock_uipath.jobs.create_attachment_async = AsyncMock(return_value="att-id-4")
with (
patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt,
patch(
"uipath_langchain.agent.tools.context_tool.UiPath",
return_value=mock_uipath,
),
):
mock_interrupt.return_value = {"mocked": "response"}
assert tool.coroutine is not None
# Call with only query, no destination_path
await tool.coroutine(query="runtime provided query")
call_args = mock_interrupt.call_args[0][0]
assert call_args.prompt == "runtime provided query"
assert call_args.destination_path == "output.csv"
@pytest.mark.asyncio
@patch.dict(os.environ, {"UIPATH_FOLDER_PATH": "/Shared/TestFolder"})
async def test_batch_transform_uses_execution_folder_path(
self, batch_transform_config
):
"""Test that CreateBatchTransform receives index_folder_path from the execution environment."""
tool = handle_batch_transform("batch_transform_tool", batch_transform_config)
mock_uipath = MagicMock()
mock_uipath.jobs.create_attachment_async = AsyncMock(return_value="att-id")
with (
patch(
"uipath_langchain._utils.durable_interrupt.decorator.interrupt"
) as mock_interrupt,
patch(
"uipath_langchain.agent.tools.context_tool.UiPath",
return_value=mock_uipath,
),
):
mock_interrupt.return_value = MagicMock()
assert tool.coroutine is not None
await tool.coroutine(destination_path="output.csv")
batch_transform_arg = mock_interrupt.call_args[0][0]
assert batch_transform_arg.index_folder_path == "/Shared/TestFolder"
class TestBuildGlobPattern:
"""Test cases for build_glob_pattern function."""
# --- No prefix ---
def test_no_prefix_no_extension(self):
assert build_glob_pattern(None, None) == "**/*"
def test_empty_string_prefix_treated_as_no_prefix(self):
assert build_glob_pattern("", None) == "**/*"
def test_no_prefix_with_extension(self):
assert build_glob_pattern(None, "pdf") == "**/*.pdf"
def test_no_prefix_extension_uppercased(self):
"""Extension should be lowercased before building the pattern."""
assert build_glob_pattern(None, "PDF") == "**/*.pdf"
def test_no_prefix_extension_mixed_case(self):
assert build_glob_pattern(None, "TxT") == "**/*.txt"
# --- Explicit "**" prefix ---
def test_double_star_prefix_no_extension(self):
assert build_glob_pattern("**", None) == "**/*"
def test_double_star_prefix_with_extension(self):
assert build_glob_pattern("**", "pdf") == "**/*.pdf"
# --- Prefix with trailing slash stripped ---
def test_prefix_trailing_slash_stripped(self):
assert build_glob_pattern("documents/", "pdf") == "documents/*.pdf"
def test_prefix_multiple_trailing_slashes_stripped(self):
assert build_glob_pattern("documents///", "pdf") == "documents/*.pdf"
# --- Prefix with leading slash stripped ---
def test_prefix_leading_slash_stripped(self):
assert build_glob_pattern("/documents", "pdf") == "documents/*.pdf"
def test_prefix_leading_and_trailing_slash_stripped(self):
assert build_glob_pattern("/documents/", None) == "documents/*"
# --- Prefix starting with "**" is kept as-is ---
def test_prefix_starting_with_double_star_kept(self):
assert build_glob_pattern("**/documents", "pdf") == "**/documents/*.pdf"
def test_prefix_starting_with_double_star_leading_slash_not_stripped(self):
"""A prefix that already starts with ** is not modified further."""
assert build_glob_pattern("**/docs/", "txt") == "**/docs/*.txt"
# --- Normal prefix without slashes ---
def test_simple_prefix_no_extension(self):
assert build_glob_pattern("folder", None) == "folder/*"
def test_simple_prefix_with_extension(self):
assert build_glob_pattern("folder", "pdf") == "folder/*.pdf"
def test_nested_prefix_with_extension(self):
assert (
build_glob_pattern("folder/subfolder", "docx") == "folder/subfolder/*.docx"
)
# --- All supported extensions ---
@pytest.mark.parametrize("ext", ["pdf", "txt", "docx", "csv"])
def test_supported_extensions(self, ext):
assert build_glob_pattern(None, ext) == f"**/*.{ext}"
def test_unsupported_extension_still_works(self):
"""Extensions outside the named set are handled identically."""
assert build_glob_pattern("data", "xlsx") == "data/*.xlsx"
# --- Trailing file-matching globs stripped ---
def test_prefix_with_trailing_star(self):
"""Trailing /* is stripped since extension is appended separately."""
assert build_glob_pattern("documents/*", "pdf") == "documents/*.pdf"
def test_prefix_with_trailing_double_star(self):
"""Trailing /** is stripped."""
assert build_glob_pattern("documents/**", "pdf") == "documents/*.pdf"