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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
"""
Test suite for the API Contract Debugger environment.

Coverage:
  - Violation detection (all violation types)
  - Grader scoring
  - Per-step reward shaping
  - Environment reset / step / state
  - All three tasks end-to-end
  - Edge cases: malformed actions, double-fix, already-clean spec
  - HTTP API routes (via TestClient)
"""

from __future__ import annotations

import copy
import sys
import os

import pytest

# Make sure the project root is on the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from server.fixtures import TASK_EASY, TASK_HARD, TASK_MEDIUM, TASKS
from server.graders import detect_violations, grade_episode, step_reward
from server.models import ActionKind, DebugAction
from server.environment import APIContractDebuggerEnv


# ===========================================================================
# Helpers
# ===========================================================================

def make_env(task: str = "easy") -> APIContractDebuggerEnv:
    env = APIContractDebuggerEnv(task_name=task)
    env.reset()
    return env


def action(**kwargs) -> DebugAction:
    defaults = dict(
        kind=ActionKind.NO_OP,
        endpoint_index=0,
        location="response_body",
        field_name=None,
        new_value=None,
    )
    defaults.update(kwargs)
    return DebugAction(**defaults)


# ===========================================================================
# 1. Fixture sanity
# ===========================================================================

class TestFixtures:
    def test_all_tasks_present(self):
        assert set(TASKS.keys()) == {"easy", "medium", "hard"}

    def test_easy_has_violations(self):
        v = detect_violations(TASK_EASY["broken_endpoints"], TASK_EASY["golden_endpoints"])
        assert len(v) == 1

    def test_medium_has_three_violations(self):
        v = detect_violations(TASK_MEDIUM["broken_endpoints"], TASK_MEDIUM["golden_endpoints"])
        assert len(v) == 3

    def test_hard_has_six_violations(self):
        v = detect_violations(TASK_HARD["broken_endpoints"], TASK_HARD["golden_endpoints"])
        assert len(v) == 6

    def test_golden_specs_are_clean(self):
        for task in TASKS.values():
            v = detect_violations(task["golden_endpoints"], task["golden_endpoints"])
            assert v == [], f"Golden spec for '{task['name']}' has violations: {v}"

    def test_broken_and_golden_same_length(self):
        for task in TASKS.values():
            assert len(task["broken_endpoints"]) == len(task["golden_endpoints"])


# ===========================================================================
# 2. Violation detection
# ===========================================================================

class TestViolationDetection:
    def test_missing_field_detected(self):
        current = [{"method": "GET", "path": "/x", "status_code": 200,
                     "request_body": {}, "response_body": {}}]
        golden  = [{"method": "GET", "path": "/x", "status_code": 200,
                     "request_body": {}, "response_body": {
                         "id": {"type": "integer", "required": True, "description": ""}
                     }}]
        v = detect_violations(current, golden)
        assert len(v) == 1
        assert v[0]["violation_type"] == "missing_field"
        assert v[0]["field_name"] == "id"

    def test_extra_field_detected(self):
        current = [{"method": "GET", "path": "/x", "status_code": 200,
                     "request_body": {}, "response_body": {
                         "secret": {"type": "string", "required": False, "description": ""}
                     }}]
        golden  = [{"method": "GET", "path": "/x", "status_code": 200,
                     "request_body": {}, "response_body": {}}]
        v = detect_violations(current, golden)
        assert len(v) == 1
        assert v[0]["violation_type"] == "extra_field"

    def test_wrong_type_detected(self):
        current = [{"method": "GET", "path": "/x", "status_code": 200,
                     "request_body": {}, "response_body": {
                         "count": {"type": "string", "required": True, "description": ""}
                     }}]
        golden  = [{"method": "GET", "path": "/x", "status_code": 200,
                     "request_body": {}, "response_body": {
                         "count": {"type": "integer", "required": True, "description": ""}
                     }}]
        v = detect_violations(current, golden)
        assert len(v) == 1
        assert v[0]["violation_type"] == "wrong_type"

    def test_wrong_status_detected(self):
        current = [{"method": "DELETE", "path": "/x", "status_code": 200,
                     "request_body": {}, "response_body": {}}]
        golden  = [{"method": "DELETE", "path": "/x", "status_code": 204,
                     "request_body": {}, "response_body": {}}]
        v = detect_violations(current, golden)
        assert len(v) == 1
        assert v[0]["violation_type"] == "wrong_status"

    def test_no_violations_on_matching_spec(self):
        golden = TASK_EASY["golden_endpoints"]
        v = detect_violations(golden, golden)
        assert v == []

    def test_violation_severity_range(self):
        v = detect_violations(TASK_HARD["broken_endpoints"], TASK_HARD["golden_endpoints"])
        for viol in v:
            assert 0.0 < viol["severity"] <= 1.0


# ===========================================================================
# 3. Grader scoring
# ===========================================================================

class TestGrader:
    def test_perfect_score_when_all_fixed(self):
        golden = TASK_EASY["golden_endpoints"]
        initial = detect_violations(TASK_EASY["broken_endpoints"], golden)
        score = grade_episode(golden, golden, initial)
        assert score == pytest.approx(1.0)

    def test_zero_score_when_nothing_fixed(self):
        broken = TASK_EASY["broken_endpoints"]
        golden = TASK_EASY["golden_endpoints"]
        initial = detect_violations(broken, golden)
        score = grade_episode(broken, golden, initial)
        assert score == pytest.approx(0.0)

    def test_partial_score_medium(self):
        broken = copy.deepcopy(TASK_MEDIUM["broken_endpoints"])
        golden = TASK_MEDIUM["golden_endpoints"]
        initial = detect_violations(broken, golden)

        # Fix only violation 1: product_id type
        broken[0]["response_body"]["product_id"]["type"] = "integer"

        score = grade_episode(broken, golden, initial)
        assert 0.0 < score < 1.0

    def test_score_clamped_to_zero_when_extra_violations_introduced(self):
        broken = copy.deepcopy(TASK_EASY["broken_endpoints"])
        golden = TASK_EASY["golden_endpoints"]
        initial = detect_violations(broken, golden)

        # Introduce more violations
        broken[0]["response_body"]["user_id"]["type"] = "string"
        broken[0]["response_body"]["username"]["type"] = "boolean"

        score = grade_episode(broken, golden, initial)
        assert score == 0.0

    def test_score_in_range(self):
        for task in TASKS.values():
            broken = task["broken_endpoints"]
            golden = task["golden_endpoints"]
            initial = detect_violations(broken, golden)
            score = grade_episode(broken, golden, initial)
            assert 0.0 <= score <= 1.0, f"Out-of-range score for task '{task['name']}'"

    def test_already_clean_spec_scores_one(self):
        golden = TASK_EASY["golden_endpoints"]
        initial: list = []  # no violations at start
        score = grade_episode(golden, golden, initial)
        assert score == pytest.approx(1.0)


# ===========================================================================
# 4. Step reward
# ===========================================================================

class TestStepReward:
    def _make_violation(self, vtype="missing_field", severity=1.0):
        return {
            "endpoint_index": 0, "location": "response_body",
            "field_name": "foo", "violation_type": vtype,
            "description": "test", "severity": severity,
        }

    def test_positive_reward_for_fix(self):
        v = self._make_violation()
        r = step_reward(prev_violations=[v], new_violations=[], initial_violations=[v], action_error=False)
        assert r > 0

    def test_negative_reward_for_introduction(self):
        v = self._make_violation()
        r = step_reward(prev_violations=[], new_violations=[v], initial_violations=[], action_error=False)
        assert r < 0

    def test_penalty_for_action_error(self):
        r = step_reward(prev_violations=[], new_violations=[], initial_violations=[], action_error=True)
        assert r == pytest.approx(-0.05)

    def test_zero_reward_for_no_op(self):
        r = step_reward(prev_violations=[], new_violations=[], initial_violations=[], action_error=False)
        assert r == pytest.approx(0.0)


# ===========================================================================
# 5. Environment — reset
# ===========================================================================

class TestEnvReset:
    def test_reset_returns_observation(self):
        env = APIContractDebuggerEnv(task_name="easy")
        obs = env.reset()
        assert obs.task_name == "easy"
        assert len(obs.violations) == 1
        assert obs.done is False
        assert obs.step_count == 0

    def test_reset_clears_state(self):
        env = make_env("easy")
        # Take a step, then reset
        env.step(action(
            kind=ActionKind.ADD_FIELD,
            location="response_body",
            field_name="created_at",
            new_value={"type": "string", "required": True, "description": "timestamp"},
        ))
        obs = env.reset()
        assert obs.step_count == 0
        assert len(obs.violations) == 1  # back to broken state

    def test_reset_switches_task(self):
        env = APIContractDebuggerEnv(task_name="easy")
        obs = env.reset(task_name="medium")
        assert obs.task_name == "medium"
        assert len(obs.violations) == 3

    def test_reset_preserves_golden(self):
        env = make_env("hard")
        obs = env.reset()
        assert obs.total_violations_at_start == 6

    def test_episode_id_set_on_reset(self):
        env = APIContractDebuggerEnv(task_name="easy")
        env.reset(episode_id="test-123")
        assert env.state.episode_id == "test-123"


# ===========================================================================
# 6. Environment — step mechanics
# ===========================================================================

class TestEnvStep:
    def test_add_missing_field_fixes_easy(self):
        env = make_env("easy")
        obs = env.step(action(
            kind=ActionKind.ADD_FIELD,
            location="response_body",
            field_name="created_at",
            new_value={"type": "string", "required": True, "description": "ISO timestamp"},
        ))
        assert len(obs.violations) == 0
        assert obs.done is True
        assert obs.reward > 0

    def test_wrong_type_action_introduces_violation(self):
        env = make_env("easy")
        obs = env.step(action(
            kind=ActionKind.ADD_FIELD,
            location="response_body",
            field_name="created_at",
            new_value={"type": "integer", "required": True, "description": "wrong type"},
        ))
        # Still has a violation (wrong type now)
        assert len(obs.violations) == 1
        assert obs.violations[0]["violation_type"] == "wrong_type"

    def test_out_of_range_endpoint_index(self):
        env = make_env("easy")
        obs = env.step(action(kind=ActionKind.ADD_FIELD, endpoint_index=99,
                               field_name="x", new_value={"type": "string"}))
        assert obs.last_action_error is not None
        assert "out of range" in obs.last_action_error

    def test_change_type_fixes_medium_violation(self):
        env = make_env("medium")
        # Fix violation 1: product_id type string→integer in response
        obs = env.step(action(
            kind=ActionKind.CHANGE_TYPE,
            endpoint_index=0,
            location="response_body",
            field_name="product_id",
            new_value="integer",
        ))
        assert obs.violations_fixed_this_step == 1
        assert len(obs.violations) == 2  # 2 remaining

    def test_change_status_fixes_medium_violation(self):
        env = make_env("medium")
        obs = env.step(action(
            kind=ActionKind.CHANGE_STATUS,
            endpoint_index=2,
            location="status_code",
            new_value=204,
        ))
        assert obs.violations_fixed_this_step == 1

    def test_remove_field_fixes_hard_extra_field(self):
        env = make_env("hard")
        obs = env.step(action(
            kind=ActionKind.REMOVE_FIELD,
            endpoint_index=1,
            location="response_body",
            field_name="password_hash",
        ))
        assert obs.violations_fixed_this_step == 1

    def test_no_op_does_not_change_violations(self):
        env = make_env("easy")
        before = len(env.state.violations)
        obs = env.step(action(kind=ActionKind.NO_OP))
        assert len(obs.violations) == before

    def test_step_after_done_returns_done(self):
        env = make_env("easy")
        # Solve it
        env.step(action(
            kind=ActionKind.ADD_FIELD,
            location="response_body",
            field_name="created_at",
            new_value={"type": "string", "required": True, "description": "ts"},
        ))
        # Step again — should get done=True with error message
        obs = env.step(action(kind=ActionKind.NO_OP))
        assert obs.done is True
        assert obs.last_action_error is not None

    def test_max_steps_terminates_episode(self):
        env = APIContractDebuggerEnv(task_name="easy")
        env.reset()
        obs = None
        for _ in range(env._task_cfg["max_steps"]):
            obs = env.step(action(kind=ActionKind.NO_OP))
        assert obs.done is True

    def test_step_count_increments(self):
        env = make_env("easy")
        env.step(action(kind=ActionKind.NO_OP))
        env.step(action(kind=ActionKind.NO_OP))
        assert env.state.step_count == 2


# ===========================================================================
# 7. Environment — state
# ===========================================================================

class TestEnvState:
    def test_state_reflects_current_endpoints(self):
        env = make_env("easy")
        state = env.state
        assert len(state.current_endpoints) == 1
        assert state.task_name == "easy"

    def test_state_tracks_step_count(self):
        env = make_env("easy")
        env.step(action(kind=ActionKind.NO_OP))
        assert env.state.step_count == 1

    def test_original_endpoints_unchanged_after_steps(self):
        env = make_env("easy")
        original_before = copy.deepcopy(env.state.original_endpoints)
        env.step(action(
            kind=ActionKind.ADD_FIELD,
            location="response_body",
            field_name="created_at",
            new_value={"type": "string", "required": True, "description": "ts"},
        ))
        assert env.state.original_endpoints == original_before


# ===========================================================================
# 8. Full episode walkthroughs
# ===========================================================================

class TestFullEpisodes:
    def test_easy_perfect_solve(self):
        env = make_env("easy")
        env.step(action(
            kind=ActionKind.ADD_FIELD,
            location="response_body",
            field_name="created_at",
            new_value={"type": "string", "required": True, "description": "ISO timestamp"},
        ))
        assert env.score() == pytest.approx(1.0)

    def test_medium_perfect_solve(self):
        env = make_env("medium")
        # Fix 1: product_id type
        env.step(action(kind=ActionKind.CHANGE_TYPE, endpoint_index=0,
                        location="response_body", field_name="product_id", new_value="integer"))
        # Fix 2: quantity type
        env.step(action(kind=ActionKind.CHANGE_TYPE, endpoint_index=1,
                        location="request_body", field_name="quantity", new_value="integer"))
        # Fix 3: DELETE status code
        env.step(action(kind=ActionKind.CHANGE_STATUS, endpoint_index=2,
                        location="status_code", new_value=204))
        assert env.score() == pytest.approx(1.0)

    def test_hard_perfect_solve(self):
        env = make_env("hard")
        # Fix 1: add refresh_token to /auth/login response
        env.step(action(kind=ActionKind.ADD_FIELD, endpoint_index=0,
                        location="response_body", field_name="refresh_token",
                        new_value={"type": "string", "required": True, "description": "Refresh token"}))
        # Fix 2: expires_in type string→integer in /auth/login response
        env.step(action(kind=ActionKind.CHANGE_TYPE, endpoint_index=0,
                        location="response_body", field_name="expires_in", new_value="integer"))
        # Fix 3: add created_at to /users/{id}/profile response
        env.step(action(kind=ActionKind.ADD_FIELD, endpoint_index=1,
                        location="response_body", field_name="created_at",
                        new_value={"type": "string", "required": True, "description": "ISO timestamp"}))
        # Fix 4: remove password_hash from /users/{id}/profile response
        env.step(action(kind=ActionKind.REMOVE_FIELD, endpoint_index=1,
                        location="response_body", field_name="password_hash"))
        # Fix 5: PATCH status 500→200
        env.step(action(kind=ActionKind.CHANGE_STATUS, endpoint_index=2,
                        location="status_code", new_value=200))
        # Fix 6: add updated_at to PATCH response
        env.step(action(kind=ActionKind.ADD_FIELD, endpoint_index=2,
                        location="response_body", field_name="updated_at",
                        new_value={"type": "string", "required": True, "description": "ISO timestamp"}))

        assert env.score() == pytest.approx(1.0)

    def test_score_after_partial_solve(self):
        env = make_env("medium")
        # Fix only 1 of 3
        env.step(action(kind=ActionKind.CHANGE_TYPE, endpoint_index=0,
                        location="response_body", field_name="product_id", new_value="integer"))
        score = env.score()
        assert 0.0 < score < 1.0

    def test_unknown_task_raises(self):
        with pytest.raises(ValueError, match="Unknown task"):
            APIContractDebuggerEnv(task_name="impossible")


# ===========================================================================
# 9. HTTP API routes (FastAPI TestClient)
# ===========================================================================

class TestHTTPRoutes:
    @pytest.fixture(autouse=True)
    def client(self):
        from fastapi.testclient import TestClient
        from server.app import app
        self.client = TestClient(app)

    def test_health_endpoint(self):
        r = self.client.get("/health")
        assert r.status_code == 200

    def test_reset_returns_200(self):
        r = self.client.post("/reset", json={})
        assert r.status_code == 200
        data = r.json()
        assert "violations" in data
        assert "endpoints" in data

    def test_reset_switches_task(self):
        r = self.client.post("/reset", json={"task_name": "medium"})
        assert r.status_code == 200
        assert r.json()["task_name"] == "medium"

    def test_reset_unknown_task_422(self):
        r = self.client.post("/reset", json={"task_name": "impossible"})
        assert r.status_code == 422

    def test_step_add_field(self):
        self.client.post("/reset", json={"task_name": "easy"})
        r = self.client.post("/step", json={
            "action": {
                "kind": "add_field",
                "endpoint_index": 0,
                "location": "response_body",
                "field_name": "created_at",
                "new_value": {"type": "string", "required": True, "description": "ts"},
            }
        })
        assert r.status_code == 200
        data = r.json()
        assert data["done"] is True
        assert data["reward"] > 0

    def test_step_invalid_action_422(self):
        self.client.post("/reset", json={})
        r = self.client.post("/step", json={"action": {"kind": "nonexistent_kind"}})
        assert r.status_code == 422

    def test_state_endpoint(self):
        self.client.post("/reset", json={"task_name": "easy"})
        r = self.client.get("/state")
        assert r.status_code == 200
        assert "current_endpoints" in r.json()

    def test_score_endpoint(self):
        self.client.post("/reset", json={"task_name": "easy"})
        r = self.client.get("/score")
        assert r.status_code == 200
        data = r.json()
        assert "score" in data
        assert 0.0 <= data["score"] <= 1.0

    def test_tasks_endpoint(self):
        r = self.client.get("/tasks")
        assert r.status_code == 200
        data = r.json()
        assert len(data["tasks"]) == 3

    def test_schema_endpoint(self):
        r = self.client.get("/schema")
        assert r.status_code == 200
        schema = r.json()
        assert "action" in schema
        assert "observation" in schema

    def test_full_easy_solve_via_http(self):
        self.client.post("/reset", json={"task_name": "easy"})
        r = self.client.post("/step", json={
            "action": {
                "kind": "add_field",
                "endpoint_index": 0,
                "location": "response_body",
                "field_name": "created_at",
                "new_value": {"type": "string", "required": True, "description": "ts"},
            }
        })
        assert r.json()["done"] is True
        score_r = self.client.get("/score")
        assert score_r.json()["score"] == pytest.approx(1.0)