|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import uuid |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from dotenv import load_dotenv |
| 8 | +from uipath._cli._runtime._contracts import UiPathTraceContext |
| 9 | + |
| 10 | +from uipath_langchain._cli._runtime._context import LangGraphRuntimeContext |
| 11 | +from uipath_langchain._cli._runtime._runtime import LangGraphRuntime |
| 12 | +from uipath_langchain._cli._utils._graph import LangGraphConfig |
| 13 | + |
| 14 | +load_dotenv() |
| 15 | + |
| 16 | + |
| 17 | +def _create_test_runtime_context(config: LangGraphConfig) -> LangGraphRuntimeContext: |
| 18 | + """Helper function to create and configure LangGraphRuntimeContext for tests.""" |
| 19 | + context = LangGraphRuntimeContext.from_config( |
| 20 | + os.environ.get("UIPATH_CONFIG_PATH", "uipath.json") |
| 21 | + ) |
| 22 | + |
| 23 | + context.entrypoint = ( |
| 24 | + None # Or a specific graph name if needed, None will pick the single one |
| 25 | + ) |
| 26 | + context.input = '{ "graph_state": "GET Assets API does not enforce proper permissions Assets.View" }' |
| 27 | + context.resume = False |
| 28 | + context.langgraph_config = config |
| 29 | + context.logs_min_level = os.environ.get("LOG_LEVEL", "INFO") |
| 30 | + context.job_id = str(uuid.uuid4()) |
| 31 | + context.trace_id = str(uuid.uuid4()) |
| 32 | + # Convert string "True" or "False" to boolean for tracing_enabled |
| 33 | + tracing_enabled_str = os.environ.get("UIPATH_TRACING_ENABLED", "True") |
| 34 | + context.tracing_enabled = tracing_enabled_str.lower() == "true" |
| 35 | + context.trace_context = UiPathTraceContext( |
| 36 | + enabled=context.tracing_enabled, |
| 37 | + trace_id=str( |
| 38 | + uuid.uuid4() |
| 39 | + ), # Consider passing trace_id if it needs to match context.trace_id |
| 40 | + parent_span_id=os.environ.get("UIPATH_PARENT_SPAN_ID"), |
| 41 | + root_span_id=os.environ.get("UIPATH_ROOT_SPAN_ID"), |
| 42 | + job_id=os.environ.get( |
| 43 | + "UIPATH_JOB_KEY" |
| 44 | + ), # Consider passing job_id if it needs to match context.job_id |
| 45 | + org_id=os.environ.get("UIPATH_ORGANIZATION_ID"), |
| 46 | + tenant_id=os.environ.get("UIPATH_TENANT_ID"), |
| 47 | + process_key=os.environ.get("UIPATH_PROCESS_UUID"), |
| 48 | + folder_key=os.environ.get("UIPATH_FOLDER_KEY"), |
| 49 | + ) |
| 50 | + # Convert string "True" or "False" to boolean |
| 51 | + langsmith_tracing_enabled_str = os.environ.get("LANGSMITH_TRACING", "False") |
| 52 | + context.langsmith_tracing_enabled = langsmith_tracing_enabled_str.lower() == "true" |
| 53 | + return context |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_langgraph_runtime(): |
| 58 | + test_folder_path = os.path.dirname(os.path.abspath(__file__)) |
| 59 | + sample_path = os.path.join(test_folder_path, "samples", "1-simple-graph") |
| 60 | + |
| 61 | + sys.path.append(sample_path) |
| 62 | + os.chdir(sample_path) |
| 63 | + |
| 64 | + config = LangGraphConfig() |
| 65 | + if not config.exists: |
| 66 | + raise AssertionError("langgraph.json not found in sample path") |
| 67 | + |
| 68 | + context = _create_test_runtime_context(config) |
| 69 | + |
| 70 | + # Mocking UiPath SDK for action creation |
| 71 | + with patch("uipath_langchain._cli._runtime._output.UiPath") as MockUiPathClass: |
| 72 | + mock_uipath_sdk_instance = MagicMock() |
| 73 | + MockUiPathClass.return_value = mock_uipath_sdk_instance |
| 74 | + mock_actions_client = MagicMock() |
| 75 | + mock_uipath_sdk_instance.actions = mock_actions_client |
| 76 | + |
| 77 | + mock_created_action = MagicMock() |
| 78 | + mock_created_action.key = "mock_action_key_from_test" |
| 79 | + mock_actions_client.create.return_value = mock_created_action |
| 80 | + |
| 81 | + result = None |
| 82 | + async with LangGraphRuntime.from_context(context) as runtime: |
| 83 | + result = await runtime.execute() |
| 84 | + print("Result:", result) |
| 85 | + |
| 86 | + context.resume = True |
| 87 | + context.input = '{ "answer": "John Doe"}' # Simulate some resume data |
| 88 | + async with LangGraphRuntime.from_context(context) as runtime: |
| 89 | + result = await runtime.execute() |
| 90 | + print("Result:", result) |
| 91 | + |
| 92 | + context.resume = True |
| 93 | + context.input = ( |
| 94 | + '{ "ActionData": "Test-ActionData" }' # Simulate some resume data |
| 95 | + ) |
| 96 | + async with LangGraphRuntime.from_context(context) as runtime: |
| 97 | + result = await runtime.execute() |
| 98 | + print("Result:", result) |
| 99 | + |
| 100 | + assert result is not None, "Result should not be None after execution" |
| 101 | + assert ( |
| 102 | + result.output["graph_state"] == "Hello, I am John Doe!Test-ActionData end" |
| 103 | + ) |
0 commit comments