import os import tempfile from unittest.mock import patch import pytest from PIL import Image from browser_harness import helpers def _run(fake_png, width, height, **kwargs): fake = lambda method, **_: {"data": fake_png(width, height)} with patch("browser_harness.helpers.cdp", side_effect=fake), tempfile.TemporaryDirectory() as d: path = os.path.join(d, "shot.png") helpers.capture_screenshot(path, **kwargs) return Image.open(path).size def test_max_dim_downsizes_oversized_image(fake_png): assert max(_run(fake_png, 4592, 2286, max_dim=1800)) == 1800 def test_max_dim_skips_when_image_already_small(fake_png): assert _run(fake_png, 800, 400, max_dim=1800) == (800, 400) def test_max_dim_default_is_no_resize(fake_png): assert _run(fake_png, 4592, 2286) == (4592, 2286) def _seed_skill(tmp_path): site = tmp_path / "domain-skills" / "example" site.mkdir(parents=True) (site / "scraping.md").write_text("hi") def test_goto_url_omits_domain_skills_by_default(tmp_path, monkeypatch): monkeypatch.delenv("BH_DOMAIN_SKILLS", raising=False) monkeypatch.setattr(helpers, "AGENT_WORKSPACE", tmp_path) _seed_skill(tmp_path) with patch("browser_harness.helpers.cdp", return_value={"frameId": "f"}): result = helpers.goto_url("https://www.example.com/") assert result == {"frameId": "f"} def test_goto_url_includes_domain_skills_when_enabled(tmp_path, monkeypatch): monkeypatch.setenv("BH_DOMAIN_SKILLS", "1") monkeypatch.setattr(helpers, "AGENT_WORKSPACE", tmp_path) _seed_skill(tmp_path) with patch("browser_harness.helpers.cdp", return_value={"frameId": "f"}): result = helpers.goto_url("https://www.example.com/") assert result == {"frameId": "f", "domain_skills": ["scraping.md"]} def test_page_info_raises_clear_error_on_js_exception(): def fake_send(req): return {} def fake_cdp(method, **kwargs): return { "result": { "type": "object", "subtype": "error", "description": "ReferenceError: location is not defined", }, "exceptionDetails": { "text": "Uncaught", "lineNumber": 0, "columnNumber": 16, }, } with patch("browser_harness.helpers._send", side_effect=fake_send), \ patch("browser_harness.helpers.cdp", side_effect=fake_cdp): with pytest.raises(RuntimeError, match="ReferenceError"): helpers.page_info()