Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

test(doctor): remove mocking, use real objects per test policy

ShawnPana99b0fab32035
diff --git a/tests/ci/test_doctor_command.py b/tests/ci/test_doctor_command.py
index 746a2ace4..b7842c557 100644
--- a/tests/ci/test_doctor_command.py
+++ b/tests/ci/test_doctor_command.py
@@ -1,6 +1,6 @@
"""Tests for doctor command."""
-from unittest.mock import patch
+import os
import pytest
@@ -8,101 +8,95 @@ from browser_use.skill_cli.commands import doctor
@pytest.mark.asyncio
-async def test_doctor_healthy():
- """Test doctor when all checks pass."""
- with patch.object(doctor, '_check_package') as mock_pkg:
- mock_pkg.return_value = {'status': 'ok', 'message': 'browser-use installed'}
+async def test_doctor_handle_returns_valid_structure():
+ """Test that doctor.handle() returns a valid result structure."""
+ result = await doctor.handle()
- with patch.object(doctor, '_check_browser') as mock_browser:
- mock_browser.return_value = {'status': 'ok', 'message': 'Browser available'}
+ # Verify structure
+ assert 'status' in result
+ assert result['status'] in ('healthy', 'issues_found')
+ assert 'checks' in result
+ assert 'summary' in result
- with patch.object(doctor, '_check_api_key_config') as mock_api:
- mock_api.return_value = {'status': 'ok', 'message': 'API key configured'}
+ # Verify all expected checks are present
+ expected_checks = ['package', 'browser', 'api_key', 'cloudflared', 'network']
+ for check in expected_checks:
+ assert check in result['checks']
+ assert 'status' in result['checks'][check]
+ assert 'message' in result['checks'][check]
- with patch.object(doctor, '_check_cloudflared') as mock_cloudflared:
- mock_cloudflared.return_value = {'status': 'ok', 'message': 'Cloudflared available'}
- with patch.object(doctor, '_check_network') as mock_network:
- mock_network.return_value = {'status': 'ok', 'message': 'Network OK'}
+def test_check_package_installed():
+ """Test _check_package returns ok when browser-use is installed."""
+ # browser-use is always installed in the test environment
+ result = doctor._check_package()
+ assert result['status'] == 'ok'
+ assert 'browser-use' in result['message']
- result = await doctor.handle()
- assert result['status'] == 'healthy'
- assert all(c['status'] == 'ok' for c in result['checks'].values())
- assert 'summary' in result
+def test_check_browser_returns_valid_structure():
+ """Test _check_browser returns a valid result."""
+ result = doctor._check_browser()
+ assert 'status' in result
+ assert result['status'] in ('ok', 'warning')
+ assert 'message' in result
-async def test_doctor_issues():
- """Test doctor when issues are found."""
- with patch.object(doctor, '_check_package') as mock_pkg:
- mock_pkg.return_value = {'status': 'error', 'message': 'browser-use not installed'}
+def test_check_api_key_with_env_var(monkeypatch):
+ """Test _check_api_key_config when API key is set via env var."""
+ monkeypatch.setenv('BROWSER_USE_API_KEY', 'test_key_12345')
- with patch.object(doctor, '_check_browser'):
- with patch.object(doctor, '_check_api_key_config'):
- with patch.object(doctor, '_check_cloudflared'):
- with patch.object(doctor, '_check_network'):
- result = await doctor.handle()
+ result = doctor._check_api_key_config()
+ assert result['status'] == 'ok'
+ assert 'configured' in result['message'].lower()
- assert result['status'] == 'issues_found'
- assert result['checks']['package']['status'] == 'error'
+def test_check_api_key_missing(monkeypatch):
+ """Test _check_api_key_config when API key is not available."""
+ # Remove env var if set
+ monkeypatch.delenv('BROWSER_USE_API_KEY', raising=False)
-def test_check_package_installed():
- """Test _check_package when installed."""
+ # Also need to ensure no config file provides a key
+ # by temporarily setting XDG_CONFIG_HOME to empty temp dir
+ import tempfile
- result = doctor._check_package()
- assert result['status'] == 'ok'
- assert 'browser-use' in result['message']
+ with tempfile.TemporaryDirectory() as tmpdir:
+ monkeypatch.setenv('XDG_CONFIG_HOME', tmpdir)
+ # On macOS, also need to handle ~/Library/Application Support
+ monkeypatch.setenv('HOME', tmpdir)
+ # Clear any cached config
+ from browser_use.skill_cli import api_key
-def test_check_package_not_installed():
- """Test _check_package when not installed."""
- with patch('builtins.__import__', side_effect=ImportError):
- result = doctor._check_package()
- assert result['status'] == 'error'
- assert 'not installed' in result['message']
+ if hasattr(api_key, '_cached_key'):
+ monkeypatch.setattr(api_key, '_cached_key', None)
-
-def test_check_api_key_available():
- """Test _check_api_key_config when API key is available."""
- with patch('browser_use.skill_cli.api_key.check_api_key') as mock_check:
- mock_check.return_value = {'available': True, 'source': 'env'}
result = doctor._check_api_key_config()
- assert result['status'] == 'ok'
+ assert result['status'] == 'missing'
+ assert 'no api key' in result['message'].lower()
-def test_check_api_key_missing():
- """Test _check_api_key_config when API key is missing."""
- with patch('browser_use.skill_cli.api_key.check_api_key') as mock_check:
- mock_check.return_value = {'available': False}
- result = doctor._check_api_key_config()
- assert result['status'] == 'missing'
+def test_check_cloudflared_returns_valid_structure():
+ """Test _check_cloudflared returns a valid result structure."""
+ result = doctor._check_cloudflared()
+ assert 'status' in result
+ assert result['status'] in ('ok', 'missing')
+ assert 'message' in result
-def test_check_cloudflared_available():
- """Test _check_cloudflared when available."""
- with patch('browser_use.skill_cli.tunnel.get_tunnel_manager') as mock_mgr:
- mock_instance = mock_mgr.return_value
- mock_instance.get_status.return_value = {
- 'available': True,
- 'source': 'system',
- 'note': 'System installed',
- }
- result = doctor._check_cloudflared()
- assert result['status'] == 'ok'
-
-
-def test_check_cloudflared_missing():
- """Test _check_cloudflared when not available."""
- with patch('browser_use.skill_cli.tunnel.get_tunnel_manager') as mock_mgr:
- mock_instance = mock_mgr.return_value
- mock_instance.get_status.return_value = {
- 'available': False,
- 'note': 'Not installed',
- }
- result = doctor._check_cloudflared()
- assert result['status'] == 'missing'
+ # If available, should have details
+ if result['status'] == 'ok':
+ assert 'available' in result['message'].lower() or 'cloudflared' in result['message'].lower()
+
+
+async def test_check_network_returns_valid_structure():
+ """Test _check_network returns a valid result structure."""
+ result = await doctor._check_network()
+
+ assert 'status' in result
+ assert result['status'] in ('ok', 'warning')
+ assert 'message' in result
def test_summarize_checks_all_ok():
@@ -127,3 +121,14 @@ def test_summarize_checks_mixed():
assert '1/3' in summary
assert '1 warning' in summary
assert '1 missing' in summary
+
+
+def test_summarize_checks_with_errors():
+ """Test _summarize_checks with errors."""
+ checks = {
+ 'check1': {'status': 'ok'},
+ 'check2': {'status': 'error'},
+ }
+ summary = doctor._summarize_checks(checks)
+ assert '1/2' in summary
+ assert '1 error' in summary