Files
TieMeasureFlow/src/backend/tests/test_recipes_ai_parse_error.py
T
Adriano 403c923188 fix(ai): risposta AI malformata -> 502, non 400 (#36 punto 7)
json.JSONDecodeError e' un ValueError: KeyError/JSONDecodeError sulla
risposta del modello finivano nell'except ValueError del router (pensato
per i nostri errori di configurazione, es. API key mancante) e uscivano
come 400 invece di 502. Nuova AIResponseError dedicata in ai_service.py
per contenuto malformato o JSON non valido; recipes.py la traduce in 502,
toccato solo per questo except.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-21 22:43:42 +02:00

34 lines
1.2 KiB
Python

"""Router-level test for gitea#36 point 7: recipes.py translates
ai_service.AIResponseError to 502, not the config-error 400.
A dedicated file (not test_recipes.py) to avoid colliding with concurrent
work on the copy-on-write recipe logic in that shared test module.
"""
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from src.backend.models.orm.user import User
from src.backend.services import ai_service
from src.backend.tests.conftest import auth_headers, create_test_recipe
async def test_ai_response_error_is_502(
client: AsyncClient,
maker_user: User,
db_session: AsyncSession,
monkeypatch,
):
recipe = await create_test_recipe(db_session, maker_user.id, code="AI-PARSE-502")
async def _raise_ai_response_error(_pdf_bytes):
raise ai_service.AIResponseError("Risposta del modello AI non e' JSON valido: boom")
monkeypatch.setattr(ai_service, "parse_technical_sheet", _raise_ai_response_error)
resp = await client.post(
f"/api/recipes/{recipe.id}/parse-technical-sheet",
headers=auth_headers(maker_user),
files={"file": ("sheet.pdf", b"%PDF-1.4 fake", "application/pdf")},
)
assert resp.status_code == 502