Base de connaissances : import idempotent des extraits, sans suppression
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d75fb6172
commit
4d8d61cb71
3 files changed
+236
No files matched your search
@@ -0,0 +1,23 @@
|
||||
"""Le paquet base_connaissances ne parle à LibreDesk que par les routes des extraits (spec §3, plan Global Constraints)."""
|
||||
import re
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
PAQUET = Path(__file__).resolve().parents[1] / "base_connaissances"
|
||||
AUTORISEES = {"/api/v1/ai/snippets", "/api/v1/ai/snippets/{id}"} # la route PUT s'écrit f"/api/v1/ai/snippets/{id}"
|
||||
|
||||
|
||||
class TestGardeFouBase(unittest.TestCase):
|
||||
def test_seules_les_routes_des_extraits(self):
|
||||
for f in PAQUET.glob("*.py"):
|
||||
texte = f.read_text()
|
||||
self.assertEqual(set(re.findall(r"/api/v1/[a-z0-9_/{}-]+", texte)) - AUTORISEES, set(), f.name)
|
||||
for interdit in ("/messages", "/conversations", "DELETE", "automations", "assistants"):
|
||||
self.assertNotIn(interdit, texte, f"{interdit} dans {f.name}")
|
||||
|
||||
def test_le_paquet_a_bien_un_client(self): # sans importer.py, le test ci-dessus passerait à vide
|
||||
self.assertTrue((PAQUET / "importer.py").exists())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,84 @@
|
||||
import unittest
|
||||
|
||||
from base_connaissances.decoupage import Extrait
|
||||
from base_connaissances.importer import Extraits, html, synchroniser
|
||||
from faux_libredesk import FauxLibreDesk
|
||||
|
||||
LISTE = ("GET", "/api/v1/ai/snippets")
|
||||
E1 = Extrait("Which formats?", "CSV < 2 Mo.\n\n- csv\n- json", "AIMsight FAQ, rév. 1.5")
|
||||
E2 = Extrait("Which timezone?", "UTC.", "AIMsight FAQ, rév. 1.5")
|
||||
|
||||
|
||||
def snippet(id_, titre, contenu, source="manual"):
|
||||
return {"id": id_, "title": titre, "content": contenu, "enabled": True, "source": source}
|
||||
|
||||
|
||||
class TestImporter(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.faux = FauxLibreDesk()
|
||||
self.addCleanup(self.faux.arreter)
|
||||
|
||||
def api(self, ecrire=True):
|
||||
return Extraits(self.faux.url, "k", "s", ecrire=ecrire)
|
||||
|
||||
def test_cree_les_absents(self):
|
||||
self.faux.reponses[LISTE] = []
|
||||
b = synchroniser(self.api(), [E1, E2])
|
||||
self.assertEqual(b.crees, [E1.titre, E2.titre])
|
||||
self.assertEqual([(m, c["title"]) for m, _, c in self.faux.ecritures()], [("POST", E1.titre), ("POST", E2.titre)])
|
||||
self.assertEqual(self.faux.ecritures()[0][2], {"title": E1.titre, "content": html(E1), "enabled": True})
|
||||
|
||||
def test_rejouer_ne_cree_pas_de_doublon(self):
|
||||
self.faux.reponses[LISTE] = [snippet(7, E1.titre, html(E1))]
|
||||
self.assertEqual(synchroniser(self.api(), [E1]).inchanges, [E1.titre])
|
||||
self.assertEqual(self.faux.ecritures(), [])
|
||||
|
||||
def test_html_reformate_reste_inchange(self):
|
||||
reformate = html(E1).replace("><", ">\n<").replace("<", "<")
|
||||
self.faux.reponses[LISTE] = [snippet(7, E1.titre, reformate)]
|
||||
self.assertEqual(synchroniser(self.api(), [E1]).inchanges, [E1.titre])
|
||||
|
||||
def test_contenu_change_met_a_jour(self):
|
||||
self.faux.reponses[LISTE] = [snippet(7, E1.titre, "<p>ancien</p>")]
|
||||
b = synchroniser(self.api(), [E1])
|
||||
self.assertEqual(b.modifies, [E1.titre])
|
||||
self.assertEqual(self.faux.ecritures()[0][:2], ("PUT", "/api/v1/ai/snippets/7"))
|
||||
|
||||
def test_extrait_inconnu_signale_pas_supprime(self):
|
||||
self.faux.reponses[LISTE] = [snippet(9, "Ancien titre", "x")]
|
||||
b = synchroniser(self.api(), [E1])
|
||||
self.assertEqual(b.inconnus, ["Ancien titre"])
|
||||
self.assertNotIn("DELETE", [m for m, _, _ in self.faux.requetes])
|
||||
|
||||
def test_extrait_appris_ignore(self): # source « conversation » : appris par LibreDesk, pas à nous
|
||||
self.faux.reponses[LISTE] = [snippet(9, "Appris", "x", source="conversation")]
|
||||
self.assertEqual(synchroniser(self.api(), [E1]).inconnus, [])
|
||||
|
||||
def test_simulation_n_ecrit_rien(self):
|
||||
self.faux.reponses[LISTE] = [snippet(7, E2.titre, "<p>ancien</p>")]
|
||||
b = synchroniser(self.api(ecrire=False), [E1, E2])
|
||||
self.assertEqual((b.crees, b.modifies), ([E1.titre], [E2.titre]))
|
||||
self.assertEqual(self.faux.ecritures(), [])
|
||||
|
||||
def test_cle_envoyee(self):
|
||||
self.faux.reponses[LISTE] = []
|
||||
self.api().lister()
|
||||
self.assertEqual(self.faux.entetes[-1]["Authorization"], "token k:s")
|
||||
|
||||
def test_erreur_http_arrete(self):
|
||||
self.faux.reponses[LISTE] = 403
|
||||
with self.assertRaises(SystemExit):
|
||||
synchroniser(self.api(), [E1])
|
||||
|
||||
|
||||
class TestHtml(unittest.TestCase):
|
||||
def test_html_echappe_et_listes(self):
|
||||
h = html(Extrait("T", "a < b\n\n- un\n- deux", "S"))
|
||||
self.assertEqual(h, "<p>a < b</p><ul><li>un</li><li>deux</li></ul><p>Source : S</p>")
|
||||
|
||||
def test_lignes_d_un_paragraphe_gardees(self):
|
||||
self.assertEqual(html(Extrait("T", "ligne 1\nligne 2", "S")), "<p>ligne 1<br>ligne 2</p><p>Source : S</p>")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user