|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import pytest |
6 | | -from unittest.mock import Mock, patch |
| 6 | +from unittest.mock import Mock, patch, MagicMock |
7 | 7 | from bnbagent import ERC8004Agent, AgentEndpoint |
| 8 | +import requests |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class TestERC8004Agent: |
@@ -300,3 +301,184 @@ def test_generate_agent_uri_requires_endpoints(self, sdk): |
300 | 301 |
|
301 | 302 | with pytest.raises(ValueError, match="endpoints is required"): |
302 | 303 | sdk.generate_agent_uri(name="Test", description="Test", endpoints=[]) |
| 304 | + |
| 305 | + def test_get_all_agents(self, sdk): |
| 306 | + """Test get_all_agents API call""" |
| 307 | + with patch("bnbagent.erc8004_agent.requests.get") as mock_get: |
| 308 | + mock_response = Mock() |
| 309 | + mock_response.json.return_value = { |
| 310 | + "items": [ |
| 311 | + {"token_id": 1, "name": "Agent 1"}, |
| 312 | + {"token_id": 2, "name": "Agent 2"}, |
| 313 | + ], |
| 314 | + "total": 2, |
| 315 | + "limit": 10, |
| 316 | + "offset": 0, |
| 317 | + } |
| 318 | + mock_response.raise_for_status = Mock() |
| 319 | + mock_get.return_value = mock_response |
| 320 | + |
| 321 | + result = sdk.get_all_agents(limit=10, offset=0) |
| 322 | + |
| 323 | + assert "items" in result |
| 324 | + assert len(result["items"]) == 2 |
| 325 | + assert result["total"] == 2 |
| 326 | + |
| 327 | + def test_get_all_agents_with_pagination(self, sdk): |
| 328 | + """Test get_all_agents with pagination parameters""" |
| 329 | + with patch("bnbagent.erc8004_agent.requests.get") as mock_get: |
| 330 | + mock_response = Mock() |
| 331 | + mock_response.json.return_value = { |
| 332 | + "items": [{"token_id": 11, "name": "Agent 11"}], |
| 333 | + "total": 15, |
| 334 | + "limit": 5, |
| 335 | + "offset": 10, |
| 336 | + } |
| 337 | + mock_response.raise_for_status = Mock() |
| 338 | + mock_get.return_value = mock_response |
| 339 | + |
| 340 | + result = sdk.get_all_agents(limit=5, offset=10) |
| 341 | + |
| 342 | + assert result["offset"] == 10 |
| 343 | + assert result["limit"] == 5 |
| 344 | + mock_get.assert_called_once() |
| 345 | + call_args = mock_get.call_args |
| 346 | + assert call_args[1]["params"]["limit"] == 5 |
| 347 | + assert call_args[1]["params"]["offset"] == 10 |
| 348 | + |
| 349 | + def test_get_all_agents_connection_error(self, sdk): |
| 350 | + """Test get_all_agents handles connection errors""" |
| 351 | + with patch("bnbagent.erc8004_agent.requests.get") as mock_get: |
| 352 | + mock_get.side_effect = requests.exceptions.ConnectionError("Network error") |
| 353 | + |
| 354 | + with pytest.raises(ConnectionError, match="8004scan API request failed"): |
| 355 | + sdk.get_all_agents() |
| 356 | + |
| 357 | + def test_get_all_agents_uses_network_chain_id(self, sdk): |
| 358 | + """Test get_all_agents uses chain_id from network config""" |
| 359 | + with patch("bnbagent.erc8004_agent.requests.get") as mock_get: |
| 360 | + mock_response = Mock() |
| 361 | + mock_response.json.return_value = {"items": [], "total": 0} |
| 362 | + mock_response.raise_for_status = Mock() |
| 363 | + mock_get.return_value = mock_response |
| 364 | + |
| 365 | + sdk.get_all_agents() |
| 366 | + |
| 367 | + call_args = mock_get.call_args |
| 368 | + # Should use chain_id from network config (97 for bsc-testnet) |
| 369 | + assert call_args[1]["params"]["chain_id"] == 97 |
| 370 | + |
| 371 | + def test_get_local_agent_info_not_found(self, sdk): |
| 372 | + """Test get_local_agent_info returns None for non-existent agent""" |
| 373 | + with patch.object(sdk.state_manager, "get", return_value=[]): |
| 374 | + result = sdk.get_local_agent_info("NonExistent Agent") |
| 375 | + assert result is None |
| 376 | + |
| 377 | + def test_get_local_agent_info_found(self, sdk): |
| 378 | + """Test get_local_agent_info returns agent info when found""" |
| 379 | + registered_agents = [ |
| 380 | + { |
| 381 | + "name": "Test Agent", |
| 382 | + "agent_uri": "data:application/json;base64,xxx", |
| 383 | + "agent_id": 1, |
| 384 | + "transaction_hash": "0x123", |
| 385 | + } |
| 386 | + ] |
| 387 | + with patch.object(sdk.state_manager, "get", return_value=registered_agents): |
| 388 | + result = sdk.get_local_agent_info("Test Agent") |
| 389 | + assert result is not None |
| 390 | + assert result["name"] == "Test Agent" |
| 391 | + assert result["agent_id"] == 1 |
| 392 | + |
| 393 | + def test_get_local_agent_info_empty_name(self, sdk): |
| 394 | + """Test get_local_agent_info with empty name""" |
| 395 | + result = sdk.get_local_agent_info("") |
| 396 | + assert result is None |
| 397 | + |
| 398 | + result = sdk.get_local_agent_info(None) |
| 399 | + assert result is None |
| 400 | + |
| 401 | + def test_wallet_address_property(self, sdk, mock_wallet_provider): |
| 402 | + """Test wallet_address property""" |
| 403 | + assert sdk.wallet_address == mock_wallet_provider.address |
| 404 | + |
| 405 | + def test_contract_address_property(self, sdk): |
| 406 | + """Test contract_address property""" |
| 407 | + assert sdk.contract_address == self.DEFAULT_CONTRACT_ADDRESS |
| 408 | + |
| 409 | + def test_network_property(self, sdk): |
| 410 | + """Test network property returns config""" |
| 411 | + network = sdk.network |
| 412 | + assert "name" in network |
| 413 | + assert "chain_id" in network |
| 414 | + assert network["name"] == "bsc-testnet" |
| 415 | + |
| 416 | + def test_generate_agent_uri_with_supported_trust(self, sdk): |
| 417 | + """Test generate_agent_uri with supportedTrust field""" |
| 418 | + agent_uri = sdk.generate_agent_uri( |
| 419 | + name="Trust Agent", |
| 420 | + description="Agent with trust mechanisms", |
| 421 | + endpoints=[ |
| 422 | + AgentEndpoint( |
| 423 | + name="A2A", |
| 424 | + endpoint="https://agent.example/.well-known/agent-card.json", |
| 425 | + ) |
| 426 | + ], |
| 427 | + supported_trust=["reputation", "crypto-economic"], |
| 428 | + ) |
| 429 | + |
| 430 | + assert isinstance(agent_uri, str) |
| 431 | + agent_data = sdk.parse_agent_uri(agent_uri) |
| 432 | + assert "supportedTrust" in agent_data |
| 433 | + assert "reputation" in agent_data["supportedTrust"] |
| 434 | + |
| 435 | + def test_generate_agent_uri_with_image(self, sdk): |
| 436 | + """Test generate_agent_uri with image field""" |
| 437 | + agent_uri = sdk.generate_agent_uri( |
| 438 | + name="Image Agent", |
| 439 | + description="Agent with image", |
| 440 | + endpoints=[ |
| 441 | + AgentEndpoint( |
| 442 | + name="A2A", |
| 443 | + endpoint="https://agent.example/.well-known/agent-card.json", |
| 444 | + ) |
| 445 | + ], |
| 446 | + image="https://example.com/agent-image.png", |
| 447 | + ) |
| 448 | + |
| 449 | + agent_data = sdk.parse_agent_uri(agent_uri) |
| 450 | + assert agent_data["image"] == "https://example.com/agent-image.png" |
| 451 | + |
| 452 | + |
| 453 | +class TestERC8004AgentInitialization: |
| 454 | + """Test cases for SDK initialization edge cases""" |
| 455 | + |
| 456 | + def test_invalid_network_raises_error(self): |
| 457 | + """Test that invalid network raises ValueError""" |
| 458 | + mock_wallet = Mock() |
| 459 | + mock_wallet.address = "0x" + "0" * 40 |
| 460 | + |
| 461 | + with pytest.raises(ValueError, match="Unknown network"): |
| 462 | + with patch("bnbagent.erc8004_agent.Web3") as mock_web3_class: |
| 463 | + mock_web3 = Mock() |
| 464 | + mock_web3.is_connected.return_value = True |
| 465 | + mock_web3_class.return_value = mock_web3 |
| 466 | + ERC8004Agent(wallet_provider=mock_wallet, network="invalid-network") |
| 467 | + |
| 468 | + def test_missing_wallet_provider_raises_error(self): |
| 469 | + """Test that missing wallet_provider raises ValueError""" |
| 470 | + with pytest.raises(ValueError, match="wallet_provider is required"): |
| 471 | + ERC8004Agent(wallet_provider=None, network="bsc-testnet") |
| 472 | + |
| 473 | + def test_rpc_connection_failure(self): |
| 474 | + """Test that RPC connection failure raises ConnectionError""" |
| 475 | + mock_wallet = Mock() |
| 476 | + mock_wallet.address = "0x" + "0" * 40 |
| 477 | + |
| 478 | + with patch("bnbagent.erc8004_agent.Web3") as mock_web3_class: |
| 479 | + mock_web3 = Mock() |
| 480 | + mock_web3.is_connected.return_value = False |
| 481 | + mock_web3_class.return_value = mock_web3 |
| 482 | + |
| 483 | + with pytest.raises(ConnectionError, match="Failed to connect to RPC"): |
| 484 | + ERC8004Agent(wallet_provider=mock_wallet, network="bsc-testnet") |
0 commit comments