-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathtest_event_router.py
More file actions
809 lines (686 loc) · 31 KB
/
test_event_router.py
File metadata and controls
809 lines (686 loc) · 31 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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
"""Tests for event_router.py endpoints."""
from datetime import UTC, datetime, timedelta, timezone
from pathlib import Path
from typing import cast
from unittest.mock import AsyncMock, MagicMock
from uuid import uuid4
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from openhands.agent_server.dependencies import get_event_service
from openhands.agent_server.event_router import (
event_router,
normalize_datetime_to_server_timezone,
)
from openhands.agent_server.event_service import EventService
from openhands.agent_server.models import SendMessageRequest
from openhands.sdk import Message
from openhands.sdk.event.llm_convertible.message import MessageEvent
from openhands.sdk.llm.message import ImageContent, TextContent
def test_normalize_datetime_naive_passthrough():
"""Naive datetimes should be returned unchanged."""
naive_dt = datetime(2025, 1, 15, 10, 30, 0)
result = normalize_datetime_to_server_timezone(naive_dt)
assert result == naive_dt
assert result.tzinfo is None
def test_normalize_datetime_utc_converted_to_naive():
"""UTC datetime should be converted to server local time and made naive."""
utc_dt = datetime(2025, 1, 15, 10, 30, 0, tzinfo=UTC)
result = normalize_datetime_to_server_timezone(utc_dt)
assert result.tzinfo is None
expected = utc_dt.astimezone(None).replace(tzinfo=None)
assert result == expected
def test_normalize_datetime_preserves_microseconds():
"""Microseconds should be preserved through conversion."""
utc_dt = datetime(2025, 1, 15, 10, 30, 0, 123456, tzinfo=UTC)
result = normalize_datetime_to_server_timezone(utc_dt)
assert result.microsecond == 123456
def test_normalize_datetime_fixed_offset_timezone():
"""Test with a specific fixed offset timezone (UTC+5:30)."""
ist = timezone(timedelta(hours=5, minutes=30))
ist_dt = datetime(2025, 1, 15, 16, 0, 0, tzinfo=ist)
result = normalize_datetime_to_server_timezone(ist_dt)
assert result.tzinfo is None
expected = ist_dt.astimezone(None).replace(tzinfo=None)
assert result == expected
@pytest.fixture
def client():
"""Create a test client for the FastAPI app without authentication."""
app = FastAPI()
app.include_router(event_router, prefix="/api")
return TestClient(app)
@pytest.fixture
def sample_conversation_id():
"""Return a sample conversation ID."""
return uuid4()
@pytest.fixture
def mock_event_service():
"""Create a mock EventService for testing."""
service = AsyncMock(spec=EventService)
service.send_message = AsyncMock()
return service
class TestSendMessageEndpoint:
"""Test cases for the send_message endpoint."""
@pytest.mark.asyncio
async def test_send_message_with_run_true(
self, client, sample_conversation_id, mock_event_service
):
"""Test send_message endpoint with run=True."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
request_data = {
"role": "user",
"content": [{"type": "text", "text": "Hello, world!"}],
"run": True,
}
response = client.post(
f"/api/conversations/{sample_conversation_id}/events", json=request_data
)
assert response.status_code == 200
assert response.json() == {"success": True}
# Verify send_message was called with correct parameters
mock_event_service.send_message.assert_called_once()
call_args = mock_event_service.send_message.call_args
message, run_flag = call_args[0]
assert isinstance(message, Message)
assert message.role == "user"
assert len(message.content) == 1
assert isinstance(message.content[0], TextContent)
assert message.content[0].text == "Hello, world!"
assert run_flag is True
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_send_message_with_run_false(
self, client, sample_conversation_id, mock_event_service
):
"""Test send_message endpoint with run=False."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
request_data = {
"role": "assistant",
"content": [{"type": "text", "text": "I understand."}],
"run": False,
}
response = client.post(
f"/api/conversations/{sample_conversation_id}/events", json=request_data
)
assert response.status_code == 200
assert response.json() == {"success": True}
# Verify send_message was called with run=False
mock_event_service.send_message.assert_called_once()
call_args = mock_event_service.send_message.call_args
message, run_flag = call_args[0]
assert isinstance(message, Message)
assert message.role == "assistant"
assert run_flag is False
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_send_message_default_run_value(
self, client, sample_conversation_id, mock_event_service
):
"""Test send_message endpoint with default run value."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Request without run field should use default value
request_data = {
"role": "user",
"content": [{"type": "text", "text": "Test message"}],
}
response = client.post(
f"/api/conversations/{sample_conversation_id}/events", json=request_data
)
assert response.status_code == 200
assert response.json() == {"success": True}
# Verify send_message was called with default run value (False)
mock_event_service.send_message.assert_called_once()
call_args = mock_event_service.send_message.call_args
message, run_flag = call_args[0]
assert isinstance(message, Message)
assert message.role == "user"
assert run_flag is False # Default value from SendMessageRequest
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_send_message_conversation_not_found(
self, client, sample_conversation_id
):
"""Test send_message endpoint when conversation is not found."""
from fastapi import HTTPException, status
def raise_not_found():
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Conversation not found: {sample_conversation_id}",
)
# Override the dependency to raise HTTPException
client.app.dependency_overrides[get_event_service] = raise_not_found
try:
request_data = {
"role": "user",
"content": [{"type": "text", "text": "Hello"}],
"run": True,
}
response = client.post(
f"/api/conversations/{sample_conversation_id}/events", json=request_data
)
assert response.status_code == 404
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_send_message_with_different_content_types(
self, client, sample_conversation_id, mock_event_service
):
"""Test send_message endpoint with different content types."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Test with multiple content items
request_data = {
"role": "user",
"content": [
{"type": "text", "text": "First part"},
{"type": "text", "text": "Second part"},
],
"run": False,
}
response = client.post(
f"/api/conversations/{sample_conversation_id}/events", json=request_data
)
assert response.status_code == 200
assert response.json() == {"success": True}
# Verify message content was parsed correctly
mock_event_service.send_message.assert_called_once()
call_args = mock_event_service.send_message.call_args
message, run_flag = call_args[0]
assert isinstance(message, Message)
assert message.role == "user"
assert len(message.content) == 2
assert all(isinstance(content, TextContent) for content in message.content)
text_content = cast(list[TextContent], message.content)
assert text_content[0].text == "First part"
assert text_content[1].text == "Second part"
assert run_flag is False
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_send_message_with_system_role(
self, client, sample_conversation_id, mock_event_service
):
"""Test send_message endpoint with system role."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
request_data = {
"role": "system",
"content": [{"type": "text", "text": "System initialization message"}],
"run": True,
}
response = client.post(
f"/api/conversations/{sample_conversation_id}/events", json=request_data
)
assert response.status_code == 200
assert response.json() == {"success": True}
# Verify system message was processed correctly
mock_event_service.send_message.assert_called_once()
call_args = mock_event_service.send_message.call_args
message, run_flag = call_args[0]
assert isinstance(message, Message)
assert message.role == "system"
assert run_flag is True
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_send_message_invalid_request_data(
self, client, sample_conversation_id
):
"""Test send_message endpoint with invalid request data."""
# Override the dependency (though it shouldn't be called for validation errors)
client.app.dependency_overrides[get_event_service] = lambda: None
try:
# Test with invalid role value
invalid_role_data = {
"role": "invalid_role",
"content": [{"type": "text", "text": "Hello"}],
"run": True,
}
response = client.post(
f"/api/conversations/{sample_conversation_id}/events",
json=invalid_role_data,
)
assert response.status_code == 422 # Validation error
# Test with invalid content structure
invalid_content_data = {
"role": "user",
"content": "invalid_content_should_be_list", # Should be a list
"run": True,
}
response = client.post(
f"/api/conversations/{sample_conversation_id}/events",
json=invalid_content_data,
)
assert response.status_code == 422 # Validation error
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
def test_create_message(self):
content: list[TextContent | ImageContent] = [
TextContent(
text="This is a message",
)
]
request = SendMessageRequest(
role="user",
content=content,
)
message = request.create_message()
assert message.content == content
class TestSearchEventsEndpoint:
"""Test cases for the search events endpoint with timestamp filtering."""
@pytest.mark.asyncio
async def test_search_events_with_naive_datetime(
self, client, sample_conversation_id, mock_event_service
):
"""Test search events with naive datetime (no timezone)."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Mock the search_events method to return a sample result
mock_event_service.search_events = AsyncMock(
return_value={"items": [], "next_page_id": None}
)
# Test with naive datetime
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={
"timestamp__gte": "2025-01-01T12:00:00", # Naive datetime string
"limit": 10,
},
)
assert response.status_code == 200
mock_event_service.search_events.assert_called_once()
# Verify that the datetime was normalized (converted to datetime object)
call_args = mock_event_service.search_events.call_args
# Check args: (page_id, limit, kind, source, body, sort_order,
# timestamp__gte, timestamp__lt)
assert len(call_args[0]) >= 7 # Should have at least 7 positional args
assert call_args[0][6] is not None # timestamp__gte should be normalized
assert call_args[0][7] is None # timestamp__lt should be None
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_search_events_with_timezone_aware_datetime(
self, client, sample_conversation_id, mock_event_service
):
"""Test search events with timezone-aware datetime."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Mock the search_events method to return a sample result
mock_event_service.search_events = AsyncMock(
return_value={"items": [], "next_page_id": None}
)
# Test with timezone-aware datetime (UTC)
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={
"timestamp__gte": "2025-01-01T12:00:00Z", # UTC timezone
"limit": 10,
},
)
assert response.status_code == 200
mock_event_service.search_events.assert_called_once()
# Verify that the datetime was normalized
call_args = mock_event_service.search_events.call_args
# Check args: (page_id, limit, kind, source, body, sort_order,
# timestamp__gte, timestamp__lt)
assert len(call_args[0]) >= 7 # Should have at least 7 positional args
assert call_args[0][6] is not None # timestamp__gte should be normalized
assert call_args[0][7] is None # timestamp__lt should be None
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_search_events_with_timezone_range(
self, client, sample_conversation_id, mock_event_service
):
"""Test search events with both timestamp filters using
timezone-aware datetimes."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Mock the search_events method to return a sample result
mock_event_service.search_events = AsyncMock(
return_value={"items": [], "next_page_id": None}
)
# Test with both timestamp filters using timezone-aware datetimes
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={
"timestamp__gte": "2025-01-01T10:00:00+05:00", # UTC+5
"timestamp__lt": "2025-01-01T14:00:00-08:00", # UTC-8
"limit": 10,
},
)
assert response.status_code == 200
mock_event_service.search_events.assert_called_once()
# Verify that both datetimes were normalized
call_args = mock_event_service.search_events.call_args
# Check args: (page_id, limit, kind, source, body, sort_order,
# timestamp__gte, timestamp__lt)
assert len(call_args[0]) >= 8 # Should have at least 8 positional args
assert call_args[0][6] is not None # timestamp__gte should be normalized
assert call_args[0][7] is not None # timestamp__lt should be normalized
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_count_events_with_timezone_aware_datetime(
self, client, sample_conversation_id, mock_event_service
):
"""Test count events with timezone-aware datetime."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Mock the count_events method to return a sample result
mock_event_service.count_events = AsyncMock(return_value=5)
# Test with timezone-aware datetime
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/count",
params={
"timestamp__gte": "2025-01-01T12:00:00+02:00", # UTC+2
},
)
assert response.status_code == 200
assert response.json() == 5
mock_event_service.count_events.assert_called_once()
# Verify that the datetime was normalized
call_args = mock_event_service.count_events.call_args
# Check args: (kind, source, body, timestamp__gte, timestamp__lt)
assert len(call_args[0]) >= 4 # Should have at least 4 positional args
assert call_args[0][3] is not None # timestamp__gte should be normalized
assert call_args[0][4] is None # timestamp__lt should be None
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_count_events_with_source_filter(
self, client, sample_conversation_id, mock_event_service
):
"""Test count events with source filter."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Mock the count_events method to return a sample result
mock_event_service.count_events = AsyncMock(return_value=3)
# Test with source filter
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/count",
params={
"source": "environment",
},
)
assert response.status_code == 200
assert response.json() == 3
mock_event_service.count_events.assert_called_once()
# Verify that the source parameter was passed correctly
call_args = mock_event_service.count_events.call_args
# Check positional arguments: (kind, source, timestamp__gte, timestamp__lt)
assert len(call_args[0]) >= 2 # Should have at least 2 positional args
assert call_args[0][0] is None # kind should be None
assert call_args[0][1] == "environment" # source should be "environment"
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_search_events_timezone_normalization_consistency(
self, client, sample_conversation_id, mock_event_service
):
"""Test that different timezone representations of the same moment
normalize consistently."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Mock the search_events method to return a sample result
mock_event_service.search_events = AsyncMock(
return_value={"items": [], "next_page_id": None}
)
# Test 1: UTC timezone
response1 = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={
"timestamp__gte": "2025-01-01T12:00:00Z", # 12:00 UTC
"limit": 10,
},
)
# Test 2: EST timezone (UTC-5) - same moment as 12:00 UTC
response2 = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={
# 07:00 EST = 12:00 UTC
"timestamp__gte": "2025-01-01T07:00:00-05:00",
"limit": 10,
},
)
assert response1.status_code == 200
assert response2.status_code == 200
# Both calls should have been made
assert mock_event_service.search_events.call_count == 2
# Get the normalized datetimes from both calls
call1_args = mock_event_service.search_events.call_args_list[0]
call2_args = mock_event_service.search_events.call_args_list[1]
# Both should normalize to the same server time
# Check positional arguments: (page_id, limit, kind, source, sort_order,
# timestamp__gte, timestamp__lt)
normalized_time1 = call1_args[0][5] # timestamp__gte from first call
normalized_time2 = call2_args[0][5] # timestamp__gte from second call
# They should be the same after normalization
assert normalized_time1 == normalized_time2
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_search_events_with_source_filter(
self, client, sample_conversation_id, mock_event_service
):
"""Test search events with source filter."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Mock the search_events method to return a sample result
mock_event_service.search_events = AsyncMock(
return_value={"items": [], "next_page_id": None}
)
# Test with source filter
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={
"source": "agent",
"limit": 10,
},
)
assert response.status_code == 200
mock_event_service.search_events.assert_called_once()
# Verify that the source parameter was passed correctly
call_args = mock_event_service.search_events.call_args
# Check args: (page_id, limit, kind, source, body, sort_order,
# timestamp__gte, timestamp__lt)
assert len(call_args[0]) >= 4 # Should have at least 4 positional args
assert call_args[0][3] == "agent" # source should be "agent"
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_search_events_with_multiple_filters(
self, client, sample_conversation_id, mock_event_service
):
"""Test search events with multiple filters including source."""
# Override the dependency to return our mock
client.app.dependency_overrides[get_event_service] = lambda: mock_event_service
try:
# Mock the search_events method to return a sample result
mock_event_service.search_events = AsyncMock(
return_value={"items": [], "next_page_id": None}
)
# Test with multiple filters including source
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={
"kind": "MessageEvent",
"source": "user",
"timestamp__gte": "2025-01-01T12:00:00Z",
"limit": 20,
},
)
assert response.status_code == 200
mock_event_service.search_events.assert_called_once()
# Verify that all parameters were passed correctly
call_args = mock_event_service.search_events.call_args
# Check args: (page_id, limit, kind, source, body, sort_order,
# timestamp__gte, timestamp__lt)
assert len(call_args[0]) >= 8 # Should have at least 8 positional args
assert call_args[0][1] == 20 # limit
assert call_args[0][2] == "MessageEvent" # kind
assert call_args[0][3] == "user" # source
assert call_args[0][4] is None # body should be None
assert call_args[0][6] is not None # timestamp__gte should be normalized
assert call_args[0][7] is None # timestamp__lt should be None
finally:
# Clean up the dependency override
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_search_events_with_source_filter_real_events(
self, client, sample_conversation_id
):
"""Test source filtering with real events."""
from openhands.agent_server.event_service import EventService
from openhands.sdk.llm.message import TextContent
# Create real EventService with sample events
event_service = EventService(
stored=MagicMock(), conversations_dir=Path("test_dir")
)
# Create events with different sources
events = [
MessageEvent(
id="user1",
source="user",
llm_message=Message(role="user", content=[TextContent(text="Hello")]),
),
MessageEvent(
id="agent1",
source="agent",
llm_message=Message(
role="assistant", content=[TextContent(text="Hi there")]
),
),
MessageEvent(
id="user2",
source="user",
llm_message=Message(role="user", content=[TextContent(text="Help me")]),
),
MessageEvent(
id="agent2",
source="agent",
llm_message=Message(
role="assistant", content=[TextContent(text="Sure")]
),
),
]
# Setup conversation mock
conversation = MagicMock()
state = MagicMock()
state.events = events
state.__enter__ = MagicMock(return_value=state)
state.__exit__ = MagicMock(return_value=None)
conversation._state = state
event_service._conversation = conversation
client.app.dependency_overrides[get_event_service] = lambda: event_service
try:
# Test filtering by source="user" - should return 2 events
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={"source": "user", "limit": 10},
)
assert response.status_code == 200
result = response.json()
assert len(result["items"]) == 2
returned_ids = [event["id"] for event in result["items"]]
assert "user1" in returned_ids
assert "user2" in returned_ids
finally:
client.app.dependency_overrides.clear()
@pytest.mark.asyncio
async def test_search_events_with_body_filter_real_events(
self, client, sample_conversation_id
):
"""Test body filtering with real events."""
from openhands.agent_server.event_service import EventService
from openhands.sdk.llm.message import TextContent
# Create real EventService with sample events
event_service = EventService(
stored=MagicMock(), conversations_dir=Path("test_dir")
)
# Create events with different message content
events = [
MessageEvent(
id="hello1",
source="user",
llm_message=Message(
role="user", content=[TextContent(text="Hello world")]
),
),
MessageEvent(
id="python1",
source="agent",
llm_message=Message(
role="assistant", content=[TextContent(text="Python is great")]
),
),
MessageEvent(
id="hello2",
source="user",
llm_message=Message(
role="user", content=[TextContent(text="Say hello to everyone")]
),
),
MessageEvent(
id="other1",
source="agent",
llm_message=Message(
role="assistant", content=[TextContent(text="JavaScript rocks")]
),
),
]
# Setup conversation mock
conversation = MagicMock()
state = MagicMock()
state.events = events
state.__enter__ = MagicMock(return_value=state)
state.__exit__ = MagicMock(return_value=None)
conversation._state = state
event_service._conversation = conversation
client.app.dependency_overrides[get_event_service] = lambda: event_service
try:
# Test filtering by body="hello" (case-insensitive) - should return 2 events
response = client.get(
f"/api/conversations/{sample_conversation_id}/events/search",
params={"body": "hello", "limit": 10},
)
assert response.status_code == 200
result = response.json()
assert len(result["items"]) == 2
returned_ids = [event["id"] for event in result["items"]]
assert "hello1" in returned_ids
assert "hello2" in returned_ids
finally:
client.app.dependency_overrides.clear()