CenaForge: primeira versão pública
Gerador de vídeos narrativos (Godot + FFmpeg), painel Next.js, serviço de voz, Clipador de lives e Publicador para YouTube, TikTok e Instagram. Inclui a leitura das visualizações dos posts para o Dashboard e o botão "Publicar no <rede>" nos cartões das publicações. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from app.config import Settings
|
||||
from app.engine import FakeEngine
|
||||
from app.main import create_app
|
||||
from app.stt import FakeSttEngine
|
||||
|
||||
KEY = "k" * 40
|
||||
HEADERS = {"X-Service-Key": KEY}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def anyio_backend():
|
||||
return "asyncio"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def settings(tmp_path: Path) -> Settings:
|
||||
models = tmp_path / "models"
|
||||
(models / "piper").mkdir(parents=True)
|
||||
files = {"piper/fake.onnx": b"fake-model", "piper/fake.onnx.json": b"{}"}
|
||||
lock = []
|
||||
for rel, content in files.items():
|
||||
(models / rel).write_bytes(content)
|
||||
lock.append({"path": rel, "url": "https://example.invalid/" + rel, "sha256": hashlib.sha256(content).hexdigest()})
|
||||
(tmp_path / "lock.json").write_text(json.dumps({"files": lock}))
|
||||
catalog = {
|
||||
"voices": [
|
||||
{
|
||||
"id": "pt-br-test", "name": "Teste", "language": "pt-BR", "engine": "fake",
|
||||
"model": "piper/fake.onnx", "config": "piper/fake.onnx.json", "sampleRate": 22050,
|
||||
"default": True, "source": "teste", "license": "teste",
|
||||
}
|
||||
]
|
||||
}
|
||||
(tmp_path / "catalog.json").write_text(json.dumps(catalog))
|
||||
return Settings(
|
||||
service_key=KEY, models_dir=models, data_dir=tmp_path / "data",
|
||||
catalog_path=tmp_path / "catalog.json", lock_path=tmp_path / "lock.json",
|
||||
max_concurrency=1, max_queue_per_key=4, queue_timeout_s=5, synth_timeout_s=5,
|
||||
max_text_chars=200, max_audio_s=10,
|
||||
# Limites do contrato herdado do dropsz_voice (os testes de transcrição contam com 60 s / 8 MiB).
|
||||
stt_max_audio_s=60, stt_max_bytes=8 * 1024 * 1024, stt_queue_timeout_s=90, stt_timeout_s=45,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def engine() -> FakeEngine:
|
||||
return FakeEngine()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def stt_engine() -> FakeSttEngine:
|
||||
return FakeSttEngine()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_client(settings: Settings, engine: FakeEngine, stt_engine: FakeSttEngine):
|
||||
@asynccontextmanager
|
||||
async def _make(stt: FakeSttEngine | None | bool = True, **overrides):
|
||||
# stt=True usa o motor falso do fixture; None desliga a transcrição; outro objeto substitui.
|
||||
chosen = stt_engine if stt is True else stt
|
||||
app = create_app(replace(settings, **overrides), engine=engine, stt_engine=chosen)
|
||||
async with app.router.lifespan_context(app):
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url="http://voice") as client:
|
||||
yield client
|
||||
|
||||
return _make
|
||||
|
||||
|
||||
def body(request_id: str = "req-00000001", text: str = "Olá, valeu pela doação!", **extra) -> dict:
|
||||
payload = {"requestId": request_id, "text": text, "voiceId": "pt-br-test"}
|
||||
payload.update(extra)
|
||||
return payload
|
||||
@@ -0,0 +1,65 @@
|
||||
import pytest
|
||||
|
||||
from app.config import ConfigError, Settings
|
||||
from tests.conftest import HEADERS, body
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("method", "path", "headers"),
|
||||
[
|
||||
("POST", "/health", {}),
|
||||
("GET", "/v1/voices", {"X-Service-Key": "errada"}),
|
||||
("GET", "/v1/voices/pt-br-test", {"X-Service-Key": "errada"}),
|
||||
("POST", "/v1/synthesize", {}),
|
||||
("GET", "/rota-que-nao-existe", {}),
|
||||
("GET", "/docs", {}),
|
||||
],
|
||||
)
|
||||
async def test_without_valid_key_is_401(make_client, method, path, headers):
|
||||
async with make_client() as client:
|
||||
response = await client.request(method, path, headers=headers, json=body())
|
||||
assert response.status_code == 401
|
||||
assert response.json()["error"]["code"] == "unauthorized"
|
||||
|
||||
|
||||
async def test_malformed_json_without_key_is_401_not_422(make_client):
|
||||
async with make_client() as client:
|
||||
response = await client.post("/v1/synthesize", content=b"{nao-e-json", headers={"Content-Type": "application/json"})
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
async def test_key_is_not_accepted_as_bearer(make_client):
|
||||
async with make_client() as client:
|
||||
response = await client.get("/v1/voices", headers={"Authorization": f"Bearer {HEADERS['X-Service-Key']}"})
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
async def test_health_is_liveness_without_key_and_without_data(make_client):
|
||||
async with make_client() as client:
|
||||
anonymous = await client.get("/health")
|
||||
wrong = await client.get("/health", headers={"X-Service-Key": "errada"})
|
||||
for response in (anonymous, wrong):
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"status": "ok"}
|
||||
|
||||
|
||||
async def test_valid_key_opens_status_and_voices(make_client):
|
||||
async with make_client() as client:
|
||||
status = await client.get("/v1/status", headers=HEADERS)
|
||||
voices = await client.get("/v1/voices", headers=HEADERS)
|
||||
assert status.status_code == 200 and status.json()["status"] == "ok"
|
||||
assert [v["id"] for v in voices.json()["items"]] == ["pt-br-test"]
|
||||
|
||||
|
||||
async def test_openapi_is_disabled(make_client):
|
||||
async with make_client() as client:
|
||||
assert (await client.get("/openapi.json", headers=HEADERS)).status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", ["", "curta"])
|
||||
def test_service_refuses_to_start_without_strong_key(monkeypatch, key):
|
||||
monkeypatch.setenv("VOICE_SERVICE_KEY", key)
|
||||
with pytest.raises(ConfigError):
|
||||
Settings.from_env()
|
||||
@@ -0,0 +1,373 @@
|
||||
"""Funções que o CenaForge acrescentou ao serviço herdado do dropsz_voice.
|
||||
|
||||
Vozes Edge (sem arquivo local), vozes de personagem do usuário (voz base + RVC), saída WAV para o render,
|
||||
transcrição com tempos (legenda) e recarga do catálogo pelo painel.
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from app.catalog import load_catalog
|
||||
from app.engine import edge_rate
|
||||
from tests.conftest import HEADERS, body
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
ONE_SECOND = b"OggS" + b"x" * 100
|
||||
|
||||
|
||||
def stt_headers(detail: str, key: str = "ep01:legenda") -> dict:
|
||||
return {
|
||||
**HEADERS, "Content-Type": "audio/ogg", "X-Idempotency-Key": key,
|
||||
"X-Audio-Duration-Ms": "1000", "X-Language": "pt-BR", "X-Detail": detail,
|
||||
}
|
||||
|
||||
|
||||
def add_edge_voice(settings) -> None:
|
||||
catalog = json.loads(settings.catalog_path.read_text())
|
||||
catalog["voices"].append({
|
||||
"id": "pt-br-francisca", "name": "Francisca", "language": "pt-BR", "engine": "edge",
|
||||
"edgeVoice": "pt-BR-FranciscaNeural", "sampleRate": 24000, "source": "edge", "license": "edge",
|
||||
"gender": "female",
|
||||
})
|
||||
settings.catalog_path.write_text(json.dumps(catalog))
|
||||
|
||||
|
||||
def add_user_voice(settings, tmp_path: Path, model_sha: str | None = None) -> Path:
|
||||
folder = settings.models_dir / "rvc" / "heroi"
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
(folder / "heroi.pth").write_bytes(b"pesos-rvc")
|
||||
(folder / "heroi.index").write_bytes(b"indice")
|
||||
user = tmp_path / "user-voices.json"
|
||||
user.write_text(json.dumps({"voices": [{
|
||||
"id": "heroi", "name": "Herói", "baseVoice": "pt-br-test",
|
||||
"rvc": {
|
||||
"model": "rvc/heroi/heroi.pth", "modelSha256": model_sha or hashlib.sha256(b"pesos-rvc").hexdigest(),
|
||||
"index": "rvc/heroi/heroi.index", "indexSha256": hashlib.sha256(b"indice").hexdigest(), "pitch": 2,
|
||||
},
|
||||
}]}))
|
||||
return user
|
||||
|
||||
|
||||
def test_edge_rate():
|
||||
assert edge_rate(1.0) == "+0%"
|
||||
assert edge_rate(1.25) == "+25%"
|
||||
assert edge_rate(0.8) == "-20%"
|
||||
|
||||
|
||||
def test_edge_voice_needs_no_local_file(settings):
|
||||
add_edge_voice(settings)
|
||||
voices = load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir)
|
||||
edge = voices["pt-br-francisca"]
|
||||
assert edge.engine == "edge" and edge.model is None and edge.edge_voice == "pt-BR-FranciscaNeural"
|
||||
# Revisão estável: a mesma voz Edge sempre cai na mesma chave de cache.
|
||||
again = load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir)
|
||||
assert again["pt-br-francisca"].revision == edge.revision
|
||||
|
||||
|
||||
def test_user_voice_is_verified_and_bad_one_is_skipped(settings, tmp_path, caplog):
|
||||
user = add_user_voice(settings, tmp_path)
|
||||
voices = load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir, True, user)
|
||||
heroi = voices["heroi"]
|
||||
assert heroi.user and heroi.base_voice == "pt-br-test" and heroi.rvc["pitch"] == 2
|
||||
assert heroi.rvc["modelPath"].name == "heroi.pth" and not heroi.default
|
||||
|
||||
user = add_user_voice(settings, tmp_path, model_sha="0" * 64)
|
||||
voices = load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir, True, user)
|
||||
assert "heroi" not in voices and "pt-br-test" in voices # a voz quebrada não derruba as outras
|
||||
assert "voz do usuário ignorada" in caplog.text
|
||||
|
||||
|
||||
async def test_user_voice_descriptor_and_rvc_disabled(make_client, settings, tmp_path):
|
||||
user = add_user_voice(settings, tmp_path)
|
||||
async with make_client(user_catalog_path=user) as client:
|
||||
described = (await client.get("/v1/voices/heroi", headers=HEADERS)).json()
|
||||
response = await client.post("/v1/synthesize", json=body(voiceId="heroi"), headers=HEADERS)
|
||||
assert described["engine"] == "tts+rvc" and described["user"] is True and described["baseVoice"] == "pt-br-test"
|
||||
assert described["rvc"] == {"pitch": 2, "f0Method": "rmvpe", "indexRate": 0.5, "protect": 0.33, "hasIndex": True}
|
||||
assert "heroi.pth" not in json.dumps(described) # nunca o caminho do arquivo
|
||||
assert response.status_code == 501 and response.json()["error"]["code"] == "rvc_disabled"
|
||||
|
||||
|
||||
async def test_wav_output_for_render(make_client):
|
||||
async with make_client() as client:
|
||||
wav = await client.post("/v1/synthesize", json=body(params={"format": "wav"}), headers=HEADERS)
|
||||
ogg = await client.post("/v1/synthesize", json=body("req-00000002"), headers=HEADERS)
|
||||
assert wav.status_code == 200 and wav.headers["content-type"] == "audio/wav"
|
||||
assert wav.content[:4] == b"RIFF"
|
||||
assert ogg.content[:4] == b"OggS"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("detail", ["segments", "words"])
|
||||
async def test_transcribe_with_timestamps(make_client, stt_engine, detail):
|
||||
async with make_client() as client:
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers(detail))
|
||||
data = response.json()
|
||||
assert response.status_code == 200 and data["durationS"] == pytest.approx(1.0)
|
||||
[segment] = data["segments"]
|
||||
assert segment["text"] == "valeu pela live" and segment["start"] == 0.0
|
||||
if detail == "words":
|
||||
assert [w["text"] for w in segment["words"]] == ["valeu", "pela", "live"]
|
||||
assert segment["words"][-1]["end"] == pytest.approx(1.0)
|
||||
else:
|
||||
assert "words" not in segment
|
||||
assert stt_engine.details == [detail]
|
||||
|
||||
|
||||
async def test_detail_changes_the_job(make_client, stt_engine):
|
||||
# Mesma idempotencyKey com outro detalhe = outro conteúdo: conflito, não resposta errada do cache.
|
||||
async with make_client() as client:
|
||||
await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers("text"))
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers("words"))
|
||||
assert response.status_code == 409
|
||||
|
||||
|
||||
async def test_reload_picks_up_new_user_voice(make_client, settings, tmp_path):
|
||||
user = tmp_path / "user-voices.json"
|
||||
async with make_client(user_catalog_path=user) as client:
|
||||
assert (await client.get("/v1/voices/heroi", headers=HEADERS)).status_code == 404
|
||||
add_user_voice(settings, tmp_path)
|
||||
reloaded = await client.post("/v1/voices/reload", headers=HEADERS)
|
||||
assert reloaded.json() == {"status": "ok", "count": 2}
|
||||
assert (await client.get("/v1/voices/heroi", headers=HEADERS)).status_code == 200
|
||||
|
||||
|
||||
def _add_catalog_voice(settings, voice_id: str, model: str = "piper/fake.onnx") -> None:
|
||||
catalog = json.loads(settings.catalog_path.read_text())
|
||||
catalog["voices"].append({
|
||||
"id": voice_id, "name": "Nova", "language": "pt-BR", "engine": "fake", "model": model,
|
||||
"config": "piper/fake.onnx.json", "sampleRate": 22050, "source": "teste", "license": "teste",
|
||||
})
|
||||
settings.catalog_path.write_text(json.dumps(catalog))
|
||||
stat = settings.catalog_path.stat()
|
||||
# Garante data de modificação diferente mesmo em sistema de arquivos com resolução grossa.
|
||||
os.utime(settings.catalog_path, ns=(stat.st_atime_ns, stat.st_mtime_ns + 10_000_000_000))
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_catalog_change_shows_up_without_reload(make_client, settings):
|
||||
async with make_client() as client:
|
||||
assert (await client.get("/v1/voices/pt-br-nova", headers=HEADERS)).status_code == 404
|
||||
_add_catalog_voice(settings, "pt-br-nova")
|
||||
listed = await client.get("/v1/voices", headers=HEADERS)
|
||||
assert "pt-br-nova" in [v["id"] for v in listed.json()["items"]]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_broken_catalog_change_keeps_current_voices(make_client, settings):
|
||||
async with make_client() as client:
|
||||
_add_catalog_voice(settings, "pt-br-quebrada", model="piper/ausente.onnx")
|
||||
listed = await client.get("/v1/voices", headers=HEADERS)
|
||||
assert listed.status_code == 200
|
||||
assert [v["id"] for v in listed.json()["items"]] == ["pt-br-test"]
|
||||
|
||||
|
||||
def test_real_decoder_reads_wav_and_mp3_like_containers():
|
||||
import io
|
||||
|
||||
import soundfile
|
||||
|
||||
from app.decode import decode_audio
|
||||
from app.errors import InvalidAudio
|
||||
|
||||
tone = (np.sin(np.arange(22050) * 2 * np.pi * 220 / 22050) * 8000).astype(np.int16)
|
||||
buffer = io.BytesIO()
|
||||
soundfile.write(buffer, tone, 22050, format="WAV", subtype="PCM_16")
|
||||
samples = decode_audio(buffer.getvalue(), 16000, max_seconds=10)
|
||||
assert samples.dtype == np.float32 and abs(samples.size / 16000 - 1.0) < 0.05
|
||||
pcm = decode_audio(buffer.getvalue(), 24000, max_seconds=10, sample_format="s16")
|
||||
assert pcm.dtype == np.int16
|
||||
with pytest.raises(InvalidAudio):
|
||||
decode_audio(b"isto nao e audio", 16000, max_seconds=10)
|
||||
|
||||
|
||||
def test_settings_defaults_point_inside_project(monkeypatch):
|
||||
from app.config import PROJECT_ROOT, Settings
|
||||
|
||||
monkeypatch.setenv("VOICE_SERVICE_KEY", "k" * 40)
|
||||
for name in ("VOICE_MODELS_DIR", "VOICE_DATA_DIR", "VOICE_STT_MODEL", "VOICE_DEVICE"):
|
||||
monkeypatch.delenv(name, raising=False)
|
||||
settings = Settings.from_env()
|
||||
assert settings.models_dir == PROJECT_ROOT / "data" / "voice" / "models"
|
||||
assert settings.device in ("auto", "cuda", "cpu")
|
||||
monkeypatch.setenv("VOICE_MODELS_DIR", "outra/pasta")
|
||||
assert Settings.from_env().models_dir == PROJECT_ROOT / "outra" / "pasta" # relativo = raiz do projeto
|
||||
assert replace(settings, device="cpu").device == "cpu"
|
||||
|
||||
|
||||
async def test_duration_header_is_optional(make_client, stt_engine):
|
||||
headers = {k: v for k, v in stt_headers("text", key="sem-duracao").items() if k != "X-Audio-Duration-Ms"}
|
||||
async with make_client() as client:
|
||||
ok = await client.post("/v1/transcribe", content=ONE_SECOND, headers=headers)
|
||||
# O teto continua valendo pelo áudio decodificado (61 s > 60 s do fixture).
|
||||
long = await client.post("/v1/transcribe", content=b"OggS" + b"x" * 6100, headers={**headers, "X-Idempotency-Key": "longo"})
|
||||
assert ok.status_code == 200 and ok.json()["text"] == "valeu pela live"
|
||||
assert long.status_code == 422 and long.json()["error"]["code"] == "audio_too_long"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("smi", "busy"),
|
||||
[("7000, 10", False), ("1024, 10", True), ("7000, 99", True)],
|
||||
)
|
||||
def test_gpu_busy_reason_reads_nvidia_smi(monkeypatch, smi, busy):
|
||||
import subprocess
|
||||
from types import SimpleNamespace
|
||||
|
||||
from app import device
|
||||
|
||||
monkeypatch.setattr(subprocess, "run", lambda *a, **k: SimpleNamespace(stdout=smi + "\n"))
|
||||
reason = device.gpu_busy_reason()
|
||||
assert (reason is not None) == busy
|
||||
if busy:
|
||||
assert "GPU ocupada" in reason
|
||||
|
||||
|
||||
def test_gpu_busy_reason_without_nvidia_smi(monkeypatch):
|
||||
import subprocess
|
||||
|
||||
from app import device
|
||||
|
||||
def boom(*a, **k):
|
||||
raise FileNotFoundError("nvidia-smi")
|
||||
|
||||
monkeypatch.setattr(subprocess, "run", boom)
|
||||
assert device.gpu_busy_reason() is None # sem a ferramenta, não bloqueia a GPU
|
||||
|
||||
|
||||
def _write_catalog(settings, extra: dict) -> None:
|
||||
catalog = json.loads(settings.catalog_path.read_text())
|
||||
catalog["voices"].append({
|
||||
"id": "extra", "name": "Extra", "language": "pt-BR", "sampleRate": 24000, "source": "teste", "license": "teste",
|
||||
**extra,
|
||||
})
|
||||
settings.catalog_path.write_text(json.dumps(catalog))
|
||||
|
||||
|
||||
def test_catalog_reads_kokoro_and_google_voices(settings):
|
||||
_write_catalog(settings, {"engine": "kokoro", "model": "piper/fake.onnx", "config": "piper/fake.onnx.json",
|
||||
"kokoroVoice": "pf_dora"})
|
||||
voice = load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir)["extra"]
|
||||
assert (voice.engine, voice.engine_voice, voice.engine_lang) == ("kokoro", "pf_dora", "pt-br")
|
||||
|
||||
catalog = json.loads(settings.catalog_path.read_text())
|
||||
catalog["voices"][-1] = {**catalog["voices"][-1], "engine": "google", "googleTld": "pt"}
|
||||
settings.catalog_path.write_text(json.dumps(catalog))
|
||||
voice = load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir)["extra"]
|
||||
assert (voice.engine, voice.engine_voice, voice.engine_lang) == ("google", "pt", "pt")
|
||||
|
||||
|
||||
def test_kokoro_voice_without_style_is_rejected(settings):
|
||||
from app.config import ConfigError
|
||||
|
||||
_write_catalog(settings, {"engine": "kokoro", "model": "piper/fake.onnx", "config": "piper/fake.onnx.json"})
|
||||
with pytest.raises(ConfigError, match="kokoroVoice"):
|
||||
load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_online_flag_follows_engine(make_client, settings):
|
||||
_write_catalog(settings, {"engine": "google"})
|
||||
async with make_client() as client:
|
||||
items = {v["id"]: v for v in (await client.get("/v1/voices", headers=HEADERS)).json()["items"]}
|
||||
assert items["extra"]["offline"] is False and items["pt-br-test"]["offline"] is True
|
||||
|
||||
|
||||
def test_azure_ssml_escapes_text_and_sets_rate():
|
||||
from app.engine import AzureEngine
|
||||
|
||||
voice = replace(_azure_voice(), engine_voice="pt-BR-FabioNeural")
|
||||
ssml = AzureEngine("k", "eastus").ssml(voice, 'Oi <b>"x"</b> & tchau', 1.25).decode("utf-8")
|
||||
assert "<b>" in ssml and "&" in ssml and "<b>" not in ssml
|
||||
assert 'name="pt-BR-FabioNeural"' in ssml and 'xml:lang="pt-BR"' in ssml and 'rate="+25%"' in ssml
|
||||
assert "xmlns='http://www.w3.org/2001/10/synthesis'" in ssml
|
||||
|
||||
|
||||
def _azure_voice():
|
||||
from app.catalog import Voice
|
||||
|
||||
return Voice(id="azure-x", name="X", language="pt-BR", engine="azure", model=None, config=None, sample_rate=24000,
|
||||
default=False, source="t", license="t", revision="r", engine_voice="pt-BR-FabioNeural")
|
||||
|
||||
|
||||
def test_azure_synthesis_sends_key_and_decodes_wav(monkeypatch):
|
||||
import io
|
||||
import urllib.request
|
||||
|
||||
import soundfile
|
||||
|
||||
from app.engine import AzureEngine
|
||||
|
||||
tone = (np.sin(np.arange(24000) * 2 * np.pi * 220 / 24000) * 8000).astype(np.int16)
|
||||
buffer = io.BytesIO()
|
||||
soundfile.write(buffer, tone, 24000, format="WAV", subtype="PCM_16")
|
||||
seen = {}
|
||||
|
||||
class FakeResponse(io.BytesIO):
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc):
|
||||
return False
|
||||
|
||||
def fake_urlopen(request, timeout):
|
||||
seen["url"], seen["key"] = request.full_url, request.get_header("Ocp-apim-subscription-key")
|
||||
return FakeResponse(buffer.getvalue())
|
||||
|
||||
monkeypatch.setattr(urllib.request, "urlopen", fake_urlopen)
|
||||
pcm = AzureEngine("segredo", "brazilsouth").synthesize(_azure_voice(), "Olá", 1.0, 10)
|
||||
assert seen == {"url": "https://brazilsouth.tts.speech.microsoft.com/cognitiveservices/v1", "key": "segredo"}
|
||||
assert pcm.sample_rate == 24000 and abs(pcm.duration_s - 1.0) < 0.05
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_azure_voices_hidden_without_key(make_client, settings):
|
||||
_write_catalog(settings, {"engine": "azure", "azureVoice": "pt-BR-FabioNeural"})
|
||||
async with make_client() as client:
|
||||
ids = [v["id"] for v in (await client.get("/v1/voices", headers=HEADERS)).json()["items"]]
|
||||
assert "extra" not in ids
|
||||
async with make_client(azure_key="chave") as client:
|
||||
items = {v["id"]: v for v in (await client.get("/v1/voices", headers=HEADERS)).json()["items"]}
|
||||
assert items["extra"]["offline"] is False and items["extra"]["provider"] == "azure"
|
||||
|
||||
|
||||
def _add_clone_voice(settings, tmp_path, consent=True):
|
||||
reference = settings.models_dir / "clone" / "clone-eu" / "reference.wav"
|
||||
reference.parent.mkdir(parents=True)
|
||||
reference.write_bytes(b"RIFF-fake-wav")
|
||||
entry = {
|
||||
"id": "clone-eu", "name": "Eu", "kind": "clone", "gender": "male", "source": "minha voz", "license": "minha",
|
||||
"consent": consent,
|
||||
"clone": {"reference": "clone/clone-eu/reference.wav",
|
||||
"referenceSha256": hashlib.sha256(b"RIFF-fake-wav").hexdigest(), "exaggeration": 0.7},
|
||||
}
|
||||
user = tmp_path / "user-voices.json"
|
||||
user.write_text(json.dumps({"voices": [entry]}))
|
||||
return user
|
||||
|
||||
|
||||
def test_clone_voice_loads_with_consent(settings, tmp_path):
|
||||
user = _add_clone_voice(settings, tmp_path)
|
||||
voice = load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir, user_catalog_path=user)["clone-eu"]
|
||||
assert (voice.engine, voice.user, voice.engine_lang) == ("clone", True, "pt")
|
||||
assert voice.clone == {"exaggeration": 0.7, "cfgWeight": 0.5} and voice.model.name == "reference.wav"
|
||||
|
||||
|
||||
def test_clone_voice_without_consent_is_skipped(settings, tmp_path):
|
||||
user = _add_clone_voice(settings, tmp_path, consent=False)
|
||||
voices = load_catalog(settings.catalog_path, settings.lock_path, settings.models_dir, user_catalog_path=user)
|
||||
assert "clone-eu" not in voices
|
||||
|
||||
|
||||
def test_clone_engine_reports_worker_down():
|
||||
from app.engine import CloneEngine
|
||||
from app.errors import CloneUnavailable
|
||||
|
||||
voice = replace(_azure_voice(), engine="clone", model=Path("x.wav"), clone={"exaggeration": 0.5, "cfgWeight": 0.5})
|
||||
with pytest.raises(CloneUnavailable):
|
||||
CloneEngine(4599, "k", timeout_s=2).synthesize(voice, "Olá", 1.0, 10)
|
||||
@@ -0,0 +1,82 @@
|
||||
import pytest
|
||||
|
||||
from app.store import Store
|
||||
from tests.conftest import HEADERS, body
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
|
||||
async def test_same_request_id_same_payload_is_replay(make_client, engine):
|
||||
async with make_client() as client:
|
||||
first = await client.post("/v1/synthesize", json=body(), headers=HEADERS)
|
||||
second = await client.post("/v1/synthesize", json=body(), headers=HEADERS)
|
||||
assert first.status_code == second.status_code == 200
|
||||
assert second.headers["x-idempotent-replay"] == "true"
|
||||
assert second.headers["x-cache"] == "hit"
|
||||
assert first.content == second.content
|
||||
assert engine.calls == 1
|
||||
|
||||
|
||||
async def test_same_request_id_other_payload_is_409(make_client):
|
||||
async with make_client() as client:
|
||||
await client.post("/v1/synthesize", json=body(), headers=HEADERS)
|
||||
other_text = await client.post("/v1/synthesize", json=body(text="Outra coisa"), headers=HEADERS)
|
||||
other_speed = await client.post("/v1/synthesize", json=body(params={"speed": 1.5}), headers=HEADERS)
|
||||
assert other_text.status_code == other_speed.status_code == 409
|
||||
assert other_text.json()["error"]["code"] == "idempotency_conflict"
|
||||
|
||||
|
||||
async def test_normalized_text_is_the_same_request(make_client, engine):
|
||||
async with make_client() as client:
|
||||
await client.post("/v1/synthesize", json=body(text="Olá, valeu\n pela doação!"), headers=HEADERS)
|
||||
again = await client.post("/v1/synthesize", json=body(text=" Olá, valeu pela doação! "), headers=HEADERS)
|
||||
assert again.status_code == 200
|
||||
assert engine.calls == 1
|
||||
|
||||
|
||||
async def test_cache_hit_across_request_ids(make_client, engine):
|
||||
async with make_client() as client:
|
||||
first = await client.post("/v1/synthesize", json=body("req-aaaaaaaa"), headers=HEADERS)
|
||||
second = await client.post("/v1/synthesize", json=body("req-bbbbbbbb"), headers=HEADERS)
|
||||
assert first.headers["x-cache"] == "miss"
|
||||
assert second.headers["x-cache"] == "hit"
|
||||
assert second.headers["x-idempotent-replay"] == "false"
|
||||
assert first.headers["x-audio-sha256"] == second.headers["x-audio-sha256"]
|
||||
assert engine.calls == 1
|
||||
|
||||
|
||||
async def test_different_params_or_text_miss(make_client, engine):
|
||||
async with make_client() as client:
|
||||
await client.post("/v1/synthesize", json=body("req-aaaaaaaa"), headers=HEADERS)
|
||||
speed = await client.post("/v1/synthesize", json=body("req-bbbbbbbb", params={"speed": 1.25}), headers=HEADERS)
|
||||
text = await client.post("/v1/synthesize", json=body("req-cccccccc", text="Outro texto"), headers=HEADERS)
|
||||
assert speed.headers["x-cache"] == text.headers["x-cache"] == "miss"
|
||||
assert engine.calls == 3
|
||||
|
||||
|
||||
async def test_cache_survives_restart(make_client, engine):
|
||||
async with make_client() as client:
|
||||
await client.post("/v1/synthesize", json=body("req-aaaaaaaa"), headers=HEADERS)
|
||||
async with make_client() as client:
|
||||
again = await client.post("/v1/synthesize", json=body("req-aaaaaaaa"), headers=HEADERS)
|
||||
assert again.headers["x-cache"] == "hit"
|
||||
assert again.headers["x-idempotent-replay"] == "true"
|
||||
assert engine.calls == 1
|
||||
|
||||
|
||||
def test_store_evicts_least_recently_used(tmp_path):
|
||||
store = Store(tmp_path, cache_max_mb=1, idempotency_ttl_h=1)
|
||||
chunk = b"x" * (400 * 1024)
|
||||
store.put("a" * 64, chunk, 1000, "h")
|
||||
store.put("b" * 64, chunk, 1000, "h")
|
||||
store.get("a" * 64) # "a" fica mais recente que "b"
|
||||
store.put("c" * 64, chunk, 1000, "h")
|
||||
assert store.get("b" * 64) is None
|
||||
assert store.get("a" * 64) is not None and store.get("c" * 64) is not None
|
||||
assert store.stats()["bytes"] <= 1024 * 1024
|
||||
|
||||
|
||||
def test_request_id_bound_by_primary_key(tmp_path):
|
||||
store = Store(tmp_path, cache_max_mb=1, idempotency_ttl_h=1)
|
||||
assert store.bind_request("env", "req-1", "k1") == (True, "k1")
|
||||
assert store.bind_request("env", "req-1", "k2") == (False, "k1")
|
||||
@@ -0,0 +1,274 @@
|
||||
"""Isolamento entre chaves de serviço (homologação e produção no mesmo serviço): fila, idempotência e jobs."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import sqlite3
|
||||
import threading
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app.errors import QueueFull, QueueTimeout, SynthesisTimeout
|
||||
from app.keys import issue_key, keys_path
|
||||
from app.store import Store
|
||||
from app.work_queue import WorkQueue
|
||||
from tests.conftest import HEADERS, body
|
||||
from tests.test_queue import _wait_until
|
||||
from tests.test_transcribe import ONE_SECOND, stt_headers
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
|
||||
def _issue(settings, scopes=("tts", "transcribe", "status"), name="homolog"):
|
||||
record, plaintext = issue_key(keys_path(settings.data_dir), name, tuple(scopes))
|
||||
return record, {"X-Service-Key": plaintext}
|
||||
|
||||
|
||||
# ---------- fila ----------
|
||||
|
||||
async def test_full_queue_of_one_key_does_not_refuse_the_other():
|
||||
gate = threading.Event()
|
||||
queue = WorkQueue(max_concurrency=1, max_queue_per_key=1, queue_timeout_s=5, run_timeout_s=5)
|
||||
running = asyncio.create_task(queue.run(lambda: gate.wait(5), owner="homolog"))
|
||||
await _wait_until(lambda: queue.running == 1)
|
||||
homolog_waiting = asyncio.create_task(queue.run(lambda: "homolog-2", owner="homolog"))
|
||||
await _wait_until(lambda: queue.waiting_for("homolog") == 1)
|
||||
with pytest.raises(QueueFull):
|
||||
await queue.run(lambda: "homolog-3", owner="homolog")
|
||||
assert not queue.is_full("prod")
|
||||
prod = asyncio.create_task(queue.run(lambda: "prod-1", owner="prod"))
|
||||
await _wait_until(lambda: queue.waiting_for("prod") == 1)
|
||||
gate.set()
|
||||
assert await running is True
|
||||
assert await homolog_waiting == "homolog-2"
|
||||
assert await prod == "prod-1"
|
||||
queue.shutdown()
|
||||
|
||||
|
||||
async def test_cpu_slot_rotates_between_keys():
|
||||
"""A vaga de CPU é global, mas entregue em rodízio: a produção não espera a fila inteira da homologação."""
|
||||
gate = threading.Event()
|
||||
order: list[str] = []
|
||||
queue = WorkQueue(max_concurrency=1, max_queue_per_key=5, queue_timeout_s=5, run_timeout_s=5)
|
||||
first = asyncio.create_task(queue.run(lambda: gate.wait(5), owner="homolog"))
|
||||
await _wait_until(lambda: queue.running == 1)
|
||||
tasks = [asyncio.create_task(queue.run(lambda i=i: order.append(f"homolog-{i}"), owner="homolog")) for i in range(3)]
|
||||
await _wait_until(lambda: queue.waiting_for("homolog") == 3)
|
||||
tasks.append(asyncio.create_task(queue.run(lambda: order.append("prod"), owner="prod")))
|
||||
await _wait_until(lambda: queue.waiting_for("prod") == 1)
|
||||
gate.set()
|
||||
await first
|
||||
await asyncio.gather(*tasks)
|
||||
assert order == ["prod", "homolog-0", "homolog-1", "homolog-2"]
|
||||
queue.shutdown()
|
||||
|
||||
|
||||
async def test_http_full_queue_of_one_key_does_not_block_the_other(make_client, settings, engine):
|
||||
engine.gate = threading.Event()
|
||||
async with make_client(max_concurrency=1, max_queue_per_key=1) as client:
|
||||
_, homolog = _issue(settings)
|
||||
first = asyncio.create_task(client.post("/v1/synthesize", json=body("req-h0000001", text="um"), headers=homolog))
|
||||
await _wait_until(lambda: engine.calls == 1)
|
||||
second = asyncio.create_task(client.post("/v1/synthesize", json=body("req-h0000002", text="dois"), headers=homolog))
|
||||
await asyncio.sleep(0.1)
|
||||
refused = await client.post("/v1/synthesize", json=body("req-h0000003", text="três"), headers=homolog)
|
||||
status_homolog = (await client.get("/v1/status", headers=homolog)).json()["queues"]["synthesis"]
|
||||
status_prod = (await client.get("/v1/status", headers=HEADERS)).json()["queues"]["synthesis"]
|
||||
prod = asyncio.create_task(client.post("/v1/synthesize", json=body("req-p0000001", text="pago"), headers=HEADERS))
|
||||
await asyncio.sleep(0.1)
|
||||
engine.gate.set()
|
||||
results = [await first, await second, await prod]
|
||||
assert refused.status_code == 503 and refused.json()["error"]["code"] == "queue_full"
|
||||
assert [r.status_code for r in results] == [200, 200, 200]
|
||||
# O /v1/status mostra a fila da própria chave: a homologação cheia não aparece como cheia para a produção.
|
||||
assert status_homolog["waiting"] == 1 and status_homolog["maxQueue"] == 1
|
||||
assert status_prod["waiting"] == 0 and status_prod["running"] == 1
|
||||
|
||||
|
||||
# ---------- idempotência da síntese ----------
|
||||
|
||||
async def test_same_request_id_on_two_keys_are_distinct_requests(make_client, settings):
|
||||
async with make_client() as client:
|
||||
_, homolog = _issue(settings)
|
||||
prod = await client.post("/v1/synthesize", json=body("req-colide01", text="texto da produção"), headers=HEADERS)
|
||||
other = await client.post("/v1/synthesize", json=body("req-colide01", text="texto da homologação"), headers=homolog)
|
||||
assert prod.status_code == 200 and other.status_code == 200 # sem 409: são pedidos diferentes
|
||||
assert prod.headers["x-idempotent-replay"] == "false"
|
||||
assert other.headers["x-idempotent-replay"] == "false"
|
||||
assert prod.content != other.content
|
||||
|
||||
|
||||
async def test_same_key_stays_idempotent(make_client, settings):
|
||||
async with make_client() as client:
|
||||
_, homolog = _issue(settings)
|
||||
first = await client.post("/v1/synthesize", json=body("req-idem0001"), headers=homolog)
|
||||
again = await client.post("/v1/synthesize", json=body("req-idem0001"), headers=homolog)
|
||||
conflict = await client.post("/v1/synthesize", json=body("req-idem0001", text="outro texto"), headers=homolog)
|
||||
assert first.headers["x-idempotent-replay"] == "false"
|
||||
assert again.headers["x-idempotent-replay"] == "true" and again.content == first.content
|
||||
assert conflict.status_code == 409
|
||||
|
||||
|
||||
# ---------- transcrição ----------
|
||||
|
||||
async def test_job_of_another_key_answers_like_an_unknown_job(make_client, settings):
|
||||
async with make_client(stt_sync_wait_s=0.0) as client:
|
||||
_, homolog = _issue(settings)
|
||||
created = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers())
|
||||
ref = created.headers["x-job-ref"]
|
||||
await asyncio.sleep(0.05)
|
||||
own = await client.get(f"/v1/transcribe/jobs/{ref}", headers=HEADERS)
|
||||
foreign = await client.get(f"/v1/transcribe/jobs/{ref}", headers=homolog)
|
||||
unknown = await client.get("/v1/transcribe/jobs/abcdefghijkl", headers=homolog)
|
||||
assert own.status_code in (200, 202)
|
||||
# Mesmo status e mesmo corpo de um job inexistente: não revela que o job existe.
|
||||
assert foreign.status_code == unknown.status_code == 410
|
||||
assert foreign.json() == unknown.json()
|
||||
assert "x-job-ref" not in foreign.headers
|
||||
|
||||
|
||||
async def test_same_idempotency_key_on_two_keys_does_not_collide(make_client, settings, stt_engine):
|
||||
async with make_client() as client:
|
||||
_, homolog = _issue(settings)
|
||||
prod = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers(key="order:1:t"))
|
||||
other = await client.post(
|
||||
"/v1/transcribe", content=ONE_SECOND + b"z", headers=stt_headers(key="order:1:t", **homolog)
|
||||
)
|
||||
same_audio = await client.post(
|
||||
"/v1/transcribe", content=ONE_SECOND, headers=stt_headers(key="order:2:t", **homolog)
|
||||
)
|
||||
replay = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers(key="order:1:t"))
|
||||
assert prod.status_code == other.status_code == 200 # sem 409 entre chaves
|
||||
assert other.headers["x-idempotent-replay"] == "false"
|
||||
# O mesmo áudio por outra chave vira outro job (nada de jobRef compartilhado entre chaves).
|
||||
assert same_audio.headers["x-job-ref"] != prod.headers["x-job-ref"]
|
||||
assert replay.headers["x-idempotent-replay"] == "true"
|
||||
assert replay.headers["x-job-ref"] == prod.headers["x-job-ref"]
|
||||
|
||||
|
||||
# ---------- banco no formato antigo ----------
|
||||
|
||||
LEGACY_SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS requests (request_id TEXT PRIMARY KEY, cache_key TEXT NOT NULL, created_at INTEGER NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS transcribe_requests (
|
||||
idempotency_key TEXT PRIMARY KEY, content_key TEXT NOT NULL, created_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS cache (
|
||||
cache_key TEXT PRIMARY KEY, size INTEGER NOT NULL, duration_ms INTEGER NOT NULL, sha256 TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL, last_used_at INTEGER NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
|
||||
def _legacy_bind(db_path, request_id, cache_key):
|
||||
"""O que a imagem anterior faz no bind_request: INSERT sem key_id e, no conflito, lê a linha gravada."""
|
||||
db = sqlite3.connect(db_path)
|
||||
try:
|
||||
db.execute("INSERT INTO requests(request_id, cache_key, created_at) VALUES (?, ?, strftime('%s','now'))",
|
||||
(request_id, cache_key))
|
||||
db.commit()
|
||||
return True, cache_key
|
||||
except sqlite3.IntegrityError:
|
||||
return False, db.execute("SELECT cache_key FROM requests WHERE request_id = ?", (request_id,)).fetchone()[0]
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def test_store_boots_over_legacy_database_and_keeps_rollback_working(tmp_path):
|
||||
db_path = tmp_path / "voice.sqlite3"
|
||||
db = sqlite3.connect(db_path)
|
||||
db.executescript(LEGACY_SCHEMA)
|
||||
db.execute("INSERT INTO requests VALUES ('req-antigo1', 'k-antiga', strftime('%s','now'))")
|
||||
db.execute("INSERT INTO transcribe_requests VALUES ('order:9:t', 'c-antiga', strftime('%s','now'))")
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
store = Store(tmp_path, cache_max_mb=1, idempotency_ttl_h=1)
|
||||
cached = store.put("a" * 64, b"ogg", 1000, "h")
|
||||
assert store.bind_request("env", "req-antigo1", "k-nova") == (True, "k-nova")
|
||||
assert store.bind_request("homolog", "req-antigo1", "k-outra") == (True, "k-outra")
|
||||
assert store.bind_request("env", "req-antigo1", "k-x") == (False, "k-nova")
|
||||
assert store.bind_transcription("env", "order:9:t", "c-nova") == (True, "c-nova")
|
||||
store.close()
|
||||
|
||||
# As tabelas antigas continuam intactas: mesmo esquema e mesmas linhas.
|
||||
db = sqlite3.connect(db_path)
|
||||
assert [r[1] for r in db.execute("PRAGMA table_info(requests)")] == ["request_id", "cache_key", "created_at"]
|
||||
assert db.execute("SELECT request_id, cache_key FROM requests").fetchall() == [("req-antigo1", "k-antiga")]
|
||||
assert db.execute("SELECT idempotency_key, content_key FROM transcribe_requests").fetchall() == [
|
||||
("order:9:t", "c-antiga")
|
||||
]
|
||||
db.close()
|
||||
# Rollback: a imagem anterior segue gravando e fazendo replay sobre o mesmo arquivo, e enxerga o cache.
|
||||
assert _legacy_bind(db_path, "req-rollback", "k-r") == (True, "k-r")
|
||||
assert _legacy_bind(db_path, "req-rollback", "k-r") == (False, "k-r")
|
||||
assert _legacy_bind(db_path, "req-antigo1", "k-antiga") == (False, "k-antiga")
|
||||
|
||||
# Volta para a versão nova: as amarrações dela continuam lá.
|
||||
store = Store(tmp_path, cache_max_mb=1, idempotency_ttl_h=1)
|
||||
assert store.bind_request("env", "req-antigo1", "k-x") == (False, "k-nova")
|
||||
assert store.get(cached.cache_key) is not None
|
||||
store.close()
|
||||
|
||||
|
||||
# ---------- timeouts ----------
|
||||
|
||||
async def _hog_then_other(queue_timeout_s: float):
|
||||
"""Chave A ocupa a CPU com um trabalho no teto (a thread passa do teto um pouco) e tem mais um esperando;
|
||||
a chave B chega depois. Devolve o resultado (ou a exceção) de B."""
|
||||
import time
|
||||
|
||||
queue = WorkQueue(max_concurrency=1, max_queue_per_key=4, queue_timeout_s=queue_timeout_s, run_timeout_s=0.3)
|
||||
hog = asyncio.create_task(queue.run(lambda: time.sleep(0.35), owner="homolog"))
|
||||
await _wait_until(lambda: queue.running == 1)
|
||||
second = asyncio.create_task(queue.run(lambda: time.sleep(0.35), owner="homolog"))
|
||||
await _wait_until(lambda: queue.waiting_for("homolog") == 1)
|
||||
other = asyncio.create_task(queue.run(lambda: "prod", owner="prod"))
|
||||
results = await asyncio.gather(hog, second, other, return_exceptions=True)
|
||||
queue.shutdown()
|
||||
return results
|
||||
|
||||
|
||||
async def test_other_key_is_served_when_a_job_hits_the_ceiling():
|
||||
# Espera = 2x execução (a relação dos padrões): B passa na frente do 2º trabalho de A e é atendido,
|
||||
# mesmo com a thread de A passando um pouco do teto.
|
||||
hog, second, other = await _hog_then_other(queue_timeout_s=0.6)
|
||||
assert isinstance(hog, SynthesisTimeout)
|
||||
assert other == "prod"
|
||||
|
||||
|
||||
async def test_short_queue_timeout_lets_long_job_starve_the_other_key():
|
||||
# Contraprova: espera menor que a execução, B leva queue_timeout atrás de um trabalho da outra chave.
|
||||
_, _, other = await _hog_then_other(queue_timeout_s=0.2)
|
||||
assert isinstance(other, QueueTimeout)
|
||||
|
||||
|
||||
def test_default_timeouts_hold_the_relations():
|
||||
from app.config import BACKEND_CLIENT_TIMEOUT_S, Settings
|
||||
|
||||
defaults = Settings(service_key="k" * 40, models_dir=Path("."), data_dir=Path("."), catalog_path=Path("."),
|
||||
lock_path=Path("."))
|
||||
assert defaults.timeout_warnings() == []
|
||||
assert defaults.queue_timeout_s >= 2 * defaults.synth_timeout_s
|
||||
assert defaults.stt_queue_timeout_s >= 2 * defaults.stt_timeout_s
|
||||
assert defaults.queue_timeout_s + defaults.synth_timeout_s < BACKEND_CLIENT_TIMEOUT_S
|
||||
bad = replace(defaults, queue_timeout_s=20.0, synth_timeout_s=30.0, stt_queue_timeout_s=90.0, stt_timeout_s=90.0)
|
||||
warnings = bad.timeout_warnings()
|
||||
assert any(w.startswith("VOICE_QUEUE_TIMEOUT_S=20 abaixo de 2x") for w in warnings)
|
||||
assert any(w.startswith("VOICE_STT_QUEUE_TIMEOUT_S=90 abaixo de 2x") for w in warnings)
|
||||
|
||||
|
||||
async def test_boot_logs_timeout_warning(make_client, caplog):
|
||||
caplog.set_level(logging.WARNING)
|
||||
async with make_client(queue_timeout_s=5, synth_timeout_s=5):
|
||||
pass
|
||||
assert "config: VOICE_QUEUE_TIMEOUT_S=5 abaixo de 2x VOICE_SYNTH_TIMEOUT_S=5" in caplog.text
|
||||
|
||||
|
||||
def test_retired_queue_variable_is_reported(monkeypatch):
|
||||
from app.config import Settings
|
||||
|
||||
monkeypatch.setenv("VOICE_MAX_QUEUE", "8")
|
||||
monkeypatch.delenv("VOICE_STT_MAX_QUEUE", raising=False)
|
||||
assert Settings.retired_vars_in_env() == [("VOICE_MAX_QUEUE", "VOICE_MAX_QUEUE_PER_KEY")]
|
||||
@@ -0,0 +1,216 @@
|
||||
"""Chaves nomeadas, escopos e /v1/status (ADR 0027 §9 e §10)."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import sys
|
||||
from dataclasses import replace
|
||||
|
||||
import pytest
|
||||
|
||||
from app import keys
|
||||
from app.keys import KEY_RE, issue_key, keys_path, revoke_key
|
||||
from tests.conftest import HEADERS, KEY, body
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
|
||||
def _issue(settings, scopes, name="backend-test"):
|
||||
record, plaintext = issue_key(keys_path(settings.data_dir), name, tuple(scopes))
|
||||
return record, {"X-Service-Key": plaintext}
|
||||
|
||||
|
||||
def test_issued_key_has_the_contract_format_and_only_its_hash_is_stored(settings):
|
||||
record, plaintext = issue_key(keys_path(settings.data_dir), "backend-test", ("tts", "status"))
|
||||
match = KEY_RE.fullmatch(plaintext)
|
||||
assert match and match.group(1) == record.key_id
|
||||
stored = keys_path(settings.data_dir).read_text(encoding="utf-8")
|
||||
secret = match.group(2)
|
||||
assert secret not in stored and plaintext not in stored
|
||||
entry = json.loads(stored)["keys"][0]
|
||||
assert set(entry) == {"keyId", "name", "scopes", "secretSha256", "createdAt", "revokedAt"}
|
||||
assert entry["scopes"] == ["status", "tts"] and entry["revokedAt"] is None
|
||||
if sys.platform != "win32": # permissão POSIX; no Windows o arquivo herda a ACL da pasta do usuário
|
||||
assert (keys_path(settings.data_dir).stat().st_mode & 0o077) == 0 # 0600
|
||||
|
||||
|
||||
async def test_env_key_keeps_working_with_every_scope(make_client):
|
||||
async with make_client() as client:
|
||||
status = await client.get("/v1/status", headers=HEADERS)
|
||||
synth = await client.post("/v1/synthesize", headers=HEADERS, json=body())
|
||||
health = await client.get("/health", headers=HEADERS)
|
||||
assert status.status_code == 200 and synth.status_code == 200 and health.status_code == 200
|
||||
assert status.json()["key"] == {"keyId": "env", "name": "env", "scopes": ["admin", "convert", "status", "transcribe", "tts"]}
|
||||
|
||||
|
||||
async def test_key_issued_while_running_works_without_restart(make_client, settings):
|
||||
async with make_client() as client:
|
||||
_, headers = _issue(settings, ["tts"])
|
||||
response = await client.post("/v1/synthesize", headers=headers, json=body())
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
async def test_revoked_key_stops_working_without_restart(make_client, settings):
|
||||
async with make_client() as client:
|
||||
record, headers = _issue(settings, ["tts", "status"])
|
||||
assert (await client.get("/v1/voices", headers=headers)).status_code == 200
|
||||
revoke_key(keys_path(settings.data_dir), record.key_id)
|
||||
response = await client.get("/v1/voices", headers=headers)
|
||||
env = await client.get("/v1/voices", headers=HEADERS)
|
||||
assert response.status_code == 401 and response.json()["error"]["code"] == "unauthorized"
|
||||
assert env.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("method", "path", "scope"),
|
||||
[
|
||||
("GET", "/v1/status", "status"),
|
||||
("POST", "/v1/transcribe", "transcribe"),
|
||||
("GET", "/v1/transcribe/jobs/abcdefgh12", "transcribe"),
|
||||
],
|
||||
)
|
||||
async def test_key_without_route_scope_is_403(make_client, settings, method, path, scope):
|
||||
async with make_client() as client:
|
||||
_, headers = _issue(settings, ["tts"])
|
||||
response = await client.request(method, path, headers=headers)
|
||||
assert response.status_code == 403
|
||||
assert response.json() == {
|
||||
"error": {"code": "insufficient_scope", "message": "A chave não tem o escopo desta operação.", "requiredScope": scope}
|
||||
}
|
||||
|
||||
|
||||
async def test_transcribe_only_key_cannot_synthesize_nor_list_voices(make_client, settings):
|
||||
async with make_client() as client:
|
||||
_, headers = _issue(settings, ["transcribe"])
|
||||
synth = await client.post("/v1/synthesize", headers=headers, json=body())
|
||||
voices = await client.get("/v1/voices/pt-br-test", headers=headers)
|
||||
assert synth.status_code == 403 and voices.status_code == 403
|
||||
|
||||
|
||||
async def test_unknown_route_is_404_with_valid_key_and_401_without(make_client, settings):
|
||||
async with make_client() as client:
|
||||
_, headers = _issue(settings, ["tts"])
|
||||
known_key = await client.get("/rota-que-nao-existe", headers=headers)
|
||||
no_key = await client.get("/rota-que-nao-existe")
|
||||
assert known_key.status_code == 404 and no_key.status_code == 401
|
||||
|
||||
|
||||
async def test_wrong_secret_for_existing_key_id_is_401(make_client, settings):
|
||||
async with make_client() as client:
|
||||
record, _ = _issue(settings, ["tts"])
|
||||
forged = {"X-Service-Key": f"dvk_{record.key_id}_{'A' * 43}"}
|
||||
response = await client.get("/v1/voices", headers=forged)
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
async def test_corrupted_key_file_fails_closed_but_env_key_recovers(make_client, settings):
|
||||
async with make_client() as client:
|
||||
_, headers = _issue(settings, ["tts"])
|
||||
assert (await client.get("/v1/voices", headers=headers)).status_code == 200
|
||||
keys_path(settings.data_dir).write_text("{nao-e-json", encoding="utf-8")
|
||||
named = await client.get("/v1/voices", headers=headers)
|
||||
env = await client.get("/v1/voices", headers=HEADERS)
|
||||
assert named.status_code == 401 and env.status_code == 200
|
||||
|
||||
|
||||
async def test_status_exposes_operations_queues_voices_and_key(make_client, settings):
|
||||
async with make_client() as client:
|
||||
record, headers = _issue(settings, ["status", "tts"], name="srv-1")
|
||||
response = await client.get("/v1/status", headers=headers)
|
||||
assert response.status_code == 200 and response.headers["cache-control"] == "no-store"
|
||||
data = response.json()
|
||||
assert set(data) == {"status", "version", "device", "rvc", "azure", "engines", "operations", "queues", "voices", "key"}
|
||||
assert data["operations"] == ["tts", "transcribe"]
|
||||
assert data["engines"] == {"tts": "fake", "transcribe": "fake-stt"}
|
||||
assert data["queues"]["synthesis"] == {"running": 0, "waiting": 0, "maxConcurrency": 1, "maxQueue": 4}
|
||||
assert data["queues"]["transcription"]["maxQueue"] == 4 # padrão do CenaForge (episódios longos)
|
||||
assert data["voices"]["count"] == 1 and len(data["voices"]["catalogHash"]) == 64
|
||||
assert data["voices"]["items"] == [{"id": "pt-br-test", "revision": data["voices"]["items"][0]["revision"], "operations": ["tts"]}]
|
||||
assert data["key"] == {"keyId": record.key_id, "name": "srv-1", "scopes": ["status", "tts"]}
|
||||
text = response.text
|
||||
for leak in (str(settings.data_dir), str(settings.models_dir), KEY, headers["X-Service-Key"], record.secret_sha256):
|
||||
assert leak not in text
|
||||
|
||||
|
||||
async def test_status_catalog_hash_is_stable_and_follows_revision(make_client, settings):
|
||||
async with make_client() as client:
|
||||
first = (await client.get("/v1/status", headers=HEADERS)).json()["voices"]["catalogHash"]
|
||||
again = (await client.get("/v1/status", headers=HEADERS)).json()["voices"]["catalogHash"]
|
||||
assert first == again
|
||||
lock = json.loads(settings.lock_path.read_text())
|
||||
(settings.models_dir / "piper" / "fake.onnx").write_bytes(b"outro-modelo")
|
||||
lock["files"][0]["sha256"] = hashlib.sha256(b"outro-modelo").hexdigest()
|
||||
settings.lock_path.write_text(json.dumps(lock))
|
||||
async with make_client() as client:
|
||||
changed = (await client.get("/v1/status", headers=HEADERS)).json()["voices"]["catalogHash"]
|
||||
assert changed != first
|
||||
|
||||
|
||||
async def test_status_without_stt_omits_transcribe(make_client):
|
||||
async with make_client(stt=None) as client:
|
||||
data = (await client.get("/v1/status", headers=HEADERS)).json()
|
||||
assert data["operations"] == ["tts"]
|
||||
assert data["engines"]["transcribe"] is None and data["queues"]["transcription"] is None
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def rvc_settings(settings, tmp_path):
|
||||
# No CenaForge o RVC vive nas vozes de personagem do usuário (voz base + .pth), não no catálogo base.
|
||||
(settings.models_dir / "rvc").mkdir(parents=True, exist_ok=True)
|
||||
(settings.models_dir / "rvc" / "fake.pth").write_bytes(b"pth")
|
||||
path = tmp_path / "user-voices.json"
|
||||
path.write_text(json.dumps({"voices": [{
|
||||
"id": "heroi", "name": "Herói", "baseVoice": "pt-br-test",
|
||||
"rvc": {"model": "rvc/fake.pth", "modelSha256": hashlib.sha256(b"pth").hexdigest()},
|
||||
}]}))
|
||||
return replace(settings, user_catalog_path=path, enable_rvc=True)
|
||||
|
||||
|
||||
async def test_rvc_voice_requires_convert_scope(make_client, rvc_settings):
|
||||
async with make_client(user_catalog_path=rvc_settings.user_catalog_path, enable_rvc=True) as client:
|
||||
_, headers = _issue(rvc_settings, ["tts", "status"])
|
||||
synth = await client.post("/v1/synthesize", headers=headers, json=body(voiceId="heroi"))
|
||||
status = (await client.get("/v1/status", headers=headers)).json()
|
||||
assert synth.status_code == 403
|
||||
assert synth.json()["error"] == {
|
||||
"code": "insufficient_scope", "message": "A chave não tem o escopo desta operação.", "requiredScope": "convert"
|
||||
}
|
||||
assert status["operations"] == ["tts", "transcribe", "convert"]
|
||||
assert status["voices"]["items"][0]["operations"] == ["tts", "convert"]
|
||||
|
||||
|
||||
# ---------- CLI ----------
|
||||
def test_cli_create_prints_key_once_and_list_never_shows_secret(settings, monkeypatch, capsys):
|
||||
monkeypatch.setenv("VOICE_DATA_DIR", str(settings.data_dir))
|
||||
assert keys.main(["create", "--name", "backend-prod", "--scopes", "tts,transcribe,status"]) == 0
|
||||
created = capsys.readouterr().out
|
||||
plaintext = next(line for line in created.splitlines() if line.startswith("dvk_"))
|
||||
assert KEY_RE.fullmatch(plaintext)
|
||||
assert keys.main(["list"]) == 0
|
||||
listed = json.loads(capsys.readouterr().out)
|
||||
assert listed[0]["name"] == "backend-prod" and listed[0]["scopes"] == ["status", "transcribe", "tts"]
|
||||
assert "secretSha256" not in listed[0] and plaintext not in json.dumps(listed)
|
||||
key_id = listed[0]["keyId"]
|
||||
assert keys.main(["revoke", key_id]) == 0
|
||||
assert key_id in capsys.readouterr().out
|
||||
assert keys.main(["list"]) == 0
|
||||
assert json.loads(capsys.readouterr().out)[0]["revokedAt"] is not None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"argv",
|
||||
[
|
||||
["create", "--name", "x", "--scopes", "root"],
|
||||
["create", "--name", "x", "--scopes", ""],
|
||||
["create", "--name", "nome com espaço", "--scopes", "tts"],
|
||||
["revoke", "nao-e-um-id"],
|
||||
],
|
||||
)
|
||||
def test_cli_rejects_bad_input(settings, monkeypatch, argv):
|
||||
monkeypatch.setenv("VOICE_DATA_DIR", str(settings.data_dir))
|
||||
assert keys.main(argv) == 2
|
||||
assert not keys_path(settings.data_dir).exists()
|
||||
|
||||
|
||||
def test_cli_revoke_unknown_key_id_is_1(settings, monkeypatch):
|
||||
monkeypatch.setenv("VOICE_DATA_DIR", str(settings.data_dir))
|
||||
assert keys.main(["revoke", "abcdefghijkl"]) == 1
|
||||
@@ -0,0 +1,98 @@
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
from app.errors import QueueFull, QueueTimeout, SynthesisTimeout
|
||||
from app.work_queue import WorkQueue
|
||||
from tests.conftest import HEADERS, body
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
|
||||
async def _wait_until(predicate, timeout=3.0):
|
||||
loop = asyncio.get_running_loop()
|
||||
end = loop.time() + timeout
|
||||
while not predicate():
|
||||
if loop.time() > end:
|
||||
raise AssertionError("condição não atingida")
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
|
||||
async def test_concurrency_limit_and_queue_full():
|
||||
gate = threading.Event()
|
||||
queue = WorkQueue(max_concurrency=1, max_queue_per_key=1, queue_timeout_s=5, run_timeout_s=5)
|
||||
running = asyncio.create_task(queue.run(lambda: gate.wait(5), owner="a"))
|
||||
await _wait_until(lambda: queue.running == 1)
|
||||
waiting = asyncio.create_task(queue.run(lambda: "segundo", owner="a"))
|
||||
await _wait_until(lambda: queue.waiting == 1)
|
||||
with pytest.raises(QueueFull):
|
||||
await queue.run(lambda: "terceiro", owner="a")
|
||||
gate.set()
|
||||
assert await running is True
|
||||
assert await waiting == "segundo"
|
||||
queue.shutdown()
|
||||
|
||||
|
||||
async def test_queue_timeout():
|
||||
gate = threading.Event()
|
||||
queue = WorkQueue(max_concurrency=1, max_queue_per_key=5, queue_timeout_s=0.2, run_timeout_s=5)
|
||||
running = asyncio.create_task(queue.run(lambda: gate.wait(5), owner="a"))
|
||||
await _wait_until(lambda: queue.running == 1)
|
||||
with pytest.raises(QueueTimeout):
|
||||
await queue.run(lambda: "nunca", owner="a")
|
||||
gate.set()
|
||||
await running
|
||||
queue.shutdown()
|
||||
|
||||
|
||||
async def test_synthesis_timeout_keeps_slot_until_thread_ends():
|
||||
gate = threading.Event()
|
||||
queue = WorkQueue(max_concurrency=1, max_queue_per_key=5, queue_timeout_s=5, run_timeout_s=0.2)
|
||||
with pytest.raises(SynthesisTimeout):
|
||||
await queue.run(lambda: gate.wait(5), owner="a")
|
||||
# O cliente já recebeu timeout, mas a thread segue rodando: a vaga continua ocupada.
|
||||
assert queue.running == 1
|
||||
gate.set()
|
||||
await _wait_until(lambda: queue.running == 0)
|
||||
assert await queue.run(lambda: "livre", owner="a") == "livre"
|
||||
queue.shutdown()
|
||||
|
||||
|
||||
async def test_http_queue_full_returns_503_with_retry_after(make_client, engine):
|
||||
engine.gate = threading.Event()
|
||||
async with make_client(max_concurrency=1, max_queue_per_key=1) as client:
|
||||
first = asyncio.create_task(client.post("/v1/synthesize", json=body("req-aaaaaaaa", text="um"), headers=HEADERS))
|
||||
await _wait_until(lambda: engine.calls == 1)
|
||||
second = asyncio.create_task(client.post("/v1/synthesize", json=body("req-bbbbbbbb", text="dois"), headers=HEADERS))
|
||||
await asyncio.sleep(0.1)
|
||||
third = await client.post("/v1/synthesize", json=body("req-cccccccc", text="três"), headers=HEADERS)
|
||||
engine.gate.set()
|
||||
assert (await first).status_code == 200
|
||||
assert (await second).status_code == 200
|
||||
assert third.status_code == 503
|
||||
assert third.json()["error"]["code"] == "queue_full"
|
||||
assert third.headers["retry-after"] == "5"
|
||||
|
||||
|
||||
async def test_http_synthesis_timeout_is_504(make_client, engine):
|
||||
engine.gate = threading.Event()
|
||||
async with make_client(synth_timeout_s=0.2) as client:
|
||||
response = await client.post("/v1/synthesize", json=body(), headers=HEADERS)
|
||||
engine.gate.set()
|
||||
assert response.status_code == 504
|
||||
assert response.json()["error"]["code"] == "synthesis_timeout"
|
||||
|
||||
|
||||
async def test_inflight_same_content_is_rendered_once(make_client, engine):
|
||||
engine.gate = threading.Event()
|
||||
async with make_client(max_concurrency=2) as client:
|
||||
a = asyncio.create_task(client.post("/v1/synthesize", json=body("req-aaaaaaaa"), headers=HEADERS))
|
||||
await _wait_until(lambda: engine.calls == 1)
|
||||
b = asyncio.create_task(client.post("/v1/synthesize", json=body("req-bbbbbbbb"), headers=HEADERS))
|
||||
await asyncio.sleep(0.1)
|
||||
engine.gate.set()
|
||||
ra, rb = await a, await b
|
||||
assert ra.status_code == rb.status_code == 200
|
||||
assert ra.content == rb.content
|
||||
assert engine.calls == 1
|
||||
@@ -0,0 +1,92 @@
|
||||
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"
|
||||
@@ -0,0 +1,185 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from app.stt import FakeSttEngine, join_text, mean_no_speech
|
||||
from tests.conftest import HEADERS, KEY
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
# Convenção do FakeSttEngine: cada byte depois de "OggS" vale 10 ms de áudio.
|
||||
ONE_SECOND = b"OggS" + b"x" * 100
|
||||
|
||||
|
||||
def stt_headers(key: str = "order:123:transcribe", duration_ms: int = 1000, language: str = "pt-BR", **extra) -> dict:
|
||||
headers = {
|
||||
**HEADERS,
|
||||
"Content-Type": "audio/ogg",
|
||||
"X-Idempotency-Key": key,
|
||||
"X-Audio-Duration-Ms": str(duration_ms),
|
||||
"X-Language": language,
|
||||
}
|
||||
headers.update(extra)
|
||||
return headers
|
||||
|
||||
|
||||
async def test_requires_service_key(make_client):
|
||||
async with make_client() as client:
|
||||
no_key = {k: v for k, v in stt_headers().items() if k != "X-Service-Key"}
|
||||
assert (await client.post("/v1/transcribe", content=ONE_SECOND, headers=no_key)).status_code == 401
|
||||
wrong = stt_headers(**{"X-Service-Key": "x" * len(KEY)})
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=wrong)
|
||||
assert response.status_code == 401
|
||||
assert response.json()["error"]["code"] == "unauthorized"
|
||||
assert (await client.get("/v1/transcribe/jobs/abcdefghijkl")).status_code == 401
|
||||
|
||||
|
||||
async def test_ready_with_mean_no_speech_prob(make_client, stt_engine):
|
||||
async with make_client() as client:
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers())
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"status": "ready", "text": "valeu pela live", "noSpeechProb": 0.2}
|
||||
assert response.headers["x-idempotent-replay"] == "false"
|
||||
assert stt_engine.calls == 1
|
||||
|
||||
|
||||
async def test_no_segments_means_no_speech(make_client):
|
||||
async with make_client(stt=FakeSttEngine(text="", probs=())) as client:
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers())
|
||||
assert response.json() == {"status": "ready", "text": "", "noSpeechProb": 1.0}
|
||||
|
||||
|
||||
def test_no_speech_helpers():
|
||||
assert mean_no_speech([]) == 1.0
|
||||
assert mean_no_speech([0.2, 0.4]) == pytest.approx(0.3)
|
||||
assert mean_no_speech([1.5]) == 1.0 # preso em 0..1
|
||||
assert join_text([" oi ", "", " tudo bem"], 4000) == "oi tudo bem"
|
||||
assert len(join_text(["a" * 5000], 4000)) == 4000
|
||||
|
||||
|
||||
async def test_idempotency_replay_and_conflict(make_client, stt_engine):
|
||||
async with make_client() as client:
|
||||
first = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers())
|
||||
again = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers())
|
||||
other_audio = await client.post("/v1/transcribe", content=ONE_SECOND + b"y", headers=stt_headers())
|
||||
assert again.status_code == 200 and again.json() == first.json()
|
||||
assert again.headers["x-idempotent-replay"] == "true"
|
||||
assert stt_engine.calls == 1 # o retry não transcreve de novo
|
||||
assert other_audio.status_code == 409
|
||||
assert other_audio.json()["error"]["code"] == "idempotency_conflict"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("overrides", "code", "status"),
|
||||
[
|
||||
({"X-Audio-Duration-Ms": "60001"}, "audio_too_long", 422),
|
||||
({"X-Audio-Duration-Ms": "0"}, "invalid_request", 422),
|
||||
({"X-Language": "fr"}, "language_not_supported", 422),
|
||||
({"X-Idempotency-Key": "com espaco"}, "invalid_request", 422),
|
||||
({"Content-Type": "text/plain"}, "invalid_request", 422),
|
||||
({"X-Detail": "frases"}, "invalid_request", 422),
|
||||
],
|
||||
)
|
||||
async def test_header_validation(make_client, stt_engine, overrides, code, status):
|
||||
async with make_client() as client:
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers(**overrides))
|
||||
assert response.status_code == status
|
||||
assert response.json()["error"]["code"] == code
|
||||
assert stt_engine.calls == 0
|
||||
|
||||
|
||||
async def test_byte_limit(make_client, stt_engine):
|
||||
async with make_client(stt_max_bytes=1024) as client:
|
||||
response = await client.post("/v1/transcribe", content=b"OggS" + b"x" * 2000, headers=stt_headers())
|
||||
assert response.status_code == 413
|
||||
assert response.json()["error"]["code"] == "audio_too_large"
|
||||
assert stt_engine.calls == 0
|
||||
|
||||
|
||||
async def test_decoded_duration_limit(make_client, stt_engine):
|
||||
# Declara 60 s, mas o áudio decodificado tem 61 s: vale o decodificado.
|
||||
async with make_client() as client:
|
||||
response = await client.post(
|
||||
"/v1/transcribe", content=b"OggS" + b"x" * 6100, headers=stt_headers(duration_ms=60000)
|
||||
)
|
||||
assert response.status_code == 422
|
||||
assert response.json()["error"]["code"] == "audio_too_long"
|
||||
assert stt_engine.calls == 0
|
||||
|
||||
|
||||
async def test_invalid_audio(make_client):
|
||||
async with make_client() as client:
|
||||
response = await client.post("/v1/transcribe", content=b"RIFFxxxx", headers=stt_headers())
|
||||
assert response.status_code == 422
|
||||
assert response.json()["error"]["code"] == "invalid_audio"
|
||||
|
||||
|
||||
async def test_pending_then_job_ready(make_client):
|
||||
async with make_client(stt=FakeSttEngine(delay_s=0.3), stt_sync_wait_s=0.0) as client:
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers())
|
||||
assert response.status_code == 202
|
||||
pending = response.json()
|
||||
assert pending["status"] == "pending" and pending["retryAfterMs"] >= 500
|
||||
for _ in range(100):
|
||||
job = await client.get(f"/v1/transcribe/jobs/{pending['jobRef']}", headers=HEADERS)
|
||||
if job.json()["status"] == "ready":
|
||||
break
|
||||
await asyncio.sleep(0.05)
|
||||
assert job.status_code == 200
|
||||
assert job.json() == {"status": "ready", "text": "valeu pela live", "noSpeechProb": 0.2}
|
||||
|
||||
|
||||
async def test_unknown_job_and_disabled(make_client):
|
||||
async with make_client() as client:
|
||||
response = await client.get("/v1/transcribe/jobs/abcdefghijkl", headers=HEADERS)
|
||||
assert response.status_code == 410
|
||||
assert response.json()["error"]["code"] == "job_not_found"
|
||||
async with make_client(stt=None) as client:
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers())
|
||||
assert response.status_code == 501
|
||||
assert response.json()["error"]["code"] == "stt_disabled"
|
||||
|
||||
|
||||
async def test_queue_full(make_client):
|
||||
async with make_client(stt=FakeSttEngine(delay_s=0.5), stt_sync_wait_s=0.0, stt_max_queue_per_key=0) as client:
|
||||
first = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers(key="a-1"))
|
||||
assert first.status_code == 202
|
||||
for _ in range(100):
|
||||
if (await client.get("/v1/status", headers=HEADERS)).json()["queues"]["transcription"]["running"] == 1:
|
||||
break
|
||||
await asyncio.sleep(0.01)
|
||||
second = await client.post("/v1/transcribe", content=ONE_SECOND + b"z", headers=stt_headers(key="b-2"))
|
||||
assert second.status_code == 503
|
||||
assert second.json()["error"]["code"] == "queue_full"
|
||||
assert second.headers["retry-after"] == "5"
|
||||
|
||||
|
||||
async def test_no_text_in_logs(make_client, caplog):
|
||||
secret = "segredo do espectador"
|
||||
caplog.set_level(logging.DEBUG)
|
||||
async with make_client(stt=FakeSttEngine(text=secret)) as client:
|
||||
response = await client.post("/v1/transcribe", content=ONE_SECOND, headers=stt_headers())
|
||||
assert response.json()["text"] == secret
|
||||
assert secret not in caplog.text
|
||||
assert "transcribe job=" in caplog.text
|
||||
|
||||
|
||||
def test_real_ogg_opus_decoder():
|
||||
# Decodificador de verdade (PyAV), com OGG/Opus gerado pelo mesmo codificador da síntese.
|
||||
import numpy as np
|
||||
|
||||
from app.audio import encode_ogg_opus
|
||||
from app.engine import Pcm
|
||||
from app.errors import AudioTooLong, InvalidAudio
|
||||
from app.stt import SAMPLE_RATE, decode_ogg_opus
|
||||
|
||||
tone = (np.sin(np.arange(22050 * 2) * 2 * np.pi * 220 / 22050) * 8000).astype(np.int16)
|
||||
data = encode_ogg_opus(Pcm(tone, 22050))
|
||||
samples = decode_ogg_opus(data, max_seconds=60)
|
||||
assert samples.dtype == np.float32
|
||||
assert abs(samples.size / SAMPLE_RATE - 2.0) < 0.1
|
||||
with pytest.raises(AudioTooLong):
|
||||
decode_ogg_opus(data, max_seconds=1.0)
|
||||
with pytest.raises(InvalidAudio):
|
||||
decode_ogg_opus(b"OggS" + b"\x00" * 64, max_seconds=60)
|
||||
@@ -0,0 +1,66 @@
|
||||
"""GET /v1/voices (paginado, com busca) e GET /v1/voices/{voiceId}."""
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
HEADERS = {"X-Service-Key": "k" * 40}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def many_voices(settings):
|
||||
base = json.loads(settings.catalog_path.read_text())["voices"][0]
|
||||
base.update(description="Voz de teste.", gender="female", ageRange="young", accent="pt-BR")
|
||||
voices = [base]
|
||||
for vid, name in (("pt-br-ana", "Ana"), ("pt-br-jose", "José"), ("pt-br-zeca", "Zeca")):
|
||||
voices.append({**base, "id": vid, "name": name, "default": False})
|
||||
settings.catalog_path.write_text(json.dumps({"voices": voices}))
|
||||
return settings
|
||||
|
||||
|
||||
async def test_list_shape_and_no_internal_fields(make_client):
|
||||
async with make_client() as client:
|
||||
response = await client.get("/v1/voices", headers=HEADERS)
|
||||
body = response.json()
|
||||
assert response.status_code == 200 and body["nextCursor"] is None
|
||||
item = body["items"][0]
|
||||
assert item["id"] == "pt-br-test" and item["languages"] == ["pt-BR"] and item["engine"] == "tts"
|
||||
|
||||
assert not {"model", "config", "source", "license", "path"} & item.keys()
|
||||
|
||||
|
||||
async def test_pagination_walks_all_pages(make_client, many_voices):
|
||||
async with make_client() as client:
|
||||
first = (await client.get("/v1/voices?limit=3", headers=HEADERS)).json()
|
||||
assert len(first["items"]) == 3 and first["nextCursor"]
|
||||
second = (await client.get("/v1/voices", params={"limit": 3, "cursor": first["nextCursor"]}, headers=HEADERS)).json()
|
||||
ids = [v["id"] for v in first["items"] + second["items"]]
|
||||
assert ids == sorted(ids) and len(set(ids)) == 4 and second["nextCursor"] is None
|
||||
|
||||
|
||||
async def test_query_ignores_case_and_accents(make_client, many_voices):
|
||||
async with make_client() as client:
|
||||
by_name = (await client.get("/v1/voices?query=JOSE", headers=HEADERS)).json()
|
||||
by_id = (await client.get("/v1/voices?query=ZECA", headers=HEADERS)).json()
|
||||
none = (await client.get("/v1/voices?query=nada", headers=HEADERS)).json()
|
||||
assert [v["id"] for v in by_name["items"]] == ["pt-br-jose"]
|
||||
assert [v["id"] for v in by_id["items"]] == ["pt-br-zeca"]
|
||||
assert none == {"items": [], "nextCursor": None}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("qs", ["limit=0", "limit=101", "limit=abc", "cursor=%25%25", "cursor=" + "a" * 513, "query=" + "a" * 65])
|
||||
async def test_invalid_params_are_422(make_client, qs):
|
||||
async with make_client() as client:
|
||||
response = await client.get("/v1/voices?" + qs, headers=HEADERS)
|
||||
assert response.status_code == 422 and response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
async def test_get_voice_and_404(make_client):
|
||||
async with make_client() as client:
|
||||
ok = await client.get("/v1/voices/pt-br-test", headers=HEADERS)
|
||||
missing = await client.get("/v1/voices/nao-existe", headers=HEADERS)
|
||||
weird = await client.get("/v1/voices/..%2Fx", headers=HEADERS)
|
||||
assert ok.status_code == 200 and ok.json()["id"] == "pt-br-test" and ok.json()["revision"]
|
||||
assert missing.status_code == 404 and missing.json()["error"]["code"] == "voice_not_found"
|
||||
assert weird.status_code == 404
|
||||
Reference in New Issue
Block a user