Spaces:
Running
Running
File size: 8,586 Bytes
35ea9cd ca37eed b6d1ff0 4b84bac b6d1ff0 35ea9cd ca37eed 35ea9cd ca37eed 35ea9cd b6d1ff0 35ea9cd 4b84bac 35ea9cd b9d7c95 35ea9cd af2ccc5 35ea9cd b6d1ff0 35ea9cd 18aa055 35ea9cd 18aa055 ca37eed 35ea9cd b6d1ff0 af2ccc5 b6d1ff0 af2ccc5 b6d1ff0 35ea9cd | 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | import unittest
from fastapi.testclient import TestClient
from app import app, completed_states, sessions
from environment import IncidentEnv, validate_ticket_dataset
from incidents import TICKETS
from models import IncidentAction, IncidentState, TaskType
class IncidentEnvApiTests(unittest.TestCase):
def setUp(self) -> None:
sessions.clear()
completed_states.clear()
self.client = TestClient(app)
def tearDown(self) -> None:
sessions.clear()
completed_states.clear()
def test_health_schema_and_mcp_helper_endpoints(self) -> None:
health_response = self.client.get("/health")
self.assertEqual(health_response.status_code, 200)
self.assertEqual(health_response.json()["status"], "healthy")
schema_response = self.client.get("/schema")
self.assertEqual(schema_response.status_code, 200)
schema_body = schema_response.json()
self.assertIn("action", schema_body)
self.assertIn("observation", schema_body)
self.assertIn("state", schema_body)
mcp_response = self.client.post("/mcp", json={"jsonrpc": "2.0", "id": 1, "method": "ping"})
self.assertEqual(mcp_response.status_code, 200)
mcp_body = mcp_response.json()
self.assertEqual(mcp_body["jsonrpc"], "2.0")
self.assertEqual(mcp_body["id"], 1)
grader_response = self.client.get("/grader")
self.assertEqual(grader_response.status_code, 200)
grader_body = grader_response.json()
self.assertIn("notes", grader_body)
self.assertIn("task2", grader_body["notes"])
def test_tickets_endpoint_returns_safe_ticket_inventory(self) -> None:
response = self.client.get("/tickets")
self.assertEqual(response.status_code, 200)
body = response.json()
self.assertEqual(body["count"], len(TICKETS))
self.assertEqual(body["tickets"][0]["incident_id"], "INC-001")
self.assertIn("expected_field", body["tickets"][0])
self.assertNotIn("ground_truth", body["tickets"][0])
def test_ui_routes_and_assets_are_served(self) -> None:
home_response = self.client.get("/")
self.assertEqual(home_response.status_code, 200)
self.assertIn("Incident Triage Environment", home_response.text)
status_response = self.client.get("/status")
self.assertEqual(status_response.status_code, 200)
self.assertIn("Environment readiness dashboard", status_response.text)
playground_response = self.client.get("/playground")
self.assertEqual(playground_response.status_code, 200)
self.assertIn("Interactive playground", playground_response.text)
api_response = self.client.get("/api")
self.assertEqual(api_response.status_code, 200)
self.assertIn("API Explorer", api_response.text)
asset_response = self.client.get("/assets/app.js")
self.assertEqual(asset_response.status_code, 200)
self.assertIn("bootstrap", asset_response.text)
def test_reset_returns_requested_ticket_and_session_state(self) -> None:
response = self.client.post(
"/reset",
json={"task_type": "task3", "ticket_id": "INC-014"},
)
self.assertEqual(response.status_code, 200)
body = response.json()
self.assertEqual(body["observation"]["incident_id"], "INC-014")
self.assertEqual(body["observation"]["task_type"], "task3")
self.assertEqual(body["reward"]["value"], 0.01)
self.assertFalse(body["done"])
self.assertIn("session_id", body["info"])
self.assertEqual(body["info"]["state"]["status"], "awaiting_action")
def test_reset_without_seed_is_deterministic_for_same_task(self) -> None:
first_response = self.client.post("/reset", json={"task_type": "task2"})
second_response = self.client.post("/reset", json={"task_type": "task2"})
self.assertEqual(first_response.status_code, 200)
self.assertEqual(second_response.status_code, 200)
self.assertEqual(
first_response.json()["observation"]["incident_id"],
second_response.json()["observation"]["incident_id"],
)
def test_step_completes_episode_and_state_endpoint_reflects_completion(self) -> None:
reset_response = self.client.post(
"/reset",
json={"task_type": "task3", "ticket_id": "INC-014"},
)
session_id = reset_response.json()["info"]["session_id"]
step_response = self.client.post(
f"/step?session_id={session_id}",
json={
"incident_id": "INC-014",
"task_type": "task3",
"action": "FAILOVER",
},
)
self.assertEqual(step_response.status_code, 200)
step_body = step_response.json()
self.assertTrue(step_body["done"])
self.assertEqual(step_body["reward"]["value"], 0.99)
self.assertTrue(step_body["info"]["correct"])
self.assertEqual(step_body["info"]["ground_truth"], "FAILOVER")
state_response = self.client.get(f"/state?session_id={session_id}")
self.assertEqual(state_response.status_code, 200)
state_body = state_response.json()
self.assertTrue(state_body["done"])
self.assertEqual(state_body["status"], "completed")
self.assertEqual(state_body["last_reward"], 0.99)
self.assertNotIn(session_id, sessions)
self.assertIn(session_id, completed_states)
repeated_step_response = self.client.post(
f"/step?session_id={session_id}",
json={
"incident_id": "INC-014",
"task_type": "task3",
"action": "FAILOVER",
},
)
self.assertEqual(repeated_step_response.status_code, 400)
self.assertIn("already completed", repeated_step_response.json()["detail"])
def test_step_rejects_action_for_wrong_task_type(self) -> None:
reset_response = self.client.post(
"/reset",
json={"task_type": "task3", "ticket_id": "INC-014"},
)
session_id = reset_response.json()["info"]["session_id"]
step_response = self.client.post(
f"/step?session_id={session_id}",
json={
"incident_id": "INC-014",
"task_type": "task2",
"root_cause": "NETWORK",
},
)
self.assertEqual(step_response.status_code, 400)
self.assertIn("does not match", step_response.json()["detail"])
def test_dataset_validation_rejects_empty_ground_truth(self) -> None:
with self.assertRaisesRegex(RuntimeError, "empty ground_truth"):
validate_ticket_dataset(
[
{
"incident_id": "INC-BAD",
"task_type": "task1",
"alert_text": "Broken test ticket",
"context": {},
"ground_truth": {},
}
]
)
def test_step_raises_clear_dataset_error_for_invalid_ground_truth(self) -> None:
env = IncidentEnv()
env.current_ticket = {
"incident_id": "INC-BAD",
"task_type": "task1",
"alert_text": "Broken test ticket",
"context": {},
"ground_truth": {},
}
env.episode_id = "episode-bad"
with self.assertRaisesRegex(RuntimeError, "dataset integrity error"):
env.step(
IncidentAction(
incident_id="INC-BAD",
task_type="task1",
severity="SEV1",
)
)
def test_lifespan_shutdown_clears_session_stores(self) -> None:
sessions["active-session"] = IncidentEnv()
completed_states["done-session"] = IncidentState(
episode_id="episode-1",
session_id="done-session",
step_count=1,
max_steps=1,
total_reward=0.99,
done=True,
incident_id="INC-001",
task_type=TaskType.TASK1,
difficulty="easy",
status="completed",
last_reward=0.99,
)
with TestClient(app) as client:
response = client.get("/health")
self.assertEqual(response.status_code, 200)
self.assertEqual(sessions, {})
self.assertEqual(completed_states, {})
if __name__ == "__main__":
unittest.main()
|