|
| 1 | +import asyncio |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from hyperbrowser.client.managers.async_manager.session import ( |
| 7 | + SessionManager as AsyncSessionManager, |
| 8 | +) |
| 9 | +from hyperbrowser.client.managers.sync_manager.session import ( |
| 10 | + SessionManager as SyncSessionManager, |
| 11 | +) |
| 12 | +from hyperbrowser.models.session import UpdateSessionProfileParams |
| 13 | + |
| 14 | + |
| 15 | +class _SyncTransport: |
| 16 | + def __init__(self) -> None: |
| 17 | + self.calls = [] |
| 18 | + |
| 19 | + def put(self, url: str, data=None) -> SimpleNamespace: |
| 20 | + self.calls.append((url, data)) |
| 21 | + return SimpleNamespace(data={"success": True}) |
| 22 | + |
| 23 | + |
| 24 | +class _AsyncTransport: |
| 25 | + def __init__(self) -> None: |
| 26 | + self.calls = [] |
| 27 | + |
| 28 | + async def put(self, url: str, data=None) -> SimpleNamespace: |
| 29 | + self.calls.append((url, data)) |
| 30 | + return SimpleNamespace(data={"success": True}) |
| 31 | + |
| 32 | + |
| 33 | +class _SyncClient: |
| 34 | + def __init__(self) -> None: |
| 35 | + self.transport = _SyncTransport() |
| 36 | + |
| 37 | + def _build_url(self, path: str) -> str: |
| 38 | + return path |
| 39 | + |
| 40 | + |
| 41 | +class _AsyncClient: |
| 42 | + def __init__(self) -> None: |
| 43 | + self.transport = _AsyncTransport() |
| 44 | + |
| 45 | + def _build_url(self, path: str) -> str: |
| 46 | + return path |
| 47 | + |
| 48 | + |
| 49 | +def test_sync_update_profile_params_uses_serialized_param_model(): |
| 50 | + client = _SyncClient() |
| 51 | + manager = SyncSessionManager(client) |
| 52 | + |
| 53 | + manager.update_profile_params( |
| 54 | + "session-1", |
| 55 | + UpdateSessionProfileParams(persist_changes=True), |
| 56 | + ) |
| 57 | + |
| 58 | + _, payload = client.transport.calls[0] |
| 59 | + assert payload == {"type": "profile", "params": {"persistChanges": True}} |
| 60 | + |
| 61 | + |
| 62 | +def test_sync_update_profile_params_bool_warns_and_serializes(): |
| 63 | + SyncSessionManager._has_warned_update_profile_params_boolean_deprecated = False |
| 64 | + client = _SyncClient() |
| 65 | + manager = SyncSessionManager(client) |
| 66 | + |
| 67 | + with pytest.warns(DeprecationWarning): |
| 68 | + manager.update_profile_params("session-1", True) |
| 69 | + |
| 70 | + _, payload = client.transport.calls[0] |
| 71 | + assert payload == {"type": "profile", "params": {"persistChanges": True}} |
| 72 | + |
| 73 | + |
| 74 | +def test_sync_update_profile_params_rejects_conflicting_arguments(): |
| 75 | + manager = SyncSessionManager(_SyncClient()) |
| 76 | + |
| 77 | + with pytest.raises(TypeError, match="not both"): |
| 78 | + manager.update_profile_params( |
| 79 | + "session-1", |
| 80 | + UpdateSessionProfileParams(persist_changes=True), |
| 81 | + persist_changes=True, |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def test_async_update_profile_params_bool_warns_and_serializes(): |
| 86 | + AsyncSessionManager._has_warned_update_profile_params_boolean_deprecated = False |
| 87 | + client = _AsyncClient() |
| 88 | + manager = AsyncSessionManager(client) |
| 89 | + |
| 90 | + async def run() -> None: |
| 91 | + with pytest.warns(DeprecationWarning): |
| 92 | + await manager.update_profile_params("session-1", True) |
| 93 | + |
| 94 | + asyncio.run(run()) |
| 95 | + |
| 96 | + _, payload = client.transport.calls[0] |
| 97 | + assert payload == {"type": "profile", "params": {"persistChanges": True}} |
0 commit comments