|
| 1 | +import asyncio |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +import hyperbrowser.client.managers.computer_action_request_utils as request_utils |
| 5 | + |
| 6 | + |
| 7 | +def test_execute_computer_action_request_posts_and_parses_response(): |
| 8 | + captured = {} |
| 9 | + |
| 10 | + class _SyncTransport: |
| 11 | + def post(self, endpoint, data=None): |
| 12 | + captured["endpoint"] = endpoint |
| 13 | + captured["data"] = data |
| 14 | + return SimpleNamespace(data={"success": True}) |
| 15 | + |
| 16 | + class _Client: |
| 17 | + transport = _SyncTransport() |
| 18 | + |
| 19 | + def _fake_parse_response_model(data, **kwargs): |
| 20 | + captured["parse_data"] = data |
| 21 | + captured["parse_kwargs"] = kwargs |
| 22 | + return {"parsed": True} |
| 23 | + |
| 24 | + original_parse = request_utils.parse_response_model |
| 25 | + request_utils.parse_response_model = _fake_parse_response_model |
| 26 | + try: |
| 27 | + result = request_utils.execute_computer_action_request( |
| 28 | + client=_Client(), |
| 29 | + endpoint="https://example.com/cua", |
| 30 | + payload={"action": {"type": "screenshot"}}, |
| 31 | + operation_name="computer action", |
| 32 | + ) |
| 33 | + finally: |
| 34 | + request_utils.parse_response_model = original_parse |
| 35 | + |
| 36 | + assert result == {"parsed": True} |
| 37 | + assert captured["endpoint"] == "https://example.com/cua" |
| 38 | + assert captured["data"] == {"action": {"type": "screenshot"}} |
| 39 | + assert captured["parse_data"] == {"success": True} |
| 40 | + assert captured["parse_kwargs"]["operation_name"] == "computer action" |
| 41 | + |
| 42 | + |
| 43 | +def test_execute_computer_action_request_async_posts_and_parses_response(): |
| 44 | + captured = {} |
| 45 | + |
| 46 | + class _AsyncTransport: |
| 47 | + async def post(self, endpoint, data=None): |
| 48 | + captured["endpoint"] = endpoint |
| 49 | + captured["data"] = data |
| 50 | + return SimpleNamespace(data={"success": True}) |
| 51 | + |
| 52 | + class _Client: |
| 53 | + transport = _AsyncTransport() |
| 54 | + |
| 55 | + def _fake_parse_response_model(data, **kwargs): |
| 56 | + captured["parse_data"] = data |
| 57 | + captured["parse_kwargs"] = kwargs |
| 58 | + return {"parsed": True} |
| 59 | + |
| 60 | + original_parse = request_utils.parse_response_model |
| 61 | + request_utils.parse_response_model = _fake_parse_response_model |
| 62 | + try: |
| 63 | + result = asyncio.run( |
| 64 | + request_utils.execute_computer_action_request_async( |
| 65 | + client=_Client(), |
| 66 | + endpoint="https://example.com/cua", |
| 67 | + payload={"action": {"type": "screenshot"}}, |
| 68 | + operation_name="computer action", |
| 69 | + ) |
| 70 | + ) |
| 71 | + finally: |
| 72 | + request_utils.parse_response_model = original_parse |
| 73 | + |
| 74 | + assert result == {"parsed": True} |
| 75 | + assert captured["endpoint"] == "https://example.com/cua" |
| 76 | + assert captured["data"] == {"action": {"type": "screenshot"}} |
| 77 | + assert captured["parse_data"] == {"success": True} |
| 78 | + assert captured["parse_kwargs"]["operation_name"] == "computer action" |
0 commit comments