CoolFace
Apppublic

RohanExploit/Meta-hackathon

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
test_api.py81 linesDownload Raw Back to root
1"""2Test script to demonstrate the API endpoints work correctly3"""4import sys5import json6sys.path.insert(0, '.')7 8from environment.retail_env import MultiChannelRetailEnv9from environment.models import OrderAction, ActionType10 11def test_api_simulation():12    """Simulate what the API endpoints would do"""13    print("Testing API-like interaction...")14    15    # Create environment (simulating server startup)16    env = MultiChannelRetailEnv(seed=42)17    18    # Simulate /reset endpoint19    print("\n1. Testing reset (like POST /reset)")20    task_config = {21        "products": ["Widget"],22        "initial_inventory": {"Widget": 10},23        "initial_cash": 100.0,24        "base_demand_luxury": {"Widget": 1.0},25        "base_demand_budget": {"Widget": 2.0},26        "product_costs": {"Widget": 5.0},27        "holding_costs": {"Widget": 0.1},28        "max_inventory": {"Widget": 100},29        "initial_prices_luxury": {"Widget": 10.0},30        "initial_prices_budget": {"Widget": 7.0},31        "price_bounds": {"Widget": {"min": 5.5, "max": 15.0}},32    }33    34    obs = env.reset(task_config)35    reset_response = {36        "observation": obs.model_dump(),37        "reward": 0.0,38        "done": False,39        "info": {}40    }41    print(f"Reset response keys: {list(reset_response.keys())}")42    print(f"Observation: inventory={reset_response['observation']['inventory']}, "43          f"cash={reset_response['observation']['cash']}, "44          f"day={reset_response['observation']['day']}")45    46    # Simulate /step endpoint47    print("\n2. Testing step (like POST /step)")48    action_dict = {"action": "order", "product": "Widget", "quantity": 3}49    50    # In real API, this would come from request body51    # We'll validate it like the API would52    try:53        action = OrderAction(**action_dict)54        print(f"Valid action: {action}")55        56        obs, reward, done, info = env.step(action)57        step_response = {58            "observation": obs.model_dump(),59            "reward": reward,60            "done": done,61            "info": info62        }63        print(f"Step response: reward={reward:.2f}, done={done}")64        print(f"Updated observation: inventory={obs.inventory}, cash={obs.cash:.2f}")65        66    except Exception as e:67        print(f"Error processing action: {e}")68    69    # Simulate /state endpoint70    print("\n3. Testing state (like GET /state)")71    state = env.get_state()72    state_response = {"state": state}73    print(f"State keys: {list(state_response['state'].keys())}")74    print(f"Cash in state: ${state['cash']:.2f}")75    print(f"Day in state: {state['day']}")76    77    print("\nAPI simulation test completed successfully!")78 79if __name__ == "__main__":80    test_api_simulation()81