File size: 3,116 Bytes
9c21115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
Test script for Policy-to-Logic RL Environment API endpoints.

Run the server first (in another terminal):
    uv run python main.py

Then run this script:
    uv run python test_endpoints.py
"""

import requests
import json
from typing import Any

BASE_URL = "http://localhost:7860"

def test_endpoint(method: str, endpoint: str, data: dict = None, description: str = "") -> Any:
    """Test an API endpoint and print results."""
    url = f"{BASE_URL}{endpoint}"
    print(f"\n{'='*70}")
    print(f"🧪 {description or endpoint}")
    print(f"{'='*70}")
    print(f"{method} {url}")
    
    try:
        if method == "POST":
            response = requests.post(url, json=data)
        else:
            response = requests.get(url)
        
        print(f"Status: {response.status_code}")
        
        try:
            result = response.json()
            print(f"Response:\n{json.dumps(result, indent=2)}")
            return result
        except:
            print(f"Response (text):\n{response.text}")
            return response.text
            
    except requests.exceptions.ConnectionError:
        print(f"❌ Connection error! Is the server running on {BASE_URL}?")
        return None
    except Exception as e:
        print(f"❌ Error: {e}")
        return None

def main():
    print("\n🚀 Policy-to-Logic RL Environment - API Test Suite\n")
    
    # 1. Health check
    test_endpoint("GET", "/health", description="Health Check")
    
    # 2. List available tasks
    test_endpoint("GET", "/tasks", description="List Available Tasks")
    
    # 3. Reset environment (start new episode)
    reset_result = test_endpoint(
        "POST", 
        "/reset", 
        data={"task_name": None},
        description="Reset Environment (Start New Episode)"
    )
    
    # 4. Get current state
    test_endpoint("GET", "/state", description="Get Current State")
    
    # 5. Take a step - ask clarification
    if reset_result:
        step_result = test_endpoint(
            "POST",
            "/step",
            data={
                "action_type": "ask_clarification",
                "content": "What are the business hours?"
            },
            description="Step 1: Ask Clarification"
        )
    
    # 6. Get state after step
    test_endpoint("GET", "/state", description="Get State After Step")
    
    # 7. Take another step - propose rules
    if reset_result:
        test_endpoint(
            "POST",
            "/step",
            data={
                "action_type": "propose_rules",
                "content": {
                    "rules": [
                        {
                            "condition": "user.role == 'admin'",
                            "action": "ALLOW"
                        }
                    ]
                }
            },
            description="Step 2: Propose Rules"
        )
    
    # 8. Get final state
    test_endpoint("GET", "/state", description="Get Final State")
    
    print(f"\n{'='*70}")
    print("✅ Test suite completed!")
    print(f"{'='*70}\n")

if __name__ == "__main__":
    main()