|
15 | 15 | MockObjectView, |
16 | 16 | MockSnapshotView, |
17 | 17 | MockBlueprintView, |
| 18 | + MockAgentView, |
18 | 19 | create_mock_httpx_response, |
19 | 20 | ) |
20 | | -from runloop_api_client.sdk import AsyncDevbox, AsyncSnapshot, AsyncBlueprint, AsyncStorageObject |
| 21 | +from runloop_api_client.sdk import AsyncDevbox, AsyncSnapshot, AsyncBlueprint, AsyncStorageObject, AsyncAgent |
21 | 22 | from runloop_api_client.sdk.async_ import ( |
22 | 23 | AsyncDevboxOps, |
23 | 24 | AsyncRunloopSDK, |
24 | 25 | AsyncSnapshotOps, |
25 | 26 | AsyncBlueprintOps, |
26 | 27 | AsyncStorageObjectOps, |
| 28 | + AsyncAgentOps, |
27 | 29 | ) |
28 | 30 | from runloop_api_client.lib.polling import PollingConfig |
29 | 31 |
|
@@ -515,13 +517,126 @@ async def test_upload_from_dir_with_string_path( |
515 | 517 | mock_async_client.objects.complete.assert_awaited_once() |
516 | 518 |
|
517 | 519 |
|
| 520 | +class TestAsyncAgentClient: |
| 521 | + """Tests for AsyncAgentClient class.""" |
| 522 | + |
| 523 | + @pytest.mark.asyncio |
| 524 | + async def test_create(self, mock_async_client: AsyncMock, agent_view: MockAgentView) -> None: |
| 525 | + """Test create method.""" |
| 526 | + mock_async_client.agents.create = AsyncMock(return_value=agent_view) |
| 527 | + |
| 528 | + client = AsyncAgentOps(mock_async_client) |
| 529 | + agent = await client.create( |
| 530 | + name="test-agent", |
| 531 | + metadata={"key": "value"}, |
| 532 | + ) |
| 533 | + |
| 534 | + assert isinstance(agent, AsyncAgent) |
| 535 | + assert agent.id == "agent_123" |
| 536 | + mock_async_client.agents.create.assert_called_once() |
| 537 | + |
| 538 | + def test_from_id(self, mock_async_client: AsyncMock) -> None: |
| 539 | + """Test from_id method.""" |
| 540 | + client = AsyncAgentOps(mock_async_client) |
| 541 | + agent = client.from_id("agent_123") |
| 542 | + |
| 543 | + assert isinstance(agent, AsyncAgent) |
| 544 | + assert agent.id == "agent_123" |
| 545 | + |
| 546 | + @pytest.mark.asyncio |
| 547 | + async def test_list(self, mock_async_client: AsyncMock) -> None: |
| 548 | + """Test list method.""" |
| 549 | + # Create three agent views with different data |
| 550 | + agent_view_1 = MockAgentView( |
| 551 | + id="agent_001", |
| 552 | + name="first-agent", |
| 553 | + create_time_ms=1234567890000, |
| 554 | + is_public=False, |
| 555 | + source=None, |
| 556 | + ) |
| 557 | + agent_view_2 = MockAgentView( |
| 558 | + id="agent_002", |
| 559 | + name="second-agent", |
| 560 | + create_time_ms=1234567891000, |
| 561 | + is_public=True, |
| 562 | + source={"type": "git", "git": {"repository": "https://github.com/example/repo"}}, |
| 563 | + ) |
| 564 | + agent_view_3 = MockAgentView( |
| 565 | + id="agent_003", |
| 566 | + name="third-agent", |
| 567 | + create_time_ms=1234567892000, |
| 568 | + is_public=False, |
| 569 | + source={"type": "npm", "npm": {"package_name": "example-package"}}, |
| 570 | + ) |
| 571 | + |
| 572 | + page = SimpleNamespace(agents=[agent_view_1, agent_view_2, agent_view_3]) |
| 573 | + mock_async_client.agents.list = AsyncMock(return_value=page) |
| 574 | + |
| 575 | + # Mock retrieve to return the corresponding agent_view when called |
| 576 | + async def mock_retrieve(agent_id, **kwargs): |
| 577 | + if agent_id == "agent_001": |
| 578 | + return agent_view_1 |
| 579 | + elif agent_id == "agent_002": |
| 580 | + return agent_view_2 |
| 581 | + elif agent_id == "agent_003": |
| 582 | + return agent_view_3 |
| 583 | + return None |
| 584 | + |
| 585 | + mock_async_client.agents.retrieve = AsyncMock(side_effect=mock_retrieve) |
| 586 | + |
| 587 | + client = AsyncAgentOps(mock_async_client) |
| 588 | + agents = await client.list( |
| 589 | + limit=10, |
| 590 | + starting_after="agent_000", |
| 591 | + ) |
| 592 | + |
| 593 | + # Verify we got three agents |
| 594 | + assert len(agents) == 3 |
| 595 | + assert all(isinstance(agent, AsyncAgent) for agent in agents) |
| 596 | + |
| 597 | + # Verify the agent IDs |
| 598 | + assert agents[0].id == "agent_001" |
| 599 | + assert agents[1].id == "agent_002" |
| 600 | + assert agents[2].id == "agent_003" |
| 601 | + |
| 602 | + # Test that get_info() retrieves the AgentView for the first agent |
| 603 | + info = await agents[0].get_info() |
| 604 | + assert info.id == "agent_001" |
| 605 | + assert info.name == "first-agent" |
| 606 | + assert info.create_time_ms == 1234567890000 |
| 607 | + assert info.is_public is False |
| 608 | + assert info.source is None |
| 609 | + |
| 610 | + # Test that get_info() retrieves the AgentView for the second agent |
| 611 | + info = await agents[1].get_info() |
| 612 | + assert info.id == "agent_002" |
| 613 | + assert info.name == "second-agent" |
| 614 | + assert info.create_time_ms == 1234567891000 |
| 615 | + assert info.is_public is True |
| 616 | + assert info.source == {"type": "git", "git": {"repository": "https://github.com/example/repo"}} |
| 617 | + |
| 618 | + # Test that get_info() retrieves the AgentView for the third agent |
| 619 | + info = await agents[2].get_info() |
| 620 | + assert info.id == "agent_003" |
| 621 | + assert info.name == "third-agent" |
| 622 | + assert info.create_time_ms == 1234567892000 |
| 623 | + assert info.is_public is False |
| 624 | + assert info.source == {"type": "npm", "npm": {"package_name": "example-package"}} |
| 625 | + |
| 626 | + # Verify that agents.retrieve was called three times (once for each get_info) |
| 627 | + assert mock_async_client.agents.retrieve.call_count == 3 |
| 628 | + |
| 629 | + mock_async_client.agents.list.assert_called_once() |
| 630 | + |
| 631 | + |
518 | 632 | class TestAsyncRunloopSDK: |
519 | 633 | """Tests for AsyncRunloopSDK class.""" |
520 | 634 |
|
521 | 635 | def test_init(self) -> None: |
522 | 636 | """Test AsyncRunloopSDK initialization.""" |
523 | 637 | sdk = AsyncRunloopSDK(bearer_token="test-token") |
524 | 638 | assert sdk.api is not None |
| 639 | + assert isinstance(sdk.agent, AsyncAgentOps) |
525 | 640 | assert isinstance(sdk.devbox, AsyncDevboxOps) |
526 | 641 | assert isinstance(sdk.snapshot, AsyncSnapshotOps) |
527 | 642 | assert isinstance(sdk.blueprint, AsyncBlueprintOps) |
|
0 commit comments