import io import logging import pytest import soundfile from tests.conftest import HEADERS, body pytestmark = pytest.mark.anyio async def test_returns_ogg_opus(make_client): async with make_client() as client: response = await client.post("/v1/synthesize", json=body(), headers=HEADERS) assert response.status_code == 200 assert response.headers["content-type"].startswith("audio/ogg") assert response.headers["x-cache"] == "miss" assert response.headers["x-idempotent-replay"] == "false" assert int(response.headers["x-audio-duration-ms"]) > 0 info = soundfile.info(io.BytesIO(response.content)) assert (info.format, info.subtype, info.samplerate, info.channels) == ("OGG", "OPUS", 48000, 1) @pytest.mark.parametrize( "payload", [ body(extra="campo"), # campo desconhecido body(request_id="curto"), # requestId fora do padrão body(request_id="tem espaço no meio!"), body(text=" \n\t "), # vazio depois de normalizar body(text=""), body(params={"speed": 3.0}), # fora da faixa body(params={"format": "mp3"}), # formato não suportado body(params={"pitch": 2}), # parâmetro desconhecido {**body(), "voiceId": "../../etc/passwd"}, # voz fora do padrão ], ) async def test_validation_rejects(make_client, payload): async with make_client() as client: response = await client.post("/v1/synthesize", json=payload, headers=HEADERS) assert response.status_code == 422 assert response.json()["error"]["code"] == "invalid_request" async def test_validation_error_does_not_echo_text(make_client): secret = "texto-secreto-do-espectador" async with make_client() as client: response = await client.post("/v1/synthesize", json=body(text=secret, extra=secret), headers=HEADERS) assert response.status_code == 422 assert secret not in response.text async def test_unknown_voice(make_client): async with make_client() as client: response = await client.post("/v1/synthesize", json={**body(), "voiceId": "pt-br-nao-existe"}, headers=HEADERS) assert response.status_code == 422 assert response.json()["error"]["code"] == "voice_not_found" async def test_text_too_long(make_client): async with make_client() as client: response = await client.post("/v1/synthesize", json=body(text="a" * 201), headers=HEADERS) assert response.json()["error"]["code"] == "text_too_long" async def test_audio_too_long(make_client): # FakeEngine: ~15 caracteres/s; 190 caracteres ≈ 12,7 s > limite de 2 s. async with make_client(max_audio_s=2) as client: response = await client.post("/v1/synthesize", json=body(text="b" * 190), headers=HEADERS) assert response.status_code == 422 assert response.json()["error"]["code"] == "audio_too_long" async def test_text_never_logged(make_client, caplog): secret = "segredo do espectador 12345" caplog.set_level(logging.DEBUG) async with make_client() as client: await client.post("/v1/synthesize", json=body(text=secret), headers=HEADERS) await client.post("/v1/synthesize", json=body(request_id="req-00000002", text=secret), headers=HEADERS) assert "synthesize request_id=" in caplog.text assert secret not in caplog.text async def test_unreadable_body_uses_contract_error_format(make_client): async with make_client() as client: response = await client.post( "/v1/synthesize", content=b'{"text": "\xe7\xe3o"}', headers={**HEADERS, "Content-Type": "application/json"} ) missing = await client.get("/v1/nao-existe", headers=HEADERS) assert response.status_code in (400, 422) assert response.json()["error"]["code"] == "invalid_request" assert missing.status_code == 404 and missing.json()["error"]["code"] == "not_found"