-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_context.py
More file actions
318 lines (240 loc) · 10.3 KB
/
Copy pathtest_context.py
File metadata and controls
318 lines (240 loc) · 10.3 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
import json
from pathlib import Path
from typing import Any
import pytest
from uipath.core.errors import ErrorCategory, UiPathFaultedTriggerError
from uipath.runtime.context import UiPathRuntimeContext
from uipath.runtime.errors import (
UiPathErrorCode,
UiPathRuntimeError,
)
from uipath.runtime.result import UiPathRuntimeResult, UiPathRuntimeStatus
class DummyLogsInterceptor:
"""Minimal interceptor used to avoid touching real logging in tests."""
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.setup_called = False
self.teardown_called = False
def setup(self) -> None:
self.setup_called = True
def teardown(self) -> None:
self.teardown_called = True
@pytest.fixture(autouse=True)
def patch_logs_interceptor(monkeypatch: pytest.MonkeyPatch) -> None:
"""Patch UiPathRuntimeLogsInterceptor with a dummy so tests don't depend on logging."""
monkeypatch.setattr(
"uipath.runtime.context.UiPathRuntimeLogsInterceptor",
DummyLogsInterceptor,
)
def test_context_loads_json_input_file(tmp_path: Path) -> None:
input_data = {"foo": "bar", "answer": 42}
input_path = tmp_path / "input.json"
input_path.write_text(json.dumps(input_data))
ctx = UiPathRuntimeContext(input_file=str(input_path))
with ctx:
# input should be loaded from the JSON file
assert ctx.get_input() == input_data
# logs interceptor should have been set up
assert isinstance(ctx.logs_interceptor, DummyLogsInterceptor)
assert ctx.logs_interceptor.setup_called
# After leaving the context, interceptor should be torn down
assert ctx.logs_interceptor.teardown_called
def test_context_raises_for_invalid_json(tmp_path: Path) -> None:
bad_input_path = tmp_path / "input.json"
bad_input_path.write_text("{not: valid json") # invalid JSON
ctx = UiPathRuntimeContext(input_file=str(bad_input_path))
with pytest.raises(UiPathRuntimeError) as excinfo:
with ctx:
# Explicitly call get_input() which will raise
ctx.get_input()
err = excinfo.value.error_info
assert err.code == f"Python.{UiPathErrorCode.INPUT_INVALID_JSON.value}"
def test_output_file_written_on_successful_execution(tmp_path: Path) -> None:
output_path = tmp_path / "output.json"
ctx = UiPathRuntimeContext(
output_file=str(output_path),
)
with ctx:
# Simulate a successful runtime that produced some output
ctx.result = UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"foo": "bar"},
)
pass
assert output_path.exists()
written = json.loads(output_path.read_text())
assert written == {"foo": "bar"}
def test_result_file_written_on_success_contains_output(tmp_path: Path) -> None:
runtime_dir = tmp_path / "runtime"
ctx = UiPathRuntimeContext(
job_id="job-123", # triggers writing result file
runtime_dir=str(runtime_dir),
result_file="result.json",
)
with ctx:
ctx.result = UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"foo": "bar"},
)
pass
# Assert: result file is written whether successful or faulted
result_path = Path(ctx.resolved_result_file_path)
assert result_path.exists()
content = json.loads(result_path.read_text())
# Should contain output and no error
assert content["output"] == {"foo": "bar"}
assert "error" not in content or content["error"] is None
def test_result_file_written_on_fault_contains_error_contract(tmp_path: Path) -> None:
runtime_dir = tmp_path / "runtime"
ctx = UiPathRuntimeContext(
job_id="job-456", # triggers writing result file
runtime_dir=str(runtime_dir),
result_file="result.json",
)
# No pre-set result -> context will create a default UiPathRuntimeResult()
# Act: simulate a failing runtime
with pytest.raises(RuntimeError, match="Stream blew up"):
with ctx:
raise RuntimeError("Stream blew up")
# Assert: result file is written even when faulted
result_path = Path(ctx.resolved_result_file_path)
assert result_path.exists()
content = json.loads(result_path.read_text())
# We always have an output key, even if it's an empty dict
assert "output" in content
# Status should be FAULTED
assert "status" in content
assert content["status"] == UiPathRuntimeStatus.FAULTED.value
# Error contract should be present and structured
assert "error" in content
error = content["error"]
assert error["code"] == "ERROR_RuntimeError"
assert error["title"] == "Runtime error: RuntimeError"
assert "Stream blew up" in error["detail"]
def test_parse_input_string_returns_none_for_empty_string() -> None:
"""Test that empty input string returns None, not empty dict."""
ctx = UiPathRuntimeContext(input="")
result = ctx.get_input()
assert result is None
def test_parse_input_string_returns_none_for_whitespace_only() -> None:
"""Test that whitespace-only input string returns None, not empty dict."""
ctx = UiPathRuntimeContext(input=" ")
result = ctx.get_input()
assert result is None
def test_parse_input_string_returns_none_for_none() -> None:
"""Test that None input returns None."""
ctx = UiPathRuntimeContext(input=None)
result = ctx.get_input()
assert result is None
def test_from_config_extracts_fps_properties_without_runtime(tmp_path: Path) -> None:
"""fpsProperties should be loaded even if 'runtime' block is missing."""
cfg = {
"fpsProperties": {
"conversationalService.conversationId": "conv-123",
"conversationalService.exchangeId": "ex-456",
"conversationalService.messageId": "msg-789",
"mcpServer.id": "server-id-123",
"mcpServer.slug": "my-mcp-server",
}
}
config_path = tmp_path / "uipath.json"
config_path.write_text(json.dumps(cfg))
ctx = UiPathRuntimeContext.from_config(config_path=str(config_path))
assert ctx.conversation_id == "conv-123"
assert ctx.exchange_id == "ex-456"
assert ctx.message_id == "msg-789"
assert ctx.mcp_server_id == "server-id-123"
assert ctx.mcp_server_slug == "my-mcp-server"
def test_from_config_loads_runtime_and_fps_properties(tmp_path: Path) -> None:
"""runtime.* keys and fpsProperties.* keys should both be applied."""
cfg = {
"runtime": {
"dir": "my_runtime",
"outputFile": "my_output.json",
"stateFile": "my_state.db",
"logsFile": "my_logs.log",
"internalArguments": {"parentOperationId": "operationId-123"},
},
"fpsProperties": {
"conversationalService.conversationId": "conv-abc",
"conversationalService.exchangeId": "ex-def",
"conversationalService.messageId": "msg-ghi",
"mcpServer.id": "mcp-server-456",
"mcpServer.slug": "test-server-slug",
},
}
config_path = tmp_path / "uipath.json"
config_path.write_text(json.dumps(cfg))
ctx = UiPathRuntimeContext.from_config(config_path=str(config_path))
# runtime mapping
assert ctx.runtime_dir == "my_runtime"
assert (
ctx.result_file == "my_output.json"
) # outputFile maps to result_file (serverless contract)
assert ctx.state_file == "my_state.db"
assert ctx.logs_file == "my_logs.log"
# parentOperationId is mapped correctly from internal_arguments
assert ctx.parent_operation_id == "operationId-123"
# fpsProperties mapping
assert ctx.conversation_id == "conv-abc"
assert ctx.exchange_id == "ex-def"
assert ctx.message_id == "msg-ghi"
assert ctx.mcp_server_id == "mcp-server-456"
assert ctx.mcp_server_slug == "test-server-slug"
def test_from_config_maps_end_exchange_fps_property(tmp_path: Path) -> None:
"""conversationalService.endExchange should map onto end_exchange."""
cfg = {
"fpsProperties": {
"conversationalService.conversationId": "conv-123",
"conversationalService.exchangeId": "ex-456",
"conversationalService.endExchange": False,
}
}
config_path = tmp_path / "uipath.json"
config_path.write_text(json.dumps(cfg))
ctx = UiPathRuntimeContext.from_config(config_path=str(config_path))
assert ctx.end_exchange is False
def test_end_exchange_defaults_true_when_fps_property_absent(tmp_path: Path) -> None:
"""end_exchange defaults to True (legacy behavior) when the fps key is missing."""
cfg = {
"fpsProperties": {
"conversationalService.conversationId": "conv-123",
}
}
config_path = tmp_path / "uipath.json"
config_path.write_text(json.dumps(cfg))
ctx = UiPathRuntimeContext.from_config(config_path=str(config_path))
assert ctx.end_exchange is True
def test_result_file_written_on_faulted_trigger_error(tmp_path: Path) -> None:
runtime_dir = tmp_path / "runtime"
ctx = UiPathRuntimeContext(
job_id="job-trigger-test",
runtime_dir=str(runtime_dir),
result_file="result.json",
)
trigger_error = UiPathFaultedTriggerError(
ErrorCategory.SYSTEM, "Failed to create HITL action", "validation error"
)
trigger_error.category = ErrorCategory.SYSTEM
trigger_error.message = "Failed to create HITL action"
with pytest.raises(UiPathFaultedTriggerError):
with ctx:
raise trigger_error
result_path = Path(ctx.resolved_result_file_path)
assert result_path.exists()
content = json.loads(result_path.read_text())
assert content["status"] == UiPathRuntimeStatus.FAULTED.value
assert "error" in content
error = content["error"]
assert error["code"] == f"Python.{UiPathErrorCode.RESUME_TRIGGER_ERROR.value}"
assert error["title"] == "Resume trigger error"
assert "Failed to create HITL action" in error["detail"]
assert error["category"] == ErrorCategory.SYSTEM.value
def test_string_output_wrapped_in_dict() -> None:
"""Test that string output is wrapped in a dict with key 'output'."""
result = UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output="primitive str",
)
result_dict = result.to_dict()
assert result_dict["output"] == {"output": "primitive str"}
assert result_dict["status"] == UiPathRuntimeStatus.SUCCESSFUL