-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_debugger.py
More file actions
394 lines (308 loc) · 13.1 KB
/
Copy pathtest_debugger.py
File metadata and controls
394 lines (308 loc) · 13.1 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""Tests for UiPathDebugRuntime with mocked runtime and debug bridge."""
from __future__ import annotations
from typing import Any, AsyncGenerator, Sequence, cast
from unittest.mock import AsyncMock, Mock
import pytest
from uipath.core.triggers import UiPathResumeTrigger, UiPathResumeTriggerType
from uipath.runtime import (
UiPathBreakpointResult,
UiPathExecuteOptions,
UiPathRuntimeContext,
UiPathRuntimeResult,
UiPathRuntimeStatus,
UiPathStreamNotSupportedError,
UiPathStreamOptions,
)
from uipath.runtime.debug import (
UiPathDebugProtocol,
UiPathDebugQuitError,
UiPathDebugRuntime,
)
from uipath.runtime.events import UiPathRuntimeEvent, UiPathRuntimeStateEvent
from uipath.runtime.schema import UiPathRuntimeSchema
def make_debug_bridge_mock() -> UiPathDebugProtocol:
"""Create a debug bridge mock with all methods that UiPathDebugRuntime uses.
We use `spec=UiPathDebugBridge` so invalid attributes raise at runtime,
but still operate as a unittest.mock.Mock with AsyncMock methods.
"""
bridge_mock: Mock = Mock(spec=UiPathDebugProtocol)
bridge_mock.connect = AsyncMock()
bridge_mock.disconnect = AsyncMock()
bridge_mock.emit_execution_started = AsyncMock()
bridge_mock.emit_execution_completed = AsyncMock()
bridge_mock.emit_execution_error = AsyncMock()
bridge_mock.emit_breakpoint_hit = AsyncMock()
bridge_mock.emit_state_update = AsyncMock()
bridge_mock.wait_for_resume = AsyncMock()
bridge_mock.get_breakpoints = Mock(return_value=["node-1"])
return cast(UiPathDebugProtocol, bridge_mock)
class StreamingMockRuntime:
"""Mock runtime that streams state events, breakpoint hits and a final result."""
def __init__(
self,
node_sequence: Sequence[str],
*,
stream_unsupported: bool = False,
error_in_stream: bool = False,
) -> None:
super().__init__()
self.node_sequence: list[str] = list(node_sequence)
self.stream_unsupported: bool = stream_unsupported
self.error_in_stream: bool = error_in_stream
self.execute_called: bool = False
async def dispose(self) -> None:
pass
async def execute(
self,
input: dict[str, Any] | None = None,
options: UiPathExecuteOptions | None = None,
) -> UiPathRuntimeResult:
"""Fallback execute path (used when streaming is not supported)."""
self.execute_called = True
return UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"mode": "execute"},
)
async def stream(
self,
input: dict[str, Any] | None = None,
options: UiPathStreamOptions | None = None,
) -> AsyncGenerator[UiPathRuntimeEvent, None]:
"""Async generator yielding state events, breakpoint events, and final result."""
if self.stream_unsupported:
raise UiPathStreamNotSupportedError("Streaming not supported")
if self.error_in_stream:
raise RuntimeError("Stream blew up")
for idx, node in enumerate(self.node_sequence):
# 1) Always emit a state update event
yield UiPathRuntimeStateEvent(
node_name=node,
payload={"index": idx, "node": node},
)
# 2) Check for breakpoints on this node
if options:
breakpoints = options.breakpoints
else:
breakpoints = []
hit_breakpoint = False
if breakpoints == "*":
hit_breakpoint = True
elif isinstance(breakpoints, list) and node in breakpoints:
hit_breakpoint = True
if hit_breakpoint:
next_nodes = self.node_sequence[idx + 1 : idx + 2] # at most one
yield UiPathBreakpointResult(
breakpoint_node=node,
breakpoint_type="before",
next_nodes=next_nodes,
current_state={"node": node, "index": idx},
)
# 3) Final result at the end of streaming
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"visited_nodes": self.node_sequence},
)
async def get_schema(self) -> UiPathRuntimeSchema:
"""NotImplemented."""
raise NotImplementedError()
class SuspendedThenSuccessfulRuntime:
"""Mock runtime that suspends once and completes after resume."""
def __init__(self, trigger: UiPathResumeTrigger) -> None:
self.trigger = trigger
self.inputs: list[dict[str, Any] | None] = []
self.options: list[UiPathStreamOptions | None] = []
async def dispose(self) -> None:
pass
async def execute(
self,
input: dict[str, Any] | None = None,
options: UiPathExecuteOptions | None = None,
) -> UiPathRuntimeResult:
raise NotImplementedError()
async def stream(
self,
input: dict[str, Any] | None = None,
options: UiPathStreamOptions | None = None,
) -> AsyncGenerator[UiPathRuntimeEvent, None]:
self.inputs.append(input)
self.options.append(options)
if options and options.resume:
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"resumed_with": input},
)
return
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
trigger=self.trigger,
)
async def get_schema(self) -> UiPathRuntimeSchema:
raise NotImplementedError()
@pytest.mark.asyncio
async def test_debug_runtime_streams_and_handles_breakpoints_and_state():
"""UiPathDebugRuntime should stream events, handle breakpoints and state updates."""
runtime_impl = StreamingMockRuntime(
node_sequence=["node-1", "node-2", "node-3"],
)
bridge = make_debug_bridge_mock()
# Initial resume (before streaming) + resume after breakpoint hit
cast(AsyncMock, bridge.wait_for_resume).side_effect = [None, None]
cast(Mock, bridge.get_breakpoints).return_value = ["node-2"]
debug_runtime = UiPathDebugRuntime(
delegate=runtime_impl,
debug_bridge=bridge,
)
result = await debug_runtime.execute({})
# Result propagation
assert isinstance(result, UiPathRuntimeResult)
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
assert result.output == {"visited_nodes": ["node-1", "node-2", "node-3"]}
# Bridge lifecycle
cast(AsyncMock, bridge.connect).assert_awaited_once()
cast(AsyncMock, bridge.emit_execution_started).assert_awaited_once()
cast(AsyncMock, bridge.emit_execution_completed).assert_awaited_once_with(result)
# Streaming interactions
assert cast(AsyncMock, bridge.emit_state_update).await_count >= 1
cast(AsyncMock, bridge.emit_breakpoint_hit).assert_awaited()
assert (
cast(AsyncMock, bridge.wait_for_resume).await_count == 2
) # initial + after breakpoint
@pytest.mark.asyncio
async def test_debug_runtime_waits_for_timer_resume_without_polling():
"""Timer triggers should wait for external resume in debug mode."""
trigger = UiPathResumeTrigger(
interrupt_id="timer-interrupt",
trigger_type=UiPathResumeTriggerType.TIMER,
payload={"kind": "timeout"},
)
runtime_impl = SuspendedThenSuccessfulRuntime(trigger)
bridge = make_debug_bridge_mock()
cast(AsyncMock, bridge.wait_for_resume).side_effect = [
None,
{"__uipath": {"kind": "timeout"}},
]
cast(Mock, bridge.get_breakpoints).return_value = []
trigger_manager = Mock()
trigger_manager.read_trigger = AsyncMock(
side_effect=AssertionError("Timer triggers must not be polled")
)
debug_runtime = UiPathDebugRuntime(
delegate=runtime_impl,
debug_bridge=bridge,
)
debug_runtime.get_resumable_runtime = Mock( # type: ignore[method-assign]
return_value=Mock(trigger_manager=trigger_manager)
)
result = await debug_runtime.execute({})
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
assert result.output == {
"resumed_with": {
"timer-interrupt": {"__uipath": {"kind": "timeout"}},
},
}
assert cast(AsyncMock, bridge.wait_for_resume).await_count == 2
cast(AsyncMock, bridge.emit_execution_suspended).assert_awaited_once()
cast(AsyncMock, bridge.emit_execution_resumed).assert_awaited_once_with(
{"timer-interrupt": {"__uipath": {"kind": "timeout"}}}
)
trigger_manager.read_trigger.assert_not_awaited()
@pytest.mark.asyncio
async def test_debug_runtime_falls_back_when_stream_not_supported():
"""If runtime raises UiPathStreamNotSupportedError, we fall back to execute()."""
runtime_impl = StreamingMockRuntime(
node_sequence=["node-1"],
stream_unsupported=True,
)
bridge = make_debug_bridge_mock()
# Initial resume (even if streaming fails, debug runtime will still call it once)
cast(AsyncMock, bridge.wait_for_resume).return_value = None
debug_runtime = UiPathDebugRuntime(
delegate=runtime_impl,
debug_bridge=bridge,
)
result = await debug_runtime.execute({})
# Fallback to execute() path
assert runtime_impl.execute_called is True
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
assert result.output == {"mode": "execute"}
# Bridge interactions
cast(AsyncMock, bridge.connect).assert_awaited_once()
cast(AsyncMock, bridge.emit_execution_started).assert_awaited_once()
cast(AsyncMock, bridge.emit_execution_completed).assert_awaited_once_with(result)
# No streaming-specific events
cast(AsyncMock, bridge.emit_state_update).assert_not_awaited()
cast(AsyncMock, bridge.emit_breakpoint_hit).assert_not_awaited()
@pytest.mark.asyncio
async def test_debug_runtime_quit_creates_successful_result():
"""UiPathDebugRuntime should handle UiPathDebugQuitError and return SUCCESSFUL."""
runtime_impl = StreamingMockRuntime(
node_sequence=["node-quit"],
)
bridge = make_debug_bridge_mock()
# First resume: initial start; second resume: at breakpoint -> raises quit
cast(AsyncMock, bridge.wait_for_resume).side_effect = [
None,
UiPathDebugQuitError("quit"),
]
cast(Mock, bridge.get_breakpoints).return_value = ["node-quit"]
debug_runtime = UiPathDebugRuntime(
delegate=runtime_impl,
debug_bridge=bridge,
)
result = await debug_runtime.execute({})
# Quit result is synthesized as SUCCESSFUL (no specific output required)
assert isinstance(result, UiPathRuntimeResult)
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
# emit_breakpoint_hit should have been called once
cast(AsyncMock, bridge.emit_breakpoint_hit).assert_awaited()
assert cast(AsyncMock, bridge.wait_for_resume).await_count == 2
# Completion event emitted with synthesized result
cast(AsyncMock, bridge.emit_execution_completed).assert_awaited_once_with(result)
@pytest.mark.asyncio
async def test_debug_runtime_execute_reports_errors_and_marks_faulted():
"""On unexpected errors, UiPathDebugRuntime should emit error and mark result FAULTED."""
# This runtime will raise an error as soon as stream() is used
runtime_impl = StreamingMockRuntime(
node_sequence=["node-1"],
error_in_stream=True,
)
bridge = make_debug_bridge_mock()
cast(AsyncMock, bridge.wait_for_resume).return_value = None
debug_runtime = UiPathDebugRuntime(
delegate=runtime_impl,
debug_bridge=bridge,
)
with pytest.raises(RuntimeError, match="Stream blew up"):
with UiPathRuntimeContext.with_defaults() as ctx:
ctx.result = await debug_runtime.execute(input=ctx.input)
# Context should be marked FAULTED
assert ctx.result is not None
assert ctx.result.status == UiPathRuntimeStatus.FAULTED
# Error should be emitted to debug bridge
cast(AsyncMock, bridge.emit_execution_error).assert_awaited_once()
# Completion should not be emitted in error path
cast(AsyncMock, bridge.emit_execution_completed).assert_not_awaited()
@pytest.mark.asyncio
async def test_debug_runtime_dispose_calls_disconnect():
"""dispose() should call debug bridge disconnect."""
runtime_impl = StreamingMockRuntime(node_sequence=["node-1"])
bridge = make_debug_bridge_mock()
debug_runtime = UiPathDebugRuntime(
delegate=runtime_impl,
debug_bridge=bridge,
)
await debug_runtime.dispose()
cast(AsyncMock, bridge.disconnect).assert_awaited_once()
@pytest.mark.asyncio
async def test_debug_runtime_dispose_suppresses_disconnect_errors():
"""Errors from debug_bridge.disconnect should be suppressed."""
runtime_impl = StreamingMockRuntime(node_sequence=["node-1"])
bridge = make_debug_bridge_mock()
cast(AsyncMock, bridge.disconnect).side_effect = RuntimeError("disconnect failed")
debug_runtime = UiPathDebugRuntime(
delegate=runtime_impl,
debug_bridge=bridge,
)
# No exception should bubble up from dispose()
await debug_runtime.dispose()
cast(AsyncMock, bridge.disconnect).assert_awaited_once()