Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XGaEeWkqZYNP6Wn3mGQsrL
62 lines
2.9 KiB
Python
62 lines
2.9 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app, mirror_client, _dossier_court
|
|
|
|
STATIC = Path(__file__).resolve().parent.parent / "app" / "static"
|
|
|
|
|
|
def brut(i, jour, sujet="Offre", dossier="Boîte de réception", pj=False):
|
|
return {"message_id": f"m{i}", "date_reception": f"2026-11-{jour:02d}T09:00:00+01:00", "expediteur": "Ali <ali@exemple.tn>",
|
|
"sujet": sujet, "dossier": dossier, "has_attachments": pj, "etiquettes": ["sfax"]}
|
|
|
|
|
|
def test_recherche_exige_le_jeton():
|
|
assert TestClient(app).get("/api/v1/recherche?q=offre").status_code == 401
|
|
|
|
|
|
@pytest.mark.parametrize("q", ["", "a", " a "])
|
|
def test_recherche_trop_courte(client, q):
|
|
assert client.get("/api/v1/recherche", params={"q": q}).status_code == 422
|
|
|
|
|
|
def test_recherche_resultats_tries_par_date(client, monkeypatch):
|
|
vus = {}
|
|
def faux(q, since=None, until=None, n=100):
|
|
vus.update(q=q, since=since, until=until, n=n)
|
|
return [brut(1, 10), brut(2, 15, pj=True), brut(3, 12, dossier="Boîte de réception-00 2026-🌍 DR Zone Sud-📍 Kebili")]
|
|
monkeypatch.setattr(mirror_client, "rechercher", faux)
|
|
j = client.get("/api/v1/recherche", params={"q": " offre\x00 prix ", "depuis": "2026-11-01", "jusqu_a": "2026-11-30"}).json()
|
|
assert vus == {"q": "offre prix", "since": "2026-11-01", "until": "2026-12-01", "n": 100}
|
|
assert [r["message_id"] for r in j["resultats"]] == ["m2", "m3", "m1"]
|
|
assert j["resultats"][0]["pj"] is True and j["resultats"][1]["dossier"] == "DR Zone Sud-📍 Kebili"
|
|
assert j["total"] == 3 and j["plafond"] is False
|
|
|
|
|
|
def test_recherche_plafond(client, monkeypatch):
|
|
monkeypatch.setattr(mirror_client, "rechercher", lambda q, since=None, until=None, n=100: [brut(i, 10) for i in range(5)])
|
|
assert client.get("/api/v1/recherche?q=offre&n=5").json()["plafond"] is True
|
|
|
|
|
|
def test_recherche_dates_invalides_et_miroir_indisponible(client, monkeypatch):
|
|
assert client.get("/api/v1/recherche?q=offre&depuis=hier").status_code == 422
|
|
assert client.get("/api/v1/recherche?q=offre&jusqu_a=2026-13-45").status_code == 422
|
|
def casse(*a, **k): raise ConnectionError("x")
|
|
monkeypatch.setattr(mirror_client, "rechercher", casse)
|
|
assert client.get("/api/v1/recherche?q=offre").status_code == 502
|
|
|
|
|
|
def test_dossier_court():
|
|
assert _dossier_court("Boîte de réception") == "Boîte de réception"
|
|
assert _dossier_court("Boîte de réception-00 2026-🌍 DR Zone Sud-📍 Kebili") == "DR Zone Sud-📍 Kebili"
|
|
assert _dossier_court("Messages envoyés") == "Messages envoyés"
|
|
assert _dossier_court("Boîte de réception-00 2026-📎FB") == "FB"
|
|
|
|
|
|
def test_interface_recherche():
|
|
js = (STATIC / "app.js").read_text(encoding="utf-8")
|
|
assert "vueRecherche" in js and "lancerRecherche" in js and "innerHTML" not in js
|
|
assert "['recherche', 'Recherche', true]" in js and "['pj'," not in js
|