|
| 1 | +from typing import Any, AsyncGenerator |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from uipath.runtime import ( |
| 6 | + UiPathExecuteOptions, |
| 7 | + UiPathRuntimeEvent, |
| 8 | + UiPathRuntimeProtocol, |
| 9 | + UiPathRuntimeResult, |
| 10 | + UiPathRuntimeSchema, |
| 11 | + UiPathStreamOptions, |
| 12 | +) |
| 13 | +from uipath.runtime.factory import UiPathRuntimeCreatorProtocol |
| 14 | + |
| 15 | + |
| 16 | +class MockRuntime: |
| 17 | + """Mock runtime that implements UiPathRuntimeProtocol.""" |
| 18 | + |
| 19 | + def __init__(self, settings: dict[str, Any] | None = None) -> None: |
| 20 | + self.settings = settings |
| 21 | + |
| 22 | + async def execute( |
| 23 | + self, |
| 24 | + input: dict[str, Any] | None = None, |
| 25 | + options: UiPathExecuteOptions | None = None, |
| 26 | + ) -> UiPathRuntimeResult: |
| 27 | + return UiPathRuntimeResult(output={}) |
| 28 | + |
| 29 | + async def stream( |
| 30 | + self, |
| 31 | + input: dict[str, Any] | None = None, |
| 32 | + options: UiPathStreamOptions | None = None, |
| 33 | + ) -> AsyncGenerator[UiPathRuntimeEvent, None]: |
| 34 | + yield UiPathRuntimeResult(output={}) |
| 35 | + |
| 36 | + async def get_schema(self) -> UiPathRuntimeSchema: |
| 37 | + return UiPathRuntimeSchema( |
| 38 | + filePath="agent.json", |
| 39 | + type="agent", |
| 40 | + uniqueId="unique-id", |
| 41 | + input={}, |
| 42 | + output={}, |
| 43 | + ) |
| 44 | + |
| 45 | + async def dispose(self) -> None: |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +class CreatorWithKwargs: |
| 50 | + """Implementation with kwargs.""" |
| 51 | + |
| 52 | + async def new_runtime( |
| 53 | + self, entrypoint: str, runtime_id: str, **kwargs |
| 54 | + ) -> UiPathRuntimeProtocol: |
| 55 | + return MockRuntime(kwargs.get("settings")) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_protocol_works_with_kwargs_not_specified(): |
| 60 | + """Test protocol works with implementation that has kwargs.""" |
| 61 | + creator: UiPathRuntimeCreatorProtocol = CreatorWithKwargs() |
| 62 | + runtime = await creator.new_runtime("main.py", "runtime-123") |
| 63 | + assert isinstance(runtime, MockRuntime) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.asyncio |
| 67 | +async def test_protocol_works_with_kwargs_specified(): |
| 68 | + """Test protocol works with implementation that has kwargs.""" |
| 69 | + creator: UiPathRuntimeCreatorProtocol = CreatorWithKwargs() |
| 70 | + runtime = await creator.new_runtime( |
| 71 | + "main.py", "runtime-123", settings={"timeout": 30, "model": "gpt-4"} |
| 72 | + ) |
| 73 | + assert isinstance(runtime, MockRuntime) |
| 74 | + assert runtime.settings == {"timeout": 30, "model": "gpt-4"} |
0 commit comments