File size: 2,739 Bytes
d061422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest

from fastapi.testclient import TestClient

from server.main import app


class TestAPI(unittest.TestCase):
    def setUp(self) -> None:
        self.client = TestClient(app)
        self.session_id = "test-session"

    def test_health_and_tasks(self) -> None:
        r = self.client.get("/health")
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.json()["status"], "ok")

        r = self.client.get("/tasks")
        self.assertEqual(r.status_code, 200)
        tasks = r.json()["tasks"]
        task_ids = {t["task_id"] for t in tasks}
        self.assertIn("easy_syntax_fix", task_ids)
        self.assertIn("medium_logic_fix", task_ids)
        self.assertIn("hard_multi_bug", task_ids)
        self.assertIn("hard_finance_explosion", task_ids)

    def test_reset_step_state_roundtrip(self) -> None:
        r = self.client.post(
            "/reset",
            headers={"x-session-id": self.session_id},
            json={"task_id": "easy_syntax_fix"},
        )
        self.assertEqual(r.status_code, 200)
        payload = r.json()
        self.assertEqual(payload["observation"]["task_id"], "easy_syntax_fix")
        self.assertEqual(payload["observation"]["steps_taken"], 0)

        r = self.client.post(
            "/step",
            headers={"x-session-id": self.session_id},
            json={"action": {"action_type": "inspect_schema"}},
        )
        self.assertEqual(r.status_code, 200)
        payload = r.json()
        self.assertEqual(payload["observation"]["steps_taken"], 1)
        self.assertEqual(payload["observation"]["last_action_type"], "inspect_schema")
        self.assertIsInstance(payload["reward"], float)

        r = self.client.get("/state", headers={"x-session-id": self.session_id})
        self.assertEqual(r.status_code, 200)
        state = r.json()
        self.assertEqual(state["task_id"], "easy_syntax_fix")
        self.assertEqual(state["steps_taken"], 1)

    def test_step_with_review_rejects_non_select(self) -> None:
        self.client.post(
            "/reset",
            headers={"x-session-id": self.session_id},
            json={"task_id": "easy_syntax_fix"},
        )

        r = self.client.post(
            "/step_with_review",
            headers={"x-session-id": self.session_id},
            json={"action": {"action_type": "submit_query", "query": "DELETE FROM customers;"}},
        )
        self.assertEqual(r.status_code, 200)
        payload = r.json()
        self.assertEqual(payload["info"]["review_rejected"], True)
        self.assertEqual(payload["reward"], 0.001)
        self.assertEqual(payload["observation"]["last_action_type"], "review_rejected")


if __name__ == "__main__":
    unittest.main()