|
1 | 1 | import asyncio |
2 | 2 | from types import SimpleNamespace |
3 | 3 |
|
| 4 | +import hyperbrowser.client.managers.async_manager.agents.browser_use as async_browser_use_module |
| 5 | +import hyperbrowser.client.managers.async_manager.web.batch_fetch as async_batch_fetch_module |
4 | 6 | import hyperbrowser.client.managers.async_manager.crawl as async_crawl_module |
5 | 7 | import hyperbrowser.client.managers.async_manager.extract as async_extract_module |
| 8 | +import hyperbrowser.client.managers.sync_manager.agents.browser_use as sync_browser_use_module |
| 9 | +import hyperbrowser.client.managers.sync_manager.web.batch_fetch as sync_batch_fetch_module |
6 | 10 | import hyperbrowser.client.managers.sync_manager.crawl as sync_crawl_module |
7 | 11 | import hyperbrowser.client.managers.sync_manager.extract as sync_extract_module |
8 | 12 |
|
@@ -250,3 +254,150 @@ async def fake_retry_operation_async(**kwargs): |
250 | 254 | assert captured["fetch_operation_name"] == captured["poll_operation_name"] |
251 | 255 |
|
252 | 256 | asyncio.run(run()) |
| 257 | + |
| 258 | + |
| 259 | +def test_sync_batch_fetch_manager_bounds_operation_name_for_fetch_retry_path( |
| 260 | + monkeypatch, |
| 261 | +): |
| 262 | + manager = sync_batch_fetch_module.BatchFetchManager(_DummyClient()) |
| 263 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 264 | + captured = {} |
| 265 | + |
| 266 | + monkeypatch.setattr( |
| 267 | + manager, |
| 268 | + "start", |
| 269 | + lambda params: SimpleNamespace(job_id=long_job_id), |
| 270 | + ) |
| 271 | + |
| 272 | + def fake_poll_until_terminal_status(**kwargs): |
| 273 | + operation_name = kwargs["operation_name"] |
| 274 | + _assert_valid_operation_name(operation_name) |
| 275 | + captured["poll_operation_name"] = operation_name |
| 276 | + return "completed" |
| 277 | + |
| 278 | + def fake_retry_operation(**kwargs): |
| 279 | + operation_name = kwargs["operation_name"] |
| 280 | + _assert_valid_operation_name(operation_name) |
| 281 | + captured["fetch_operation_name"] = operation_name |
| 282 | + return {"ok": True} |
| 283 | + |
| 284 | + monkeypatch.setattr( |
| 285 | + sync_batch_fetch_module, |
| 286 | + "poll_until_terminal_status", |
| 287 | + fake_poll_until_terminal_status, |
| 288 | + ) |
| 289 | + monkeypatch.setattr( |
| 290 | + sync_batch_fetch_module, |
| 291 | + "retry_operation", |
| 292 | + fake_retry_operation, |
| 293 | + ) |
| 294 | + |
| 295 | + result = manager.start_and_wait(params=object(), return_all_pages=False) # type: ignore[arg-type] |
| 296 | + |
| 297 | + assert result == {"ok": True} |
| 298 | + assert captured["fetch_operation_name"] == captured["poll_operation_name"] |
| 299 | + |
| 300 | + |
| 301 | +def test_async_batch_fetch_manager_bounds_operation_name_for_fetch_retry_path( |
| 302 | + monkeypatch, |
| 303 | +): |
| 304 | + async def run() -> None: |
| 305 | + manager = async_batch_fetch_module.BatchFetchManager(_DummyClient()) |
| 306 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 307 | + captured = {} |
| 308 | + |
| 309 | + async def fake_start(params): |
| 310 | + return SimpleNamespace(job_id=long_job_id) |
| 311 | + |
| 312 | + async def fake_poll_until_terminal_status_async(**kwargs): |
| 313 | + operation_name = kwargs["operation_name"] |
| 314 | + _assert_valid_operation_name(operation_name) |
| 315 | + captured["poll_operation_name"] = operation_name |
| 316 | + return "completed" |
| 317 | + |
| 318 | + async def fake_retry_operation_async(**kwargs): |
| 319 | + operation_name = kwargs["operation_name"] |
| 320 | + _assert_valid_operation_name(operation_name) |
| 321 | + captured["fetch_operation_name"] = operation_name |
| 322 | + return {"ok": True} |
| 323 | + |
| 324 | + monkeypatch.setattr(manager, "start", fake_start) |
| 325 | + monkeypatch.setattr( |
| 326 | + async_batch_fetch_module, |
| 327 | + "poll_until_terminal_status_async", |
| 328 | + fake_poll_until_terminal_status_async, |
| 329 | + ) |
| 330 | + monkeypatch.setattr( |
| 331 | + async_batch_fetch_module, |
| 332 | + "retry_operation_async", |
| 333 | + fake_retry_operation_async, |
| 334 | + ) |
| 335 | + |
| 336 | + result = await manager.start_and_wait( |
| 337 | + params=object(), # type: ignore[arg-type] |
| 338 | + return_all_pages=False, |
| 339 | + ) |
| 340 | + |
| 341 | + assert result == {"ok": True} |
| 342 | + assert captured["fetch_operation_name"] == captured["poll_operation_name"] |
| 343 | + |
| 344 | + asyncio.run(run()) |
| 345 | + |
| 346 | + |
| 347 | +def test_sync_browser_use_manager_bounds_operation_name_in_wait_helper(monkeypatch): |
| 348 | + manager = sync_browser_use_module.BrowserUseManager(_DummyClient()) |
| 349 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 350 | + captured = {} |
| 351 | + |
| 352 | + monkeypatch.setattr( |
| 353 | + manager, |
| 354 | + "start", |
| 355 | + lambda params: SimpleNamespace(job_id=long_job_id), |
| 356 | + ) |
| 357 | + |
| 358 | + def fake_wait_for_job_result(**kwargs): |
| 359 | + operation_name = kwargs["operation_name"] |
| 360 | + _assert_valid_operation_name(operation_name) |
| 361 | + captured["operation_name"] = operation_name |
| 362 | + return {"ok": True} |
| 363 | + |
| 364 | + monkeypatch.setattr( |
| 365 | + sync_browser_use_module, |
| 366 | + "wait_for_job_result", |
| 367 | + fake_wait_for_job_result, |
| 368 | + ) |
| 369 | + |
| 370 | + result = manager.start_and_wait(params=object()) # type: ignore[arg-type] |
| 371 | + |
| 372 | + assert result == {"ok": True} |
| 373 | + assert captured["operation_name"].startswith("browser-use task job ") |
| 374 | + |
| 375 | + |
| 376 | +def test_async_browser_use_manager_bounds_operation_name_in_wait_helper(monkeypatch): |
| 377 | + async def run() -> None: |
| 378 | + manager = async_browser_use_module.BrowserUseManager(_DummyClient()) |
| 379 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 380 | + captured = {} |
| 381 | + |
| 382 | + async def fake_start(params): |
| 383 | + return SimpleNamespace(job_id=long_job_id) |
| 384 | + |
| 385 | + async def fake_wait_for_job_result_async(**kwargs): |
| 386 | + operation_name = kwargs["operation_name"] |
| 387 | + _assert_valid_operation_name(operation_name) |
| 388 | + captured["operation_name"] = operation_name |
| 389 | + return {"ok": True} |
| 390 | + |
| 391 | + monkeypatch.setattr(manager, "start", fake_start) |
| 392 | + monkeypatch.setattr( |
| 393 | + async_browser_use_module, |
| 394 | + "wait_for_job_result_async", |
| 395 | + fake_wait_for_job_result_async, |
| 396 | + ) |
| 397 | + |
| 398 | + result = await manager.start_and_wait(params=object()) # type: ignore[arg-type] |
| 399 | + |
| 400 | + assert result == {"ok": True} |
| 401 | + assert captured["operation_name"].startswith("browser-use task job ") |
| 402 | + |
| 403 | + asyncio.run(run()) |
0 commit comments