# Conflicts: # apps/desktop/e2e/archived-hidden-session-recoverable.spec.ts # apps/desktop/e2e/bot-chat-message-agent-friendly-name.spec.ts # apps/desktop/e2e/bot-mailbox-unreadable-ticket.spec.ts # apps/desktop/e2e/bot-mode-roster-localized.spec.ts # apps/desktop/e2e/bot-mode-row-click-mirrors-registry.spec.ts # apps/desktop/e2e/bot-mode-tab-shows-bot-name.spec.ts # apps/desktop/e2e/bot-roster-group-row-organisation.spec.ts # apps/desktop/e2e/bot-roster-ignores-infra-dirs.spec.ts # apps/desktop/e2e/bot-roster-timestamp-meta.spec.ts # apps/desktop/e2e/bot-roster-user-sections.spec.ts # apps/desktop/e2e/bot-routines-pane-narrow.spec.ts # apps/desktop/e2e/bot-row-open-recent-session.spec.ts # apps/desktop/e2e/bot-tile-ignores-ambient-composer-model.spec.ts # apps/desktop/e2e/group-composer-auto-grow.spec.ts # apps/desktop/e2e/group-create-gate-remote-roster.spec.ts # apps/desktop/e2e/group-prompt-renamed-primary-handle.spec.ts # apps/desktop/e2e/hosted-room-backend-continuity.spec.ts # apps/desktop/e2e/hosted-room-legacy-store-migration.spec.ts # apps/desktop/e2e/settings-scope-chips-bot-title.spec.ts # apps/desktop/e2e/worktree-branch-status.spec.ts # apps/desktop/electron/backend-probes.test.ts # apps/desktop/electron/connection-apply.test.ts # apps/desktop/electron/desktop-electron-pin.test.ts # apps/desktop/electron/desktop-uninstall.test.ts # apps/desktop/electron/gateway-file-download-transport.test.ts # apps/desktop/electron/gateway-stop-before-update.test.ts # apps/desktop/electron/github-api-auth.test.ts # apps/desktop/electron/registry-primary-profile-scope.test.ts # apps/desktop/electron/update-api-check.test.ts # apps/desktop/electron/update-handoff-marker.test.ts # apps/desktop/electron/venv-blocker-scan.test.ts # apps/desktop/scripts/after-extract.test.mjs # apps/desktop/scripts/local-pack-publish.test.mjs # apps/desktop/scripts/tasks-scroll.test.mjs # apps/desktop/src/app/settings/model-settings.test.tsx # apps/desktop/src/app/updates-overlay.blockers.test.tsx # apps/desktop/src/components/desktop-install-overlay.test.tsx # apps/desktop/src/lib/update-copy.test.ts # scripts/ci/check_os_marker_fakes.py # tests-js/desktop-mac-usage-descriptions.test.ts # tests-js/node-engine-alignment.test.ts # tests/agent/lsp/test_install_and_lint_fixes.py # tests/agent/test_command_token_source.py # tests/agent/test_compression_boundary_hook.py # tests/agent/test_create_openai_client_ssl_verify.py # tests/agent/test_custom_provider_ca_probes.py # tests/agent/test_endpoint_blackhole.py # tests/agent/test_estimator_parity.py # tests/agent/test_in_place_compaction.py # tests/agent/test_moa_loop_mode.py # tests/agent/test_model_metadata.py # tests/agent/test_skill_session_platform_gate.py # tests/agent/test_skill_utils.py # tests/agent/test_ssl_ca_guard.py # tests/computer_use/test_doctor.py # tests/cron/test_codex_execution_paths.py # tests/cron/test_cron_bot_chat_delivery.py # tests/cron/test_cron_script.py # tests/cron/test_media_delivery_parity.py # tests/cron/test_misfire_catchup.py # tests/cron/test_parallel_pool.py # tests/cron/test_recurring_eagain_redispatch.py # tests/gateway/test_choice_picker.py # tests/gateway/test_control_socket_windows_live.py # tests/gateway/test_dingtalk.py # tests/gateway/test_feishu.py # tests/gateway/test_feishu_onboard.py # tests/gateway/test_gateway_shutdown.py # tests/gateway/test_matrix.py # tests/gateway/test_model_command_custom_providers.py # tests/gateway/test_reasoning_command.py # tests/gateway/test_runtime_footer.py # tests/gateway/test_session.py # tests/gateway/test_session_hygiene.py # tests/gateway/test_status.py # tests/gateway/test_teams.py # tests/gateway/test_turn_lease.py # tests/gateway/test_whatsapp_connect.py # tests/hermes_cli/test_approvals_command.py # tests/hermes_cli/test_auth_store_lock_concurrent.py # tests/hermes_cli/test_backup.py # tests/hermes_cli/test_banner_git_state.py # tests/hermes_cli/test_certifi_repair.py # tests/hermes_cli/test_cmd_update.py # tests/hermes_cli/test_compat_manifest_targets.py # tests/hermes_cli/test_computer_use_cli.py # tests/hermes_cli/test_cpr_local_leak.py # tests/hermes_cli/test_dashboard_auth_gate.py # tests/hermes_cli/test_dashboard_procs_kill_grace.py # tests/hermes_cli/test_desktop_lifecycle_windows_live.py # tests/hermes_cli/test_doctor.py # tests/hermes_cli/test_doctor_command_install.py # tests/hermes_cli/test_fleet_config_migration_windows_live.py # tests/hermes_cli/test_gateway.py # tests/hermes_cli/test_gateway_platform_gating.py # tests/hermes_cli/test_gateway_restart_loop.py # tests/hermes_cli/test_gateway_task_probe.py # tests/hermes_cli/test_gateway_wsl.py # tests/hermes_cli/test_gui_command.py # tests/hermes_cli/test_install_cua_driver.py # tests/hermes_cli/test_kanban_db.py # tests/hermes_cli/test_lazy_command_exports.py # tests/hermes_cli/test_lazy_refresh_venv_repair.py # tests/hermes_cli/test_linux_desktop_entry.py # tests/hermes_cli/test_local_runtime.py # tests/hermes_cli/test_local_runtime_updates.py # tests/hermes_cli/test_managed_uv.py # tests/hermes_cli/test_mcp_reload_confirm_gate.py # tests/hermes_cli/test_nous_subscription.py # tests/hermes_cli/test_npm_engine.py # tests/hermes_cli/test_personality_none.py # tests/hermes_cli/test_pet_toggle.py # tests/hermes_cli/test_plan_reconciliation_windows_live.py # tests/hermes_cli/test_plugin_event_bus.py # tests/hermes_cli/test_plugin_manifest_v2.py # tests/hermes_cli/test_plugin_packs.py # tests/hermes_cli/test_plugins_cmd.py # tests/hermes_cli/test_plugins_cmd_enable_disable_nested.py # tests/hermes_cli/test_process_identity.py # tests/hermes_cli/test_profiles.py # tests/hermes_cli/test_profiles_sidebar_cache.py # tests/hermes_cli/test_pty_bridge.py # tests/hermes_cli/test_resolve_turn_limit.py # tests/hermes_cli/test_serve_runtime_inventory.py # tests/hermes_cli/test_session_vacuum_config.py # tests/hermes_cli/test_set_config_value.py # tests/hermes_cli/test_signal_handler_kanban_worker.py # tests/hermes_cli/test_slash_confirm_windows.py # tests/hermes_cli/test_stale_pid_guard.py # tests/hermes_cli/test_startup_fast_guards.py # tests/hermes_cli/test_status.py # tests/hermes_cli/test_telegram_managed_bot.py # tests/hermes_cli/test_tools_config.py # tests/hermes_cli/test_update_apply_shallow_count.py # tests/hermes_cli/test_update_autostash.py # tests/hermes_cli/test_update_concurrent_quarantine.py # tests/hermes_cli/test_update_fetch_failure_classifier.py # tests/hermes_cli/test_update_fleet_probe_resume_token.py # tests/hermes_cli/test_update_handoff_backend_reap.py # tests/hermes_cli/test_update_handoff_desktop_rebuild.py # tests/hermes_cli/test_update_head_moved_gate.py # tests/hermes_cli/test_update_host_obligation.py # tests/hermes_cli/test_update_import_guard.py # tests/hermes_cli/test_update_interrupted_recovery.py # tests/hermes_cli/test_update_inventory.py # tests/hermes_cli/test_update_launchd_unloaded_gateway.py # tests/hermes_cli/test_update_missing_configured_deps.py # tests/hermes_cli/test_update_modified_notice.py # tests/hermes_cli/test_update_multiplex_migration_hook.py # tests/hermes_cli/test_update_no_gateway_restart.py # tests/hermes_cli/test_update_orphan_backend_reap.py # tests/hermes_cli/test_update_parked_branch_guard.py # tests/hermes_cli/test_update_post_pull_syntax_guard.py # tests/hermes_cli/test_update_receipt.py # tests/hermes_cli/test_update_self_lock.py # tests/hermes_cli/test_update_shim_fail_closed.py # tests/hermes_cli/test_update_shim_self_lock.py # tests/hermes_cli/test_update_sqlite_remediation.py # tests/hermes_cli/test_update_stale_dashboard.py # tests/hermes_cli/test_update_stale_virtualenv.py # tests/hermes_cli/test_update_venv_health.py # tests/hermes_cli/test_update_venv_ownership_preflight.py # tests/hermes_cli/test_update_wedged_gateway.py # tests/hermes_cli/test_update_yes_flag.py # tests/hermes_cli/test_update_zip_two_phase.py # tests/hermes_cli/test_urllib_security.py # tests/hermes_cli/test_ux_messages_auth_config.py # tests/hermes_cli/test_ux_messages_startup.py # tests/hermes_cli/test_venv_holder_classifier.py # tests/hermes_cli/test_verify_console_scripts.py # tests/hermes_cli/test_verify_core_dependencies.py # tests/hermes_cli/test_web_server.py # tests/hermes_cli/test_web_server_console_ws.py # tests/hermes_cli/test_web_server_ws_ping.py # tests/hermes_cli/test_web_ui_build.py # tests/hermes_state/test_fts_rebuild_admission.py # tests/hermes_state/test_hermes_state.py # tests/plugins/memory/test_memory_lazy_install.py # tests/plugins/test_google_meet_plugin.py # tests/plugins/test_langfuse_plugin.py # tests/plugins/test_security_guidance_plugin.py # tests/plugins/test_transform_llm_output_hook.py # tests/scripts/desktop_update/test_desktop_update_windows_gateway_flag.py # tests/scripts/desktop_update/test_desktop_update_windows_python_handoff.py # tests/scripts/desktop_update/test_desktop_update_windows_timestamp.py # tests/scripts/install/test_install_clone_throttle_fallback.py # tests/scripts/install/test_install_lockfile_churn.py # tests/scripts/install/test_install_no_initial_commit.py # tests/scripts/install/test_install_sh_browser_install.py # tests/scripts/install/test_install_sh_node_prerelease.py # tests/scripts/install/test_install_sh_symlink_stomp.py # tests/scripts/install/test_install_sh_uv_lock_config.py # tests/scripts/install/test_install_unmerged_index.py # tests/scripts/test_contributor_map.py # tests/scripts/test_run_tests_parallel.py # tests/skills/test_competitor_news_monitor_skill.py # tests/skills/test_document_to_action_items_skill.py # tests/skills/test_google_workspace_setup.py # tests/skills/test_google_workspace_setup_deps.py # tests/skills/test_grounded_citations_skill.py # tests/skills/test_ip_as_logo_skill.py # tests/skills/test_live_dashboard_skill.py # tests/skills/test_mcp_oauth_remote_gateway_skill.py # tests/skills/test_office_document_skills.py # tests/skills/test_openclaw_migration.py # tests/skills/test_product_price_monitor_skill.py # tests/skills/test_scrollcraft_skill.py # tests/skills/test_setup_wizard_generator_skill.py # tests/skills/test_weekly_review_planning_skill.py # tests/test_engines_satisfiable.py # tests/test_fast_safe_load.py # tests/test_hermes_bootstrap.py # tests/test_hermes_constants.py # tests/test_hermes_logging.py # tests/test_managed_runtime_resolution.py # tests/test_model_tools_async_bridge.py # tests/test_packaging_build_guard.py # tests/test_packaging_metadata.py # tests/test_yaml_indent_consistency.py # tests/tools/test_approval_timeout_overflow.py # tests/tools/test_base_environment.py # tests/tools/test_bot_mode_dm.py # tests/tools/test_browser_chromium_check.py # tests/tools/test_browser_hardening.py # tests/tools/test_browser_homebrew_paths.py # tests/tools/test_browser_npx_warmup.py # tests/tools/test_browser_orphan_reaper.py # tests/tools/test_browser_real_profile.py # tests/tools/test_browser_use_cli.py # tests/tools/test_clipboard.py # tests/tools/test_code_execution.py # tests/tools/test_code_execution_modes.py # tests/tools/test_code_execution_windows_env.py # tests/tools/test_computer_use.py # tests/tools/test_delegate_liveness_timeout.py # tests/tools/test_execute_code_approval_cluster.py # tests/tools/test_execution_flag_detection.py # tests/tools/test_fal_common.py # tests/tools/test_file_operations.py # tests/tools/test_file_tools.py # tests/tools/test_file_tools_cwd_resolution.py # tests/tools/test_file_tools_live.py # tests/tools/test_lazy_deps.py # tests/tools/test_lazy_deps_durable_target.py # tests/tools/test_lazy_deps_managed.py # tests/tools/test_local_env_blocklist.py # tests/tools/test_local_tempdir.py # tests/tools/test_macos_protected_search.py # tests/tools/test_mcp_npx_cached_bin.py # tests/tools/test_oneshot_completion_linger.py # tests/tools/test_process_registry.py # tests/tools/test_read_file_schema_gating.py # tests/tools/test_skill_improvements.py # tests/tools/test_skills_sync.py # tests/tools/test_termux_api_detection.py # tests/tools/test_tirith_security.py # tests/tools/test_transcription_tools.py # tests/tools/test_tts_streaming.py # tests/tools/test_wake_word.py # tests/tui_gateway/test_compute_host_borrowed_lease.py # tests/tui_gateway/test_compute_host_turn_protocol.py # tests/tui_gateway/test_isolated_orphan_activity.py # tests/tui_gateway/test_protocol.py # tests/tui_gateway/test_slash_worker_profile_home.py # tests/tui_gateway/test_subprocess_encoding.py # tests/tui_gateway/test_tui_gateway_server.py # ui-tui/src/__tests__/terminalParity.test.ts # ui-tui/src/__tests__/termuxComposerLayout.test.ts # ui-tui/src/__tests__/textInputFastEcho.test.ts
719 lines
32 KiB
Python
719 lines
32 KiB
Python
"""Tests for Lightpanda engine support in browser_tool.py."""
|
|
|
|
import json
|
|
import os
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from tools import browser_tool_lifecycle as bt_lifecycle
|
|
from tools import browser_tool_lightpanda_fallback as bt_lightpanda_fallback
|
|
from tools import browser_tool_session as bt_session
|
|
from tools import browser_tool_install as bt_install
|
|
from tools import browser_tool_cloud as bt_cloud
|
|
from tools import browser_tool_cdp as bt_cdp
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _reset_engine_cache():
|
|
"""Reset the module-level engine cache so tests start clean."""
|
|
import tools.browser_tool as bt
|
|
bt._cached_browser_engine = None
|
|
bt._browser_engine_resolved = False
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_engine_cache():
|
|
"""Reset engine cache before and after each test."""
|
|
_reset_engine_cache()
|
|
yield
|
|
_reset_engine_cache()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _get_browser_engine
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestGetBrowserEngine:
|
|
"""Test engine resolution from config and env vars."""
|
|
|
|
|
|
def test_config_lightpanda(self):
|
|
"""Config browser.engine = 'lightpanda' is respected."""
|
|
from tools.browser_tool_cloud import _get_browser_engine
|
|
cfg = {"browser": {"engine": "lightpanda"}}
|
|
with patch("hermes_cli.config.read_raw_config", return_value=cfg):
|
|
assert _get_browser_engine() == "lightpanda"
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _should_inject_engine
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestShouldInjectEngine:
|
|
"""Test whether --engine flag is injected based on mode."""
|
|
|
|
def test_auto_never_injects(self):
|
|
from tools.browser_tool_cloud import _should_inject_engine
|
|
assert _should_inject_engine("auto") is False
|
|
|
|
def test_lightpanda_injects_in_local_mode(self):
|
|
from tools.browser_tool_cloud import _should_inject_engine
|
|
with patch("tools.browser_tool._is_camofox_mode", return_value=False), \
|
|
patch("tools.browser_tool_cdp._get_cdp_override", return_value=""), \
|
|
patch("tools.browser_tool_cloud._get_cloud_provider", return_value=None):
|
|
assert _should_inject_engine("lightpanda") is True
|
|
|
|
def test_chrome_injects_in_local_mode(self):
|
|
from tools.browser_tool_cloud import _should_inject_engine
|
|
with patch("tools.browser_tool._is_camofox_mode", return_value=False), \
|
|
patch("tools.browser_tool_cdp._get_cdp_override", return_value=""), \
|
|
patch("tools.browser_tool_cloud._get_cloud_provider", return_value=None):
|
|
assert _should_inject_engine("chrome") is True
|
|
|
|
def test_no_inject_in_camofox_mode(self):
|
|
from tools.browser_tool_cloud import _should_inject_engine
|
|
with patch("tools.browser_tool._is_camofox_mode", return_value=True):
|
|
assert _should_inject_engine("lightpanda") is False
|
|
|
|
def test_no_inject_with_cdp_override(self):
|
|
from tools.browser_tool_cloud import _should_inject_engine
|
|
with patch("tools.browser_tool._is_camofox_mode", return_value=False), \
|
|
patch("tools.browser_tool_cdp._get_cdp_override_raw", return_value="ws://localhost:9222"):
|
|
assert _should_inject_engine("lightpanda") is False
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _needs_lightpanda_fallback
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestNeedsLightpandaFallback:
|
|
"""Test fallback detection for Lightpanda results."""
|
|
|
|
def test_non_lightpanda_never_falls_back(self):
|
|
from tools.browser_tool_lightpanda_fallback import _needs_lightpanda_fallback
|
|
result = {"success": False, "error": "timeout"}
|
|
assert _needs_lightpanda_fallback("chrome", "open", result) is False
|
|
assert _needs_lightpanda_fallback("auto", "open", result) is False
|
|
|
|
def test_failed_command_triggers_fallback(self):
|
|
from tools.browser_tool_lightpanda_fallback import _needs_lightpanda_fallback
|
|
result = {"success": False, "error": "page.goto: Timeout"}
|
|
assert _needs_lightpanda_fallback("lightpanda", "open", result) is True
|
|
|
|
|
|
def test_empty_snapshot_triggers_fallback(self):
|
|
from tools.browser_tool_lightpanda_fallback import _needs_lightpanda_fallback
|
|
result = {"success": True, "data": {"snapshot": ""}}
|
|
assert _needs_lightpanda_fallback("lightpanda", "snapshot", result) is True
|
|
|
|
|
|
def test_unknown_command_does_not_trigger_fallback(self):
|
|
"""Commands not in the whitelist should not trigger fallback."""
|
|
from tools.browser_tool_lightpanda_fallback import _needs_lightpanda_fallback
|
|
result = {"success": False, "error": "nope"}
|
|
assert _needs_lightpanda_fallback("lightpanda", "some_future_cmd", result) is False
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Config integration
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TestLightpandaRequirements:
|
|
"""Lightpanda should expose browser tools without local Chromium."""
|
|
|
|
def test_lightpanda_local_mode_does_not_require_chromium(self):
|
|
|
|
with patch("tools.browser_tool._is_camofox_mode", return_value=False), \
|
|
patch("tools.browser_tool_cdp._get_cdp_override", return_value=""), \
|
|
patch("tools.browser_tool_install._find_agent_browser", return_value="/usr/bin/agent-browser"), \
|
|
patch("tools.browser_tool_cloud._get_cloud_provider", return_value=None), \
|
|
patch("tools.browser_tool_cloud._get_browser_engine", return_value="lightpanda"), \
|
|
patch("tools.browser_tool_install._chromium_installed", return_value=False):
|
|
assert bt_install.check_browser_requirements() is True
|
|
|
|
def test_chrome_local_mode_still_requires_chromium(self):
|
|
|
|
with patch("tools.browser_tool._is_camofox_mode", return_value=False), \
|
|
patch("tools.browser_tool_cdp._get_cdp_override", return_value=""), \
|
|
patch("tools.browser_tool_install._find_agent_browser", return_value="/usr/bin/agent-browser"), \
|
|
patch("tools.browser_tool_cloud._get_cloud_provider", return_value=None), \
|
|
patch("tools.browser_tool_cloud._get_browser_engine", return_value="auto"), \
|
|
patch("tools.browser_tool_install._chromium_installed", return_value=False):
|
|
assert bt_install.check_browser_requirements() is False
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# cleanup_all_browsers resets engine cache
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Chrome fallback behavior
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestChromeFallback:
|
|
"""Chrome fallback must hand off from Lightpanda without leaking engine policy."""
|
|
|
|
|
|
def test_chrome_fallback_injects_required_sandbox_args(self, tmp_path):
|
|
|
|
captured_envs = []
|
|
mock_proc = MagicMock()
|
|
mock_proc.wait.return_value = None
|
|
mock_proc.returncode = 1
|
|
|
|
def capture_popen(_cmd, **kwargs):
|
|
captured_envs.append(kwargs["env"])
|
|
return mock_proc
|
|
|
|
# Keep the fallback's socket dir under this test's private tmp_path.
|
|
# Using the real shared tmpdir raced concurrent orphan reapers from
|
|
# sibling pytest processes (atexit _emergency_cleanup_all_sessions),
|
|
# which rmtree'd the fresh pidless dir mid-command — the CI flake
|
|
# this test kept hitting before the reaper grace fix.
|
|
with patch("tools.browser_tool_session._run_browser_command", return_value={
|
|
"success": True, "data": {"url": "https://example.com/"}
|
|
}), \
|
|
patch("tools.browser_tool._socket_safe_tmpdir", return_value=str(tmp_path)), \
|
|
patch("tools.browser_tool_install._find_agent_browser", return_value="/usr/bin/agent-browser"), \
|
|
patch("tools.browser_tool_install._chromium_installed", return_value=True), \
|
|
patch("tools.browser_tool_session._needs_chromium_sandbox_bypass", return_value=True), \
|
|
patch("subprocess.Popen", side_effect=capture_popen):
|
|
result = bt_lightpanda_fallback._run_chrome_fallback_command(
|
|
"task1", "screenshot", [], timeout=30
|
|
)
|
|
|
|
assert result["success"] is False
|
|
assert captured_envs
|
|
assert all(
|
|
env.get("AGENT_BROWSER_ARGS") == "--no-sandbox,--disable-dev-shm-usage"
|
|
for env in captured_envs
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# fallback warning annotation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestLightpandaFallbackWarning:
|
|
"""Verify Chrome fallback results are annotated for users."""
|
|
|
|
def test_fallback_result_gets_user_visible_warning(self):
|
|
from tools.browser_tool_lightpanda_fallback import _annotate_lightpanda_fallback
|
|
|
|
result = {"success": True, "data": {"snapshot": "- heading \"Hello\" [ref=e1]"}}
|
|
annotated = _annotate_lightpanda_fallback(
|
|
result,
|
|
"Lightpanda returned an empty/too-short snapshot; retried with Chrome.",
|
|
)
|
|
|
|
assert annotated["browser_engine"] == "chrome"
|
|
assert "Lightpanda fallback" in annotated["fallback_warning"]
|
|
assert annotated["browser_engine_fallback"] == {
|
|
"from": "lightpanda",
|
|
"to": "chrome",
|
|
"reason": "Lightpanda returned an empty/too-short snapshot; retried with Chrome.",
|
|
}
|
|
assert annotated["data"]["fallback_warning"] == annotated["fallback_warning"]
|
|
assert annotated["data"]["browser_engine"] == "chrome"
|
|
|
|
|
|
def test_browser_navigate_surfaces_fallback_warning(self):
|
|
import json
|
|
import tools.browser_tool as bt
|
|
|
|
result = bt_lightpanda_fallback._annotate_lightpanda_fallback(
|
|
{"success": True, "data": {"title": "Fallback OK", "url": "https://example.com/"}},
|
|
"synthetic Lightpanda failure; retried with Chrome.",
|
|
)
|
|
|
|
with patch("tools.browser_tool_cloud._is_local_backend", return_value=True), \
|
|
patch("tools.browser_tool_cloud._get_cloud_provider", return_value=None), \
|
|
patch("tools.browser_tool_session._get_session_info", return_value={
|
|
"session_name": "test", "_first_nav": False, "features": {"local": True, "proxies": True}
|
|
}), \
|
|
patch("tools.browser_tool_session._run_browser_command", side_effect=[
|
|
result,
|
|
{"success": True, "data": {"snapshot": "- heading \"Fallback OK\" [ref=e1]", "refs": {"e1": {}}}},
|
|
]):
|
|
response = json.loads(bt.browser_navigate("https://example.com", task_id="warn-test"))
|
|
|
|
assert response["success"] is True
|
|
assert response["browser_engine"] == "chrome"
|
|
assert "Lightpanda fallback" in response["fallback_warning"]
|
|
assert response["browser_engine_fallback"]["from"] == "lightpanda"
|
|
assert response["browser_engine_fallback"]["to"] == "chrome"
|
|
bt._last_active_session_key.pop("warn-test", None)
|
|
|
|
|
|
def test_browser_vision_lightpanda_response_has_structured_fallback(self, tmp_path):
|
|
import json
|
|
import tools.browser_tool as bt
|
|
|
|
chrome_shot = tmp_path / "chrome-structured.png"
|
|
chrome_shot.write_bytes(b"\x89PNG" + b"0" * 128)
|
|
|
|
class _Msg:
|
|
content = "Example Domain screenshot"
|
|
|
|
class _Choice:
|
|
message = _Msg()
|
|
|
|
class _Response:
|
|
choices = [_Choice()]
|
|
|
|
with patch("tools.browser_tool_cloud._get_browser_engine", return_value="lightpanda"), \
|
|
patch("tools.browser_tool_cloud._should_inject_engine", return_value=True), \
|
|
patch("tools.browser_tool_lightpanda_fallback._chrome_fallback_screenshot", return_value={
|
|
"success": True, "data": {"path": str(chrome_shot)}
|
|
}), \
|
|
patch("hermes_constants.get_hermes_dir", return_value=tmp_path), \
|
|
patch("agent.auxiliary_client.call_llm", return_value=_Response()):
|
|
response = json.loads(bt.browser_vision("what is this?", task_id="vision-structured"))
|
|
|
|
assert response["success"] is True
|
|
assert response["browser_engine"] == "chrome"
|
|
assert response["browser_engine_fallback"]["from"] == "lightpanda"
|
|
assert response["browser_engine_fallback"]["to"] == "chrome"
|
|
assert response["browser_engine_fallback"]["reason"]
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _engine_override parameter
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestEngineOverride:
|
|
"""Verify _engine_override bypasses the cached engine."""
|
|
|
|
@patch("tools.browser_tool_session._get_session_info")
|
|
@patch("tools.browser_tool_install._find_agent_browser", return_value="/usr/bin/agent-browser")
|
|
@patch("tools.browser_tool_cloud._is_local_mode", return_value=True)
|
|
@patch("tools.browser_tool_install._chromium_installed", return_value=True)
|
|
@patch("tools.browser_tool_cloud._get_cloud_provider", return_value=None)
|
|
@patch("tools.browser_tool_cdp._get_cdp_override", return_value="")
|
|
@patch("tools.browser_tool._is_camofox_mode", return_value=False)
|
|
def test_override_prevents_engine_injection(
|
|
self, _camofox, _cdp, _cloud, _chromium, _local, _find, _session
|
|
):
|
|
"""When _engine_override='auto', --engine flag is NOT injected."""
|
|
import tools.browser_tool as bt
|
|
|
|
# Set the global cache to lightpanda
|
|
bt._cached_browser_engine = "lightpanda"
|
|
bt._browser_engine_resolved = True
|
|
|
|
_session.return_value = {"session_name": "test-sess"}
|
|
|
|
# Track the cmd_parts that Popen receives
|
|
captured_cmds = []
|
|
mock_proc = MagicMock()
|
|
mock_proc.wait.return_value = None
|
|
mock_proc.returncode = 0
|
|
|
|
def capture_popen(cmd, **kwargs):
|
|
captured_cmds.append(cmd)
|
|
return mock_proc
|
|
|
|
# We need to mock the file operations too
|
|
with patch("subprocess.Popen", side_effect=capture_popen), \
|
|
patch("os.open", return_value=99), \
|
|
patch("os.close"), \
|
|
patch("os.unlink"), \
|
|
patch("os.makedirs"), \
|
|
patch("builtins.open", MagicMock(return_value=MagicMock(
|
|
__enter__=MagicMock(return_value=MagicMock(read=MagicMock(return_value='{"success": true, "data": {}}'))),
|
|
__exit__=MagicMock(return_value=False),
|
|
))), \
|
|
patch("tools.interrupt.is_interrupted", return_value=False), \
|
|
patch("tools.browser_tool_lifecycle._write_owner_pid"):
|
|
bt_session._run_browser_command("task1", "snapshot", [], _engine_override="auto")
|
|
|
|
# Should NOT contain "--engine" since override is "auto"
|
|
assert len(captured_cmds) == 1
|
|
assert "--engine" not in captured_cmds[0]
|
|
|
|
@patch("tools.browser_tool_session._get_session_info")
|
|
@patch("tools.browser_tool_install._find_agent_browser", return_value="/usr/bin/agent-browser")
|
|
@patch("tools.browser_tool_cloud._is_local_mode", return_value=True)
|
|
@patch("tools.browser_tool_install._chromium_installed", return_value=True)
|
|
@patch("tools.browser_tool_cloud._get_cloud_provider", return_value=None)
|
|
@patch("tools.browser_tool_cdp._get_cdp_override", return_value="")
|
|
@patch("tools.browser_tool._is_camofox_mode", return_value=False)
|
|
def test_no_override_uses_cached_engine(
|
|
self, _camofox, _cdp, _cloud, _chromium, _local, _find, _session
|
|
):
|
|
"""Lightpanda gets neither auto-injected nor inherited Chrome arguments."""
|
|
import tools.browser_tool as bt
|
|
|
|
bt._cached_browser_engine = "lightpanda"
|
|
bt._browser_engine_resolved = True
|
|
|
|
_session.return_value = {"session_name": "test-sess"}
|
|
|
|
captured_cmds = []
|
|
captured_envs = []
|
|
mock_proc = MagicMock()
|
|
mock_proc.wait.return_value = None
|
|
mock_proc.returncode = 0
|
|
|
|
def capture_popen(cmd, **kwargs):
|
|
captured_cmds.append(cmd)
|
|
captured_envs.append(kwargs["env"])
|
|
return mock_proc
|
|
|
|
# Return a substantive snapshot so the LP fallback does NOT trigger.
|
|
mock_stdout = '{"success": true, "data": {"snapshot": "- heading \\"Hello\\" [ref=e1]", "refs": {"e1": {}}}}'
|
|
with patch("subprocess.Popen", side_effect=capture_popen), \
|
|
patch("os.open", return_value=99), \
|
|
patch("os.close"), \
|
|
patch("os.unlink"), \
|
|
patch("os.makedirs"), \
|
|
patch("builtins.open", MagicMock(return_value=MagicMock(
|
|
__enter__=MagicMock(return_value=MagicMock(read=MagicMock(return_value=mock_stdout))),
|
|
__exit__=MagicMock(return_value=False),
|
|
))), \
|
|
patch("tools.interrupt.is_interrupted", return_value=False), \
|
|
patch("tools.browser_tool_session._needs_chromium_sandbox_bypass", return_value=True), \
|
|
patch("tools.browser_tool_lifecycle._write_owner_pid"), \
|
|
patch.dict(os.environ, {}, clear=False):
|
|
# AppArmor/root detection would normally auto-inject Chromium args.
|
|
bt_session._run_browser_command("task1", "snapshot", [])
|
|
|
|
# User-supplied current and legacy Chromium knobs must also be removed.
|
|
with patch.dict(os.environ, {
|
|
"AGENT_BROWSER_ARGS": "--no-sandbox",
|
|
"AGENT_BROWSER_CHROME_FLAGS": "--disable-dev-shm-usage",
|
|
}):
|
|
bt_session._run_browser_command("task1", "snapshot", [])
|
|
|
|
assert len(captured_cmds) == 2
|
|
for command, environment in zip(captured_cmds, captured_envs):
|
|
assert "--engine" in command
|
|
engine_idx = command.index("--engine")
|
|
assert command[engine_idx + 1] == "lightpanda"
|
|
assert "AGENT_BROWSER_ARGS" not in environment
|
|
assert "AGENT_BROWSER_CHROME_FLAGS" not in environment
|
|
|
|
def test_hybrid_local_sidecar_injects_engine_even_with_cloud_provider(self):
|
|
"""A task::local sidecar is local even when global cloud config exists."""
|
|
import tools.browser_tool as bt
|
|
|
|
bt._cached_browser_engine = "lightpanda"
|
|
bt._browser_engine_resolved = True
|
|
captured_cmds = []
|
|
mock_provider = MagicMock()
|
|
|
|
mock_proc = MagicMock()
|
|
mock_proc.wait.return_value = None
|
|
mock_proc.returncode = 0
|
|
|
|
def capture_popen(cmd, **kwargs):
|
|
captured_cmds.append(cmd)
|
|
return mock_proc
|
|
|
|
mock_stdout = json.dumps({
|
|
"success": True,
|
|
"data": {"snapshot": '- heading "Hello" [ref=e1]', "refs": {"e1": {}}},
|
|
})
|
|
with patch("tools.browser_tool_session._get_session_info", return_value={"session_name": "local-sidecar"}), \
|
|
patch("tools.browser_tool_install._find_agent_browser", return_value="/usr/bin/agent-browser"), \
|
|
patch("tools.browser_tool_cloud._is_local_mode", return_value=False), \
|
|
patch("tools.browser_tool_install._chromium_installed", return_value=True), \
|
|
patch("tools.browser_tool_cloud._get_cloud_provider", return_value=mock_provider), \
|
|
patch("tools.browser_tool_cdp._get_cdp_override", return_value=""), \
|
|
patch("tools.browser_tool._is_camofox_mode", return_value=False), \
|
|
patch("subprocess.Popen", side_effect=capture_popen), \
|
|
patch("os.open", return_value=99), \
|
|
patch("os.close"), \
|
|
patch("os.unlink"), \
|
|
patch("os.makedirs"), \
|
|
patch("builtins.open", MagicMock(return_value=MagicMock(
|
|
__enter__=MagicMock(return_value=MagicMock(read=MagicMock(return_value=mock_stdout))),
|
|
__exit__=MagicMock(return_value=False),
|
|
))), \
|
|
patch("tools.interrupt.is_interrupted", return_value=False), \
|
|
patch("tools.browser_tool_lifecycle._write_owner_pid"):
|
|
bt_session._run_browser_command("task::local", "snapshot", [])
|
|
|
|
assert len(captured_cmds) == 1
|
|
assert "--engine" in captured_cmds[0]
|
|
assert captured_cmds[0][captured_cmds[0].index("--engine") + 1] == "lightpanda"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# lightpanda_engine_status — is the engine in effect, or shadowed?
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestLightpandaEngineStatus:
|
|
def _gates(self, monkeypatch, **overrides):
|
|
import tools.browser_tool as bt
|
|
|
|
gates = dict(
|
|
_using_lightpanda_engine=lambda: True,
|
|
_get_cdp_override_raw=lambda: "",
|
|
_is_camofox_mode=lambda: False,
|
|
_get_cloud_provider=lambda: None,
|
|
_is_browser_use_cli_mode=lambda: True,
|
|
_use_real_profile=lambda: False,
|
|
)
|
|
gates.update(overrides)
|
|
homes = {
|
|
"_using_lightpanda_engine": bt_lightpanda_fallback, "_get_cdp_override_raw": bt_cdp,
|
|
"_get_cloud_provider": bt_cloud, "_use_real_profile": bt_cloud,
|
|
}
|
|
for name, fn in gates.items():
|
|
monkeypatch.setattr(homes.get(name, bt), name, fn)
|
|
monkeypatch.setattr(
|
|
"tools.browser_use_cli.is_legacy_browser_use_cloud_config", lambda cfg: False
|
|
)
|
|
return bt
|
|
|
|
def test_not_lightpanda(self, monkeypatch):
|
|
self._gates(monkeypatch, _using_lightpanda_engine=lambda: False)
|
|
assert bt_lightpanda_fallback.lightpanda_engine_status() == (False, "")
|
|
|
|
def test_used_in_browser_use_mode(self, monkeypatch):
|
|
self._gates(monkeypatch)
|
|
used, reason = bt_lightpanda_fallback.lightpanda_engine_status()
|
|
assert used is True
|
|
assert "lightpanda serve" in reason
|
|
|
|
def test_used_with_builtin_tools(self, monkeypatch):
|
|
self._gates(monkeypatch, _is_browser_use_cli_mode=lambda: False)
|
|
used, reason = bt_lightpanda_fallback.lightpanda_engine_status()
|
|
assert used is True
|
|
assert "--engine lightpanda" in reason
|
|
|
|
def test_shadowed_by_cdp_override(self, monkeypatch):
|
|
self._gates(monkeypatch, _get_cdp_override_raw=lambda: "ws://x")
|
|
used, reason = bt_lightpanda_fallback.lightpanda_engine_status()
|
|
assert used is False and "CDP override" in reason
|
|
|
|
def test_shadowed_by_camofox(self, monkeypatch):
|
|
self._gates(monkeypatch, _is_camofox_mode=lambda: True)
|
|
used, reason = bt_lightpanda_fallback.lightpanda_engine_status()
|
|
assert used is False and "Camofox" in reason
|
|
|
|
def test_shadowed_by_cloud_provider(self, monkeypatch):
|
|
provider = MagicMock()
|
|
provider.display_name = "Browserbase"
|
|
self._gates(monkeypatch, _get_cloud_provider=lambda: provider)
|
|
used, reason = bt_lightpanda_fallback.lightpanda_engine_status()
|
|
assert used is False and "Browserbase" in reason
|
|
|
|
def test_shadowed_by_legacy_browser_use_cloud(self, monkeypatch):
|
|
self._gates(monkeypatch)
|
|
monkeypatch.setattr(
|
|
"tools.browser_use_cli.is_legacy_browser_use_cloud_config", lambda cfg: True
|
|
)
|
|
used, reason = bt_lightpanda_fallback.lightpanda_engine_status()
|
|
assert used is False and "Browser Use cloud" in reason
|
|
|
|
def test_shadowed_by_real_profile(self, monkeypatch):
|
|
self._gates(monkeypatch, _use_real_profile=lambda: True)
|
|
used, reason = bt_lightpanda_fallback.lightpanda_engine_status()
|
|
assert used is False and "use_real_profile" in reason
|
|
|
|
def test_real_profile_wins_over_cloud_provider(self, monkeypatch):
|
|
"""browser_exec resolves real-profile before the backend, so with
|
|
both set the real-profile toggle is the actual shadow."""
|
|
provider = MagicMock()
|
|
provider.display_name = "Browserbase"
|
|
self._gates(
|
|
monkeypatch,
|
|
_use_real_profile=lambda: True,
|
|
_get_cloud_provider=lambda: provider,
|
|
)
|
|
used, reason = bt_lightpanda_fallback.lightpanda_engine_status()
|
|
assert used is False and "use_real_profile" in reason
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Browser Use mode session lifecycle
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class _FakeServer:
|
|
def __init__(self, port=4321, alive=True):
|
|
self.port = port
|
|
self.cdp_url = f"http://127.0.0.1:{port}"
|
|
self._alive = alive
|
|
|
|
def is_alive(self):
|
|
return self._alive
|
|
|
|
|
|
class TestLightpandaSessionCreation:
|
|
def _common(self, monkeypatch, *, bu_mode=True, local_backend=True, launch=None):
|
|
import tools.browser_tool as bt
|
|
|
|
calls = []
|
|
|
|
def fake_launch(session_name, *, block_private_networks=False):
|
|
calls.append((session_name, block_private_networks))
|
|
if launch is not None:
|
|
return launch
|
|
return _FakeServer(), None
|
|
|
|
monkeypatch.setattr("tools.browser_tool_real_profile._real_profile_cdp", lambda: (None, None))
|
|
monkeypatch.setattr(bt, "_is_browser_use_cli_mode", lambda: bu_mode)
|
|
monkeypatch.setattr("tools.browser_tool_lightpanda_fallback._using_lightpanda_engine", lambda: True)
|
|
monkeypatch.setattr(bt_cloud, "_is_local_backend", lambda: local_backend)
|
|
monkeypatch.setattr("tools.browser_lightpanda.launch_lightpanda", fake_launch)
|
|
return bt, calls
|
|
|
|
def test_spawns_lightpanda_in_browser_use_mode(self, monkeypatch):
|
|
bt, calls = self._common(monkeypatch)
|
|
info = bt_session._create_local_session("task-1")
|
|
assert info["session_name"].startswith("lp_")
|
|
assert info["cdp_url"] == "http://127.0.0.1:4321"
|
|
assert info["features"] == {"local": True, "lightpanda": True}
|
|
assert info["bb_session_id"] is None
|
|
assert calls == [(info["session_name"], False)]
|
|
|
|
def test_blocks_private_networks_for_containerised_terminal(self, monkeypatch):
|
|
bt, calls = self._common(monkeypatch, local_backend=False)
|
|
bt_session._create_local_session("task-1")
|
|
assert calls[0][1] is True
|
|
|
|
def test_ignores_engine_outside_browser_use_mode(self, monkeypatch):
|
|
bt, calls = self._common(monkeypatch, bu_mode=False)
|
|
info = bt_session._create_local_session("task-1")
|
|
assert info["features"] == {"local": True}
|
|
assert info["cdp_url"] is None
|
|
assert calls == []
|
|
|
|
def test_launch_failure_raises(self, monkeypatch):
|
|
bt, _ = self._common(monkeypatch, launch=(None, "no lightpanda binary was found"))
|
|
with pytest.raises(RuntimeError, match="no lightpanda binary"):
|
|
bt_session._create_local_session("task-1")
|
|
|
|
|
|
class TestLightpandaSessionLifecycle:
|
|
def setup_method(self):
|
|
import tools.browser_tool as bt
|
|
|
|
self.bt = bt
|
|
self.orig_sessions = bt._active_sessions.copy()
|
|
self.orig_activity = bt._session_last_activity.copy()
|
|
self.orig_cleanup_done = bt._cleanup_done
|
|
bt._active_sessions.clear()
|
|
bt._session_last_activity.clear()
|
|
|
|
def teardown_method(self):
|
|
bt = self.bt
|
|
bt._active_sessions.clear()
|
|
bt._active_sessions.update(self.orig_sessions)
|
|
bt._session_last_activity.clear()
|
|
bt._session_last_activity.update(self.orig_activity)
|
|
bt._cleanup_done = self.orig_cleanup_done
|
|
|
|
def _seed(self, key="task-1", name="lp_dead"):
|
|
info = {
|
|
"session_name": name,
|
|
"bb_session_id": None,
|
|
"cdp_url": "http://127.0.0.1:1",
|
|
"features": {"local": True, "lightpanda": True},
|
|
}
|
|
self.bt._active_sessions[key] = info
|
|
self.bt._session_last_activity[key] = 1.0
|
|
return info
|
|
|
|
def test_dead_process_is_detected(self, monkeypatch):
|
|
info = self._seed()
|
|
monkeypatch.setattr("tools.browser_lightpanda.get_server", lambda name: None)
|
|
assert bt_session._local_backend_process_dead(info) is True
|
|
monkeypatch.setattr(
|
|
"tools.browser_lightpanda.get_server", lambda name: _FakeServer(alive=False)
|
|
)
|
|
assert bt_session._local_backend_process_dead(info) is True
|
|
monkeypatch.setattr("tools.browser_lightpanda.get_server", lambda name: _FakeServer())
|
|
assert bt_session._local_backend_process_dead(info) is False
|
|
assert bt_session._local_backend_process_dead({"features": {"local": True}}) is False
|
|
|
|
def test_get_session_info_respawns_dead_lightpanda(self, monkeypatch):
|
|
bt = self.bt
|
|
stale = self._seed()
|
|
fresh = {
|
|
"session_name": "lp_fresh",
|
|
"bb_session_id": None,
|
|
"cdp_url": "http://127.0.0.1:2",
|
|
"features": {"local": True, "lightpanda": True},
|
|
}
|
|
cleaned = []
|
|
|
|
def fake_cleanup(key):
|
|
cleaned.append(key)
|
|
bt._active_sessions.pop(key, None)
|
|
|
|
monkeypatch.setattr("tools.browser_tool_lifecycle._start_browser_cleanup_thread", lambda: None)
|
|
monkeypatch.setattr(
|
|
bt, "_browser_session_backend",
|
|
lambda key: MagicMock(ensure_healthy=lambda: True),
|
|
)
|
|
monkeypatch.setattr("tools.browser_lightpanda.get_server", lambda name: None)
|
|
monkeypatch.setattr(bt_lifecycle, "_cleanup_single_browser_session", fake_cleanup)
|
|
monkeypatch.setattr("tools.browser_tool_cdp._get_cdp_override", lambda: "")
|
|
monkeypatch.setattr(bt_cloud, "_get_cloud_provider", lambda: None)
|
|
monkeypatch.setattr("tools.browser_tool_session._create_local_session", lambda *a, **k: fresh)
|
|
supervised = []
|
|
monkeypatch.setattr("tools.browser_tool_cdp._ensure_cdp_supervisor", supervised.append)
|
|
|
|
info = bt_session._get_session_info("task-1")
|
|
assert cleaned == ["task-1"]
|
|
assert info["session_name"] == "lp_fresh"
|
|
assert bt._active_sessions["task-1"]["session_name"] == "lp_fresh"
|
|
assert info["session_name"] != stale["session_name"]
|
|
# Browser Use mode hides the browser_* tools that read supervisor
|
|
# state; a Lightpanda session never attaches one.
|
|
assert supervised == []
|
|
|
|
def test_cleanup_stops_lightpanda_without_agent_browser_close(self, monkeypatch):
|
|
bt = self.bt
|
|
self._seed()
|
|
stopped = []
|
|
monkeypatch.setattr("tools.browser_lightpanda.stop_lightpanda", stopped.append)
|
|
with patch("tools.browser_tool._maybe_stop_recording"), \
|
|
patch("tools.browser_tool_session._run_browser_command") as run, \
|
|
patch("tools.browser_tool.os.path.exists", return_value=False):
|
|
bt_lifecycle.cleanup_browser("task-1")
|
|
run.assert_not_called()
|
|
assert stopped == ["lp_dead"]
|
|
assert "task-1" not in bt._active_sessions
|
|
assert "task-1" not in bt._session_last_activity
|
|
|
|
def test_emergency_cleanup_stops_all_lightpanda(self, monkeypatch):
|
|
bt = self.bt
|
|
bt._cleanup_done = False
|
|
with patch("tools.browser_lightpanda.stop_all_lightpanda") as stop_all, \
|
|
patch("tools.browser_tool_real_profile._terminate_real_profile_chrome"), \
|
|
patch("tools.browser_tool_lifecycle.cleanup_all_browsers"), \
|
|
patch("tools.browser_tool_lifecycle._reap_orphaned_browser_sessions"):
|
|
bt_lifecycle._emergency_cleanup_all_sessions()
|
|
stop_all.assert_called_once()
|
|
|
|
def test_orphan_reaper_sweeps_lightpanda_records(self, tmp_path):
|
|
with patch("tools.browser_lightpanda.reap_orphaned_lightpanda") as reap, \
|
|
patch("tools.browser_tool._socket_safe_tmpdir", return_value=str(tmp_path)):
|
|
bt_lifecycle._reap_orphaned_browser_sessions()
|
|
reap.assert_called_once()
|
|
|
|
def test_orphan_reaper_survives_non_dict_record(self, tmp_path, monkeypatch):
|
|
"""A parseable-but-non-object state file is swept like an unreadable one —
|
|
never an AttributeError that wedges the reaper."""
|
|
from tools import browser_lightpanda
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
state_dir = browser_lightpanda._state_dir()
|
|
bad = state_dir / "broken.json"
|
|
bad.write_text('"not a record"', encoding="utf-8")
|
|
dead_owner = state_dir / "dead.json"
|
|
dead_owner.write_text(json.dumps({"owner_pid": 2**22 + 7}), encoding="utf-8")
|
|
assert browser_lightpanda.reap_orphaned_lightpanda() == 0
|
|
assert not bad.exists() and not dead_owner.exists()
|