from __future__ import annotations import importlib.util from pathlib import Path from types import ModuleType import pytest _VARIABLES = ( "AGENT_BROWSER_DATABASE_HOST", "AGENT_BROWSER_DATABASE_USER", "AGENT_BROWSER_DATABASE_PASSWORD", "AGENT_BROWSER_DATABASE_NAME", ) def _load_helper_module() -> ModuleType: path = ( Path(__file__).parents[1] / "agent" / "tools" / "builtin" / "browser" / "sync_mysql_help.py" ) spec = importlib.util.spec_from_file_location( "agent_browser_database_helper_test", path ) assert spec is not None and spec.loader is not None module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def test_legacy_browser_database_config_fails_closed( monkeypatch: pytest.MonkeyPatch, ) -> None: for name in _VARIABLES: monkeypatch.delenv(name, raising=False) module = _load_helper_module() helper = module.SyncMySQLHelper() helper._pool = None with pytest.raises( RuntimeError, match="legacy browser database configuration is missing" ): helper.get_pool() def test_legacy_browser_database_config_uses_environment( monkeypatch: pytest.MonkeyPatch, ) -> None: configured = { "AGENT_BROWSER_DATABASE_HOST": "db.example.invalid", "AGENT_BROWSER_DATABASE_USER": "unit-user", "AGENT_BROWSER_DATABASE_PASSWORD": "unit-test-only", "AGENT_BROWSER_DATABASE_NAME": "unit-database", } for name, value in configured.items(): monkeypatch.setenv(name, value) module = _load_helper_module() resolved = module._database_config() assert resolved["host"] == configured["AGENT_BROWSER_DATABASE_HOST"] assert resolved["user"] == configured["AGENT_BROWSER_DATABASE_USER"] assert resolved["password"] == configured["AGENT_BROWSER_DATABASE_PASSWORD"] assert resolved["database"] == configured["AGENT_BROWSER_DATABASE_NAME"]