clipwright/tests/test_gemini_retry.py
juancarlos.cavero 1c6431e8a0 test(ci): saltar los tests que importan main/app en el CI mínimo
El CI instala solo pytest+pillow+httpx+pydantic+sqlalchemy. test_gemini_retry
importa main (cv2/torch) y test_job_error_text importa app (dotenv/fastapi),
que faltan → ModuleNotFoundError en la colección. importorskip los salta en
CI y los corre entero en el contenedor/local donde sí están las deps.
Verificado en un venv réplica del CI: 125 passed, 2 skipped.
2026-07-23 11:40:05 +02:00

88 lines
2.6 KiB
Python

"""Retry behaviour of the Gemini scoring/detail stage.
Prod (22-jul-2026) lost 3 jobs to Gemini answering 200 with an empty body.
That raises while parsing, not while calling, so it used to escape the retry
loop and kill the job on the first blip.
"""
import types
import pytest
# main pulls in cv2/torch/mediapipe at import time; the minimal CI env lacks
# them, so skip there. Runs fully in the container/local where deps exist.
main = pytest.importorskip("main")
class _FakeResponse:
def __init__(self, parsed=None):
self.parsed = parsed
self.candidates = []
self.usage_metadata = None
@property
def text(self):
return "" if self.parsed is None else "{}"
class _FakeModels:
"""Returns an empty body for the first `blips` calls, then a good one."""
def __init__(self, blips, payload=None):
self.blips = blips
self.calls = 0
self.payload = payload if payload is not None else {"windows": [{"id": "w0", "score": 90}]}
def generate_content(self, **kwargs):
self.calls += 1
if self.calls <= self.blips:
return _FakeResponse(parsed=None)
return _FakeResponse(parsed=self.payload)
def _client(models):
return types.SimpleNamespace(models=models)
@pytest.fixture(autouse=True)
def _no_sleep(monkeypatch):
monkeypatch.setattr(main.time, "sleep", lambda *_: None)
def test_recovers_from_a_single_empty_body(monkeypatch):
models = _FakeModels(blips=1)
parsed, _cost = main._run_gemini_stage(_client(models), "m", "prompt", object)
assert models.calls == 2
assert parsed["windows"][0]["score"] == 90
def test_recovers_from_two_consecutive_blips():
models = _FakeModels(blips=2)
parsed, _cost = main._run_gemini_stage(_client(models), "m", "prompt", object)
assert models.calls == 3
assert parsed["windows"]
def test_gives_up_after_three_attempts():
models = _FakeModels(blips=99)
with pytest.raises(Exception) as exc:
main._run_gemini_stage(_client(models), "m", "prompt", object)
assert models.calls == 3
assert "empty response body" in str(exc.value)
def test_non_transient_errors_are_not_retried():
class _Boom:
calls = 0
def generate_content(self, **kwargs):
_Boom.calls += 1
raise ValueError("400 INVALID_ARGUMENT: bad request")
with pytest.raises(ValueError):
main._run_gemini_stage(_client(_Boom()), "m", "prompt", object)
assert _Boom.calls == 1
def test_succeeds_without_retrying_when_the_first_call_is_fine():
models = _FakeModels(blips=0)
main._run_gemini_stage(_client(models), "m", "prompt", object)
assert models.calls == 1