import base64 import pytest from app import analyse as a from app.classifier import manual_classify_and_propose pytestmark = pytest.mark.regles_off def _ti(prefix: bytes, suffix: bytes = b"") -> str: return base64.b64encode(prefix.ljust(22, b"\0") + suffix).decode() def test_cle_fil_commune_malgre_suffixe_different(): assert a.cle_fil(_ti(b"abc", b"12345")) == a.cle_fil(_ti(b"abc", b"zzzzzz")) assert a.cle_fil(_ti(b"abc")) != a.cle_fil(_ti(b"abd")) def test_cle_fil_illisible(): assert a.cle_fil("") is None and a.cle_fil("!!!") is None def _fiche(cat="Tunis", resume="R."): return {"categorie": cat, "confiance": "haute", "resume": resume, "course": None, "demande": {"reelle": False, "action": "", "echeance": ""}} def test_contexte_fil_reprend_le_choix_de_nabil(temp_db): fil = a.cle_fil(_ti(b"fil1")) a.enregistrer("m1", _fiche("Tunis", "Course demandee pour lundi."), "Course Tunis", "x", "2026-11-18T10:00:00", temp_db, fil) manual_classify_and_propose("m1", "Tunis", {"expediteur": "x", "objet": "Course Tunis"}, db_path=temp_db) a.enregistrer("m2", _fiche("Sans objet"), "Re: autre objet", "x", "2026-11-19T10:00:00", temp_db, fil) ctx = a.contexte_fil(fil, "m2", temp_db) assert "categorie choisie par Nabil : Tunis" in ctx and "Course demandee pour lundi." in ctx assert a.contexte_fil(fil, "m1", temp_db).count("\n") == 0 assert a.contexte_fil(None, "m2", temp_db) == "" def test_prompt_mentionne_le_contexte(): m = a.construire_messages("o", "e", "corps", ["Tunis"], "- ctx") assert "Contexte du fil" in m[1]["content"] and "conversation" in m[0]["content"] assert "Contexte du fil" not in a.construire_messages("o", "e", "corps", ["Tunis"])[1]["content"]