-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_chat_runtime.py
More file actions
370 lines (287 loc) · 11.8 KB
/
test_chat_runtime.py
File metadata and controls
370 lines (287 loc) · 11.8 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
"""Tests for UiPathChatRuntime with mocked runtime and chat bridge."""
from __future__ import annotations
from typing import Any, AsyncGenerator, Sequence, cast
from unittest.mock import AsyncMock, Mock
import pytest
from uipath.core.chat import (
UiPathConversationMessageEvent,
UiPathConversationMessageStartEvent,
)
from uipath.runtime import (
UiPathExecuteOptions,
UiPathResumeTrigger,
UiPathResumeTriggerType,
UiPathRuntimeResult,
UiPathRuntimeStatus,
UiPathStreamOptions,
)
from uipath.runtime.chat import (
UiPathChatProtocol,
UiPathChatRuntime,
)
from uipath.runtime.events import UiPathRuntimeEvent, UiPathRuntimeMessageEvent
from uipath.runtime.schema import UiPathRuntimeSchema
def make_chat_bridge_mock() -> UiPathChatProtocol:
"""Create a chat bridge mock with all methods that UiPathChatRuntime uses."""
bridge_mock: Mock = Mock(spec=UiPathChatProtocol)
bridge_mock.connect = AsyncMock()
bridge_mock.disconnect = AsyncMock()
bridge_mock.emit_message_event = AsyncMock()
bridge_mock.emit_interrupt_event = AsyncMock()
bridge_mock.wait_for_resume = AsyncMock()
return cast(UiPathChatProtocol, bridge_mock)
class StreamingMockRuntime:
"""Mock runtime that streams message events and a final result."""
def __init__(
self,
messages: Sequence[str],
*,
error_in_stream: bool = False,
) -> None:
super().__init__()
self.messages: list[str] = list(messages)
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."""
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 message events and final result."""
if self.error_in_stream:
raise RuntimeError("Stream blew up")
for idx, _message_text in enumerate(self.messages):
message_event = UiPathConversationMessageEvent(
message_id=f"msg-{idx}",
start=UiPathConversationMessageStartEvent(
role="assistant",
timestamp="2025-01-01T00:00:00.000Z",
),
)
yield UiPathRuntimeMessageEvent(payload=message_event)
# Final result at the end of streaming
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"messages": self.messages},
)
async def get_schema(self) -> UiPathRuntimeSchema:
raise NotImplementedError()
class SuspendingMockRuntime:
"""Mock runtime that can suspend with API triggers."""
def __init__(
self,
suspend_at_message: int | None = None,
) -> None:
self.suspend_at_message = suspend_at_message
async def dispose(self) -> None:
pass
async def execute(
self,
input: dict[str, Any] | None = None,
options: UiPathExecuteOptions | None = None,
) -> UiPathRuntimeResult:
"""Fallback execute path."""
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]:
"""Stream events with potential API trigger suspension."""
is_resume = options and options.resume
if not is_resume:
# Initial execution - yield message and then suspend
message_event = UiPathConversationMessageEvent(
message_id="msg-0",
start=UiPathConversationMessageStartEvent(
role="assistant",
timestamp="2025-01-01T00:00:00.000Z",
),
)
yield UiPathRuntimeMessageEvent(payload=message_event)
if self.suspend_at_message is not None:
# Suspend with API trigger
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
trigger=UiPathResumeTrigger(
trigger_type=UiPathResumeTriggerType.API,
payload={"action": "confirm_tool_call"},
),
)
return
else:
# Resumed execution - yield another message and complete
message_event = UiPathConversationMessageEvent(
message_id="msg-1",
start=UiPathConversationMessageStartEvent(
role="assistant",
timestamp="2025-01-01T00:00:01.000Z",
),
)
yield UiPathRuntimeMessageEvent(payload=message_event)
# Final successful result
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"resumed": is_resume, "input": input},
)
async def get_schema(self) -> UiPathRuntimeSchema:
raise NotImplementedError()
@pytest.mark.asyncio
async def test_chat_runtime_streams_and_emits_messages():
"""UiPathChatRuntime should stream events and emit message events to bridge."""
runtime_impl = StreamingMockRuntime(
messages=["Hello", "How are you?", "Goodbye"],
)
bridge = make_chat_bridge_mock()
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
result = await chat_runtime.execute({})
await chat_runtime.dispose()
# Result propagation
assert isinstance(result, UiPathRuntimeResult)
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
assert result.output == {"messages": ["Hello", "How are you?", "Goodbye"]}
# Bridge lifecycle
cast(AsyncMock, bridge.connect).assert_awaited_once()
cast(AsyncMock, bridge.disconnect).assert_awaited_once()
assert cast(AsyncMock, bridge.emit_message_event).await_count == 3
# Verify message events were passed as UiPathConversationMessageEvent objects
calls = cast(AsyncMock, bridge.emit_message_event).await_args_list
assert isinstance(calls[0][0][0], UiPathConversationMessageEvent)
assert calls[0][0][0].message_id == "msg-0"
assert isinstance(calls[1][0][0], UiPathConversationMessageEvent)
assert calls[1][0][0].message_id == "msg-1"
assert isinstance(calls[2][0][0], UiPathConversationMessageEvent)
assert calls[2][0][0].message_id == "msg-2"
@pytest.mark.asyncio
async def test_chat_runtime_stream_yields_all_events():
"""UiPathChatRuntime.stream() should yield all events from delegate."""
runtime_impl = StreamingMockRuntime(
messages=["Message 1", "Message 2"],
)
bridge = make_chat_bridge_mock()
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
events = []
async for event in chat_runtime.stream({}):
events.append(event)
await chat_runtime.dispose()
# Should have 2 message events + 1 final result
assert len(events) == 3
assert isinstance(events[0], UiPathRuntimeMessageEvent)
assert isinstance(events[1], UiPathRuntimeMessageEvent)
assert isinstance(events[2], UiPathRuntimeResult)
# Bridge methods called
cast(AsyncMock, bridge.connect).assert_awaited_once()
cast(AsyncMock, bridge.disconnect).assert_awaited_once()
assert cast(AsyncMock, bridge.emit_message_event).await_count == 2
@pytest.mark.asyncio
async def test_chat_runtime_handles_errors():
"""On unexpected errors, UiPathChatRuntime should propagate them."""
runtime_impl = StreamingMockRuntime(
messages=["Message"],
error_in_stream=True,
)
bridge = make_chat_bridge_mock()
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
# Error should propagate
with pytest.raises(RuntimeError, match="Stream blew up"):
await chat_runtime.execute({})
cast(AsyncMock, bridge.connect).assert_awaited_once()
@pytest.mark.asyncio
async def test_chat_runtime_dispose_calls_disconnect():
"""dispose() should call chat bridge disconnect."""
runtime_impl = StreamingMockRuntime(messages=["Message"])
bridge = make_chat_bridge_mock()
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
await chat_runtime.dispose()
# Bridge disconnect should be called
cast(AsyncMock, bridge.disconnect).assert_awaited_once()
@pytest.mark.asyncio
async def test_chat_runtime_dispose_suppresses_disconnect_errors():
"""Errors from chat_bridge.disconnect should be suppressed."""
runtime_impl = StreamingMockRuntime(messages=["Message"])
bridge = make_chat_bridge_mock()
cast(AsyncMock, bridge.disconnect).side_effect = RuntimeError("disconnect failed")
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
await chat_runtime.dispose()
cast(AsyncMock, bridge.disconnect).assert_awaited_once()
@pytest.mark.asyncio
async def test_chat_runtime_handles_api_trigger_suspension():
"""UiPathChatRuntime should intercept suspensions and resume execution."""
runtime_impl = SuspendingMockRuntime(suspend_at_message=0)
bridge = make_chat_bridge_mock()
cast(AsyncMock, bridge.wait_for_resume).return_value = {"approved": True}
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
result = await chat_runtime.execute({})
await chat_runtime.dispose()
# Result should be SUCCESSFUL
assert isinstance(result, UiPathRuntimeResult)
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
assert result.output == {"resumed": True, "input": {"approved": True}}
cast(AsyncMock, bridge.connect).assert_awaited_once()
cast(AsyncMock, bridge.disconnect).assert_awaited_once()
cast(AsyncMock, bridge.emit_interrupt_event).assert_awaited_once()
cast(AsyncMock, bridge.wait_for_resume).assert_awaited_once()
# Message events emitted (one before suspend, one after resume)
assert cast(AsyncMock, bridge.emit_message_event).await_count == 2
@pytest.mark.asyncio
async def test_chat_runtime_yields_events_during_suspension_flow():
"""UiPathChatRuntime.stream() should not yield SUSPENDED result, only final result."""
runtime_impl = SuspendingMockRuntime(suspend_at_message=0)
bridge = make_chat_bridge_mock()
# wait_for_resume returns approval data
cast(AsyncMock, bridge.wait_for_resume).return_value = {"approved": True}
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
events = []
async for event in chat_runtime.stream({}):
events.append(event)
await chat_runtime.dispose()
# Should have 2 message events + 1 final SUCCESSFUL result
# SUSPENDED result should NOT be yielded
assert len(events) == 3
assert isinstance(events[0], UiPathRuntimeMessageEvent)
assert events[0].payload.message_id == "msg-0"
assert isinstance(events[1], UiPathRuntimeMessageEvent)
assert events[1].payload.message_id == "msg-1"
assert isinstance(events[2], UiPathRuntimeResult)
assert events[2].status == UiPathRuntimeStatus.SUCCESSFUL
# Verify no SUSPENDED result was yielded
for event in events:
if isinstance(event, UiPathRuntimeResult):
assert event.status != UiPathRuntimeStatus.SUSPENDED