|
1 | 1 | import asyncio |
2 | | -from types import SimpleNamespace |
3 | 2 |
|
4 | 3 | import hyperbrowser.client.managers.agent_start_utils as agent_start_utils |
5 | 4 |
|
6 | 5 |
|
7 | | -def test_start_agent_task_builds_start_url_and_parses_response(): |
| 6 | +def test_start_agent_task_delegates_to_start_job(): |
8 | 7 | captured = {} |
9 | 8 |
|
10 | | - class _SyncTransport: |
11 | | - def post(self, url, data): |
12 | | - captured["url"] = url |
13 | | - captured["data"] = data |
14 | | - return SimpleNamespace(data={"id": "job-1"}) |
15 | | - |
16 | | - class _Client: |
17 | | - transport = _SyncTransport() |
18 | | - |
19 | | - @staticmethod |
20 | | - def _build_url(path: str) -> str: |
21 | | - return f"https://api.example.test{path}" |
22 | | - |
23 | | - def _fake_parse_response_model(data, **kwargs): |
24 | | - captured["parse_data"] = data |
25 | | - captured["parse_kwargs"] = kwargs |
| 9 | + def _fake_start_job(**kwargs): |
| 10 | + captured.update(kwargs) |
26 | 11 | return {"parsed": True} |
27 | 12 |
|
28 | | - original_parse = agent_start_utils.parse_response_model |
29 | | - agent_start_utils.parse_response_model = _fake_parse_response_model |
| 13 | + original_start_job = agent_start_utils.start_job |
| 14 | + agent_start_utils.start_job = _fake_start_job |
30 | 15 | try: |
31 | 16 | result = agent_start_utils.start_agent_task( |
32 | | - client=_Client(), |
| 17 | + client=object(), |
33 | 18 | route_prefix="/task/cua", |
34 | 19 | payload={"task": "open docs"}, |
35 | 20 | model=object, |
36 | 21 | operation_name="cua start", |
37 | 22 | ) |
38 | 23 | finally: |
39 | | - agent_start_utils.parse_response_model = original_parse |
| 24 | + agent_start_utils.start_job = original_start_job |
40 | 25 |
|
41 | 26 | assert result == {"parsed": True} |
42 | | - assert captured["url"] == "https://api.example.test/task/cua" |
43 | | - assert captured["data"] == {"task": "open docs"} |
44 | | - assert captured["parse_data"] == {"id": "job-1"} |
45 | | - assert captured["parse_kwargs"]["operation_name"] == "cua start" |
| 27 | + assert captured["route_prefix"] == "/task/cua" |
| 28 | + assert captured["payload"] == {"task": "open docs"} |
| 29 | + assert captured["operation_name"] == "cua start" |
46 | 30 |
|
47 | 31 |
|
48 | | -def test_start_agent_task_async_builds_start_url_and_parses_response(): |
| 32 | +def test_start_agent_task_async_delegates_to_start_job_async(): |
49 | 33 | captured = {} |
50 | 34 |
|
51 | | - class _AsyncTransport: |
52 | | - async def post(self, url, data): |
53 | | - captured["url"] = url |
54 | | - captured["data"] = data |
55 | | - return SimpleNamespace(data={"id": "job-2"}) |
56 | | - |
57 | | - class _Client: |
58 | | - transport = _AsyncTransport() |
59 | | - |
60 | | - @staticmethod |
61 | | - def _build_url(path: str) -> str: |
62 | | - return f"https://api.example.test{path}" |
63 | | - |
64 | | - def _fake_parse_response_model(data, **kwargs): |
65 | | - captured["parse_data"] = data |
66 | | - captured["parse_kwargs"] = kwargs |
| 35 | + async def _fake_start_job_async(**kwargs): |
| 36 | + captured.update(kwargs) |
67 | 37 | return {"parsed": True} |
68 | 38 |
|
69 | | - original_parse = agent_start_utils.parse_response_model |
70 | | - agent_start_utils.parse_response_model = _fake_parse_response_model |
| 39 | + original_start_job_async = agent_start_utils.start_job_async |
| 40 | + agent_start_utils.start_job_async = _fake_start_job_async |
71 | 41 | try: |
72 | 42 | result = asyncio.run( |
73 | 43 | agent_start_utils.start_agent_task_async( |
74 | | - client=_Client(), |
| 44 | + client=object(), |
75 | 45 | route_prefix="/task/browser-use", |
76 | 46 | payload={"task": "browse"}, |
77 | 47 | model=object, |
78 | 48 | operation_name="browser-use start", |
79 | 49 | ) |
80 | 50 | ) |
81 | 51 | finally: |
82 | | - agent_start_utils.parse_response_model = original_parse |
| 52 | + agent_start_utils.start_job_async = original_start_job_async |
83 | 53 |
|
84 | 54 | assert result == {"parsed": True} |
85 | | - assert captured["url"] == "https://api.example.test/task/browser-use" |
86 | | - assert captured["data"] == {"task": "browse"} |
87 | | - assert captured["parse_data"] == {"id": "job-2"} |
88 | | - assert captured["parse_kwargs"]["operation_name"] == "browser-use start" |
| 55 | + assert captured["route_prefix"] == "/task/browser-use" |
| 56 | + assert captured["payload"] == {"task": "browse"} |
| 57 | + assert captured["operation_name"] == "browser-use start" |
0 commit comments