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