|
6 | 6 | from unittest.mock import patch, MagicMock |
7 | 7 | import pytest |
8 | 8 |
|
| 9 | +try: |
| 10 | + import psutil |
| 11 | + |
| 12 | + PSUTIL_AVAILABLE = True |
| 13 | +except ImportError: |
| 14 | + PSUTIL_AVAILABLE = False |
| 15 | + |
9 | 16 | from eval_protocol.utils.show_results_url import ( |
10 | 17 | is_server_running, |
11 | 18 | generate_invocation_filter_url, |
@@ -193,3 +200,137 @@ def test_full_workflow_stores_urls(self, mock_store): |
193 | 200 | assert "table" in call_args[2] |
194 | 201 | assert "integration-test" in call_args[1] |
195 | 202 | assert "integration-test" in call_args[2] |
| 203 | + |
| 204 | + |
| 205 | +class TestBrowserUtilities: |
| 206 | + """Test browser utility functions.""" |
| 207 | + |
| 208 | + def test_get_pid_file_path(self): |
| 209 | + """Test PID file path generation.""" |
| 210 | + from eval_protocol.utils.browser_utils import _get_pid_file_path |
| 211 | + from eval_protocol.directory_utils import find_eval_protocol_dir |
| 212 | + from pathlib import Path |
| 213 | + |
| 214 | + pid_file = _get_pid_file_path() |
| 215 | + expected = Path(find_eval_protocol_dir()) / "logs_server.pid" |
| 216 | + assert pid_file == expected |
| 217 | + |
| 218 | + def test_is_logs_server_running_no_pid_file(self, tmp_path, monkeypatch): |
| 219 | + """Test server detection when PID file doesn't exist.""" |
| 220 | + from eval_protocol.utils.browser_utils import is_logs_server_running |
| 221 | + |
| 222 | + # Mock the PID file path to a non-existent file |
| 223 | + monkeypatch.setattr( |
| 224 | + "eval_protocol.utils.browser_utils._get_pid_file_path", lambda: tmp_path / "nonexistent.pid" |
| 225 | + ) |
| 226 | + |
| 227 | + is_running, port = is_logs_server_running() |
| 228 | + assert not is_running |
| 229 | + assert port is None |
| 230 | + |
| 231 | + def test_is_logs_server_running_invalid_pid_file(self, tmp_path, monkeypatch): |
| 232 | + """Test server detection with invalid PID file content.""" |
| 233 | + from eval_protocol.utils.browser_utils import is_logs_server_running |
| 234 | + |
| 235 | + # Create invalid PID file |
| 236 | + pid_file = tmp_path / "invalid.pid" |
| 237 | + pid_file.write_text("invalid json") |
| 238 | + monkeypatch.setattr("eval_protocol.utils.browser_utils._get_pid_file_path", lambda: pid_file) |
| 239 | + |
| 240 | + is_running, port = is_logs_server_running() |
| 241 | + assert not is_running |
| 242 | + assert port is None |
| 243 | + |
| 244 | + def test_is_logs_server_running_missing_pid_key(self, tmp_path, monkeypatch): |
| 245 | + """Test server detection with PID file missing required keys.""" |
| 246 | + from eval_protocol.utils.browser_utils import is_logs_server_running |
| 247 | + import json |
| 248 | + |
| 249 | + # Create PID file with missing pid key |
| 250 | + pid_file = tmp_path / "missing_pid.pid" |
| 251 | + pid_file.write_text(json.dumps({"port": 8000})) |
| 252 | + monkeypatch.setattr("eval_protocol.utils.browser_utils._get_pid_file_path", lambda: pid_file) |
| 253 | + |
| 254 | + is_running, port = is_logs_server_running() |
| 255 | + assert not is_running |
| 256 | + assert port is None |
| 257 | + |
| 258 | + @pytest.mark.skipif(not PSUTIL_AVAILABLE, reason="psutil not available") |
| 259 | + def test_is_logs_server_running_nonexistent_process(self, tmp_path, monkeypatch): |
| 260 | + """Test server detection with PID file pointing to non-existent process.""" |
| 261 | + from eval_protocol.utils.browser_utils import is_logs_server_running |
| 262 | + import json |
| 263 | + |
| 264 | + # Create PID file with non-existent PID |
| 265 | + pid_file = tmp_path / "nonexistent_process.pid" |
| 266 | + pid_file.write_text(json.dumps({"pid": 999999, "port": 8000})) |
| 267 | + monkeypatch.setattr("eval_protocol.utils.browser_utils._get_pid_file_path", lambda: pid_file) |
| 268 | + |
| 269 | + is_running, port = is_logs_server_running() |
| 270 | + assert not is_running |
| 271 | + assert port is None |
| 272 | + |
| 273 | + @pytest.mark.skipif(not PSUTIL_AVAILABLE, reason="psutil not available") |
| 274 | + def test_is_logs_server_running_current_process(self, tmp_path, monkeypatch): |
| 275 | + """Test server detection with PID file pointing to current process.""" |
| 276 | + from eval_protocol.utils.browser_utils import is_logs_server_running |
| 277 | + import json |
| 278 | + import os |
| 279 | + |
| 280 | + # Create PID file with current process PID |
| 281 | + pid_file = tmp_path / "current_process.pid" |
| 282 | + pid_file.write_text(json.dumps({"pid": os.getpid(), "port": 8000})) |
| 283 | + monkeypatch.setattr("eval_protocol.utils.browser_utils._get_pid_file_path", lambda: pid_file) |
| 284 | + |
| 285 | + is_running, port = is_logs_server_running() |
| 286 | + assert is_running |
| 287 | + assert port == 8000 |
| 288 | + |
| 289 | + def test_open_browser_tab(self, monkeypatch): |
| 290 | + """Test browser tab opening.""" |
| 291 | + from eval_protocol.utils.browser_utils import open_browser_tab |
| 292 | + |
| 293 | + opened_urls = [] |
| 294 | + |
| 295 | + def mock_open_new_tab(url): |
| 296 | + opened_urls.append(url) |
| 297 | + |
| 298 | + monkeypatch.setattr("webbrowser.open_new_tab", mock_open_new_tab) |
| 299 | + |
| 300 | + # Test with delay |
| 301 | + open_browser_tab("http://example.com", delay=0.01) |
| 302 | + |
| 303 | + # Wait a bit for the thread to execute |
| 304 | + import time |
| 305 | + |
| 306 | + time.sleep(0.02) |
| 307 | + |
| 308 | + assert len(opened_urls) == 1 |
| 309 | + assert opened_urls[0] == "http://example.com" |
| 310 | + |
| 311 | + |
| 312 | +class TestLogsServerPidFile: |
| 313 | + """Test logs server PID file functionality.""" |
| 314 | + |
| 315 | + def test_write_pid_file(self, tmp_path, monkeypatch): |
| 316 | + """Test PID file writing.""" |
| 317 | + from eval_protocol.utils.browser_utils import write_pid_file |
| 318 | + import json |
| 319 | + |
| 320 | + # Mock the find_eval_protocol_dir function |
| 321 | + monkeypatch.setattr("eval_protocol.directory_utils.find_eval_protocol_dir", lambda: str(tmp_path)) |
| 322 | + |
| 323 | + # Test writing PID file |
| 324 | + write_pid_file(12345, 8000) |
| 325 | + |
| 326 | + # Check that PID file was created |
| 327 | + pid_file = tmp_path / "logs_server.pid" |
| 328 | + assert pid_file.exists() |
| 329 | + |
| 330 | + # Check content |
| 331 | + with open(pid_file, "r") as f: |
| 332 | + data = json.load(f) |
| 333 | + assert "pid" in data |
| 334 | + assert "port" in data |
| 335 | + assert data["port"] == 8000 |
| 336 | + assert data["pid"] == 12345 |
0 commit comments