File size: 5,913 Bytes
37f189f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Integration tests for the FastAPI server endpoints."""

import pytest
from httpx import AsyncClient, ASGITransport

from server.app import app


@pytest.fixture
async def client():
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        yield ac


class TestHealthEndpoint:
    async def test_health_returns_200(self, client):
        response = await client.get("/health")
        assert response.status_code == 200


class TestStateEndpoint:
    async def test_state_returns_200(self, client):
        response = await client.get("/state")
        assert response.status_code == 200

    async def test_state_returns_valid_json(self, client):
        response = await client.get("/state")
        data = response.json()
        assert isinstance(data, dict)


class TestResetEndpoint:
    async def test_reset_easy_returns_200(self, client):
        response = await client.post("/reset", json={"task_id": "easy"})
        assert response.status_code == 200

    async def test_reset_medium_returns_200(self, client):
        response = await client.post("/reset", json={"task_id": "medium"})
        assert response.status_code == 200

    async def test_reset_hard_returns_200(self, client):
        response = await client.post("/reset", json={"task_id": "hard"})
        assert response.status_code == 200

    async def test_reset_invalid_task_defaults_to_easy(self, client):
        response = await client.post("/reset", json={"task_id": "invalid"})
        assert response.status_code == 200
        data = response.json()
        assert data["observation"]["task_id"] == "easy"

    async def test_reset_returns_contract_text(self, client):
        response = await client.post("/reset", json={"task_id": "easy"})
        data = response.json()
        assert "contract_text" in data["observation"]
        assert len(data["observation"]["contract_text"]) > 0

    async def test_reset_returns_rules_to_check(self, client):
        response = await client.post("/reset", json={"task_id": "easy"})
        data = response.json()
        assert "rules_to_check" in data["observation"]
        assert len(data["observation"]["rules_to_check"]) > 0

    async def test_reset_returns_feedback(self, client):
        response = await client.post("/reset", json={"task_id": "easy"})
        data = response.json()
        assert "feedback" in data["observation"]

    async def test_reset_returns_step_zero(self, client):
        response = await client.post("/reset", json={"task_id": "easy"})
        data = response.json()
        assert data["observation"]["step"] == 0

    async def test_reset_returns_done_false(self, client):
        response = await client.post("/reset", json={"task_id": "easy"})
        assert response.status_code == 200
        data = response.json()
        assert "observation" in data


class TestStepEndpoint:
    async def test_step_without_reset_returns_observation(self, client):
        response = await client.post("/step", json={"action": {"identified_violations": []}})
        assert response.status_code == 200
        data = response.json()
        assert "observation" in data

    async def test_step_with_empty_violations(self, client):
        await client.post("/reset", json={"task_id": "easy"})
        response = await client.post("/step", json={"action": {"identified_violations": []}})
        assert response.status_code == 200
        data = response.json()
        assert data["observation"]["step"] == 1

    async def test_step_increments_step_count(self, client):
        await client.post("/reset", json={"task_id": "easy"})
        response1 = await client.post("/step", json={"action": {"identified_violations": []}})
        response2 = await client.post("/step", json={"action": {"identified_violations": []}})
        data1 = response1.json()
        data2 = response2.json()
        assert data2["observation"]["step"] == data1["observation"]["step"] + 1

    async def test_step_with_valid_violations(self, client):
        await client.post("/reset", json={"task_id": "easy"})
        state_resp = await client.get("/state")
        state = state_resp.json()
        violations = [
            {
                "rule_id": v["rule_id"],
                "description": v["description"],
                "severity": v["severity"],
            }
            for v in state["gold_violations"]
        ]
        response = await client.post("/step", json={"action": {"identified_violations": violations}})
        data = response.json()
        assert data["observation"]["last_reward"] > 0

    async def test_step_with_false_positives(self, client):
        await client.post("/reset", json={"task_id": "easy"})
        violations = [
            {
                "rule_id": "RULE_99",
                "description": "fake violation",
                "severity": "low",
            }
        ]
        response = await client.post("/step", json={"action": {"identified_violations": violations}})
        data = response.json()
        assert "feedback" in data["observation"]
        assert "False positives" in data["observation"]["feedback"]

    async def test_step_returns_feedback(self, client):
        await client.post("/reset", json={"task_id": "easy"})
        response = await client.post("/step", json={"action": {"identified_violations": []}})
        data = response.json()
        assert "feedback" in data["observation"]
        assert isinstance(data["observation"]["feedback"], str)

    async def test_step_returns_last_reward(self, client):
        await client.post("/reset", json={"task_id": "easy"})
        response = await client.post("/step", json={"action": {"identified_violations": []}})
        data = response.json()
        assert "last_reward" in data["observation"]
        assert isinstance(data["observation"]["last_reward"], float)