File size: 3,734 Bytes
0109502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import httpx
import pytest
from pipeline.nim_reviewer import NIMReviewer


def _reviewer(monkeypatch, retries=3):
    monkeypatch.setenv("NIM_MODEL", "test-model")
    monkeypatch.setenv("NIM_API_KEY", "test-key")
    return NIMReviewer({"functions": "..."}, retries=retries, base_delay=0.001)


class _FakeResponse:
    def __init__(self, status_code, json_body=None, text=""):
        self.status_code = status_code
        self._json_body = json_body or {}
        self.text = text
        self.reason_phrase = "ERR"
        self.request = httpx.Request("POST", "https://example.test/chat/completions")

    def json(self):
        return self._json_body

    def raise_for_status(self):
        if self.status_code >= 400:
            raise httpx.HTTPStatusError("err", request=self.request, response=self)


def _good_body():
    content = {
        "valid": True,
        "scores": {"correctness": 90, "instruction_alignment": 90, "quality": 90,
                    "training_value": 90, "scope_fit": 90},
        "overall_score": 90, "dominant_topic": "functions", "secondary_topics": [],
        "summary": "ok", "issues": [],
    }
    return {"choices": [{"message": {"content": json.dumps(content)}}]}


def test_401_fails_immediately_without_retry(monkeypatch):
    reviewer = _reviewer(monkeypatch, retries=3)
    calls = {"n": 0}

    def fake_post(self, url, headers=None, json=None):
        calls["n"] += 1
        return _FakeResponse(401, text="bad api key")

    monkeypatch.setattr(httpx.Client, "post", fake_post)

    with pytest.raises(RuntimeError, match="nim_request_failed_non_retryable"):
        reviewer.review("do x", "", "print(1)")

    assert calls["n"] == 1  # no retries burned on a non-retryable 401


def test_400_fails_immediately_without_retry(monkeypatch):
    reviewer = _reviewer(monkeypatch, retries=3)
    calls = {"n": 0}

    def fake_post(self, url, headers=None, json=None):
        calls["n"] += 1
        return _FakeResponse(400, text="bad request")

    monkeypatch.setattr(httpx.Client, "post", fake_post)

    with pytest.raises(RuntimeError, match="nim_request_failed_non_retryable"):
        reviewer.review("do x", "", "print(1)")

    assert calls["n"] == 1


def test_429_is_retried_then_succeeds(monkeypatch):
    reviewer = _reviewer(monkeypatch, retries=3)
    calls = {"n": 0}

    def fake_post(self, url, headers=None, json=None):
        calls["n"] += 1
        if calls["n"] < 3:
            return _FakeResponse(429, text="rate limited")
        return _FakeResponse(200, json_body=_good_body())

    monkeypatch.setattr(httpx.Client, "post", fake_post)

    result, raw, attempts = reviewer.review("do x", "", "print(1)")
    assert calls["n"] == 3
    assert attempts == 3
    assert result["overall_score"] == 90


def test_500_retries_and_eventually_exhausts(monkeypatch):
    reviewer = _reviewer(monkeypatch, retries=2)
    calls = {"n": 0}

    def fake_post(self, url, headers=None, json=None):
        calls["n"] += 1
        return _FakeResponse(503, text="down")

    monkeypatch.setattr(httpx.Client, "post", fake_post)

    with pytest.raises(RuntimeError, match="nim_retry_exhausted"):
        reviewer.review("do x", "", "print(1)")

    assert calls["n"] == 2  # retried up to the configured attempt count, then gave up


def test_200_succeeds_on_first_try(monkeypatch):
    reviewer = _reviewer(monkeypatch, retries=3)

    def fake_post(self, url, headers=None, json=None):
        return _FakeResponse(200, json_body=_good_body())

    monkeypatch.setattr(httpx.Client, "post", fake_post)

    result, raw, attempts = reviewer.review("do x", "", "print(1)")
    assert attempts == 1
    assert result["dominant_topic"] == "functions"