browser-use--browser-harness
a2443d1d51
Cross-checked AGENTS.md, README.md, SKILL.md, install.md, and profile-sync.md for accuracy. Tightened the sync_local_profile docstring and the chrome://inspect stderr message to match. Cloud-bootstrap test updated to set BU_AUTOSPAWN. All unit tests pass.
54 行
2.1 KiB
Python
54 行
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 + BU_AUTOSPAWN set -> auto-provision cloud daemon."""
|
|
monkeypatch.setenv("BROWSER_USE_API_KEY", "test-key")
|
|
monkeypatch.setenv("BU_AUTOSPAWN", "1")
|
|
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"
|