File size: 8,235 Bytes
9c9ef1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Offline integration tests for the wrapper. No real model requests are made."""

import asyncio
import json

import httpx

from app.config import Settings
from app.main import create_app


REQUEST = {
    "version": "v1",
    "knowledge_submission": "I reviewed 25 saved chatbot conversations but have not compared them to expected answers.",
    "wisdom": {"content": "Test against expectations", "chapterTitle": "Testing", "BookTypeName": "Records", "BookType": "personal_record"},
    "village_goal": "Improve my customer service chatbot",
    "previous_activity_title": "Test Chatbot Conversations",
}


def call_wrapper(handler, data=None, *, settings=None, headers=None):
    """Start the real FastAPI lifespan and replace ONLY upstream HTTP traffic."""
    opts = settings or Settings(max_attempts=1)
    app = create_app(settings=opts, transport=httpx.MockTransport(handler))

    async def run():
        async with app.router.lifespan_context(app):
            async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client:
                return await client.post("/generate-activity", json=data if data is not None else REQUEST, headers=headers)

    return asyncio.run(run())


def standard_handler(calls):
    def handle(req):
        host = req.url.host
        if req.url.path == "/openapi.json":
            # Tests runtime discovery (not a hardcoded guessed POST route).
            return httpx.Response(200, json={"paths": {"/predict": {"post": {"operationId": "generate_activity"}}}})
        calls.append((host, req.url.path, json.loads(req.content)))
        if host == "mjpsm-activity-progression-api.hf.space":
            return httpx.Response(200, json={"progression": {"knowledge_state": "PARTIAL", "activity_plan": {"action": "compare responses"}}})
        if host == "mjpsm-activity-composition-api.hf.space":
            return httpx.Response(200, json={"output": {"activity_title": "Evaluate Responses", "activity_description": "Compare saved chatbot answers with expected answers."}})
        if host == "mjpsm-activity-instruction-api.hf.space":
            return httpx.Response(200, json={"output": {"instructions": ["Review the 25 conversations.", "Record recurring errors."]}})
        return httpx.Response(404)
    return handle


def test_sequential_pipeline_and_public_output():
    calls = []
    res = call_wrapper(standard_handler(calls))
    assert res.status_code == 200, res.text
    assert res.json() == {"output": {
        "activity_title": "Evaluate Responses",
        "activity_description": "Compare saved chatbot answers with expected answers.",
        "instructions": ["Review the 25 conversations.", "Record recurring errors."],
    }}
    assert len(calls) == 3
    assert calls[0][1] == calls[1][1] == calls[2][1] == "/predict"
    assert calls[0][2] == REQUEST
    assert calls[1][2] == {"progression": {"knowledge_state": "PARTIAL", "activity_plan": {"action": "compare responses"}}}
    assert calls[2][2] == {
        "progression": {"knowledge_state": "PARTIAL", "activity_plan": {"action": "compare responses"}},
        "composition": {"activity_title": "Evaluate Responses", "activity_description": "Compare saved chatbot answers with expected answers."},
    }


def test_envelope_compatibility_modes():
    calls = []
    s = Settings(max_attempts=1, composition_input_style="input", instruction_input_style="input_nested_output")
    res = call_wrapper(standard_handler(calls), settings=s)
    assert res.status_code == 200
    assert calls[1][2] == {"input": {"progression": {"knowledge_state": "PARTIAL", "activity_plan": {"action": "compare responses"}}}}
    assert calls[2][2]["input"]["composition"]["output"]["activity_title"] == "Evaluate Responses"


def test_invalid_input_rejected_before_upstream_call():
    calls = []
    bad = dict(REQUEST)
    bad["wisdom"] = {"content": "x", "chapterTitle": "y", "bookTypeName": "incorrect", "BookType": "personal_record"}
    res = call_wrapper(standard_handler(calls), bad)
    assert res.status_code == 422
    assert calls == []


def test_upstream_422_stops_pipeline():
    calls = []
    def handler(req):
        calls.append(str(req.url))
        if req.url.path == "/openapi.json":
            return httpx.Response(200, json={"paths": {"/predict": {"post": {}}}})
        if req.url.host == "mjpsm-activity-composition-api.hf.space":
            return httpx.Response(422, json={"detail": "invalid schema"})
        return standard_handler([])(req)
    res = call_wrapper(handler)
    assert res.status_code == 502
    assert res.json()["detail"]["stage"] == "composition"
    assert not any("instruction-api" in url for url in calls)
    assert "invalid schema" not in res.text


def test_invalid_instruction_list_rejected():
    calls = []
    base_handler = standard_handler(calls)
    def handler(req):
        if req.url.host == "mjpsm-activity-instruction-api.hf.space" and req.url.path != "/openapi.json":
            return httpx.Response(200, json={"output": {"instructions": "not an array"}})
        return base_handler(req)
    res = call_wrapper(handler)
    assert res.status_code == 502
    assert res.json()["detail"]["code"] == "invalid_output"
    assert res.json()["detail"]["stage"] == "instruction"


def test_bad_api_key_blocks_request():
    calls = []
    res = call_wrapper(standard_handler(calls), settings=Settings(api_key="test-secret"))
    assert res.status_code == 401
    assert calls == []
    allowed = call_wrapper(standard_handler([]), settings=Settings(api_key="test-secret", max_attempts=1), headers={"X-API-Key": "test-secret"})
    assert allowed.status_code == 200


def test_discovery_error_gives_actionable_message():
    def handler(req):
        return httpx.Response(404)
    res = call_wrapper(handler)
    assert res.status_code == 502
    assert res.json()["detail"]["stage"] == "progression"
    assert "PROGRESSION_API_URL" in res.text


def test_transient_error_retried_once():
    counts = {"progression": 0}
    base = standard_handler([])
    def handler(req):
        if req.url.host == "mjpsm-activity-progression-api.hf.space" and req.url.path == "/predict":
            counts["progression"] += 1
            if counts["progression"] == 1:
                return httpx.Response(503)
        return base(req)
    res = call_wrapper(handler, settings=Settings(max_attempts=2))
    assert res.status_code == 200
    assert counts["progression"] == 2


def test_explicit_post_urls_skip_openapi_discovery():
    calls = []
    def handler(req):
        assert req.url.path == "/my-real-post-route"
        calls.append(req.url.host)
        if "progression" in req.url.host:
            return httpx.Response(200, json={"output": {"progression": {"knowledge_state": "UNCLEAR", "activity_plan": {"action": "clarify"}}}})
        if "composition" in req.url.host:
            return httpx.Response(200, json={"output": {"activity_title": "Clarify Evidence", "activity_description": "Give one example of work completed."}})
        return httpx.Response(200, json={"output": {"instructions": ["Provide one example."]}})
    s = Settings(
        progression_api_url="https://mjpsm-activity-progression-api.hf.space/my-real-post-route",
        composition_api_url="https://mjpsm-activity-composition-api.hf.space/my-real-post-route",
        instruction_api_url="https://mjpsm-activity-instruction-api.hf.space/my-real-post-route",
        max_attempts=1,
    )
    res = call_wrapper(handler, settings=s)
    assert res.status_code == 200
    assert len(calls) == 3


def test_openapi_describes_public_contract_and_health():
    app = create_app(settings=Settings())
    async def run():
        async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client:
            spec = (await client.get("/openapi.json")).json()
            health = (await client.get("/health")).json()
            return spec, health
    spec, health = asyncio.run(run())
    assert health == {"status": "ok"}
    assert "/generate-activity" in spec["paths"]
    assert "POST" not in spec["paths"]["/generate-activity"]
    assert "post" in spec["paths"]["/generate-activity"]