|
2 | 2 | from types import SimpleNamespace |
3 | 3 |
|
4 | 4 | import hyperbrowser.client.managers.async_manager.agents.browser_use as async_browser_use_module |
| 5 | +import hyperbrowser.client.managers.async_manager.agents.cua as async_cua_module |
5 | 6 | import hyperbrowser.client.managers.async_manager.web.batch_fetch as async_batch_fetch_module |
| 7 | +import hyperbrowser.client.managers.async_manager.web.crawl as async_web_crawl_module |
6 | 8 | import hyperbrowser.client.managers.async_manager.crawl as async_crawl_module |
7 | 9 | import hyperbrowser.client.managers.async_manager.extract as async_extract_module |
8 | 10 | import hyperbrowser.client.managers.sync_manager.agents.browser_use as sync_browser_use_module |
| 11 | +import hyperbrowser.client.managers.sync_manager.agents.cua as sync_cua_module |
9 | 12 | import hyperbrowser.client.managers.sync_manager.web.batch_fetch as sync_batch_fetch_module |
| 13 | +import hyperbrowser.client.managers.sync_manager.web.crawl as sync_web_crawl_module |
10 | 14 | import hyperbrowser.client.managers.sync_manager.crawl as sync_crawl_module |
11 | 15 | import hyperbrowser.client.managers.sync_manager.extract as sync_extract_module |
| 16 | +import hyperbrowser.client.managers.sync_manager.scrape as sync_scrape_module |
| 17 | +import hyperbrowser.client.managers.async_manager.scrape as async_scrape_module |
12 | 18 |
|
13 | 19 |
|
14 | 20 | class _DummyClient: |
@@ -344,6 +350,180 @@ async def fake_retry_operation_async(**kwargs): |
344 | 350 | asyncio.run(run()) |
345 | 351 |
|
346 | 352 |
|
| 353 | +def test_sync_web_crawl_manager_bounds_operation_name_for_fetch_retry_path(monkeypatch): |
| 354 | + manager = sync_web_crawl_module.WebCrawlManager(_DummyClient()) |
| 355 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 356 | + captured = {} |
| 357 | + |
| 358 | + monkeypatch.setattr( |
| 359 | + manager, |
| 360 | + "start", |
| 361 | + lambda params: SimpleNamespace(job_id=long_job_id), |
| 362 | + ) |
| 363 | + |
| 364 | + def fake_poll_until_terminal_status(**kwargs): |
| 365 | + operation_name = kwargs["operation_name"] |
| 366 | + _assert_valid_operation_name(operation_name) |
| 367 | + captured["poll_operation_name"] = operation_name |
| 368 | + return "completed" |
| 369 | + |
| 370 | + def fake_retry_operation(**kwargs): |
| 371 | + operation_name = kwargs["operation_name"] |
| 372 | + _assert_valid_operation_name(operation_name) |
| 373 | + captured["fetch_operation_name"] = operation_name |
| 374 | + return {"ok": True} |
| 375 | + |
| 376 | + monkeypatch.setattr( |
| 377 | + sync_web_crawl_module, |
| 378 | + "poll_until_terminal_status", |
| 379 | + fake_poll_until_terminal_status, |
| 380 | + ) |
| 381 | + monkeypatch.setattr( |
| 382 | + sync_web_crawl_module, |
| 383 | + "retry_operation", |
| 384 | + fake_retry_operation, |
| 385 | + ) |
| 386 | + |
| 387 | + result = manager.start_and_wait(params=object(), return_all_pages=False) # type: ignore[arg-type] |
| 388 | + |
| 389 | + assert result == {"ok": True} |
| 390 | + assert captured["fetch_operation_name"] == captured["poll_operation_name"] |
| 391 | + |
| 392 | + |
| 393 | +def test_async_web_crawl_manager_bounds_operation_name_for_fetch_retry_path( |
| 394 | + monkeypatch, |
| 395 | +): |
| 396 | + async def run() -> None: |
| 397 | + manager = async_web_crawl_module.WebCrawlManager(_DummyClient()) |
| 398 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 399 | + captured = {} |
| 400 | + |
| 401 | + async def fake_start(params): |
| 402 | + return SimpleNamespace(job_id=long_job_id) |
| 403 | + |
| 404 | + async def fake_poll_until_terminal_status_async(**kwargs): |
| 405 | + operation_name = kwargs["operation_name"] |
| 406 | + _assert_valid_operation_name(operation_name) |
| 407 | + captured["poll_operation_name"] = operation_name |
| 408 | + return "completed" |
| 409 | + |
| 410 | + async def fake_retry_operation_async(**kwargs): |
| 411 | + operation_name = kwargs["operation_name"] |
| 412 | + _assert_valid_operation_name(operation_name) |
| 413 | + captured["fetch_operation_name"] = operation_name |
| 414 | + return {"ok": True} |
| 415 | + |
| 416 | + monkeypatch.setattr(manager, "start", fake_start) |
| 417 | + monkeypatch.setattr( |
| 418 | + async_web_crawl_module, |
| 419 | + "poll_until_terminal_status_async", |
| 420 | + fake_poll_until_terminal_status_async, |
| 421 | + ) |
| 422 | + monkeypatch.setattr( |
| 423 | + async_web_crawl_module, |
| 424 | + "retry_operation_async", |
| 425 | + fake_retry_operation_async, |
| 426 | + ) |
| 427 | + |
| 428 | + result = await manager.start_and_wait( |
| 429 | + params=object(), # type: ignore[arg-type] |
| 430 | + return_all_pages=False, |
| 431 | + ) |
| 432 | + |
| 433 | + assert result == {"ok": True} |
| 434 | + assert captured["fetch_operation_name"] == captured["poll_operation_name"] |
| 435 | + |
| 436 | + asyncio.run(run()) |
| 437 | + |
| 438 | + |
| 439 | +def test_sync_batch_scrape_manager_bounds_operation_name_for_fetch_retry_path( |
| 440 | + monkeypatch, |
| 441 | +): |
| 442 | + manager = sync_scrape_module.BatchScrapeManager(_DummyClient()) |
| 443 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 444 | + captured = {} |
| 445 | + |
| 446 | + monkeypatch.setattr( |
| 447 | + manager, |
| 448 | + "start", |
| 449 | + lambda params: SimpleNamespace(job_id=long_job_id), |
| 450 | + ) |
| 451 | + |
| 452 | + def fake_poll_until_terminal_status(**kwargs): |
| 453 | + operation_name = kwargs["operation_name"] |
| 454 | + _assert_valid_operation_name(operation_name) |
| 455 | + captured["poll_operation_name"] = operation_name |
| 456 | + return "completed" |
| 457 | + |
| 458 | + def fake_retry_operation(**kwargs): |
| 459 | + operation_name = kwargs["operation_name"] |
| 460 | + _assert_valid_operation_name(operation_name) |
| 461 | + captured["fetch_operation_name"] = operation_name |
| 462 | + return {"ok": True} |
| 463 | + |
| 464 | + monkeypatch.setattr( |
| 465 | + sync_scrape_module, |
| 466 | + "poll_until_terminal_status", |
| 467 | + fake_poll_until_terminal_status, |
| 468 | + ) |
| 469 | + monkeypatch.setattr( |
| 470 | + sync_scrape_module, |
| 471 | + "retry_operation", |
| 472 | + fake_retry_operation, |
| 473 | + ) |
| 474 | + |
| 475 | + result = manager.start_and_wait(params=object(), return_all_pages=False) # type: ignore[arg-type] |
| 476 | + |
| 477 | + assert result == {"ok": True} |
| 478 | + assert captured["fetch_operation_name"] == captured["poll_operation_name"] |
| 479 | + |
| 480 | + |
| 481 | +def test_async_batch_scrape_manager_bounds_operation_name_for_fetch_retry_path( |
| 482 | + monkeypatch, |
| 483 | +): |
| 484 | + async def run() -> None: |
| 485 | + manager = async_scrape_module.BatchScrapeManager(_DummyClient()) |
| 486 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 487 | + captured = {} |
| 488 | + |
| 489 | + async def fake_start(params): |
| 490 | + return SimpleNamespace(job_id=long_job_id) |
| 491 | + |
| 492 | + async def fake_poll_until_terminal_status_async(**kwargs): |
| 493 | + operation_name = kwargs["operation_name"] |
| 494 | + _assert_valid_operation_name(operation_name) |
| 495 | + captured["poll_operation_name"] = operation_name |
| 496 | + return "completed" |
| 497 | + |
| 498 | + async def fake_retry_operation_async(**kwargs): |
| 499 | + operation_name = kwargs["operation_name"] |
| 500 | + _assert_valid_operation_name(operation_name) |
| 501 | + captured["fetch_operation_name"] = operation_name |
| 502 | + return {"ok": True} |
| 503 | + |
| 504 | + monkeypatch.setattr(manager, "start", fake_start) |
| 505 | + monkeypatch.setattr( |
| 506 | + async_scrape_module, |
| 507 | + "poll_until_terminal_status_async", |
| 508 | + fake_poll_until_terminal_status_async, |
| 509 | + ) |
| 510 | + monkeypatch.setattr( |
| 511 | + async_scrape_module, |
| 512 | + "retry_operation_async", |
| 513 | + fake_retry_operation_async, |
| 514 | + ) |
| 515 | + |
| 516 | + result = await manager.start_and_wait( |
| 517 | + params=object(), # type: ignore[arg-type] |
| 518 | + return_all_pages=False, |
| 519 | + ) |
| 520 | + |
| 521 | + assert result == {"ok": True} |
| 522 | + assert captured["fetch_operation_name"] == captured["poll_operation_name"] |
| 523 | + |
| 524 | + asyncio.run(run()) |
| 525 | + |
| 526 | + |
347 | 527 | def test_sync_browser_use_manager_bounds_operation_name_in_wait_helper(monkeypatch): |
348 | 528 | manager = sync_browser_use_module.BrowserUseManager(_DummyClient()) |
349 | 529 | long_job_id = " \n" + ("x" * 500) + "\t" |
@@ -373,6 +553,35 @@ def fake_wait_for_job_result(**kwargs): |
373 | 553 | assert captured["operation_name"].startswith("browser-use task job ") |
374 | 554 |
|
375 | 555 |
|
| 556 | +def test_sync_cua_manager_bounds_operation_name_in_wait_helper(monkeypatch): |
| 557 | + manager = sync_cua_module.CuaManager(_DummyClient()) |
| 558 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 559 | + captured = {} |
| 560 | + |
| 561 | + monkeypatch.setattr( |
| 562 | + manager, |
| 563 | + "start", |
| 564 | + lambda params: SimpleNamespace(job_id=long_job_id), |
| 565 | + ) |
| 566 | + |
| 567 | + def fake_wait_for_job_result(**kwargs): |
| 568 | + operation_name = kwargs["operation_name"] |
| 569 | + _assert_valid_operation_name(operation_name) |
| 570 | + captured["operation_name"] = operation_name |
| 571 | + return {"ok": True} |
| 572 | + |
| 573 | + monkeypatch.setattr( |
| 574 | + sync_cua_module, |
| 575 | + "wait_for_job_result", |
| 576 | + fake_wait_for_job_result, |
| 577 | + ) |
| 578 | + |
| 579 | + result = manager.start_and_wait(params=object()) # type: ignore[arg-type] |
| 580 | + |
| 581 | + assert result == {"ok": True} |
| 582 | + assert captured["operation_name"].startswith("CUA task job ") |
| 583 | + |
| 584 | + |
376 | 585 | def test_async_browser_use_manager_bounds_operation_name_in_wait_helper(monkeypatch): |
377 | 586 | async def run() -> None: |
378 | 587 | manager = async_browser_use_module.BrowserUseManager(_DummyClient()) |
@@ -401,3 +610,33 @@ async def fake_wait_for_job_result_async(**kwargs): |
401 | 610 | assert captured["operation_name"].startswith("browser-use task job ") |
402 | 611 |
|
403 | 612 | asyncio.run(run()) |
| 613 | + |
| 614 | + |
| 615 | +def test_async_cua_manager_bounds_operation_name_in_wait_helper(monkeypatch): |
| 616 | + async def run() -> None: |
| 617 | + manager = async_cua_module.CuaManager(_DummyClient()) |
| 618 | + long_job_id = " \n" + ("x" * 500) + "\t" |
| 619 | + captured = {} |
| 620 | + |
| 621 | + async def fake_start(params): |
| 622 | + return SimpleNamespace(job_id=long_job_id) |
| 623 | + |
| 624 | + async def fake_wait_for_job_result_async(**kwargs): |
| 625 | + operation_name = kwargs["operation_name"] |
| 626 | + _assert_valid_operation_name(operation_name) |
| 627 | + captured["operation_name"] = operation_name |
| 628 | + return {"ok": True} |
| 629 | + |
| 630 | + monkeypatch.setattr(manager, "start", fake_start) |
| 631 | + monkeypatch.setattr( |
| 632 | + async_cua_module, |
| 633 | + "wait_for_job_result_async", |
| 634 | + fake_wait_for_job_result_async, |
| 635 | + ) |
| 636 | + |
| 637 | + result = await manager.start_and_wait(params=object()) # type: ignore[arg-type] |
| 638 | + |
| 639 | + assert result == {"ok": True} |
| 640 | + assert captured["operation_name"].startswith("CUA task job ") |
| 641 | + |
| 642 | + asyncio.run(run()) |
0 commit comments