File size: 6,188 Bytes
9c4d9c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""API integration tests for MissionCtrl.

Tests endpoint contracts: status codes, response shapes, and payload fields.
Run with: pytest tests/test_api.py -v
"""

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from fastapi.testclient import TestClient
from server.app import app

client = TestClient(app)


class TestRootEndpoint:
    def test_root_returns_200(self):
        resp = client.get("/")
        assert resp.status_code == 200

    def test_root_has_status(self):
        data = client.get("/").json()
        assert data["status"] == "ok"
        assert "endpoints" in data

    def test_root_has_name(self):
        data = client.get("/").json()
        assert data["name"] == "missionctrl"

    def test_root_has_heartbeat_and_logs(self):
        data = client.get("/").json()
        assert "heartbeat" in data
        assert "log_summary" in data
        assert "uptime_seconds" in data


class TestHealthEndpoint:
    def test_health_returns_200(self):
        resp = client.get("/health")
        assert resp.status_code == 200

    def test_health_has_healthy(self):
        data = client.get("/health").json()
        assert data["healthy"] is True

    def test_health_has_detailed_heartbeat_fields(self):
        data = client.get("/health").json()
        for key in ["status", "service", "container_id", "port", "timestamp_utc", "uptime_seconds"]:
            assert key in data


class TestResetEndpoint:
    def test_reset_returns_200(self):
        resp = client.post("/reset", json={"task_id": "easy"})
        assert resp.status_code == 200

    def test_reset_returns_observation(self):
        data = client.post("/reset", json={"task_id": "easy"}).json()
        assert "observation" in data
        assert data["done"] is False

    def test_reset_observation_has_tasks(self):
        data = client.post("/reset", json={"task_id": "medium"}).json()
        tasks = data["observation"]["tasks"]
        assert len(tasks) > 0
        assert "task_id" in tasks[0]
        assert "status" in tasks[0]

    def test_reset_invalid_task_returns_422(self):
        resp = client.post("/reset", json={"task_id": "nonexistent"})
        assert resp.status_code == 422

    def test_reset_all_tiers(self):
        for tier in ["easy", "medium", "hard", "special"]:
            resp = client.post("/reset", json={"task_id": tier})
            assert resp.status_code == 200, f"Failed for tier: {tier}"


class TestStepEndpoint:
    def test_step_returns_200(self):
        client.post("/reset", json={"task_id": "easy"})
        resp = client.post("/step", json={"action": "NOOP"})
        assert resp.status_code == 200

    def test_step_returns_all_fields(self):
        client.post("/reset", json={"task_id": "easy"})
        data = client.post("/step", json={"action": "NOOP"}).json()
        assert "observation" in data
        assert "reward" in data
        assert "done" in data
        assert "info" in data

    def test_step_reward_is_number(self):
        client.post("/reset", json={"task_id": "easy"})
        data = client.post("/step", json={"action": "NOOP"}).json()
        assert isinstance(data["reward"], (int, float))


class TestStateEndpoint:
    def test_state_returns_200(self):
        client.post("/reset", json={"task_id": "easy"})
        resp = client.get("/state")
        assert resp.status_code == 200

    def test_state_has_tasks(self):
        client.post("/reset", json={"task_id": "easy"})
        data = client.get("/state").json()
        assert "tasks" in data
        assert "timestamp_utc" in data


class TestHeartbeatEndpoints:
    def test_web_returns_200(self):
        resp = client.get("/web")
        assert resp.status_code == 200

    def test_ports_returns_200(self):
        resp = client.get("/ports")
        assert resp.status_code == 200

    def test_dashboard_ping_returns_200(self):
        resp = client.get("/dashboard/ping")
        assert resp.status_code == 200

    def test_web_and_ports_have_required_fields(self):
        web = client.get("/web").json()
        ports = client.get("/ports").json()
        for payload in [web, ports]:
            for key in ["status", "service", "build_id", "runtime", "host", "port", "timestamp_utc"]:
                assert key in payload

    def test_ports_payload_has_port_details(self):
        data = client.get("/ports").json()
        assert "details" in data
        assert "known_open_ports" in data["details"]


class TestHistoryEndpoint:
    def test_history_returns_200(self):
        resp = client.get("/history")
        assert resp.status_code == 200

    def test_history_is_list(self):
        data = client.get("/history").json()
        assert isinstance(data, list)

    def test_history_grows_after_step(self):
        client.post("/reset", json={"task_id": "easy"})
        before = len(client.get("/history").json())
        client.post("/step", json={"action": "NOOP"})
        after = len(client.get("/history").json())
        assert after > before


class TestDashboardEndpoint:
    def test_dashboard_returns_200(self):
        resp = client.get("/dashboard")
        assert resp.status_code == 200

    def test_dashboard_returns_html(self):
        resp = client.get("/dashboard")
        assert "text/html" in resp.headers.get("content-type", "")
        assert "MissionCtrl" in resp.text


class TestEndToEnd:
    """Full episode flow: reset → step × N → done with valid score."""

    def test_full_episode_easy(self):
        # Reset
        data = client.post("/reset", json={"task_id": "easy"}).json()
        assert data["done"] is False

        # Run steps until done
        done = False
        for i in range(20):
            step_data = client.post("/step", json={"action": "NOOP"}).json()
            if step_data["done"]:
                done = True
                # Check score
                score = step_data.get("info", {}).get("grader_score")
                if score is not None:
                    assert 0.0 < score < 1.0, f"Score out of range: {score}"
                break

        assert done, "Episode should have ended within 20 steps"