"""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