File size: 1,624 Bytes
27cdb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for OpenEnv-style API session endpoints."""

from fastapi.testclient import TestClient

from api.main import app


client = TestClient(app)


def test_openenv_reset_returns_session_and_observation():
    response = client.post(
        "/reset",
        json={"scenario_id": "missing_flask", "max_steps": 3},
    )
    assert response.status_code == 200

    payload = response.json()
    assert "session_id" in payload
    assert "observation" in payload
    assert "info" in payload
    assert payload["observation"]["scenario_id"] == "missing_flask"

    close_resp = client.post("/close", json={"session_id": payload["session_id"]})
    assert close_resp.status_code == 200
    assert close_resp.json()["closed"] is True


def test_openenv_step_unknown_session_returns_404():
    response = client.post(
        "/step",
        json={
            "session_id": "does-not-exist",
            "action": {"command": "echo hello"},
        },
    )
    assert response.status_code == 404


def test_openenv_session_auto_closes_when_done():
    reset_resp = client.post(
        "/reset",
        json={"scenario_id": "missing_flask", "max_steps": 1},
    )
    assert reset_resp.status_code == 200
    session_id = reset_resp.json()["session_id"]

    step_resp = client.post(
        "/step",
        json={
            "session_id": session_id,
            "action": {"command": "echo noop"},
        },
    )
    assert step_resp.status_code == 200
    assert step_resp.json()["done"] is True

    missing_resp = client.post("/close", json={"session_id": session_id})
    assert missing_resp.status_code == 404