Add sys import to test files and remove obsolete integration tests
Some checks failed
API Endpoint Tests / test-api-endpoints (push) Successful in 9s
Blockchain Synchronization Verification / sync-verification (push) Failing after 1s
CLI Tests / test-cli (push) Failing after 3s
Documentation Validation / validate-docs (push) Successful in 6s
Documentation Validation / validate-policies-strict (push) Successful in 2s
Integration Tests / test-service-integration (push) Successful in 40s
Multi-Node Blockchain Health Monitoring / health-check (push) Successful in 1s
P2P Network Verification / p2p-verification (push) Successful in 2s
Production Tests / Production Integration Tests (push) Successful in 21s
Python Tests / test-python (push) Successful in 13s
Security Scanning / security-scan (push) Failing after 46s
Smart Contract Tests / test-solidity (map[name:aitbc-token path:packages/solidity/aitbc-token]) (push) Successful in 17s
Smart Contract Tests / lint-solidity (push) Successful in 10s

- Add sys import to 29 test files across agent-coordinator, blockchain-event-bridge, blockchain-node, and coordinator-api
- Remove apps/blockchain-event-bridge/tests/test_integration.py (obsolete bridge integration tests)
- Remove apps/coordinator-api/tests/test_integration.py (obsolete API integration tests)
- Implement GPU registration in marketplace_gpu.py with GPURegistry model persistence
This commit is contained in:
aitbc
2026-04-23 16:43:17 +02:00
parent b8b1454573
commit e60cc3226c
134 changed files with 14321 additions and 1873 deletions

View File

@@ -0,0 +1 @@
"""Global AI agents service tests"""

View File

@@ -0,0 +1,186 @@
"""Edge case and error handling tests for global AI agents service"""
import pytest
import sys
import sys
from pathlib import Path
from fastapi.testclient import TestClient
from datetime import datetime, timedelta
from main import app, Agent, AgentMessage, CollaborationSession, AgentPerformance, global_agents, agent_messages, collaboration_sessions, agent_performance
@pytest.fixture(autouse=True)
def reset_state():
"""Reset global state before each test"""
global_agents.clear()
agent_messages.clear()
collaboration_sessions.clear()
agent_performance.clear()
yield
global_agents.clear()
agent_messages.clear()
collaboration_sessions.clear()
agent_performance.clear()
@pytest.mark.unit
def test_agent_empty_name():
"""Test Agent with empty name"""
agent = Agent(
agent_id="agent_123",
name="",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
assert agent.name == ""
@pytest.mark.unit
def test_agent_negative_performance_score():
"""Test Agent with negative performance score"""
agent = Agent(
agent_id="agent_123",
name="Test Agent",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=-4.5
)
assert agent.performance_score == -4.5
@pytest.mark.unit
def test_agent_performance_out_of_range_score():
"""Test AgentPerformance with out of range scores"""
performance = AgentPerformance(
agent_id="agent_123",
timestamp=datetime.utcnow(),
tasks_completed=10,
response_time_ms=50.5,
accuracy_score=2.0,
collaboration_score=2.0,
resource_usage={}
)
assert performance.accuracy_score == 2.0
assert performance.collaboration_score == 2.0
@pytest.mark.unit
def test_agent_message_empty_content():
"""Test AgentMessage with empty content"""
message = AgentMessage(
message_id="msg_123",
sender_id="agent_123",
recipient_id="agent_456",
message_type="request",
content={},
priority="high",
language="english",
timestamp=datetime.utcnow()
)
assert message.content == {}
@pytest.mark.integration
def test_list_agents_with_no_agents():
"""Test listing agents when no agents exist"""
client = TestClient(app)
response = client.get("/api/v1/agents")
assert response.status_code == 200
data = response.json()
assert data["total_agents"] == 0
@pytest.mark.integration
def test_get_agent_messages_agent_not_found():
"""Test getting messages for nonexistent agent"""
client = TestClient(app)
response = client.get("/api/v1/messages/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_get_collaboration_not_found():
"""Test getting nonexistent collaboration session"""
client = TestClient(app)
response = client.get("/api/v1/collaborations/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_send_collaboration_message_session_not_found():
"""Test sending message to nonexistent collaboration session"""
client = TestClient(app)
response = client.post("/api/v1/collaborations/nonexistent/message", params={"sender_id": "agent_123"}, json={"content": "test"})
assert response.status_code == 404
@pytest.mark.integration
def test_send_collaboration_message_sender_not_participant():
"""Test sending message from non-participant"""
client = TestClient(app)
# Register agent and create collaboration
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
session = CollaborationSession(
session_id="session_123",
participants=["agent_123"],
session_type="research",
objective="Research task",
created_at=datetime.utcnow(),
expires_at=datetime.utcnow() + timedelta(hours=1),
status="active"
)
client.post("/api/v1/collaborations/create", json=session.model_dump(mode='json'))
response = client.post("/api/v1/collaborations/session_123/message", params={"sender_id": "nonexistent"}, json={"content": "test"})
assert response.status_code == 400
@pytest.mark.integration
def test_get_agent_performance_agent_not_found():
"""Test getting performance for nonexistent agent"""
client = TestClient(app)
response = client.get("/api/v1/performance/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_dashboard_with_no_data():
"""Test dashboard with no data"""
client = TestClient(app)
response = client.get("/api/v1/network/dashboard")
assert response.status_code == 200
data = response.json()
assert data["dashboard"]["network_overview"]["total_agents"] == 0
@pytest.mark.integration
def test_optimize_network_with_no_agents():
"""Test network optimization with no agents"""
client = TestClient(app)
response = client.get("/api/v1/network/optimize")
assert response.status_code == 200
data = response.json()
assert "optimization_results" in data

View File

@@ -0,0 +1,590 @@
"""Integration tests for global AI agents service"""
import pytest
import sys
import sys
from pathlib import Path
from fastapi.testclient import TestClient
from datetime import datetime, timedelta
from main import app, Agent, AgentMessage, CollaborationSession, AgentPerformance, global_agents, agent_messages, collaboration_sessions, agent_performance
@pytest.fixture(autouse=True)
def reset_state():
"""Reset global state before each test"""
global_agents.clear()
agent_messages.clear()
collaboration_sessions.clear()
agent_performance.clear()
yield
global_agents.clear()
agent_messages.clear()
collaboration_sessions.clear()
agent_performance.clear()
@pytest.mark.integration
def test_root_endpoint():
"""Test root endpoint"""
client = TestClient(app)
response = client.get("/")
assert response.status_code == 200
data = response.json()
assert data["service"] == "AITBC Global AI Agent Communication Service"
assert data["status"] == "running"
@pytest.mark.integration
def test_health_check_endpoint():
"""Test health check endpoint"""
client = TestClient(app)
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
assert "total_agents" in data
@pytest.mark.integration
def test_register_agent():
"""Test registering a new agent"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Test Agent",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
response = client.post("/api/v1/agents/register", json=agent.model_dump())
assert response.status_code == 200
data = response.json()
assert data["agent_id"] == "agent_123"
assert data["status"] == "registered"
@pytest.mark.integration
def test_register_duplicate_agent():
"""Test registering duplicate agent"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Test Agent",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
response = client.post("/api/v1/agents/register", json=agent.model_dump())
assert response.status_code == 400
@pytest.mark.integration
def test_list_agents():
"""Test listing all agents"""
client = TestClient(app)
response = client.get("/api/v1/agents")
assert response.status_code == 200
data = response.json()
assert "agents" in data
assert "total_agents" in data
@pytest.mark.integration
def test_list_agents_with_filters():
"""Test listing agents with filters"""
client = TestClient(app)
# Register an agent first
agent = Agent(
agent_id="agent_123",
name="Test Agent",
type="trading",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
response = client.get("/api/v1/agents?region=us-east-1&type=trading&status=active")
assert response.status_code == 200
data = response.json()
assert "filters" in data
@pytest.mark.integration
def test_get_agent():
"""Test getting specific agent"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Test Agent",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
response = client.get("/api/v1/agents/agent_123")
assert response.status_code == 200
data = response.json()
assert data["agent_id"] == "agent_123"
@pytest.mark.integration
def test_get_agent_not_found():
"""Test getting nonexistent agent"""
client = TestClient(app)
response = client.get("/api/v1/agents/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_send_direct_message():
"""Test sending direct message"""
client = TestClient(app)
# Register two agents
agent1 = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
agent2 = Agent(
agent_id="agent_456",
name="Agent 2",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent1.model_dump())
client.post("/api/v1/agents/register", json=agent2.model_dump())
message = AgentMessage(
message_id="msg_123",
sender_id="agent_123",
recipient_id="agent_456",
message_type="request",
content={"data": "test"},
priority="high",
language="english",
timestamp=datetime.utcnow()
)
response = client.post("/api/v1/messages/send", json=message.model_dump(mode='json'))
assert response.status_code == 200
data = response.json()
assert data["message_id"] == "msg_123"
assert data["status"] == "delivered"
@pytest.mark.integration
def test_send_broadcast_message():
"""Test sending broadcast message"""
client = TestClient(app)
# Register two agents
agent1 = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
agent2 = Agent(
agent_id="agent_456",
name="Agent 2",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent1.model_dump())
client.post("/api/v1/agents/register", json=agent2.model_dump())
message = AgentMessage(
message_id="msg_123",
sender_id="agent_123",
recipient_id=None,
message_type="broadcast",
content={"data": "test"},
priority="medium",
language="english",
timestamp=datetime.utcnow()
)
response = client.post("/api/v1/messages/send", json=message.model_dump(mode='json'))
assert response.status_code == 200
data = response.json()
assert data["message_id"] == "msg_123"
@pytest.mark.integration
def test_send_message_sender_not_found():
"""Test sending message with nonexistent sender"""
client = TestClient(app)
message = AgentMessage(
message_id="msg_123",
sender_id="nonexistent",
recipient_id="agent_456",
message_type="request",
content={"data": "test"},
priority="high",
language="english",
timestamp=datetime.utcnow()
)
response = client.post("/api/v1/messages/send", json=message.model_dump(mode='json'))
assert response.status_code == 400
@pytest.mark.integration
def test_send_message_recipient_not_found():
"""Test sending message with nonexistent recipient"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
message = AgentMessage(
message_id="msg_123",
sender_id="agent_123",
recipient_id="nonexistent",
message_type="request",
content={"data": "test"},
priority="high",
language="english",
timestamp=datetime.utcnow()
)
response = client.post("/api/v1/messages/send", json=message.model_dump(mode='json'))
assert response.status_code == 400
@pytest.mark.integration
def test_get_agent_messages():
"""Test getting agent messages"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
response = client.get("/api/v1/messages/agent_123")
assert response.status_code == 200
data = response.json()
assert data["agent_id"] == "agent_123"
@pytest.mark.integration
def test_get_agent_messages_with_limit():
"""Test getting agent messages with limit parameter"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
response = client.get("/api/v1/messages/agent_123?limit=10")
assert response.status_code == 200
@pytest.mark.integration
def test_create_collaboration():
"""Test creating collaboration session"""
client = TestClient(app)
# Register two agents
agent1 = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
agent2 = Agent(
agent_id="agent_456",
name="Agent 2",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent1.model_dump())
client.post("/api/v1/agents/register", json=agent2.model_dump())
session = CollaborationSession(
session_id="session_123",
participants=["agent_123", "agent_456"],
session_type="task_force",
objective="Complete task",
created_at=datetime.utcnow(),
expires_at=datetime.utcnow() + timedelta(hours=1),
status="active"
)
response = client.post("/api/v1/collaborations/create", json=session.model_dump(mode='json'))
assert response.status_code == 200
data = response.json()
assert data["session_id"] == "session_123"
@pytest.mark.integration
def test_create_collaboration_participant_not_found():
"""Test creating collaboration with nonexistent participant"""
client = TestClient(app)
session = CollaborationSession(
session_id="session_123",
participants=["nonexistent"],
session_type="task_force",
objective="Complete task",
created_at=datetime.utcnow(),
expires_at=datetime.utcnow() + timedelta(hours=1),
status="active"
)
response = client.post("/api/v1/collaborations/create", json=session.model_dump(mode='json'))
assert response.status_code == 400
@pytest.mark.integration
def test_get_collaboration():
"""Test getting collaboration session"""
client = TestClient(app)
# Register agents and create collaboration
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
session = CollaborationSession(
session_id="session_123",
participants=["agent_123"],
session_type="research",
objective="Research task",
created_at=datetime.utcnow(),
expires_at=datetime.utcnow() + timedelta(hours=1),
status="active"
)
client.post("/api/v1/collaborations/create", json=session.model_dump(mode='json'))
response = client.get("/api/v1/collaborations/session_123")
assert response.status_code == 200
data = response.json()
assert data["session_id"] == "session_123"
@pytest.mark.integration
def test_send_collaboration_message():
"""Test sending message within collaboration session"""
client = TestClient(app)
# Register agent and create collaboration
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
session = CollaborationSession(
session_id="session_123",
participants=["agent_123"],
session_type="research",
objective="Research task",
created_at=datetime.utcnow(),
expires_at=datetime.utcnow() + timedelta(hours=1),
status="active"
)
client.post("/api/v1/collaborations/create", json=session.model_dump(mode='json'))
response = client.post("/api/v1/collaborations/session_123/message", params={"sender_id": "agent_123"}, json={"content": "test message"})
assert response.status_code == 200
data = response.json()
assert data["status"] == "delivered"
@pytest.mark.integration
def test_record_agent_performance():
"""Test recording agent performance"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
performance = AgentPerformance(
agent_id="agent_123",
timestamp=datetime.utcnow(),
tasks_completed=10,
response_time_ms=50.5,
accuracy_score=0.95,
collaboration_score=0.9,
resource_usage={"cpu": 50.0}
)
response = client.post("/api/v1/performance/record", json=performance.model_dump(mode='json'))
assert response.status_code == 200
data = response.json()
assert data["performance_id"]
assert data["status"] == "recorded"
@pytest.mark.integration
def test_record_performance_agent_not_found():
"""Test recording performance for nonexistent agent"""
client = TestClient(app)
performance = AgentPerformance(
agent_id="nonexistent",
timestamp=datetime.utcnow(),
tasks_completed=10,
response_time_ms=50.5,
accuracy_score=0.95,
collaboration_score=0.9,
resource_usage={}
)
response = client.post("/api/v1/performance/record", json=performance.model_dump(mode='json'))
assert response.status_code == 404
@pytest.mark.integration
def test_get_agent_performance():
"""Test getting agent performance"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
response = client.get("/api/v1/performance/agent_123")
assert response.status_code == 200
data = response.json()
assert data["agent_id"] == "agent_123"
@pytest.mark.integration
def test_get_agent_performance_hours_parameter():
"""Test getting agent performance with custom hours parameter"""
client = TestClient(app)
agent = Agent(
agent_id="agent_123",
name="Agent 1",
type="ai",
region="us-east-1",
capabilities=["trading"],
status="active",
languages=["english"],
specialization="trading",
performance_score=4.5
)
client.post("/api/v1/agents/register", json=agent.model_dump())
response = client.get("/api/v1/performance/agent_123?hours=12")
assert response.status_code == 200
data = response.json()
assert data["period_hours"] == 12
@pytest.mark.integration
def test_get_network_dashboard():
"""Test getting network dashboard"""
client = TestClient(app)
response = client.get("/api/v1/network/dashboard")
assert response.status_code == 200
data = response.json()
assert "dashboard" in data
@pytest.mark.integration
def test_optimize_network():
"""Test network optimization"""
client = TestClient(app)
response = client.get("/api/v1/network/optimize")
assert response.status_code == 200
data = response.json()
assert "optimization_results" in data

View File

@@ -0,0 +1,158 @@
"""Unit tests for global AI agents service"""
import pytest
import sys
import sys
from pathlib import Path
from datetime import datetime
from main import app, Agent, AgentMessage, CollaborationSession, AgentPerformance
@pytest.mark.unit
def test_app_initialization():
"""Test that the FastAPI app initializes correctly"""
assert app is not None
assert app.title == "AITBC Global AI Agent Communication Service"
assert app.version == "1.0.0"
@pytest.mark.unit
def test_agent_model():
"""Test Agent model"""
agent = Agent(
agent_id="agent_123",
name="Test Agent",
type="ai",
region="us-east-1",
capabilities=["trading", "analysis"],
status="active",
languages=["english", "chinese"],
specialization="trading",
performance_score=4.5
)
assert agent.agent_id == "agent_123"
assert agent.name == "Test Agent"
assert agent.type == "ai"
assert agent.status == "active"
assert agent.performance_score == 4.5
@pytest.mark.unit
def test_agent_empty_capabilities():
"""Test Agent with empty capabilities"""
agent = Agent(
agent_id="agent_123",
name="Test Agent",
type="ai",
region="us-east-1",
capabilities=[],
status="active",
languages=["english"],
specialization="general",
performance_score=4.5
)
assert agent.capabilities == []
@pytest.mark.unit
def test_agent_message_model():
"""Test AgentMessage model"""
message = AgentMessage(
message_id="msg_123",
sender_id="agent_123",
recipient_id="agent_456",
message_type="request",
content={"data": "test"},
priority="high",
language="english",
timestamp=datetime.utcnow()
)
assert message.message_id == "msg_123"
assert message.sender_id == "agent_123"
assert message.recipient_id == "agent_456"
assert message.message_type == "request"
assert message.priority == "high"
@pytest.mark.unit
def test_agent_message_broadcast():
"""Test AgentMessage with None recipient (broadcast)"""
message = AgentMessage(
message_id="msg_123",
sender_id="agent_123",
recipient_id=None,
message_type="broadcast",
content={"data": "test"},
priority="medium",
language="english",
timestamp=datetime.utcnow()
)
assert message.recipient_id is None
@pytest.mark.unit
def test_collaboration_session_model():
"""Test CollaborationSession model"""
session = CollaborationSession(
session_id="session_123",
participants=["agent_123", "agent_456"],
session_type="task_force",
objective="Complete trading task",
created_at=datetime.utcnow(),
expires_at=datetime.utcnow(),
status="active"
)
assert session.session_id == "session_123"
assert session.participants == ["agent_123", "agent_456"]
assert session.session_type == "task_force"
@pytest.mark.unit
def test_collaboration_session_empty_participants():
"""Test CollaborationSession with empty participants"""
session = CollaborationSession(
session_id="session_123",
participants=[],
session_type="research",
objective="Research task",
created_at=datetime.utcnow(),
expires_at=datetime.utcnow(),
status="active"
)
assert session.participants == []
@pytest.mark.unit
def test_agent_performance_model():
"""Test AgentPerformance model"""
performance = AgentPerformance(
agent_id="agent_123",
timestamp=datetime.utcnow(),
tasks_completed=10,
response_time_ms=50.5,
accuracy_score=0.95,
collaboration_score=0.9,
resource_usage={"cpu": 50.0, "memory": 60.0}
)
assert performance.agent_id == "agent_123"
assert performance.tasks_completed == 10
assert performance.response_time_ms == 50.5
assert performance.accuracy_score == 0.95
@pytest.mark.unit
def test_agent_performance_negative_values():
"""Test AgentPerformance with negative values"""
performance = AgentPerformance(
agent_id="agent_123",
timestamp=datetime.utcnow(),
tasks_completed=-10,
response_time_ms=-50.5,
accuracy_score=-0.95,
collaboration_score=-0.9,
resource_usage={}
)
assert performance.tasks_completed == -10
assert performance.response_time_ms == -50.5