|
| 1 | +import asyncio |
| 2 | +import importlib |
| 3 | +import sys |
| 4 | + |
| 5 | +snapshot_module = importlib.import_module("sentience.snapshot") |
| 6 | +from sentience.snapshot import _post_snapshot_to_gateway_async, _post_snapshot_to_gateway_sync |
| 7 | + |
| 8 | + |
| 9 | +class _DummyResponse: |
| 10 | + def raise_for_status(self): |
| 11 | + return None |
| 12 | + |
| 13 | + def json(self): |
| 14 | + return {"status": "success", "elements": [], "url": "https://example.com"} |
| 15 | + |
| 16 | + |
| 17 | +def test_post_snapshot_async_uses_default_timeout(monkeypatch): |
| 18 | + class DummyClient: |
| 19 | + last_timeout = None |
| 20 | + |
| 21 | + def __init__(self, timeout): |
| 22 | + DummyClient.last_timeout = timeout |
| 23 | + |
| 24 | + async def __aenter__(self): |
| 25 | + return self |
| 26 | + |
| 27 | + async def __aexit__(self, exc_type, exc, tb): |
| 28 | + return None |
| 29 | + |
| 30 | + async def post(self, *args, **kwargs): |
| 31 | + return _DummyResponse() |
| 32 | + |
| 33 | + dummy_httpx = type("DummyHttpx", (), {"AsyncClient": DummyClient}) |
| 34 | + monkeypatch.setitem(sys.modules, "httpx", dummy_httpx) |
| 35 | + asyncio.run( |
| 36 | + _post_snapshot_to_gateway_async( |
| 37 | + {"raw_elements": [], "url": "https://example.com", "viewport": None, "goal": None, "options": {}}, |
| 38 | + "sk_test", |
| 39 | + "https://api.sentienceapi.com", |
| 40 | + ) |
| 41 | + ) |
| 42 | + assert DummyClient.last_timeout == 30.0 |
| 43 | + |
| 44 | + |
| 45 | +def test_post_snapshot_async_uses_custom_timeout(monkeypatch): |
| 46 | + class DummyClient: |
| 47 | + last_timeout = None |
| 48 | + |
| 49 | + def __init__(self, timeout): |
| 50 | + DummyClient.last_timeout = timeout |
| 51 | + |
| 52 | + async def __aenter__(self): |
| 53 | + return self |
| 54 | + |
| 55 | + async def __aexit__(self, exc_type, exc, tb): |
| 56 | + return None |
| 57 | + |
| 58 | + async def post(self, *args, **kwargs): |
| 59 | + return _DummyResponse() |
| 60 | + |
| 61 | + dummy_httpx = type("DummyHttpx", (), {"AsyncClient": DummyClient}) |
| 62 | + monkeypatch.setitem(sys.modules, "httpx", dummy_httpx) |
| 63 | + asyncio.run( |
| 64 | + _post_snapshot_to_gateway_async( |
| 65 | + {"raw_elements": [], "url": "https://example.com", "viewport": None, "goal": None, "options": {}}, |
| 66 | + "sk_test", |
| 67 | + "https://api.sentienceapi.com", |
| 68 | + timeout_s=12.5, |
| 69 | + ) |
| 70 | + ) |
| 71 | + assert DummyClient.last_timeout == 12.5 |
| 72 | + |
| 73 | + |
| 74 | +def test_post_snapshot_sync_uses_default_timeout(monkeypatch): |
| 75 | + class DummyRequests: |
| 76 | + last_timeout = None |
| 77 | + |
| 78 | + @staticmethod |
| 79 | + def post(*args, **kwargs): |
| 80 | + DummyRequests.last_timeout = kwargs.get("timeout") |
| 81 | + return _DummyResponse() |
| 82 | + |
| 83 | + monkeypatch.setattr(snapshot_module, "requests", DummyRequests) |
| 84 | + _post_snapshot_to_gateway_sync( |
| 85 | + {"raw_elements": [], "url": "https://example.com", "viewport": None, "goal": None, "options": {}}, |
| 86 | + "sk_test", |
| 87 | + "https://api.sentienceapi.com", |
| 88 | + ) |
| 89 | + assert DummyRequests.last_timeout == 30 |
| 90 | + |
| 91 | + |
| 92 | +def test_post_snapshot_sync_uses_custom_timeout(monkeypatch): |
| 93 | + class DummyRequests: |
| 94 | + last_timeout = None |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def post(*args, **kwargs): |
| 98 | + DummyRequests.last_timeout = kwargs.get("timeout") |
| 99 | + return _DummyResponse() |
| 100 | + |
| 101 | + monkeypatch.setattr(snapshot_module, "requests", DummyRequests) |
| 102 | + _post_snapshot_to_gateway_sync( |
| 103 | + {"raw_elements": [], "url": "https://example.com", "viewport": None, "goal": None, "options": {}}, |
| 104 | + "sk_test", |
| 105 | + "https://api.sentienceapi.com", |
| 106 | + timeout_s=9.0, |
| 107 | + ) |
| 108 | + assert DummyRequests.last_timeout == 9.0 |
0 commit comments