Files
aitbc/apps/stubs/global-ai-agents/tests/test_integration_global_ai_agents.py
aitbc 3897bcbf24
Some checks failed
CLI Tests / test-cli (push) Failing after 4s
Deploy to Testnet / deploy-testnet (push) Successful in 1m40s
Documentation Validation / validate-docs (push) Failing after 12s
Documentation Validation / validate-policies-strict (push) Successful in 4s
Integration Tests / test-service-integration (push) Successful in 2m42s
Package Tests / Python package - aitbc-agent-sdk (push) Failing after 34s
Package Tests / Python package - aitbc-core (push) Successful in 27s
Package Tests / Python package - aitbc-crypto (push) Successful in 13s
Package Tests / Python package - aitbc-sdk (push) Successful in 16s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 8s
Package Tests / JavaScript package - aitbc-token (push) Successful in 18s
Python Tests / test-python (push) Failing after 50s
Security Scanning / security-scan (push) Failing after 43s
Multi-Node Stress Testing / stress-test (push) Successful in 12s
Cross-Node Transaction Testing / transaction-test (push) Successful in 9s
refactor: move version to separate module and improve logging
- Created aitbc/_version.py with centralized version definition
- Updated aitbc/__init__.py to import __version__ from _version module
- Updated constants.py to use __version__ for PACKAGE_VERSION
- Replaced print() calls with logger in decorators.py, events.py, queue_manager.py, and state.py
- Added logger initialization using get_logger(__name__) in config.py, decorators.py, events.py, queue_manager.py, and state.py
- Added cli/commands
2026-05-11 20:12:01 +02:00

591 lines
17 KiB
Python

"""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, timezone, 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.now(timezone.utc)
)
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.now(timezone.utc)
)
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.now(timezone.utc)
)
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.now(timezone.utc)
)
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.now(timezone.utc),
expires_at=datetime.now(timezone.utc) + 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.now(timezone.utc),
expires_at=datetime.now(timezone.utc) + 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.now(timezone.utc),
expires_at=datetime.now(timezone.utc) + 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.now(timezone.utc),
expires_at=datetime.now(timezone.utc) + 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.now(timezone.utc),
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.now(timezone.utc),
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