browser-use--browser-harness
49355ba7a8
Cubic flagged that the original socket.create_connection probe matches any process on 9222/9223, not just Chrome. Mirror daemon.py's fallback by hitting /json/version, so a stale or unrelated listener does not skip the cloud bootstrap. Drop the three boolean-table tests that mocked every collaborator and re-asserted the literal if-condition. Add a focused test for _local_chrome_listening that covers the false-positive case directly.
53 行
2.1 KiB
Python
53 行
2.1 KiB
Python
import sys
|
|
from io import StringIO
|
|
from unittest.mock import patch
|
|
|
|
from browser_harness import run
|
|
|
|
|
|
def test_c_flag_executes_code():
|
|
stdout = StringIO()
|
|
with patch.object(sys, "argv", ["browser-harness", "-c", "print('hello from -c')"]), \
|
|
patch("browser_harness.run.ensure_daemon"), \
|
|
patch("browser_harness.run.print_update_banner"), \
|
|
patch("sys.stdout", stdout):
|
|
run.main()
|
|
assert stdout.getvalue().strip() == "hello from -c"
|
|
|
|
|
|
def test_cloud_bootstrap_on_headless_server(monkeypatch):
|
|
"""No daemon, no local Chrome, API key set -> auto-provision cloud daemon."""
|
|
monkeypatch.setenv("BROWSER_USE_API_KEY", "test-key")
|
|
with patch.object(sys, "argv", ["browser-harness", "-c", "x = 1"]), \
|
|
patch("browser_harness.run.daemon_alive", return_value=False), \
|
|
patch("browser_harness.run._local_chrome_listening", return_value=False), \
|
|
patch("browser_harness.run.start_remote_daemon") as mock_start, \
|
|
patch("browser_harness.run.ensure_daemon"), \
|
|
patch("browser_harness.run.print_update_banner"):
|
|
run.main()
|
|
mock_start.assert_called_once()
|
|
|
|
|
|
def test_local_chrome_listening_rejects_non_chrome():
|
|
"""A bare TCP listener on 9222/9223 must not fool the probe — only a real
|
|
/json/version response counts as Chrome."""
|
|
with patch("browser_harness.run.urllib.request.urlopen", side_effect=OSError):
|
|
assert run._local_chrome_listening() is False
|
|
with patch("browser_harness.run.urllib.request.urlopen") as mock_open:
|
|
assert run._local_chrome_listening() is True
|
|
mock_open.assert_called_once()
|
|
|
|
|
|
def test_c_flag_does_not_read_stdin():
|
|
stdin_read = []
|
|
fake_stdin = StringIO("should not be read")
|
|
fake_stdin.read = lambda: stdin_read.append(True) or ""
|
|
|
|
with patch.object(sys, "argv", ["browser-harness", "-c", "x = 1"]), \
|
|
patch("browser_harness.run.ensure_daemon"), \
|
|
patch("browser_harness.run.print_update_banner"), \
|
|
patch("sys.stdin", fake_stdin):
|
|
run.main()
|
|
|
|
assert not stdin_read, "stdin should not be read when -c is passed"
|