Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XGaEeWkqZYNP6Wn3mGQsrL
84 lines
3.1 KiB
Python
84 lines
3.1 KiB
Python
import json
|
|
from app.mirror_client import MirrorClient
|
|
|
|
|
|
def mail(i, jour, hh="10:00:00", tags=None):
|
|
return {"message_id": f"m{i}", "dossier": "Boîte de réception", "date_reception": f"{jour}T{hh}+01:00",
|
|
"expediteur": "a@b.c", "sujet": f"s{i}", "etiquettes": tags or []}
|
|
|
|
|
|
class Faux(MirrorClient):
|
|
"""Miroir simulé : 300 mails, 10 par jour du 01/11 au 30/11 ; latest plafonné à 100, search à 100."""
|
|
def __init__(self):
|
|
super().__init__(base_url="http://x", token="t")
|
|
self.appels = []
|
|
self.tous = [mail(i, f"2026-11-{1 + i // 10:02d}", f"{(i % 10) + 8:02d}:00:00") for i in range(300)]
|
|
self.tous.append(mail(999, "2026-11-05", "09:00:00", ["sans_objet"]))
|
|
self.tous[-1]["sujet"] = ""
|
|
|
|
def call_tool(self, nom, args=None, use_cache=True, force=False):
|
|
args = args or {}
|
|
self.appels.append((nom, dict(args)))
|
|
n = min(args.get("n", 10), 100)
|
|
if nom == "mirror_mail_latest":
|
|
return {"donnees": sorted(self.tous, key=lambda m: m["date_reception"], reverse=True)[:n]}
|
|
if nom == "mirror_mail_search":
|
|
d, f = args["since"], args["until"]
|
|
r = [m for m in self.tous if d <= m["date_reception"][:10] < f and m["sujet"]]
|
|
return {"donnees": sorted(r, key=lambda m: m["date_reception"], reverse=True)[:n]}
|
|
if nom == "mirror_mail_by_tag":
|
|
d, f = args["periode"].split("..")
|
|
r = [m for m in self.tous if d <= m["date_reception"][:10] <= f and args["etiquette"] in m["etiquettes"]]
|
|
return {"donnees": r[:n]}
|
|
return {"donnees": []}
|
|
|
|
|
|
def test_periode_courte_sans_recherche():
|
|
c = Faux()
|
|
r = c.get_mails_since("2026-11-26")
|
|
assert len(r) == 50 or len(r) > 0
|
|
assert all(a[0] == "mirror_mail_latest" for a in c.appels)
|
|
|
|
|
|
def test_periode_longue_complete():
|
|
c = Faux()
|
|
r = c.get_mails_since("2026-11-01", max_items=800)
|
|
ids = {m["message_id"] for m in r}
|
|
assert len(ids) == len(r) == 301
|
|
assert "m999" in ids # mail sans objet récupéré par l'étiquette
|
|
assert [m["date"] for m in r] == sorted((m["date"] for m in r), reverse=True)
|
|
|
|
|
|
def test_fenetre_pleine_coupee_en_deux():
|
|
c = Faux()
|
|
c.mails_fenetre("2026-11-01", "2026-11-30")
|
|
recherches = [a for a in c.appels if a[0] == "mirror_mail_search"]
|
|
assert len(recherches) > 1
|
|
assert all(a[1]["n"] == 100 for a in recherches)
|
|
|
|
|
|
def test_cache_fenetre():
|
|
c = Faux()
|
|
c.mails_fenetre("2026-11-01", "2026-11-12")
|
|
n1 = len(c.appels)
|
|
c.mails_fenetre("2026-11-01", "2026-11-12")
|
|
assert len(c.appels) == n1
|
|
|
|
|
|
def test_echec_recherche_garde_les_100_derniers():
|
|
c = Faux()
|
|
base = c.call_tool
|
|
def casse(nom, args=None, use_cache=True, force=False):
|
|
if nom == "mirror_mail_search":
|
|
raise ConnectionError("miroir")
|
|
return base(nom, args, use_cache, force)
|
|
c.call_tool = casse
|
|
r = c.get_mails_since("2026-11-01", max_items=800)
|
|
assert len(r) == 100
|
|
|
|
|
|
def test_jour_ancien():
|
|
c = Faux()
|
|
r = c.get_mails_for_day("2026-11-03", max_items=100)
|
|
assert len(r) == 10 and all("2026-11-03" in m["date"] for m in r)
|