|
| 1 | +from pathlib import Path |
| 2 | +from types import MappingProxyType |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from hyperbrowser.client.managers.extension_create_utils import ( |
| 7 | + normalize_extension_create_input, |
| 8 | +) |
| 9 | +from hyperbrowser.exceptions import HyperbrowserError |
| 10 | +from hyperbrowser.models.extension import CreateExtensionParams |
| 11 | + |
| 12 | + |
| 13 | +def _create_test_extension_zip(tmp_path: Path) -> Path: |
| 14 | + file_path = tmp_path / "extension.zip" |
| 15 | + file_path.write_bytes(b"extension-bytes") |
| 16 | + return file_path |
| 17 | + |
| 18 | + |
| 19 | +def test_normalize_extension_create_input_returns_file_path_and_payload(tmp_path): |
| 20 | + file_path = _create_test_extension_zip(tmp_path) |
| 21 | + params = CreateExtensionParams(name="my-extension", file_path=file_path) |
| 22 | + |
| 23 | + normalized_path, payload = normalize_extension_create_input(params) |
| 24 | + |
| 25 | + assert normalized_path == str(file_path.resolve()) |
| 26 | + assert payload == {"name": "my-extension"} |
| 27 | + |
| 28 | + |
| 29 | +def test_normalize_extension_create_input_rejects_invalid_param_type(): |
| 30 | + with pytest.raises(HyperbrowserError, match="params must be CreateExtensionParams"): |
| 31 | + normalize_extension_create_input({"name": "bad"}) # type: ignore[arg-type] |
| 32 | + |
| 33 | + |
| 34 | +def test_normalize_extension_create_input_rejects_subclass_param_type(tmp_path): |
| 35 | + class _Params(CreateExtensionParams): |
| 36 | + pass |
| 37 | + |
| 38 | + params = _Params(name="bad", file_path=_create_test_extension_zip(tmp_path)) |
| 39 | + |
| 40 | + with pytest.raises(HyperbrowserError, match="params must be CreateExtensionParams"): |
| 41 | + normalize_extension_create_input(params) |
| 42 | + |
| 43 | + |
| 44 | +def test_normalize_extension_create_input_wraps_serialization_errors( |
| 45 | + tmp_path, monkeypatch: pytest.MonkeyPatch |
| 46 | +): |
| 47 | + params = CreateExtensionParams( |
| 48 | + name="serialize-extension", |
| 49 | + file_path=_create_test_extension_zip(tmp_path), |
| 50 | + ) |
| 51 | + |
| 52 | + def _raise_model_dump_error(*args, **kwargs): |
| 53 | + _ = args |
| 54 | + _ = kwargs |
| 55 | + raise RuntimeError("broken model_dump") |
| 56 | + |
| 57 | + monkeypatch.setattr(CreateExtensionParams, "model_dump", _raise_model_dump_error) |
| 58 | + |
| 59 | + with pytest.raises( |
| 60 | + HyperbrowserError, match="Failed to serialize extension create params" |
| 61 | + ) as exc_info: |
| 62 | + normalize_extension_create_input(params) |
| 63 | + |
| 64 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 65 | + |
| 66 | + |
| 67 | +def test_normalize_extension_create_input_preserves_hyperbrowser_serialization_errors( |
| 68 | + tmp_path, monkeypatch: pytest.MonkeyPatch |
| 69 | +): |
| 70 | + params = CreateExtensionParams( |
| 71 | + name="serialize-extension", |
| 72 | + file_path=_create_test_extension_zip(tmp_path), |
| 73 | + ) |
| 74 | + |
| 75 | + def _raise_model_dump_error(*args, **kwargs): |
| 76 | + _ = args |
| 77 | + _ = kwargs |
| 78 | + raise HyperbrowserError("custom model_dump failure") |
| 79 | + |
| 80 | + monkeypatch.setattr(CreateExtensionParams, "model_dump", _raise_model_dump_error) |
| 81 | + |
| 82 | + with pytest.raises( |
| 83 | + HyperbrowserError, match="custom model_dump failure" |
| 84 | + ) as exc_info: |
| 85 | + normalize_extension_create_input(params) |
| 86 | + |
| 87 | + assert exc_info.value.original_error is None |
| 88 | + |
| 89 | + |
| 90 | +def test_normalize_extension_create_input_rejects_non_dict_serialized_payload( |
| 91 | + tmp_path, monkeypatch: pytest.MonkeyPatch |
| 92 | +): |
| 93 | + params = CreateExtensionParams( |
| 94 | + name="serialize-extension", |
| 95 | + file_path=_create_test_extension_zip(tmp_path), |
| 96 | + ) |
| 97 | + |
| 98 | + monkeypatch.setattr( |
| 99 | + CreateExtensionParams, |
| 100 | + "model_dump", |
| 101 | + lambda *args, **kwargs: MappingProxyType({"name": "my-extension"}), |
| 102 | + ) |
| 103 | + |
| 104 | + with pytest.raises( |
| 105 | + HyperbrowserError, match="Failed to serialize extension create params" |
| 106 | + ) as exc_info: |
| 107 | + normalize_extension_create_input(params) |
| 108 | + |
| 109 | + assert exc_info.value.original_error is None |
| 110 | + |
| 111 | + |
| 112 | +def test_normalize_extension_create_input_rejects_missing_file(tmp_path): |
| 113 | + missing_path = tmp_path / "missing-extension.zip" |
| 114 | + params = CreateExtensionParams(name="missing-extension", file_path=missing_path) |
| 115 | + |
| 116 | + with pytest.raises(HyperbrowserError, match="Extension file not found"): |
| 117 | + normalize_extension_create_input(params) |
0 commit comments