FitMop/backend/scripts/test_ai_modifier.py

104 lines
3.3 KiB
Python

import json
import os
import sys
from typing import Any, Dict
# Ensure we can import from src
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from common.env_manager import EnvManager
from garmin.workout_manager import WorkoutManager
def load_sample_workout() -> Dict[str, Any]:
"""Create a minimal valid workout for testing."""
return {
"workoutName": "Test Workout",
"description": "Base for AI test",
"sportType": {
"sportTypeId": 1,
"sportTypeKey": "running"
},
"workoutSegments": [
{
"segmentOrder": 1,
"sportType": {
"sportTypeId": 1,
"sportTypeKey": "running"
},
"workoutSteps": [
{
"type": "ExecutableStepDTO",
"stepOrder": 1,
"stepTypeId": 1,
"childStepId": None,
"endConditionId": 2,
"endConditionValue": 300,
"targetTypeId": 1,
"targetValueOne": 10.0,
"targetValueTwo": 12.0
}
]
}
]
}
def test_ai_modification():
print("\n--- Testing AI Workout Modifier ---")
# 1. Setup Environment
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
env = EnvManager(root_dir)
env.load_service_env("gemini")
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
print("SKIP: GEMINI_API_KEY not found in environment.")
return
# 2. Prepare Data
manager = WorkoutManager()
original_workout = load_sample_workout()
# 3. specific instruction
prompt = "Add a 10 minute cooldown at the end."
print(f"Prompt: {prompt}")
# 4. Execute AI Modification
try:
modified_workout = manager.generate_workout_json(prompt, existing_workout=original_workout)
# 5. Verify Structure (Schema Validation)
errors = manager.validate_workout_json(modified_workout)
if errors:
print(f"FAIL: Schema Validation Errors: {json.dumps(errors, indent=2)}")
print(f"Invalid Workout JSON: {json.dumps(modified_workout, indent=2)}")
return
# 6. Verify Content Logic
segments = modified_workout.get("workoutSegments", [])
if not segments:
print("FAIL: No segments found in modified workout.")
return
steps = segments[0].get("workoutSteps", [])
last_step = steps[-1] if steps else None
# Check if last step looks like a cooldown
# stepTypeId 4 = Cooldown (usually, or we check description/type)
is_cooldown = last_step and (last_step.get("stepTypeId") == 4 or "cool" in str(last_step).lower())
if is_cooldown:
print("PASS: Cooldown added successfully.")
print(f"Modified Steps Count: {len(steps)}")
else:
print("WARN: Could not strictly verify cooldown type, but schema is valid.")
print(f"Last Step: {json.dumps(last_step, indent=2)}")
except Exception as e:
print(f"FAIL: Exception during AI processing: {e}")
if __name__ == "__main__":
test_ai_modification()