feat(short): generación y render de Shorts vía shortsmith
Build & Deploy ResearchOwl / build-and-push (push) Successful in 9s
Build & Deploy ResearchOwl / build-and-push (push) Successful in 9s
Añade /generate short_en y /short_spec. El pipeline genera un shot spec con Haiku, verifica cada cifra, fecha y cita contra los chunks de la sesión, lo renderiza en shortsmith y entrega el MP4 por Telegram junto a un informe de claims. - ShortsmithClient con sondeo y fallback al spec JSON si el render falla - Contrato de plantillas obtenido de GET /templates, no codificado - Comprobación de fundamento determinista, sin LLM - outputs.published_url para enlazar el artículo de Ghost - Normalización de comillas rectas a tipográficas (ver KNOWN-ISSUES.md) Lo que no aparece en los chunks se contrasta contra el ejemplo del prompt: si casa ahí es fuga, no invención, y se informa como tal. El purgado de sesiones se lleva también su MP4. La subida a YouTube queda fuera a propósito: fase 3. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
"""ShortsmithClient — bucle de sondeo, errores y fallbacks.
|
||||
|
||||
Todo con un servidor falso; el test contra el servicio vivo es
|
||||
`test_shortsmith_live.py`, que se salta salvo que se le apunte a uno.
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from src.generator.shortsmith import (
|
||||
JobResult, ShortsmithClient, ShortsmithError, ShortsmithRejected,
|
||||
ShortsmithUnavailable, _templates_cache,
|
||||
)
|
||||
|
||||
EXAMPLE = Path(__file__).resolve().parents[1] / "src/generator/examples/jal1628.json"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def spec():
|
||||
return json.loads(EXAMPLE.read_text())
|
||||
|
||||
|
||||
class FakeResp:
|
||||
def __init__(self, status, payload=None, body="", raw=b""):
|
||||
self.status = status
|
||||
self._payload = payload
|
||||
self._body = body
|
||||
self._raw = raw
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *a):
|
||||
return False
|
||||
|
||||
async def json(self):
|
||||
if self._payload is None:
|
||||
raise ValueError("no json")
|
||||
return self._payload
|
||||
|
||||
async def text(self):
|
||||
return self._body
|
||||
|
||||
async def read(self):
|
||||
return self._raw
|
||||
|
||||
|
||||
class FakeSession:
|
||||
"""Sustituye a aiohttp.ClientSession: sirve respuestas de una cola por ruta."""
|
||||
|
||||
def __init__(self, routes):
|
||||
self.routes = routes
|
||||
self.calls = []
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *a):
|
||||
return False
|
||||
|
||||
def _next(self, method, url):
|
||||
self.calls.append((method, url))
|
||||
for pattern, responses in self.routes.items():
|
||||
if pattern in url:
|
||||
if isinstance(responses, list):
|
||||
return responses.pop(0) if len(responses) > 1 else responses[0]
|
||||
return responses
|
||||
raise AssertionError(f"ruta no simulada: {method} {url}")
|
||||
|
||||
def get(self, url, **kw):
|
||||
return self._next("GET", url)
|
||||
|
||||
def post(self, url, **kw):
|
||||
return self._next("POST", url)
|
||||
|
||||
|
||||
def patch_session(client, routes):
|
||||
session = FakeSession(routes)
|
||||
client._session = lambda total: session
|
||||
return session
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_templates_cached_per_process():
|
||||
_templates_cache.clear()
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
session = patch_session(client, {"/templates": FakeResp(200, {"radar_sweep": {}})})
|
||||
|
||||
first = await client.templates()
|
||||
second = await client.templates()
|
||||
|
||||
assert first == second == {"radar_sweep": {}}
|
||||
assert len(session.calls) == 1, "la segunda llamada debe salir de la caché"
|
||||
|
||||
# refresh=True vuelve a pedirlo: el renderizador puede haberse actualizado.
|
||||
patch_session(client, {"/templates": FakeResp(200, {"radar_sweep": {}, "nueva": {}})})
|
||||
assert "nueva" in await client.templates(refresh=True)
|
||||
_templates_cache.clear()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_render_returns_job_id(spec):
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
patch_session(client, {"/render": FakeResp(202, {"job_id": "abc123", "status": "queued"})})
|
||||
assert await client.render(spec) == "abc123"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_render_422_propagates_error_paths(spec):
|
||||
detail = [{
|
||||
"type": "extra_forbidden",
|
||||
"loc": ["shots", 0, "radar_sweep", "props", "sweeeps"],
|
||||
"msg": "Extra inputs are not permitted",
|
||||
}]
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
patch_session(client, {"/render": FakeResp(422, {"detail": detail})})
|
||||
|
||||
with pytest.raises(ShortsmithRejected) as exc:
|
||||
await client.render(spec)
|
||||
assert exc.value.errors[0]["loc"][-1] == "sweeeps"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_poll_queued_then_running_then_done():
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
patch_session(client, {"/jobs/": [
|
||||
FakeResp(200, {"job_id": "j", "status": "queued", "progress": 0.0}),
|
||||
FakeResp(200, {"job_id": "j", "status": "running", "progress": 0.4}),
|
||||
FakeResp(200, {"job_id": "j", "status": "done", "progress": 1.0,
|
||||
"warnings": [{"template": "data_card", "text": "x"}]}),
|
||||
]})
|
||||
|
||||
seen = []
|
||||
|
||||
async def on_progress(fraction, status):
|
||||
seen.append((fraction, status))
|
||||
|
||||
result = await client.poll("j", on_progress=on_progress, interval=0)
|
||||
|
||||
assert result.ok and result.status == "done"
|
||||
assert result.warnings and result.warnings[0]["template"] == "data_card"
|
||||
assert seen == [(0.0, "queued"), (0.4, "running"), (1.0, "done")]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_poll_returns_error_status_without_raising():
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
patch_session(client, {"/jobs/": FakeResp(200, {
|
||||
"job_id": "j", "status": "error", "progress": 0.3,
|
||||
"error": "interrupted by a restart: the process did not survive this render",
|
||||
})})
|
||||
|
||||
result = await client.poll("j", interval=0)
|
||||
assert not result.ok
|
||||
assert "interrupted" in result.error
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_poll_gives_up_on_a_stuck_job():
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
patch_session(client, {"/jobs/": FakeResp(200, {
|
||||
"job_id": "j", "status": "running", "progress": 0.1})})
|
||||
|
||||
with pytest.raises(ShortsmithError, match="atascado"):
|
||||
await client.poll("j", interval=0, ceiling=0)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connection_refused_is_unavailable(spec):
|
||||
import aiohttp
|
||||
|
||||
class Refusing(FakeSession):
|
||||
def post(self, url, **kw):
|
||||
raise aiohttp.ClientConnectionError(
|
||||
"Cannot connect to host shortsmith-svc:8080 [Connection refused]")
|
||||
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
client._session = lambda total: Refusing({})
|
||||
|
||||
with pytest.raises(ShortsmithUnavailable):
|
||||
await client.render(spec)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_video_returns_bytes():
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
patch_session(client, {"/video": FakeResp(200, raw=b"\x00\x00\x00 ftypisom")})
|
||||
assert (await client.fetch_video("j")).startswith(b"\x00\x00\x00 ftyp")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_progress_callback_failure_never_kills_the_render():
|
||||
client = ShortsmithClient("http://fake:8080")
|
||||
patch_session(client, {"/jobs/": FakeResp(200, {
|
||||
"job_id": "j", "status": "done", "progress": 1.0})})
|
||||
|
||||
async def boom(fraction, status):
|
||||
raise RuntimeError("Telegram dijo que no")
|
||||
|
||||
assert (await client.poll("j", on_progress=boom, interval=0)).ok
|
||||
|
||||
|
||||
def test_jobresult_ok_only_when_done():
|
||||
assert JobResult("j", "done").ok
|
||||
assert not JobResult("j", "running").ok
|
||||
assert not JobResult("j", "error", error="boom").ok
|
||||
Reference in New Issue
Block a user