atropos/environments/dynastai/test_dynastai_api.py
Earl Potters e0dabe1225 Add DynastAI game environment with FastAPI backend and web frontend
- Introduced `dynastai_local_server.py` for local development and testing.
- Implemented `dynastai_server.py` as the main server entry point.
- Created FastAPI endpoints for game state management, card generation, and player choices.
- Developed a web interface with HTML, CSS, and JavaScript for user interaction.
- Added game logic for managing metrics and decision effects.
- Included configuration management and utility functions.
- Established a testing framework for API and environment functionality.
- Updated README.md with project overview and setup instructions.
2025-05-18 19:37:31 +00:00

110 lines
3.3 KiB
Python
Executable file

#!/usr/bin/env python3
"""
DynastAI API Test Script
This script tests the FastAPI endpoints of the DynastAI game:
- Creating a new game
- Getting game state
- Generating cards
- Processing choices
- Ending reigns
"""
import os
import sys
import asyncio
import json
import random
import httpx
from dotenv import load_dotenv
# Ensure the src directory is in path
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
# Load environment variables
load_dotenv()
# API configuration
API_URL = "http://localhost:9001/api"
async def test_api():
"""Test the DynastAI API endpoints"""
print("Testing DynastAI API...")
async with httpx.AsyncClient() as client:
# Test root endpoint
print("\nTesting root endpoint...")
response = await client.get(f"{API_URL}/")
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
# Create a new game
print("\nCreating new game...")
response = await client.post(f"{API_URL}/new_game")
game_data = response.json()
session_id = game_data["session_id"]
print(f"Session ID: {session_id}")
print(f"Initial metrics: {game_data['metrics']}")
# Generate a card
print("\nGenerating card...")
response = await client.post(
f"{API_URL}/generate_card",
json={"session_id": session_id}
)
card = response.json()
print(f"Card: {card['text']}")
print(f"Option Yes: {card['yes_option']}")
print(f"Option No: {card['no_option']}")
# Make a choice
print("\nMaking choice...")
choice = random.choice(["yes", "no"])
response = await client.post(
f"{API_URL}/card_choice",
json={"session_id": session_id, "choice": choice}
)
result = response.json()
print(f"Choice: {choice}")
print(f"New metrics: {result['metrics']}")
print(f"Game over: {result.get('game_over', False)}")
# End the reign
print("\nEnding reign...")
trajectory = [{
"card_id": card["id"],
"category": card["category"],
"choice": choice,
"effects": card["effects"][choice],
"post_metrics": result["metrics"]
}]
response = await client.post(
f"{API_URL}/end_reign",
json={
"session_id": session_id,
"trajectory": trajectory,
"final_metrics": result["metrics"],
"reign_length": result["metrics"]["reign_year"],
"cause_of_end": "test_termination"
}
)
end_data = response.json()
print(f"Reward: {end_data['reward']}")
print(f"New weights: {end_data['new_weights']}")
print("\nAPI tests completed successfully!")
return True
if __name__ == "__main__":
print("DynastAI API Test")
print("================")
# Check if server is running in a separate process
print("NOTE: This test assumes the DynastAI server is running.")
print("Please start the server with 'python dynastai_server.py' before running this test.")
input("Press Enter to continue...")
# Run the test
asyncio.run(test_api())