Skip to content

Commit 6fa5065

Browse files
Expand async tool response edge-case coverage
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent c2c2244 commit 6fa5065

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

tests/test_tools_response_handling.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,24 @@ async def run() -> None:
773773
asyncio.run(run())
774774

775775

776+
def test_async_screenshot_tool_wraps_invalid_utf8_bytes_field():
777+
async def run() -> None:
778+
client = _AsyncScrapeClient(
779+
_Response(data=SimpleNamespace(screenshot=b"\xff\xfe"))
780+
)
781+
with pytest.raises(
782+
HyperbrowserError,
783+
match="screenshot tool response field 'screenshot' must be a UTF-8 string",
784+
) as exc_info:
785+
await WebsiteScreenshotTool.async_runnable(
786+
client,
787+
{"url": "https://example.com"},
788+
)
789+
assert exc_info.value.original_error is not None
790+
791+
asyncio.run(run())
792+
793+
776794
def test_async_browser_use_tool_wraps_invalid_utf8_bytes_final_result():
777795
async def run() -> None:
778796
client = _AsyncBrowserUseClient(
@@ -828,6 +846,36 @@ async def run() -> None:
828846
asyncio.run(run())
829847

830848

849+
def test_async_crawl_tool_wraps_mapping_page_inspection_failures():
850+
class _BrokenContainsPage(Mapping[str, object]):
851+
def __iter__(self):
852+
yield "markdown"
853+
854+
def __len__(self) -> int:
855+
return 1
856+
857+
def __contains__(self, key: object) -> bool:
858+
_ = key
859+
raise RuntimeError("cannot inspect markdown key")
860+
861+
def __getitem__(self, key: str) -> object:
862+
_ = key
863+
return "ignored"
864+
865+
async def run() -> None:
866+
client = _AsyncCrawlClient(_Response(data=[_BrokenContainsPage()]))
867+
with pytest.raises(
868+
HyperbrowserError,
869+
match="Failed to inspect crawl tool page field 'markdown' at index 0",
870+
) as exc_info:
871+
await WebsiteCrawlTool.async_runnable(
872+
client, {"url": "https://example.com"}
873+
)
874+
assert exc_info.value.original_error is not None
875+
876+
asyncio.run(run())
877+
878+
831879
def test_async_browser_use_tool_supports_mapping_response_data():
832880
async def run() -> None:
833881
client = _AsyncBrowserUseClient(

0 commit comments

Comments
 (0)