|
| 1 | +import json |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import AsyncMock, Mock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from eval_protocol.utils.logs_server import EvaluationWatcher, WebSocketManager |
| 9 | + |
| 10 | + |
| 11 | +class TestWebSocketManagerBasic: |
| 12 | + """Basic tests for WebSocketManager without starting real loops.""" |
| 13 | + |
| 14 | + def test_initialization(self): |
| 15 | + """Test WebSocketManager initialization.""" |
| 16 | + manager = WebSocketManager() |
| 17 | + assert len(manager.active_connections) == 0 |
| 18 | + assert manager._broadcast_queue is not None |
| 19 | + assert manager._broadcast_task is None |
| 20 | + |
| 21 | + @pytest.mark.asyncio |
| 22 | + async def test_connect_disconnect(self): |
| 23 | + """Test WebSocket connection and disconnection.""" |
| 24 | + manager = WebSocketManager() |
| 25 | + mock_websocket = AsyncMock() |
| 26 | + |
| 27 | + # Test connection |
| 28 | + await manager.connect(mock_websocket) |
| 29 | + assert len(manager.active_connections) == 1 |
| 30 | + assert mock_websocket in manager.active_connections |
| 31 | + mock_websocket.accept.assert_called_once() |
| 32 | + |
| 33 | + # Test disconnection |
| 34 | + manager.disconnect(mock_websocket) |
| 35 | + assert len(manager.active_connections) == 0 |
| 36 | + assert mock_websocket not in manager.active_connections |
| 37 | + |
| 38 | + def test_broadcast_row_upserted(self): |
| 39 | + """Test broadcasting row upsert events.""" |
| 40 | + manager = WebSocketManager() |
| 41 | + |
| 42 | + # Create a simple mock row |
| 43 | + mock_row = Mock() |
| 44 | + mock_row.model_dump.return_value = {"id": "test-123", "content": "test"} |
| 45 | + |
| 46 | + # Test that broadcast doesn't fail when no connections |
| 47 | + manager.broadcast_row_upserted(mock_row) |
| 48 | + |
| 49 | + # Test that message is queued |
| 50 | + assert not manager._broadcast_queue.empty() |
| 51 | + queued_message = manager._broadcast_queue.get_nowait() |
| 52 | + assert "type" in queued_message |
| 53 | + assert "row" in queued_message |
| 54 | + json_message = json.loads(queued_message) |
| 55 | + assert json_message["row"]["id"] == "test-123" |
| 56 | + assert json_message["row"]["content"] == "test" |
| 57 | + |
| 58 | + |
| 59 | +class TestEvaluationWatcherBasic: |
| 60 | + """Basic tests for EvaluationWatcher without starting real threads.""" |
| 61 | + |
| 62 | + def test_initialization(self): |
| 63 | + """Test EvaluationWatcher initialization.""" |
| 64 | + mock_manager = Mock() |
| 65 | + watcher = EvaluationWatcher(mock_manager) |
| 66 | + assert watcher.websocket_manager == mock_manager |
| 67 | + assert watcher._thread is None |
| 68 | + assert watcher._stop_event is not None |
| 69 | + |
| 70 | + def test_start_stop(self): |
| 71 | + """Test starting and stopping the watcher.""" |
| 72 | + mock_manager = Mock() |
| 73 | + watcher = EvaluationWatcher(mock_manager) |
| 74 | + |
| 75 | + # Test start |
| 76 | + watcher.start() |
| 77 | + assert watcher._thread is not None |
| 78 | + assert watcher._thread.is_alive() |
| 79 | + |
| 80 | + # Test stop |
| 81 | + watcher.stop() |
| 82 | + assert watcher._stop_event.is_set() |
| 83 | + if watcher._thread: |
| 84 | + watcher._thread.join(timeout=1.0) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + pytest.main([__file__, "-v"]) |
0 commit comments