Files
CenaForge/voice/tests/test_idempotency_cache.py
MateuusandClaude Opus 5.5 84f30bbc2d 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>
2026-11-17 16:35:09 -03:00

83 lines
3.8 KiB
Python

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