|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
| 8 | +import hyperbrowser.tools as tools_module |
8 | 9 | from hyperbrowser.exceptions import HyperbrowserError |
9 | 10 | from hyperbrowser.tools import ( |
10 | 11 | BrowserUseTool, |
@@ -143,6 +144,26 @@ def data(self): |
143 | 144 | assert exc_info.value.original_error is not None |
144 | 145 |
|
145 | 146 |
|
| 147 | +def test_scrape_tool_wraps_declared_data_inspection_failures( |
| 148 | + monkeypatch: pytest.MonkeyPatch, |
| 149 | +): |
| 150 | + class _MissingDataResponse: |
| 151 | + pass |
| 152 | + |
| 153 | + def _raise_runtime_error(_obj: object, _attr: str): |
| 154 | + raise RuntimeError("attribute inspection exploded") |
| 155 | + |
| 156 | + monkeypatch.setattr(tools_module.inspect, "getattr_static", _raise_runtime_error) |
| 157 | + client = _SyncScrapeClient(_MissingDataResponse()) # type: ignore[arg-type] |
| 158 | + |
| 159 | + with pytest.raises( |
| 160 | + HyperbrowserError, match="Failed to inspect scrape tool response data field" |
| 161 | + ) as exc_info: |
| 162 | + WebsiteScrapeTool.runnable(client, {"url": "https://example.com"}) |
| 163 | + |
| 164 | + assert exc_info.value.original_error is not None |
| 165 | + |
| 166 | + |
146 | 167 | def test_scrape_tool_supports_mapping_response_objects(): |
147 | 168 | client = _SyncScrapeClient({"data": {"markdown": "from response mapping"}}) # type: ignore[arg-type] |
148 | 169 |
|
@@ -332,6 +353,27 @@ def markdown(self): |
332 | 353 | assert exc_info.value.original_error is not None |
333 | 354 |
|
334 | 355 |
|
| 356 | +def test_scrape_tool_wraps_declared_markdown_inspection_failures( |
| 357 | + monkeypatch: pytest.MonkeyPatch, |
| 358 | +): |
| 359 | + class _MissingMarkdownData: |
| 360 | + pass |
| 361 | + |
| 362 | + def _raise_runtime_error(_obj: object, _attr: str): |
| 363 | + raise RuntimeError("attribute inspection exploded") |
| 364 | + |
| 365 | + monkeypatch.setattr(tools_module.inspect, "getattr_static", _raise_runtime_error) |
| 366 | + client = _SyncScrapeClient(_Response(data=_MissingMarkdownData())) |
| 367 | + |
| 368 | + with pytest.raises( |
| 369 | + HyperbrowserError, |
| 370 | + match="Failed to inspect scrape tool response field 'markdown'", |
| 371 | + ) as exc_info: |
| 372 | + WebsiteScrapeTool.runnable(client, {"url": "https://example.com"}) |
| 373 | + |
| 374 | + assert exc_info.value.original_error is not None |
| 375 | + |
| 376 | + |
335 | 377 | def test_scrape_tool_decodes_utf8_bytes_markdown_field(): |
336 | 378 | client = _SyncScrapeClient(_Response(data=SimpleNamespace(markdown=b"hello"))) |
337 | 379 |
|
@@ -552,6 +594,27 @@ def markdown(self): |
552 | 594 | assert exc_info.value.original_error is not None |
553 | 595 |
|
554 | 596 |
|
| 597 | +def test_crawl_tool_wraps_declared_page_field_inspection_failures( |
| 598 | + monkeypatch: pytest.MonkeyPatch, |
| 599 | +): |
| 600 | + class _MissingMarkdownPage: |
| 601 | + pass |
| 602 | + |
| 603 | + def _raise_runtime_error(_obj: object, _attr: str): |
| 604 | + raise RuntimeError("attribute inspection exploded") |
| 605 | + |
| 606 | + monkeypatch.setattr(tools_module.inspect, "getattr_static", _raise_runtime_error) |
| 607 | + client = _SyncCrawlClient(_Response(data=[_MissingMarkdownPage()])) |
| 608 | + |
| 609 | + with pytest.raises( |
| 610 | + HyperbrowserError, |
| 611 | + match="Failed to inspect crawl tool page field 'markdown' at index 0", |
| 612 | + ) as exc_info: |
| 613 | + WebsiteCrawlTool.runnable(client, {"url": "https://example.com"}) |
| 614 | + |
| 615 | + assert exc_info.value.original_error is not None |
| 616 | + |
| 617 | + |
555 | 618 | def test_crawl_tool_rejects_non_object_page_items(): |
556 | 619 | client = _SyncCrawlClient(_Response(data=[123])) |
557 | 620 |
|
|
0 commit comments