Skip to content

Commit 14d9f69

Browse files
refactor(tests): convert test classes to standalone functions
Address review comments: - Convert TestNormalizeDatetimeToServerTimezone class to standalone functions - Convert TestAfterTimestampFiltering class to standalone functions - Remove TestAfterTimestampFilteringBehavioral class (duplicative with event_service tests) - Remove TestWebSocketSubscriber, TestWebSocketDisconnectHandling, TestResendAllFunctionality classes - Move imports to module level (datetime, cast, EventPage, Event) - Reduce test count from 35+ to 11 tests for websocket functionality Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 0e68bfb commit 14d9f69

File tree

3 files changed

+326
-819
lines changed

3 files changed

+326
-819
lines changed

tests/agent_server/test_event_router.py

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for event_router.py endpoints."""
22

3-
from datetime import UTC, datetime, timezone
3+
from datetime import UTC, datetime, timedelta, timezone
44
from pathlib import Path
55
from typing import cast
66
from unittest.mock import AsyncMock, MagicMock
@@ -22,63 +22,43 @@
2222
from openhands.sdk.llm.message import ImageContent, TextContent
2323

2424

25-
class TestNormalizeDatetimeToServerTimezone:
26-
"""Unit tests for normalize_datetime_to_server_timezone function."""
25+
def test_normalize_datetime_naive_passthrough():
26+
"""Naive datetimes should be returned unchanged."""
27+
naive_dt = datetime(2025, 1, 15, 10, 30, 0)
28+
result = normalize_datetime_to_server_timezone(naive_dt)
2729

28-
def test_naive_datetime_passthrough(self):
29-
"""Naive datetimes should be returned unchanged."""
30-
naive_dt = datetime(2025, 1, 15, 10, 30, 0)
31-
result = normalize_datetime_to_server_timezone(naive_dt)
30+
assert result == naive_dt
31+
assert result.tzinfo is None
3232

33-
assert result == naive_dt
34-
assert result.tzinfo is None
3533

36-
def test_utc_datetime_converted_to_naive(self):
37-
"""UTC datetime should be converted to server local time and made naive."""
38-
utc_dt = datetime(2025, 1, 15, 10, 30, 0, tzinfo=UTC)
39-
result = normalize_datetime_to_server_timezone(utc_dt)
34+
def test_normalize_datetime_utc_converted_to_naive():
35+
"""UTC datetime should be converted to server local time and made naive."""
36+
utc_dt = datetime(2025, 1, 15, 10, 30, 0, tzinfo=UTC)
37+
result = normalize_datetime_to_server_timezone(utc_dt)
4038

41-
# Result should be naive
42-
assert result.tzinfo is None
43-
# Result should represent the same instant converted to local time
44-
expected = utc_dt.astimezone(None).replace(tzinfo=None)
45-
assert result == expected
39+
assert result.tzinfo is None
40+
expected = utc_dt.astimezone(None).replace(tzinfo=None)
41+
assert result == expected
4642

47-
def test_non_utc_timezone_converted_to_naive(self):
48-
"""Non-UTC timezone-aware datetime should be converted to server local time."""
49-
# Create a timezone-aware datetime in UTC
50-
aware_dt = datetime(2025, 6, 15, 18, 30, 0, tzinfo=UTC)
5143

52-
result = normalize_datetime_to_server_timezone(aware_dt)
44+
def test_normalize_datetime_preserves_microseconds():
45+
"""Microseconds should be preserved through conversion."""
46+
utc_dt = datetime(2025, 1, 15, 10, 30, 0, 123456, tzinfo=UTC)
47+
result = normalize_datetime_to_server_timezone(utc_dt)
5348

54-
# Result should be naive
55-
assert result.tzinfo is None
56-
# Result should represent the same instant in server local time
57-
expected = aware_dt.astimezone(None).replace(tzinfo=None)
58-
assert result == expected
49+
assert result.microsecond == 123456
5950

60-
def test_preserves_microseconds(self):
61-
"""Microseconds should be preserved through conversion."""
62-
utc_dt = datetime(2025, 1, 15, 10, 30, 0, 123456, tzinfo=UTC)
63-
result = normalize_datetime_to_server_timezone(utc_dt)
6451

65-
assert result.microsecond == 123456
52+
def test_normalize_datetime_fixed_offset_timezone():
53+
"""Test with a specific fixed offset timezone (UTC+5:30)."""
54+
ist = timezone(timedelta(hours=5, minutes=30))
55+
ist_dt = datetime(2025, 1, 15, 16, 0, 0, tzinfo=ist)
6656

67-
def test_different_fixed_offset_timezone(self):
68-
"""Test with a specific fixed offset timezone."""
69-
# UTC+5:30 (like India Standard Time)
70-
from datetime import timedelta
57+
result = normalize_datetime_to_server_timezone(ist_dt)
7158

72-
ist = timezone(timedelta(hours=5, minutes=30))
73-
ist_dt = datetime(2025, 1, 15, 16, 0, 0, tzinfo=ist) # 4:00 PM IST
74-
75-
result = normalize_datetime_to_server_timezone(ist_dt)
76-
77-
# Should be naive
78-
assert result.tzinfo is None
79-
# Should be the same instant converted to server local time
80-
expected = ist_dt.astimezone(None).replace(tzinfo=None)
81-
assert result == expected
59+
assert result.tzinfo is None
60+
expected = ist_dt.astimezone(None).replace(tzinfo=None)
61+
assert result == expected
8262

8363

8464
@pytest.fixture

0 commit comments

Comments
 (0)