|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from hyperbrowser.exceptions import HyperbrowserError |
| 6 | +from hyperbrowser.models.extract import StartExtractJobParams |
| 7 | +from hyperbrowser.tools import WebsiteExtractTool |
| 8 | + |
| 9 | + |
| 10 | +class _Response: |
| 11 | + def __init__(self, data): |
| 12 | + self.data = data |
| 13 | + |
| 14 | + |
| 15 | +class _SyncExtractManager: |
| 16 | + def __init__(self): |
| 17 | + self.last_params = None |
| 18 | + |
| 19 | + def start_and_wait(self, params: StartExtractJobParams): |
| 20 | + self.last_params = params |
| 21 | + return _Response({"ok": True}) |
| 22 | + |
| 23 | + |
| 24 | +class _AsyncExtractManager: |
| 25 | + def __init__(self): |
| 26 | + self.last_params = None |
| 27 | + |
| 28 | + async def start_and_wait(self, params: StartExtractJobParams): |
| 29 | + self.last_params = params |
| 30 | + return _Response({"ok": True}) |
| 31 | + |
| 32 | + |
| 33 | +class _SyncClient: |
| 34 | + def __init__(self): |
| 35 | + self.extract = _SyncExtractManager() |
| 36 | + |
| 37 | + |
| 38 | +class _AsyncClient: |
| 39 | + def __init__(self): |
| 40 | + self.extract = _AsyncExtractManager() |
| 41 | + |
| 42 | + |
| 43 | +def test_extract_tool_runnable_does_not_mutate_input_params(): |
| 44 | + client = _SyncClient() |
| 45 | + params = { |
| 46 | + "urls": ["https://example.com"], |
| 47 | + "schema": '{"type":"object","properties":{"name":{"type":"string"}}}', |
| 48 | + } |
| 49 | + |
| 50 | + output = WebsiteExtractTool.runnable(client, params) |
| 51 | + |
| 52 | + assert output == '{"ok": true}' |
| 53 | + assert isinstance(client.extract.last_params, StartExtractJobParams) |
| 54 | + assert isinstance(client.extract.last_params.schema_, dict) |
| 55 | + assert params["schema"] == '{"type":"object","properties":{"name":{"type":"string"}}}' |
| 56 | + |
| 57 | + |
| 58 | +def test_extract_tool_async_runnable_does_not_mutate_input_params(): |
| 59 | + client = _AsyncClient() |
| 60 | + params = { |
| 61 | + "urls": ["https://example.com"], |
| 62 | + "schema": '{"type":"object","properties":{"name":{"type":"string"}}}', |
| 63 | + } |
| 64 | + |
| 65 | + async def run(): |
| 66 | + return await WebsiteExtractTool.async_runnable(client, params) |
| 67 | + |
| 68 | + output = asyncio.run(run()) |
| 69 | + |
| 70 | + assert output == '{"ok": true}' |
| 71 | + assert isinstance(client.extract.last_params, StartExtractJobParams) |
| 72 | + assert isinstance(client.extract.last_params.schema_, dict) |
| 73 | + assert params["schema"] == '{"type":"object","properties":{"name":{"type":"string"}}}' |
| 74 | + |
| 75 | + |
| 76 | +def test_extract_tool_runnable_raises_for_invalid_schema_json(): |
| 77 | + client = _SyncClient() |
| 78 | + params = { |
| 79 | + "urls": ["https://example.com"], |
| 80 | + "schema": "{invalid-json}", |
| 81 | + } |
| 82 | + |
| 83 | + with pytest.raises(HyperbrowserError, match="Invalid JSON string provided for `schema`"): |
| 84 | + WebsiteExtractTool.runnable(client, params) |
0 commit comments