Spaces:
Sleeping
Sleeping
Download tests/test_api.py from mjpsm/activity-generation-api: direct link, hf CLI and curl.
- Browser
- Download file 8.24 kB
-
https://huggingface.co/spaces/mjpsm/activity-generation-api/resolve/main/tests/test_api.py
- Command line
-
hf download hf://spaces/mjpsm/activity-generation-api/tests/test_api.py
-
curl -L -o test_api.py https://huggingface.co/spaces/mjpsm/activity-generation-api/resolve/main/tests/test_api.py
8.24 kB
| """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"] | |