|
| 1 | +import asyncio |
| 2 | +from collections.abc import Iterator, Mapping |
1 | 3 | from types import MappingProxyType |
2 | 4 |
|
3 | | -import asyncio |
4 | 5 | import pytest |
5 | 6 |
|
6 | 7 | from hyperbrowser.exceptions import HyperbrowserError |
@@ -95,3 +96,75 @@ async def run() -> None: |
95 | 96 | ) |
96 | 97 |
|
97 | 98 | asyncio.run(run()) |
| 99 | + |
| 100 | + |
| 101 | +def test_tool_wrappers_reject_non_string_param_keys(): |
| 102 | + client = _Client() |
| 103 | + |
| 104 | + with pytest.raises(HyperbrowserError, match="tool params keys must be strings"): |
| 105 | + WebsiteScrapeTool.runnable( |
| 106 | + client, |
| 107 | + {1: "https://example.com"}, # type: ignore[dict-item] |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +def test_tool_wrappers_wrap_param_key_read_failures(): |
| 112 | + class _BrokenKeyMapping(Mapping[str, object]): |
| 113 | + def __iter__(self) -> Iterator[str]: |
| 114 | + raise RuntimeError("cannot iterate keys") |
| 115 | + |
| 116 | + def __len__(self) -> int: |
| 117 | + return 1 |
| 118 | + |
| 119 | + def __getitem__(self, key: str) -> object: |
| 120 | + _ = key |
| 121 | + return "ignored" |
| 122 | + |
| 123 | + client = _Client() |
| 124 | + |
| 125 | + with pytest.raises(HyperbrowserError, match="Failed to read tool params keys") as exc_info: |
| 126 | + WebsiteScrapeTool.runnable(client, _BrokenKeyMapping()) |
| 127 | + |
| 128 | + assert exc_info.value.original_error is not None |
| 129 | + |
| 130 | + |
| 131 | +def test_tool_wrappers_wrap_param_value_read_failures(): |
| 132 | + class _BrokenValueMapping(Mapping[str, object]): |
| 133 | + def __iter__(self) -> Iterator[str]: |
| 134 | + yield "url" |
| 135 | + |
| 136 | + def __len__(self) -> int: |
| 137 | + return 1 |
| 138 | + |
| 139 | + def __getitem__(self, key: str) -> object: |
| 140 | + _ = key |
| 141 | + raise RuntimeError("cannot read value") |
| 142 | + |
| 143 | + client = _Client() |
| 144 | + |
| 145 | + with pytest.raises(HyperbrowserError, match="Failed to read tool param 'url'") as exc_info: |
| 146 | + WebsiteScrapeTool.runnable(client, _BrokenValueMapping()) |
| 147 | + |
| 148 | + assert exc_info.value.original_error is not None |
| 149 | + |
| 150 | + |
| 151 | +def test_tool_wrappers_preserve_hyperbrowser_param_value_read_failures(): |
| 152 | + class _BrokenValueMapping(Mapping[str, object]): |
| 153 | + def __iter__(self) -> Iterator[str]: |
| 154 | + yield "url" |
| 155 | + |
| 156 | + def __len__(self) -> int: |
| 157 | + return 1 |
| 158 | + |
| 159 | + def __getitem__(self, key: str) -> object: |
| 160 | + _ = key |
| 161 | + raise HyperbrowserError("custom param value read failure") |
| 162 | + |
| 163 | + client = _Client() |
| 164 | + |
| 165 | + with pytest.raises( |
| 166 | + HyperbrowserError, match="custom param value read failure" |
| 167 | + ) as exc_info: |
| 168 | + WebsiteScrapeTool.runnable(client, _BrokenValueMapping()) |
| 169 | + |
| 170 | + assert exc_info.value.original_error is None |
0 commit comments