|
1 | 1 | """Shared pytest fixtures for all tests.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from typing import Any, AsyncGenerator |
| 5 | + |
| 6 | +import pytest |
| 7 | +from uipath.core.tracing import UiPathTraceManager |
| 8 | +from uipath.runtime import ( |
| 9 | + UiPathExecuteOptions, |
| 10 | + UiPathRuntimeEvent, |
| 11 | + UiPathRuntimeFactorySettings, |
| 12 | + UiPathRuntimeResult, |
| 13 | + UiPathRuntimeStatus, |
| 14 | + UiPathRuntimeStorageProtocol, |
| 15 | + UiPathStreamOptions, |
| 16 | +) |
| 17 | +from uipath.runtime.schema import UiPathRuntimeSchema |
| 18 | + |
| 19 | +ENTRYPOINT_GREETING = "agent/greeting.py:main" |
| 20 | +ENTRYPOINT_NUMBERS = "agent/numbers.py:analyze" |
| 21 | + |
| 22 | + |
| 23 | +class _MockGreetingRuntime: |
| 24 | + """Lightweight greeting runtime for tests (no OTel tracing).""" |
| 25 | + |
| 26 | + def __init__(self, entrypoint: str = ENTRYPOINT_GREETING) -> None: |
| 27 | + self.entrypoint = entrypoint |
| 28 | + |
| 29 | + async def get_schema(self) -> UiPathRuntimeSchema: |
| 30 | + return UiPathRuntimeSchema( |
| 31 | + filePath=self.entrypoint, |
| 32 | + uniqueId="test-greeting", |
| 33 | + type="agent", |
| 34 | + input={ |
| 35 | + "type": "object", |
| 36 | + "properties": {"name": {"type": "string"}}, |
| 37 | + "required": ["name"], |
| 38 | + }, |
| 39 | + output={ |
| 40 | + "type": "object", |
| 41 | + "properties": {"greeting": {"type": "string"}}, |
| 42 | + }, |
| 43 | + ) |
| 44 | + |
| 45 | + async def execute( |
| 46 | + self, |
| 47 | + input: dict[str, Any] | None = None, |
| 48 | + options: UiPathExecuteOptions | None = None, |
| 49 | + ) -> UiPathRuntimeResult: |
| 50 | + payload = input or {} |
| 51 | + name = str(payload.get("name", "world")) |
| 52 | + await asyncio.sleep(0.05) |
| 53 | + return UiPathRuntimeResult( |
| 54 | + output={"greeting": f"Hello, {name}!"}, |
| 55 | + status=UiPathRuntimeStatus.SUCCESSFUL, |
| 56 | + ) |
| 57 | + |
| 58 | + async def stream( |
| 59 | + self, |
| 60 | + input: dict[str, Any] | None = None, |
| 61 | + options: UiPathStreamOptions | None = None, |
| 62 | + ) -> AsyncGenerator[UiPathRuntimeEvent, None]: |
| 63 | + yield await self.execute(input=input, options=options) |
| 64 | + |
| 65 | + async def dispose(self) -> None: |
| 66 | + pass |
| 67 | + |
| 68 | + |
| 69 | +class _MockNumbersRuntime: |
| 70 | + """Lightweight numbers runtime for tests (no OTel tracing).""" |
| 71 | + |
| 72 | + def __init__(self, entrypoint: str = ENTRYPOINT_NUMBERS) -> None: |
| 73 | + self.entrypoint = entrypoint |
| 74 | + |
| 75 | + async def get_schema(self) -> UiPathRuntimeSchema: |
| 76 | + return UiPathRuntimeSchema( |
| 77 | + filePath=self.entrypoint, |
| 78 | + uniqueId="test-numbers", |
| 79 | + type="script", |
| 80 | + input={ |
| 81 | + "type": "object", |
| 82 | + "properties": { |
| 83 | + "numbers": { |
| 84 | + "type": "array", |
| 85 | + "items": {"type": "number"}, |
| 86 | + }, |
| 87 | + "operation": { |
| 88 | + "type": "string", |
| 89 | + "enum": ["sum", "avg", "max"], |
| 90 | + "default": "sum", |
| 91 | + }, |
| 92 | + }, |
| 93 | + "required": ["numbers"], |
| 94 | + }, |
| 95 | + output={ |
| 96 | + "type": "object", |
| 97 | + "properties": { |
| 98 | + "operation": {"type": "string"}, |
| 99 | + "result": {"type": "number"}, |
| 100 | + "count": {"type": "integer"}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + ) |
| 104 | + |
| 105 | + async def execute( |
| 106 | + self, |
| 107 | + input: dict[str, Any] | None = None, |
| 108 | + options: UiPathExecuteOptions | None = None, |
| 109 | + ) -> UiPathRuntimeResult: |
| 110 | + payload = input or {} |
| 111 | + numbers = [float(x) for x in (payload.get("numbers") or [])] |
| 112 | + operation = str(payload.get("operation", "sum")).lower() |
| 113 | + await asyncio.sleep(0.05) |
| 114 | + |
| 115 | + if operation == "avg" and numbers: |
| 116 | + result = sum(numbers) / len(numbers) |
| 117 | + elif operation == "max" and numbers: |
| 118 | + result = max(numbers) |
| 119 | + else: |
| 120 | + operation = "sum" |
| 121 | + result = sum(numbers) |
| 122 | + |
| 123 | + return UiPathRuntimeResult( |
| 124 | + output={"operation": operation, "result": result, "count": len(numbers)}, |
| 125 | + status=UiPathRuntimeStatus.SUCCESSFUL, |
| 126 | + ) |
| 127 | + |
| 128 | + async def stream( |
| 129 | + self, |
| 130 | + input: dict[str, Any] | None = None, |
| 131 | + options: UiPathStreamOptions | None = None, |
| 132 | + ) -> AsyncGenerator[UiPathRuntimeEvent, None]: |
| 133 | + yield await self.execute(input=input, options=options) |
| 134 | + |
| 135 | + async def dispose(self) -> None: |
| 136 | + pass |
| 137 | + |
| 138 | + |
| 139 | +class MockRuntimeFactory: |
| 140 | + """Test runtime factory compatible with UiPathRuntimeFactoryProtocol.""" |
| 141 | + |
| 142 | + async def new_runtime(self, entrypoint: str, runtime_id: str, **kwargs): |
| 143 | + if entrypoint == ENTRYPOINT_NUMBERS: |
| 144 | + return _MockNumbersRuntime(entrypoint=entrypoint) |
| 145 | + return _MockGreetingRuntime(entrypoint=entrypoint) |
| 146 | + |
| 147 | + async def get_settings(self) -> UiPathRuntimeFactorySettings | None: |
| 148 | + return UiPathRuntimeFactorySettings() |
| 149 | + |
| 150 | + async def get_storage(self) -> UiPathRuntimeStorageProtocol | None: |
| 151 | + return None |
| 152 | + |
| 153 | + def discover_entrypoints(self) -> list[str]: |
| 154 | + return [ENTRYPOINT_GREETING, ENTRYPOINT_NUMBERS] |
| 155 | + |
| 156 | + async def dispose(self) -> None: |
| 157 | + pass |
| 158 | + |
| 159 | + |
| 160 | +@pytest.fixture() |
| 161 | +def mock_factory(): |
| 162 | + return MockRuntimeFactory() |
| 163 | + |
| 164 | + |
| 165 | +@pytest.fixture() |
| 166 | +def trace_manager(): |
| 167 | + return UiPathTraceManager() |
0 commit comments