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

@@ -98,91 +98,82 @@ async def get_supported_chains():
@app.post("/api/v1/miners/register")
async def register_miner(registration: MinerRegistration):
"""Register a miner in the marketplace"""
try:
miner_id = registration.miner_id
if miner_id in miner_registrations:
# Update existing registration
miner_registrations[miner_id].update(registration.dict())
else:
# New registration
miner_registrations[miner_id] = registration.dict()
miner_registrations[miner_id]["registered_at"] = datetime.now().isoformat()
return JSONResponse({
"success": True,
"miner_id": miner_id,
"status": "registered",
"registered_chains": registration.preferred_chains,
"message": "Miner registered successfully in marketplace"
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Registration failed: {str(e)}")
miner_id = registration.miner_id
if miner_id in miner_registrations:
# Update existing registration
miner_registrations[miner_id].update(registration.model_dump())
else:
# New registration
miner_registrations[miner_id] = registration.model_dump()
miner_registrations[miner_id]["registered_at"] = datetime.now().isoformat()
return JSONResponse({
"success": True,
"miner_id": miner_id,
"status": "registered",
"registered_chains": registration.preferred_chains,
"message": "Miner registered successfully in marketplace"
})
@app.post("/api/v1/offerings/create")
async def create_gpu_offering(offering: GPUOffering):
"""Miners create GPU offerings with chain selection"""
try:
offering_id = str(uuid.uuid4())
# Validate chains
invalid_chains = [c for c in offering.chains if c not in SUPPORTED_CHAINS]
if invalid_chains:
raise HTTPException(status_code=400, detail=f"Invalid chains: {invalid_chains}")
# Store offering
gpu_offerings[offering_id] = {
"offering_id": offering_id,
"created_at": datetime.now().isoformat(),
"status": "available",
**offering.dict()
}
# Update chain offerings
for chain in offering.chains:
if chain not in chain_offerings:
chain_offerings[chain] = []
chain_offerings[chain].append(offering_id)
return JSONResponse({
"success": True,
"offering_id": offering_id,
"status": "created",
"chains": offering.chains,
"price_per_hour": offering.price_per_hour,
"message": "GPU offering created successfully"
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Offering creation failed: {str(e)}")
offering_id = str(uuid.uuid4())
# Validate chains
invalid_chains = [c for c in offering.chains if c not in SUPPORTED_CHAINS]
if invalid_chains:
raise HTTPException(status_code=400, detail=f"Invalid chains: {invalid_chains}")
# Store offering
gpu_offerings[offering_id] = {
"offering_id": offering_id,
"created_at": datetime.now().isoformat(),
"status": "available",
**offering.model_dump()
}
# Update chain offerings
for chain in offering.chains:
if chain not in chain_offerings:
chain_offerings[chain] = []
chain_offerings[chain].append(offering_id)
return JSONResponse({
"success": True,
"offering_id": offering_id,
"status": "created",
"chains": offering.chains,
"price_per_hour": offering.price_per_hour,
"message": "GPU offering created successfully"
})
@app.get("/api/v1/offerings")
async def get_gpu_offerings(chain: Optional[str] = None, gpu_model: Optional[str] = None):
"""Get available GPU offerings, filtered by chain and model"""
try:
filtered_offerings = gpu_offerings.copy()
if chain:
filtered_offerings = {
k: v for k, v in filtered_offerings.items()
if chain in v["chains"] and v["status"] == "available"
}
if gpu_model:
filtered_offerings = {
k: v for k, v in filtered_offerings.items()
if gpu_model.lower() in v["gpu_model"].lower()
}
return JSONResponse({
"offerings": list(filtered_offerings.values()),
"total_count": len(filtered_offerings),
"filters": {
"chain": chain,
"gpu_model": gpu_model
}
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get offerings: {str(e)}")
filtered_offerings = gpu_offerings.copy()
if chain:
filtered_offerings = {
k: v for k, v in filtered_offerings.items()
if chain in v["chains"] and v["status"] == "available"
}
if gpu_model:
filtered_offerings = {
k: v for k, v in filtered_offerings.items()
if gpu_model.lower() in v["gpu_model"].lower()
}
return JSONResponse({
"offerings": list(filtered_offerings.values()),
"total_count": len(filtered_offerings),
"filters": {
"chain": chain,
"gpu_model": gpu_model
}
})
@app.get("/api/v1/offerings/{offering_id}")
async def get_gpu_offering(offering_id: str):
@@ -196,209 +187,188 @@ async def get_gpu_offering(offering_id: str):
@app.post("/api/v1/deals/request")
async def request_deal(deal_request: DealRequest):
"""Buyers request GPU deals"""
try:
offering_id = deal_request.offering_id
if offering_id not in gpu_offerings:
raise HTTPException(status_code=404, detail="GPU offering not found")
offering = gpu_offerings[offering_id]
if offering["status"] != "available":
raise HTTPException(status_code=400, detail="GPU offering not available")
if deal_request.chain not in offering["chains"]:
raise HTTPException(status_code=400, detail="Chain not supported by this offering")
# Calculate total cost
total_cost = offering["price_per_hour"] * deal_request.rental_hours
# Create deal
deal_id = str(uuid.uuid4())
marketplace_deals[deal_id] = {
"deal_id": deal_id,
"offering_id": offering_id,
"buyer_id": deal_request.buyer_id,
"miner_id": offering["miner_id"],
"chain": deal_request.chain,
"rental_hours": deal_request.rental_hours,
"total_cost": total_cost,
"special_requirements": deal_request.special_requirements,
"status": "pending_confirmation",
"created_at": datetime.now().isoformat(),
"expires_at": (datetime.now() + timedelta(hours=1)).isoformat()
}
return JSONResponse({
"success": True,
"deal_id": deal_id,
"status": "pending_confirmation",
"total_cost": total_cost,
"expires_at": marketplace_deals[deal_id]["expires_at"],
"message": "Deal request sent to miner for confirmation"
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Deal request failed: {str(e)}")
offering_id = deal_request.offering_id
if offering_id not in gpu_offerings:
raise HTTPException(status_code=404, detail="GPU offering not found")
offering = gpu_offerings[offering_id]
if offering["status"] != "available":
raise HTTPException(status_code=400, detail="GPU offering not available")
if deal_request.chain not in offering["chains"]:
raise HTTPException(status_code=400, detail="Chain not supported by this offering")
# Calculate total cost
total_cost = offering["price_per_hour"] * deal_request.rental_hours
# Create deal
deal_id = str(uuid.uuid4())
marketplace_deals[deal_id] = {
"deal_id": deal_id,
"offering_id": offering_id,
"buyer_id": deal_request.buyer_id,
"miner_id": offering["miner_id"],
"chain": deal_request.chain,
"rental_hours": deal_request.rental_hours,
"total_cost": total_cost,
"special_requirements": deal_request.special_requirements,
"status": "pending_confirmation",
"created_at": datetime.now().isoformat(),
"expires_at": (datetime.now() + timedelta(hours=1)).isoformat()
}
return JSONResponse({
"success": True,
"deal_id": deal_id,
"status": "pending_confirmation",
"total_cost": total_cost,
"expires_at": marketplace_deals[deal_id]["expires_at"],
"message": "Deal request sent to miner for confirmation"
})
@app.post("/api/v1/deals/{deal_id}/confirm")
async def confirm_deal(deal_id: str, confirmation: DealConfirmation):
"""Miners confirm or reject deal requests"""
try:
if deal_id not in marketplace_deals:
raise HTTPException(status_code=404, detail="Deal not found")
if deal_id not in marketplace_deals:
raise HTTPException(status_code=404, detail="Deal not found")
deal = marketplace_deals[deal_id]
if deal["status"] != "pending_confirmation":
raise HTTPException(status_code=400, detail="Deal cannot be confirmed")
if confirmation.chain != deal["chain"]:
raise HTTPException(status_code=400, detail="Chain mismatch")
if confirmation.miner_confirmation:
# Accept deal
deal["status"] = "confirmed"
deal["confirmed_at"] = datetime.now().isoformat()
deal["starts_at"] = datetime.now().isoformat()
deal["ends_at"] = (datetime.now() + timedelta(hours=deal["rental_hours"])).isoformat()
deal = marketplace_deals[deal_id]
# Update offering status
offering_id = deal["offering_id"]
if offering_id in gpu_offerings:
gpu_offerings[offering_id]["status"] = "occupied"
if deal["status"] != "pending_confirmation":
raise HTTPException(status_code=400, detail="Deal cannot be confirmed")
if confirmation.chain != deal["chain"]:
raise HTTPException(status_code=400, detail="Chain mismatch")
if confirmation.miner_confirmation:
# Accept deal
deal["status"] = "confirmed"
deal["confirmed_at"] = datetime.now().isoformat()
deal["starts_at"] = datetime.now().isoformat()
deal["ends_at"] = (datetime.now() + timedelta(hours=deal["rental_hours"])).isoformat()
# Update offering status
offering_id = deal["offering_id"]
if offering_id in gpu_offerings:
gpu_offerings[offering_id]["status"] = "occupied"
message = "Deal confirmed successfully"
else:
# Reject deal
deal["status"] = "rejected"
deal["rejected_at"] = datetime.now().isoformat()
message = "Deal rejected by miner"
return JSONResponse({
"success": True,
"deal_id": deal_id,
"status": deal["status"],
"miner_confirmation": confirmation.miner_confirmation,
"message": message
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Deal confirmation failed: {str(e)}")
message = "Deal confirmed successfully"
else:
# Reject deal
deal["status"] = "rejected"
deal["rejected_at"] = datetime.now().isoformat()
message = "Deal rejected by miner"
return JSONResponse({
"success": True,
"deal_id": deal_id,
"status": deal["status"],
"miner_confirmation": confirmation.miner_confirmation,
"message": message
})
@app.get("/api/v1/deals")
async def get_deals(miner_id: Optional[str] = None, buyer_id: Optional[str] = None):
"""Get deals, filtered by miner or buyer"""
try:
filtered_deals = marketplace_deals.copy()
if miner_id:
filtered_deals = {
k: v for k, v in filtered_deals.items()
if v["miner_id"] == miner_id
}
if buyer_id:
filtered_deals = {
k: v for k, v in filtered_deals.items()
if v["buyer_id"] == buyer_id
}
return JSONResponse({
"deals": list(filtered_deals.values()),
"total_count": len(filtered_deals)
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get deals: {str(e)}")
filtered_deals = marketplace_deals.copy()
if miner_id:
filtered_deals = {
k: v for k, v in filtered_deals.items()
if v["miner_id"] == miner_id
}
if buyer_id:
filtered_deals = {
k: v for k, v in filtered_deals.items()
if v["buyer_id"] == buyer_id
}
return JSONResponse({
"deals": list(filtered_deals.values()),
"total_count": len(filtered_deals)
})
@app.get("/api/v1/miners/{miner_id}/offerings")
async def get_miner_offerings(miner_id: str):
"""Get all offerings for a specific miner"""
try:
miner_offerings = {
k: v for k, v in gpu_offerings.items()
if v["miner_id"] == miner_id
}
return JSONResponse({
"miner_id": miner_id,
"offerings": list(miner_offerings.values()),
"total_count": len(miner_offerings)
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get miner offerings: {str(e)}")
miner_offerings = {
k: v for k, v in gpu_offerings.items()
if v["miner_id"] == miner_id
}
return JSONResponse({
"miner_id": miner_id,
"offerings": list(miner_offerings.values()),
"total_count": len(miner_offerings)
})
@app.get("/api/v1/chains/{chain}/offerings")
async def get_chain_offerings(chain: str):
"""Get all offerings for a specific chain"""
try:
if chain not in SUPPORTED_CHAINS:
raise HTTPException(status_code=400, detail=f"Unsupported chain: {chain}")
chain_offering_ids = chain_offerings.get(chain, [])
chain_offs = {
k: v for k, v in gpu_offerings.items()
if k in chain_offering_ids and v["status"] == "available"
}
return JSONResponse({
"chain": chain,
"offerings": list(chain_offs.values()),
"total_count": len(chain_offs)
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get chain offerings: {str(e)}")
if chain not in SUPPORTED_CHAINS:
raise HTTPException(status_code=400, detail=f"Unsupported chain: {chain}")
chain_offering_ids = chain_offerings.get(chain, [])
chain_offs = {
k: v for k, v in gpu_offerings.items()
if k in chain_offering_ids and v["status"] == "available"
}
return JSONResponse({
"chain": chain,
"offerings": list(chain_offs.values()),
"total_count": len(chain_offs)
})
@app.delete("/api/v1/offerings/{offering_id}")
async def remove_offering(offering_id: str):
"""Miners remove their GPU offerings"""
try:
if offering_id not in gpu_offerings:
raise HTTPException(status_code=404, detail="Offering not found")
offering = gpu_offerings[offering_id]
# Remove from chain offerings
for chain in offering["chains"]:
if chain in chain_offerings and offering_id in chain_offerings[chain]:
chain_offerings[chain].remove(offering_id)
# Remove offering
del gpu_offerings[offering_id]
return JSONResponse({
"success": True,
"message": "GPU offering removed successfully"
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to remove offering: {str(e)}")
if offering_id not in gpu_offerings:
raise HTTPException(status_code=404, detail="Offering not found")
offering = gpu_offerings[offering_id]
# Remove from chain offerings
for chain in offering["chains"]:
if chain in chain_offerings and offering_id in chain_offerings[chain]:
chain_offerings[chain].remove(offering_id)
# Remove offering
del gpu_offerings[offering_id]
return JSONResponse({
"success": True,
"message": "GPU offering removed successfully"
})
@app.get("/api/v1/stats")
async def get_marketplace_stats():
"""Get marketplace statistics"""
try:
active_offerings = len([o for o in gpu_offerings.values() if o["status"] == "available"])
active_deals = len([d for d in marketplace_deals.values() if d["status"] in ["confirmed", "active"]])
active_offerings = len([o for o in gpu_offerings.values() if o["status"] == "available"])
active_deals = len([d for d in marketplace_deals.values() if d["status"] in ["confirmed", "active"]])
chain_stats = {}
for chain in SUPPORTED_CHAINS:
chain_offerings = len([o for o in gpu_offerings.values() if chain in o["chains"] and o["status"] == "available"])
chain_deals = len([d for d in marketplace_deals.values() if d["chain"] == chain and d["status"] in ["confirmed", "active"]])
chain_stats = {}
for chain in SUPPORTED_CHAINS:
chain_offerings = len([o for o in gpu_offerings.values() if chain in o["chains"] and o["status"] == "available"])
chain_deals = len([d for d in marketplace_deals.values() if d["chain"] == chain and d["status"] in ["confirmed", "active"]])
chain_stats[chain] = {
"offerings": chain_offerings,
"active_deals": chain_deals,
"total_gpu_hours": sum([o["available_hours"] for o in gpu_offerings.values() if chain in o["chains"]])
}
return JSONResponse({
"total_offerings": active_offerings,
"active_deals": active_deals,
"registered_miners": len(miner_registrations),
"supported_chains": SUPPORTED_CHAINS,
"chain_stats": chain_stats,
"timestamp": datetime.now().isoformat()
})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get stats: {str(e)}")
chain_stats[chain] = {
"offerings": chain_offerings,
"active_deals": chain_deals,
"total_gpu_hours": sum([o["available_hours"] for o in gpu_offerings.values() if chain in o["chains"]])
}
return JSONResponse({
"total_offerings": active_offerings,
"active_deals": active_deals,
"registered_miners": len(miner_registrations),
"supported_chains": SUPPORTED_CHAINS,
"chain_stats": chain_stats,
"timestamp": datetime.now().isoformat()
})
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8005, log_level="info")

View File

@@ -0,0 +1 @@
"""Agent marketplace service tests"""

View File

@@ -0,0 +1,250 @@
"""Edge case and error handling tests for agent marketplace service"""
import pytest
import sys
import sys
from pathlib import Path
from fastapi.testclient import TestClient
from agent_marketplace import app, GPUOffering, DealRequest, DealConfirmation, MinerRegistration, gpu_offerings, marketplace_deals, miner_registrations, chain_offerings
@pytest.fixture(autouse=True)
def reset_state():
"""Reset global state before each test"""
gpu_offerings.clear()
marketplace_deals.clear()
miner_registrations.clear()
chain_offerings.clear()
yield
gpu_offerings.clear()
marketplace_deals.clear()
miner_registrations.clear()
chain_offerings.clear()
@pytest.mark.unit
def test_gpu_offering_empty_chains():
"""Test GPUOffering with empty chains"""
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=[],
capabilities=["inference"]
)
assert offering.chains == []
@pytest.mark.unit
def test_gpu_offering_empty_capabilities():
"""Test GPUOffering with empty capabilities"""
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=[]
)
assert offering.capabilities == []
@pytest.mark.unit
def test_miner_registration_empty_chains():
"""Test MinerRegistration with empty preferred chains"""
registration = MinerRegistration(
miner_id="miner_123",
wallet_address="0x1234567890abcdef",
preferred_chains=[],
gpu_specs={"model": "RTX 4090"}
)
assert registration.preferred_chains == []
@pytest.mark.unit
def test_deal_request_empty_offering_id():
"""Test DealRequest with empty offering_id"""
request = DealRequest(
offering_id="",
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
assert request.offering_id == ""
@pytest.mark.unit
def test_deal_confirmation_empty_deal_id():
"""Test DealConfirmation with empty deal_id"""
confirmation = DealConfirmation(
deal_id="",
miner_confirmation=True,
chain="ait-devnet"
)
assert confirmation.deal_id == ""
@pytest.mark.integration
def test_get_gpu_offerings_empty():
"""Test getting GPU offerings when none exist"""
client = TestClient(app)
response = client.get("/api/v1/offerings")
assert response.status_code == 200
data = response.json()
assert data["total_count"] == 0
@pytest.mark.integration
def test_get_deals_empty():
"""Test getting deals when none exist"""
client = TestClient(app)
response = client.get("/api/v1/deals")
assert response.status_code == 200
data = response.json()
assert data["total_count"] == 0
@pytest.mark.integration
def test_get_miner_offerings_no_offerings():
"""Test getting offerings for miner with no offerings"""
client = TestClient(app)
response = client.get("/api/v1/miners/miner_123/offerings")
assert response.status_code == 200
data = response.json()
assert data["total_count"] == 0
@pytest.mark.integration
def test_get_chain_offerings_no_offerings():
"""Test getting chain offerings when none exist"""
client = TestClient(app)
response = client.get("/api/v1/chains/ait-devnet/offerings")
assert response.status_code == 200
data = response.json()
assert data["total_count"] == 0
@pytest.mark.integration
def test_request_deal_offering_not_available():
"""Test requesting deal for unavailable offering"""
client = TestClient(app)
# Create an offering
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
# Mark as occupied
gpu_offerings[offering_id]["status"] = "occupied"
deal_request = DealRequest(
offering_id=offering_id,
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
response = client.post("/api/v1/deals/request", json=deal_request.model_dump())
assert response.status_code == 400
@pytest.mark.integration
def test_confirm_deal_already_confirmed():
"""Test confirming a deal that's already confirmed"""
client = TestClient(app)
# Create offering and request deal
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
deal_request = DealRequest(
offering_id=offering_id,
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
deal_response = client.post("/api/v1/deals/request", json=deal_request.model_dump())
deal_id = deal_response.json()["deal_id"]
# Confirm the deal
confirmation = DealConfirmation(
deal_id=deal_id,
miner_confirmation=True,
chain="ait-devnet"
)
client.post(f"/api/v1/deals/{deal_id}/confirm", json=confirmation.model_dump())
# Try to confirm again
response = client.post(f"/api/v1/deals/{deal_id}/confirm", json=confirmation.model_dump())
assert response.status_code == 400
@pytest.mark.integration
def test_confirm_deal_chain_mismatch():
"""Test confirming deal with wrong chain"""
client = TestClient(app)
# Create offering and request deal
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
deal_request = DealRequest(
offering_id=offering_id,
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
deal_response = client.post("/api/v1/deals/request", json=deal_request.model_dump())
deal_id = deal_response.json()["deal_id"]
# Confirm with wrong chain
confirmation = DealConfirmation(
deal_id=deal_id,
miner_confirmation=True,
chain="ait-testnet"
)
response = client.post(f"/api/v1/deals/{deal_id}/confirm", json=confirmation.model_dump())
assert response.status_code == 400
@pytest.mark.integration
def test_get_marketplace_stats_empty():
"""Test getting marketplace stats with no data"""
client = TestClient(app)
response = client.get("/api/v1/stats")
assert response.status_code == 200
data = response.json()
assert data["total_offerings"] == 0
assert data["active_deals"] == 0

View File

@@ -0,0 +1,506 @@
"""Integration tests for agent marketplace service"""
import pytest
import sys
import sys
from pathlib import Path
from fastapi.testclient import TestClient
from agent_marketplace import app, GPUOffering, DealRequest, DealConfirmation, MinerRegistration, gpu_offerings, marketplace_deals, miner_registrations, chain_offerings
@pytest.fixture(autouse=True)
def reset_state():
"""Reset global state before each test"""
gpu_offerings.clear()
marketplace_deals.clear()
miner_registrations.clear()
chain_offerings.clear()
yield
gpu_offerings.clear()
marketplace_deals.clear()
miner_registrations.clear()
chain_offerings.clear()
@pytest.mark.integration
def test_health_check():
"""Test health check endpoint"""
client = TestClient(app)
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
assert "supported_chains" in data
@pytest.mark.integration
def test_get_supported_chains():
"""Test getting supported chains"""
client = TestClient(app)
response = client.get("/api/v1/chains")
assert response.status_code == 200
data = response.json()
assert "chains" in data
@pytest.mark.integration
def test_register_miner():
"""Test registering a miner"""
client = TestClient(app)
registration = MinerRegistration(
miner_id="miner_123",
wallet_address="0x1234567890abcdef",
preferred_chains=["ait-devnet"],
gpu_specs={"model": "RTX 4090"}
)
response = client.post("/api/v1/miners/register", json=registration.model_dump())
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["miner_id"] == "miner_123"
@pytest.mark.integration
def test_register_miner_update_existing():
"""Test updating existing miner registration"""
client = TestClient(app)
registration = MinerRegistration(
miner_id="miner_123",
wallet_address="0x1234567890abcdef",
preferred_chains=["ait-devnet"],
gpu_specs={"model": "RTX 4090"}
)
client.post("/api/v1/miners/register", json=registration.model_dump())
# Update with new data
registration.wallet_address = "0xabcdef1234567890"
response = client.post("/api/v1/miners/register", json=registration.model_dump())
assert response.status_code == 200
@pytest.mark.integration
def test_create_gpu_offering():
"""Test creating a GPU offering"""
client = TestClient(app)
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
response = client.post("/api/v1/offerings/create", json=offering.model_dump())
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert "offering_id" in data
@pytest.mark.integration
def test_create_gpu_offering_invalid_chain():
"""Test creating GPU offering with invalid chain"""
client = TestClient(app)
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["invalid-chain"],
capabilities=["inference"]
)
response = client.post("/api/v1/offerings/create", json=offering.model_dump())
assert response.status_code == 400
@pytest.mark.integration
def test_get_gpu_offerings():
"""Test getting GPU offerings"""
client = TestClient(app)
# Create an offering first
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
client.post("/api/v1/offerings/create", json=offering.model_dump())
response = client.get("/api/v1/offerings")
assert response.status_code == 200
data = response.json()
assert "offerings" in data
@pytest.mark.integration
def test_get_gpu_offerings_with_filters():
"""Test getting GPU offerings with filters"""
client = TestClient(app)
# Create offerings
offering1 = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
offering2 = GPUOffering(
miner_id="miner_456",
gpu_model="RTX 3080",
gpu_memory=10240,
cuda_cores=8704,
price_per_hour=0.30,
available_hours=24,
chains=["ait-testnet"],
capabilities=["inference"]
)
client.post("/api/v1/offerings/create", json=offering1.model_dump())
client.post("/api/v1/offerings/create", json=offering2.model_dump())
response = client.get("/api/v1/offerings?chain=ait-devnet&gpu_model=RTX")
assert response.status_code == 200
@pytest.mark.integration
def test_get_gpu_offering():
"""Test getting specific GPU offering"""
client = TestClient(app)
# Create an offering first
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
response = client.get(f"/api/v1/offerings/{offering_id}")
assert response.status_code == 200
data = response.json()
assert data["offering_id"] == offering_id
@pytest.mark.integration
def test_get_gpu_offering_not_found():
"""Test getting nonexistent GPU offering"""
client = TestClient(app)
response = client.get("/api/v1/offerings/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_request_deal():
"""Test requesting a deal"""
client = TestClient(app)
# Create an offering first
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
deal_request = DealRequest(
offering_id=offering_id,
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
response = client.post("/api/v1/deals/request", json=deal_request.model_dump())
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert "deal_id" in data
@pytest.mark.integration
def test_request_deal_offering_not_found():
"""Test requesting deal for nonexistent offering"""
client = TestClient(app)
deal_request = DealRequest(
offering_id="nonexistent",
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
response = client.post("/api/v1/deals/request", json=deal_request.model_dump())
assert response.status_code == 404
@pytest.mark.integration
def test_request_deal_chain_not_supported():
"""Test requesting deal with unsupported chain"""
client = TestClient(app)
# Create an offering
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
deal_request = DealRequest(
offering_id=offering_id,
buyer_id="buyer_123",
rental_hours=10,
chain="ait-testnet"
)
response = client.post("/api/v1/deals/request", json=deal_request.model_dump())
assert response.status_code == 400
@pytest.mark.integration
def test_confirm_deal():
"""Test confirming a deal"""
client = TestClient(app)
# Create offering and request deal
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
deal_request = DealRequest(
offering_id=offering_id,
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
deal_response = client.post("/api/v1/deals/request", json=deal_request.model_dump())
deal_id = deal_response.json()["deal_id"]
confirmation = DealConfirmation(
deal_id=deal_id,
miner_confirmation=True,
chain="ait-devnet"
)
response = client.post(f"/api/v1/deals/{deal_id}/confirm", json=confirmation.model_dump())
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["status"] == "confirmed"
@pytest.mark.integration
def test_confirm_deal_reject():
"""Test rejecting a deal"""
client = TestClient(app)
# Create offering and request deal
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
deal_request = DealRequest(
offering_id=offering_id,
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
deal_response = client.post("/api/v1/deals/request", json=deal_request.model_dump())
deal_id = deal_response.json()["deal_id"]
confirmation = DealConfirmation(
deal_id=deal_id,
miner_confirmation=False,
chain="ait-devnet"
)
response = client.post(f"/api/v1/deals/{deal_id}/confirm", json=confirmation.model_dump())
assert response.status_code == 200
data = response.json()
assert data["status"] == "rejected"
@pytest.mark.integration
def test_confirm_deal_not_found():
"""Test confirming nonexistent deal"""
client = TestClient(app)
confirmation = DealConfirmation(
deal_id="nonexistent",
miner_confirmation=True,
chain="ait-devnet"
)
response = client.post("/api/v1/deals/nonexistent/confirm", json=confirmation.model_dump())
assert response.status_code == 404
@pytest.mark.integration
def test_get_deals():
"""Test getting deals"""
client = TestClient(app)
response = client.get("/api/v1/deals")
assert response.status_code == 200
data = response.json()
assert "deals" in data
@pytest.mark.integration
def test_get_deals_with_filters():
"""Test getting deals with filters"""
client = TestClient(app)
# Create offering and request deal
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
deal_request = DealRequest(
offering_id=offering_id,
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
client.post("/api/v1/deals/request", json=deal_request.model_dump())
response = client.get("/api/v1/deals?miner_id=miner_123")
assert response.status_code == 200
@pytest.mark.integration
def test_get_miner_offerings():
"""Test getting offerings for a specific miner"""
client = TestClient(app)
# Create an offering
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
client.post("/api/v1/offerings/create", json=offering.model_dump())
response = client.get("/api/v1/miners/miner_123/offerings")
assert response.status_code == 200
data = response.json()
assert data["miner_id"] == "miner_123"
@pytest.mark.integration
def test_get_chain_offerings():
"""Test getting offerings for a specific chain"""
client = TestClient(app)
# Create an offering
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
client.post("/api/v1/offerings/create", json=offering.model_dump())
response = client.get("/api/v1/chains/ait-devnet/offerings")
assert response.status_code == 200
data = response.json()
assert data["chain"] == "ait-devnet"
@pytest.mark.integration
def test_get_chain_offerings_unsupported_chain():
"""Test getting offerings for unsupported chain"""
client = TestClient(app)
response = client.get("/api/v1/chains/unsupported-chain/offerings")
assert response.status_code == 400
@pytest.mark.integration
def test_remove_offering():
"""Test removing a GPU offering"""
client = TestClient(app)
# Create an offering
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
create_response = client.post("/api/v1/offerings/create", json=offering.model_dump())
offering_id = create_response.json()["offering_id"]
response = client.delete(f"/api/v1/offerings/{offering_id}")
assert response.status_code == 200
data = response.json()
assert data["success"] is True
@pytest.mark.integration
def test_remove_offering_not_found():
"""Test removing nonexistent offering"""
client = TestClient(app)
response = client.delete("/api/v1/offerings/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_get_marketplace_stats():
"""Test getting marketplace statistics"""
client = TestClient(app)
response = client.get("/api/v1/stats")
assert response.status_code == 200
data = response.json()
assert "total_offerings" in data
assert "chain_stats" in data

View File

@@ -0,0 +1,178 @@
"""Unit tests for agent marketplace service"""
import pytest
import sys
from pathlib import Path
# Add app src to path
project_root = Path(__file__).parent.parent.parent.parent
sys.path.insert(0, str(project_root / "apps" / "marketplace"))
from agent_marketplace import app, GPUOffering, DealRequest, DealConfirmation, MinerRegistration
@pytest.mark.unit
def test_app_initialization():
"""Test that the FastAPI app initializes correctly"""
assert app is not None
assert app.title == "AITBC Agent-First GPU Marketplace"
assert app.version == "1.0.0"
@pytest.mark.unit
def test_gpu_offering_model():
"""Test GPUOffering model"""
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet", "ait-testnet"],
capabilities=["inference", "training"]
)
assert offering.miner_id == "miner_123"
assert offering.gpu_model == "RTX 4090"
assert offering.gpu_memory == 24576
assert offering.price_per_hour == 0.50
assert offering.chains == ["ait-devnet", "ait-testnet"]
@pytest.mark.unit
def test_gpu_offering_defaults():
"""Test GPUOffering with default values"""
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
assert offering.min_rental_hours == 1
assert offering.max_concurrent_jobs == 1
@pytest.mark.unit
def test_deal_request_model():
"""Test DealRequest model"""
request = DealRequest(
offering_id="offering_123",
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet",
special_requirements="Need for high performance"
)
assert request.offering_id == "offering_123"
assert request.buyer_id == "buyer_123"
assert request.rental_hours == 10
assert request.chain == "ait-devnet"
@pytest.mark.unit
def test_deal_request_without_special_requirements():
"""Test DealRequest without special requirements"""
request = DealRequest(
offering_id="offering_123",
buyer_id="buyer_123",
rental_hours=10,
chain="ait-devnet"
)
assert request.special_requirements is None
@pytest.mark.unit
def test_deal_confirmation_model():
"""Test DealConfirmation model"""
confirmation = DealConfirmation(
deal_id="deal_123",
miner_confirmation=True,
chain="ait-devnet"
)
assert confirmation.deal_id == "deal_123"
assert confirmation.miner_confirmation is True
assert confirmation.chain == "ait-devnet"
@pytest.mark.unit
def test_deal_confirmation_rejection():
"""Test DealConfirmation with rejection"""
confirmation = DealConfirmation(
deal_id="deal_123",
miner_confirmation=False,
chain="ait-devnet"
)
assert confirmation.miner_confirmation is False
@pytest.mark.unit
def test_miner_registration_model():
"""Test MinerRegistration model"""
registration = MinerRegistration(
miner_id="miner_123",
wallet_address="0x1234567890abcdef",
preferred_chains=["ait-devnet", "ait-testnet"],
gpu_specs={"model": "RTX 4090", "memory": 24576}
)
assert registration.miner_id == "miner_123"
assert registration.wallet_address == "0x1234567890abcdef"
assert registration.preferred_chains == ["ait-devnet", "ait-testnet"]
@pytest.mark.unit
def test_miner_registration_defaults():
"""Test MinerRegistration with default pricing model"""
registration = MinerRegistration(
miner_id="miner_123",
wallet_address="0x1234567890abcdef",
preferred_chains=["ait-devnet"],
gpu_specs={"model": "RTX 4090"}
)
assert registration.pricing_model == "hourly"
@pytest.mark.unit
def test_gpu_offering_negative_price():
"""Test GPUOffering with negative price"""
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=-0.50,
available_hours=24,
chains=["ait-devnet"],
capabilities=["inference"]
)
assert offering.price_per_hour == -0.50
@pytest.mark.unit
def test_gpu_offering_zero_hours():
"""Test GPUOffering with zero available hours"""
offering = GPUOffering(
miner_id="miner_123",
gpu_model="RTX 4090",
gpu_memory=24576,
cuda_cores=16384,
price_per_hour=0.50,
available_hours=0,
chains=["ait-devnet"],
capabilities=["inference"]
)
assert offering.available_hours == 0
@pytest.mark.unit
def test_deal_request_negative_hours():
"""Test DealRequest with negative rental hours"""
request = DealRequest(
offering_id="offering_123",
buyer_id="buyer_123",
rental_hours=-10,
chain="ait-devnet"
)
assert request.rental_hours == -10