-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_chat.py
More file actions
597 lines (475 loc) · 19.6 KB
/
test_chat.py
File metadata and controls
597 lines (475 loc) · 19.6 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
"""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.core.triggers import UiPathResumeTrigger, UiPathResumeTriggerType
from uipath.runtime import (
UiPathExecuteOptions,
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.wait_for_tool_confirmation = 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
trigger = UiPathResumeTrigger(
interrupt_id="interrupt-1",
trigger_type=UiPathResumeTriggerType.API,
payload={"action": "confirm_tool_call"},
)
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
trigger=trigger,
triggers=[trigger],
)
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_tool_confirmation).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": {"interrupt-1": {"approved": True}},
}
cast(AsyncMock, bridge.connect).assert_awaited_once()
cast(AsyncMock, bridge.disconnect).assert_awaited_once()
cast(AsyncMock, bridge.wait_for_tool_confirmation).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_tool_confirmation returns approval data
cast(AsyncMock, bridge.wait_for_tool_confirmation).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
class MultiTriggerMockRuntime:
"""Mock runtime that suspends with multiple API triggers."""
def __init__(self) -> None:
self.execution_count = 0
async def dispose(self) -> None:
pass
async def execute(
self,
input: dict[str, Any] | None = None,
options: UiPathExecuteOptions | None = None,
) -> UiPathRuntimeResult:
"""Execute with multiple trigger suspension."""
result: UiPathRuntimeResult | None = None
async for event in self.stream(input, cast(UiPathStreamOptions, options)):
if isinstance(event, UiPathRuntimeResult):
result = event
return (
result
if result
else UiPathRuntimeResult(status=UiPathRuntimeStatus.SUCCESSFUL)
)
async def stream(
self,
input: dict[str, Any] | None = None,
options: UiPathStreamOptions | None = None,
) -> AsyncGenerator[UiPathRuntimeEvent, None]:
"""Stream with multiple trigger suspension."""
self.execution_count += 1
is_resume = options and options.resume
if not is_resume:
# Initial execution - suspend with 3 API triggers
trigger_a = UiPathResumeTrigger(
interrupt_id="email-confirm",
trigger_type=UiPathResumeTriggerType.API,
payload={"action": "send_email", "to": "user@example.com"},
)
trigger_b = UiPathResumeTrigger(
interrupt_id="file-delete",
trigger_type=UiPathResumeTriggerType.API,
payload={"action": "delete_file", "path": "/logs/old.txt"},
)
trigger_c = UiPathResumeTrigger(
interrupt_id="api-call",
trigger_type=UiPathResumeTriggerType.API,
payload={"action": "call_api", "endpoint": "/users"},
)
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
trigger=trigger_a,
triggers=[trigger_a, trigger_b, trigger_c],
)
else:
# Resumed - verify all triggers resolved
assert input is not None
assert "email-confirm" in input
assert "file-delete" in input
assert "api-call" in input
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"resumed": True, "input": input},
)
async def get_schema(self) -> UiPathRuntimeSchema:
raise NotImplementedError()
class MixedTriggerMockRuntime:
"""Mock runtime that suspends with mixed trigger types (API + non-API)."""
async def dispose(self) -> None:
pass
async def execute(
self,
input: dict[str, Any] | None = None,
options: UiPathExecuteOptions | None = None,
) -> UiPathRuntimeResult:
"""Execute with mixed triggers."""
result: UiPathRuntimeResult | None = None
async for event in self.stream(input, cast(UiPathStreamOptions, options)):
if isinstance(event, UiPathRuntimeResult):
result = event
return (
result
if result
else UiPathRuntimeResult(status=UiPathRuntimeStatus.SUCCESSFUL)
)
async def stream(
self,
input: dict[str, Any] | None = None,
options: UiPathStreamOptions | None = None,
) -> AsyncGenerator[UiPathRuntimeEvent, None]:
"""Stream with mixed trigger types."""
is_resume = options and options.resume
if not is_resume:
# Initial execution - 2 API + 1 QUEUE trigger
trigger_a = UiPathResumeTrigger(
interrupt_id="email-confirm",
trigger_type=UiPathResumeTriggerType.API,
payload={"action": "send_email"},
)
trigger_b = UiPathResumeTrigger(
interrupt_id="file-delete",
trigger_type=UiPathResumeTriggerType.API,
payload={"action": "delete_file"},
)
trigger_c = UiPathResumeTrigger(
interrupt_id="queue-item",
trigger_type=UiPathResumeTriggerType.QUEUE_ITEM,
payload={"queue": "inbox", "item": "123"},
)
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
trigger=trigger_a,
triggers=[trigger_a, trigger_b, trigger_c],
)
else:
# Resumed - verify only API triggers resolved
assert input is not None
assert "email-confirm" in input
assert "file-delete" in input
# QUEUE trigger should NOT be in input (handled externally)
# Suspend again with only QUEUE trigger
trigger_c = UiPathResumeTrigger(
interrupt_id="queue-item",
trigger_type=UiPathResumeTriggerType.QUEUE_ITEM,
payload={"queue": "inbox", "item": "123"},
)
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
trigger=trigger_c,
triggers=[trigger_c],
)
async def get_schema(self) -> UiPathRuntimeSchema:
raise NotImplementedError()
@pytest.mark.asyncio
async def test_chat_runtime_handles_multiple_api_triggers():
"""ChatRuntime should resolve all API triggers before resuming."""
runtime_impl = MultiTriggerMockRuntime()
bridge = make_chat_bridge_mock()
# Bridge returns approval for each trigger
cast(AsyncMock, bridge.wait_for_tool_confirmation).side_effect = [
{"approved": True}, # email-confirm
{"approved": True}, # file-delete
{"approved": True}, # api-call
]
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
result = await chat_runtime.execute({})
await chat_runtime.dispose()
# Result should be SUCCESSFUL
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
assert isinstance(result.output, dict)
assert result.output["resumed"] is True
# Verify all 3 triggers were wrapped with interrupt_ids
resume_input = cast(dict[str, Any], result.output["input"])
assert "email-confirm" in resume_input
assert "file-delete" in resume_input
assert "api-call" in resume_input
assert resume_input["email-confirm"] == {"approved": True}
assert resume_input["file-delete"] == {"approved": True}
assert resume_input["api-call"] == {"approved": True}
# Bridge should have been called 3 times (once per trigger)
assert cast(AsyncMock, bridge.wait_for_tool_confirmation).await_count == 3
@pytest.mark.asyncio
async def test_chat_runtime_filters_non_api_triggers():
"""ChatRuntime should only handle API triggers, pass through non-API triggers."""
runtime_impl = MixedTriggerMockRuntime()
bridge = make_chat_bridge_mock()
# Bridge returns approval for API triggers only
cast(AsyncMock, bridge.wait_for_tool_confirmation).side_effect = [
{"approved": True}, # email-confirm
{"approved": True}, # file-delete
]
chat_runtime = UiPathChatRuntime(
delegate=runtime_impl,
chat_bridge=bridge,
)
result = await chat_runtime.execute({})
await chat_runtime.dispose()
# Result should be SUSPENDED with QUEUE trigger (non-API)
assert result.status == UiPathRuntimeStatus.SUSPENDED
assert result.triggers is not None
assert len(result.triggers) == 1
assert result.triggers[0].interrupt_id == "queue-item"
assert result.triggers[0].trigger_type == UiPathResumeTriggerType.QUEUE_ITEM
# Bridge should have been called only 2 times (for 2 API triggers)
assert cast(AsyncMock, bridge.wait_for_tool_confirmation).await_count == 2