|
| 1 | +import json |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from uipath_langchain._cli.cli_run import langgraph_run_middleware |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def simple_agent() -> str: |
| 12 | + if os.path.isfile("mocks/simple_agent.py"): |
| 13 | + with open("mocks/simple_agent.py", "r") as file: |
| 14 | + data = file.read() |
| 15 | + else: |
| 16 | + with open("tests/cli/mocks/simple_agent.py", "r") as file: |
| 17 | + data = file.read() |
| 18 | + return data |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def uipath_json() -> str: |
| 23 | + if os.path.isfile("mocks/uipath.json"): |
| 24 | + with open("mocks/uipath.json", "r") as file: |
| 25 | + data = file.read() |
| 26 | + else: |
| 27 | + with open("tests/cli/mocks/uipath.json", "r") as file: |
| 28 | + data = file.read() |
| 29 | + return data |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def langgraph_json() -> str: |
| 34 | + if os.path.isfile("mocks/langgraph.json"): |
| 35 | + with open("mocks/langgraph.json", "r") as file: |
| 36 | + data = file.read() |
| 37 | + else: |
| 38 | + with open("tests/cli/mocks/langgraph.json", "r") as file: |
| 39 | + data = file.read() |
| 40 | + return data |
| 41 | + |
| 42 | + |
| 43 | +class TestRun: |
| 44 | + def test_successful_execution( |
| 45 | + self, |
| 46 | + langgraph_json: str, |
| 47 | + uipath_json: str, |
| 48 | + simple_agent: str, |
| 49 | + mock_env_vars: dict[str, str], |
| 50 | + ): |
| 51 | + os.environ.clear() |
| 52 | + os.environ.update(mock_env_vars) |
| 53 | + input_file_name = "input.json" |
| 54 | + output_file_name = "output.json" |
| 55 | + agent_file_name = "main.py" |
| 56 | + input_json_content = {"topic": "UiPath"} |
| 57 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 58 | + current_dir = os.getcwd() |
| 59 | + os.chdir(temp_dir) |
| 60 | + # Create input and output files |
| 61 | + input_file_path = os.path.join(temp_dir, input_file_name) |
| 62 | + output_file_path = os.path.join(temp_dir, output_file_name) |
| 63 | + |
| 64 | + with open(input_file_path, "w") as f: |
| 65 | + f.write(json.dumps(input_json_content)) |
| 66 | + |
| 67 | + # Create test script |
| 68 | + script_file_path = os.path.join(temp_dir, agent_file_name) |
| 69 | + with open(script_file_path, "w") as f: |
| 70 | + f.write(simple_agent) |
| 71 | + |
| 72 | + # create uipath.json |
| 73 | + uipath_json_file_path = os.path.join(temp_dir, "uipath.json") |
| 74 | + with open(uipath_json_file_path, "w") as f: |
| 75 | + f.write(uipath_json) |
| 76 | + |
| 77 | + # Create langgraph.json |
| 78 | + langgraph_json_file_path = os.path.join(temp_dir, "langgraph.json") |
| 79 | + with open(langgraph_json_file_path, "w") as f: |
| 80 | + f.write(langgraph_json) |
| 81 | + |
| 82 | + result = langgraph_run_middleware( |
| 83 | + entrypoint="agent", |
| 84 | + input=None, |
| 85 | + resume=False, |
| 86 | + input_file=input_file_path, |
| 87 | + execution_output_file=output_file_path, |
| 88 | + ) |
| 89 | + assert result.should_continue is False |
| 90 | + assert os.path.exists(output_file_path) |
| 91 | + with open(output_file_path, "r") as f: |
| 92 | + output = f.read() |
| 93 | + assert "This is mock report for" in output |
| 94 | + |
| 95 | + os.chdir(current_dir) |
0 commit comments