|
| 1 | +import pytest |
| 2 | + |
| 3 | +from hyperbrowser.client.managers.computer_action_utils import ( |
| 4 | + normalize_computer_action_endpoint, |
| 5 | +) |
| 6 | +from hyperbrowser.exceptions import HyperbrowserError |
| 7 | + |
| 8 | + |
| 9 | +class _SessionWithoutEndpoint: |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +class _SessionWithFailingEndpoint: |
| 14 | + @property |
| 15 | + def computer_action_endpoint(self) -> str: |
| 16 | + raise RuntimeError("endpoint unavailable") |
| 17 | + |
| 18 | + |
| 19 | +class _SessionWithHyperbrowserEndpointFailure: |
| 20 | + @property |
| 21 | + def computer_action_endpoint(self) -> str: |
| 22 | + raise HyperbrowserError("custom endpoint failure") |
| 23 | + |
| 24 | + |
| 25 | +class _Session: |
| 26 | + def __init__(self, endpoint): |
| 27 | + self.computer_action_endpoint = endpoint |
| 28 | + |
| 29 | + |
| 30 | +def test_normalize_computer_action_endpoint_returns_valid_value(): |
| 31 | + assert ( |
| 32 | + normalize_computer_action_endpoint(_Session("https://example.com/action")) |
| 33 | + == "https://example.com/action" |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def test_normalize_computer_action_endpoint_wraps_missing_attribute_failures(): |
| 38 | + with pytest.raises( |
| 39 | + HyperbrowserError, match="session must include computer_action_endpoint" |
| 40 | + ) as exc_info: |
| 41 | + normalize_computer_action_endpoint(_SessionWithoutEndpoint()) |
| 42 | + |
| 43 | + assert isinstance(exc_info.value.original_error, AttributeError) |
| 44 | + |
| 45 | + |
| 46 | +def test_normalize_computer_action_endpoint_wraps_runtime_attribute_failures(): |
| 47 | + with pytest.raises( |
| 48 | + HyperbrowserError, match="session must include computer_action_endpoint" |
| 49 | + ) as exc_info: |
| 50 | + normalize_computer_action_endpoint(_SessionWithFailingEndpoint()) |
| 51 | + |
| 52 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 53 | + |
| 54 | + |
| 55 | +def test_normalize_computer_action_endpoint_preserves_hyperbrowser_failures(): |
| 56 | + with pytest.raises(HyperbrowserError, match="custom endpoint failure") as exc_info: |
| 57 | + normalize_computer_action_endpoint(_SessionWithHyperbrowserEndpointFailure()) |
| 58 | + |
| 59 | + assert exc_info.value.original_error is None |
| 60 | + |
| 61 | + |
| 62 | +def test_normalize_computer_action_endpoint_rejects_missing_endpoint(): |
| 63 | + with pytest.raises( |
| 64 | + HyperbrowserError, match="Computer action endpoint not available for this session" |
| 65 | + ): |
| 66 | + normalize_computer_action_endpoint(_Session(None)) |
| 67 | + |
| 68 | + |
| 69 | +def test_normalize_computer_action_endpoint_rejects_non_string_endpoint(): |
| 70 | + with pytest.raises( |
| 71 | + HyperbrowserError, match="session computer_action_endpoint must be a string" |
| 72 | + ): |
| 73 | + normalize_computer_action_endpoint(_Session(123)) |
| 74 | + |
| 75 | + |
| 76 | +def test_normalize_computer_action_endpoint_rejects_string_subclass_endpoint(): |
| 77 | + class _EndpointString(str): |
| 78 | + pass |
| 79 | + |
| 80 | + with pytest.raises( |
| 81 | + HyperbrowserError, match="session computer_action_endpoint must be a string" |
| 82 | + ): |
| 83 | + normalize_computer_action_endpoint(_Session(_EndpointString("https://x.com"))) |
| 84 | + |
| 85 | + |
| 86 | +def test_normalize_computer_action_endpoint_rejects_surrounding_whitespace(): |
| 87 | + with pytest.raises( |
| 88 | + HyperbrowserError, |
| 89 | + match="session computer_action_endpoint must not contain leading or trailing whitespace", |
| 90 | + ): |
| 91 | + normalize_computer_action_endpoint(_Session(" https://example.com/action ")) |
| 92 | + |
| 93 | + |
| 94 | +def test_normalize_computer_action_endpoint_rejects_control_characters(): |
| 95 | + with pytest.raises( |
| 96 | + HyperbrowserError, |
| 97 | + match="session computer_action_endpoint must not contain control characters", |
| 98 | + ): |
| 99 | + normalize_computer_action_endpoint(_Session("https://example.com/\x07action")) |
0 commit comments