03-oct: superguardado automático

This commit is contained in:
minguezsanzjuanjose
2026-10-03 17:13:39 +02:00
parent f10ce5bef4
commit fdd4ea877a
31 changed files with 2229 additions and 4 deletions

48
tests/test_vistos.py Normal file
View File

@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
"""Tests de `monitor_estado.vistos` — dedup persistente (RF-8, RF-23)."""
import monitor_estado.vistos as vistos
def test_vistos_filtra_nuevos(tmp_path, monkeypatch):
"""Lo ya visto no se vuelve a contar; lo nuevo si (RF-8)."""
monkeypatch.setattr(vistos, "VISTO_DIR", tmp_path)
clave = "telegram_test"
msgs = [{"id": 1, "t": "a"}, {"id": 2, "t": "b"}]
nuevos = vistos.filtrar_nuevos(clave, msgs, id_fn=lambda m: m["id"])
assert [m["id"] for m in nuevos] == [1, 2]
# Segundo ciclo con los mismos mensajes -> nada nuevo.
assert vistos.filtrar_nuevos(clave, msgs, id_fn=lambda m: m["id"]) == []
# Llega uno nuevo -> solo ese.
mas = msgs + [{"id": 3, "t": "c"}]
assert [m["id"] for m in vistos.filtrar_nuevos(clave, mas, id_fn=lambda m: m["id"])] == [3]
def test_vistos_persiste_entre_llamadas(tmp_path, monkeypatch):
"""El estado se guarda en disco y sobrevive a una nueva lectura (RF-23)."""
monkeypatch.setattr(vistos, "VISTO_DIR", tmp_path)
clave = "correo"
vistos.filtrar_nuevos(clave, ["m1", "m2"], id_fn=lambda x: x)
assert (tmp_path / f"{clave}.json").exists()
# Nueva lectura del fichero: m1/m2 ya no son nuevos.
assert vistos.filtrar_nuevos(clave, ["m1", "m2"], id_fn=lambda x: x) == []
def test_vistos_marcar_explicito(tmp_path, monkeypatch):
"""`marcar` añade ids sin devolver nada."""
monkeypatch.setattr(vistos, "VISTO_DIR", tmp_path)
vistos.marcar("jira", ["DS-1", "DS-2"])
assert vistos.filtrar_nuevos("jira", ["DS-1"], id_fn=lambda x: x) == []
assert vistos.filtrar_nuevos("jira", ["DS-3"], id_fn=lambda x: x) == ["DS-3"]
def test_vistos_idempotente_sin_marcar(tmp_path, monkeypatch):
"""Con marcar_automatico=False, filtrar no altera el estado."""
monkeypatch.setattr(vistos, "VISTO_DIR", tmp_path)
a = vistos.filtrar_nuevos("k", [1, 2], id_fn=lambda x: x, marcar_automatico=False)
b = vistos.filtrar_nuevos("k", [1, 2], id_fn=lambda x: x, marcar_automatico=False)
assert a == [1, 2] and b == [1, 2]