File size: 5,206 Bytes
a80eade
 
 
 
7045b45
 
a80eade
 
 
 
 
 
 
 
 
 
 
 
 
 
7045b45
 
 
a80eade
 
 
 
 
 
 
 
7045b45
 
a80eade
 
 
 
 
 
 
 
 
7045b45
a80eade
 
 
 
7045b45
 
 
 
 
a80eade
 
 
 
 
7045b45
 
 
a80eade
 
 
 
 
 
 
 
 
 
7045b45
a80eade
 
 
 
7045b45
 
 
 
 
a80eade
 
 
 
 
 
7045b45
a80eade
 
 
 
 
 
 
 
 
 
 
7045b45
a80eade
 
 
 
7045b45
a80eade
 
 
 
7045b45
a80eade
 
7045b45
a80eade
 
 
 
 
7045b45
 
 
 
 
a80eade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""Integration tests for the FastAPI API surface."""

import pytest

from environment.tasks import TASKS


def test_health_endpoint(test_client) -> None:
    response = test_client.get("/health")
    assert response.status_code == 200
    payload = response.json()
    assert payload["status"] == "healthy"
    assert payload["ready"] is True


def test_tasks_endpoint_lists_all_tasks_without_answers(test_client) -> None:
    response = test_client.get("/tasks")
    assert response.status_code == 200
    payload = response.json()
    assert isinstance(payload, list)
    assert len(payload) == len(TASKS)
    assert len(payload) >= 5
    assert payload[0]["action_schema"]["required"] == ["action_type", "column", "reason"]
    assert "answer" not in payload[0]


def test_reset_endpoint_returns_observation_and_supports_empty_body(test_client) -> None:
    response = test_client.post("/reset")
    assert response.status_code == 200
    payload = response.json()
    assert "session_id" in payload
    assert payload["observation"]["task_id"] in TASKS
    assert "dataset_preview" in payload["observation"]
    assert payload["observation"]["done"] is False


def test_reset_endpoint_rejects_invalid_task(test_client) -> None:
    response = test_client.post("/reset", json={"task_id": "missing_task"})
    assert response.status_code == 400


def test_step_and_state_endpoints_work_together(test_client) -> None:
    reset = test_client.post("/reset", json={"task_id": "null_filling"})
    session_id = reset.json()["session_id"]
    step = test_client.post(
        f"/step?session_id={session_id}",
        json={
            "action_type": "fill_missing",
            "row_index": 1,
            "column": "age",
            "new_value": "unknown",
            "reason": "Row 1 age is missing.",
        },
    )
    state = test_client.get(f"/state?session_id={session_id}")

    assert step.status_code == 200
    assert step.json()["reward"]["value"] > 0.0
    assert step.json()["observation"]["done"] is False
    assert step.json()["observation"]["issues_remaining"] == 2
    assert state.status_code == 200
    assert state.json()["step_count"] == 1


def test_state_endpoint_requires_active_episode(test_client) -> None:
    response = test_client.get("/state")
    assert response.status_code == 404


def test_grader_endpoint_scores_episode_history(test_client) -> None:
    reset = test_client.post("/reset", json={"task_id": "adversarial_sensor"})
    session_id = reset.json()["session_id"]
    test_client.post(
        f"/step?session_id={session_id}",
        json={
            "action_type": "fill_missing",
            "row_index": 2,
            "column": "reading",
            "new_value": None,
            "reason": "The reading is missing.",
        },
    )
    history = test_client.get(f"/state?session_id={session_id}").json()["episode_history"]

    response = test_client.post(
        f"/grader?session_id={session_id}",
        json={"task_id": "adversarial_sensor", "episode": history},
    )

    assert response.status_code == 200
    payload = response.json()
    assert 0.0 <= payload["score"] <= 1.0
    assert "breakdown" in payload


def test_baseline_endpoint_runs_smoke_test(test_client) -> None:
    response = test_client.post(
        "/baseline",
        json={"task_ids": ["null_filling"], "max_episodes_per_task": 1, "verbose": False},
    )
    assert response.status_code == 200
    payload = response.json()
    assert payload["summary"]["total_episodes"] == 1
    assert payload["results"][0]["task_id"] == "null_filling"


def test_websocket_supports_reset_step_and_state(test_client) -> None:
    with test_client.websocket_connect("/ws") as websocket:
        websocket.send_json({"type": "reset", "payload": {"task_id": "null_filling"}})
        reset_message = websocket.receive_json()
        assert reset_message["type"] == "reset"
        assert reset_message["observation"]["task_id"] == "null_filling"

        websocket.send_json(
            {
                "type": "step",
                "payload": {
                    "action_type": "fill_missing",
                    "row_index": 1,
                    "column": "age",
                    "new_value": "unknown",
                    "reason": "The age value is missing.",
                },
            }
        )
        step_message = websocket.receive_json()
        assert step_message["type"] == "step"
        assert "reward" in step_message

        websocket.send_json({"type": "state"})
        state_message = websocket.receive_json()
        assert state_message["type"] == "state"
        assert state_message["state"]["step_count"] == 1


def test_websocket_honors_max_concurrent_envs() -> None:
    from fastapi.testclient import TestClient

    from app import create_app

    with TestClient(create_app(max_concurrent_envs=1)) as limited_client:
        with limited_client.websocket_connect("/ws") as first_ws:
            first_ws.send_json({"type": "tasks"})
            assert first_ws.receive_json()["type"] == "tasks"

            with pytest.raises(Exception):
                with limited_client.websocket_connect("/ws"):
                    pass