"""Tests hors-jeu du RAG : python3 -m unittest test_rag_minecraft (depuis le dossier connaissances/)""" import re import unittest from pathlib import Path import rag_minecraft as rag ICI = Path(__file__).parent BASE = rag.Base.charger(ICI / "fiches") # (question, fiche attendue en tête) ATTENDUS = [ ("comment je me téléporte chez un ami", "tpa"), ("combien de homes je peux avoir ?", "homes"), ("comment protéger ma maison", "claims"), ("c'est quoi la touche vocal", "vocal"), ("comment je donne accès à mon claim à un pote, /friend", "amis"), ("j'ai perdu mes affaires en mourant", "mort-gravestone"), ("comment revenir au spawn", "spawn"), ("c'est quoi /rtp", "rtp"), ("quelles sont les règles du serveur ?", "regles"), ("quels mods y a-t-il ?", "mods-liste"), ("est-ce qu'il y a un /back ?", "inexistant"), ] HORS_SUJET = ["quelle est la capitale de la France ?", "raconte-moi une blague", "comment fabriquer une pioche en diamant", "combien font 2 + 2"] class TestRag(unittest.TestCase): def test_attendus(self): for q, attendu in ATTENDUS: with self.subTest(q=q): r = BASE.rechercher(q) self.assertTrue(r, f"aucune fiche pour {q!r}") self.assertEqual(r[0].id, attendu, [f.id for f in r]) self.assertLessEqual(len(r), 4) def test_hors_sujet(self): for q in HORS_SUJET: with self.subTest(q=q): self.assertEqual(BASE.rechercher(q), []) def test_limites_reelles(self): homes = next(f for f in BASE.fiches if f.id == "homes").corps claims = next(f for f in BASE.fiches if f.id == "claims").corps self.assertIn("3 au maximum", homes) self.assertIn("25 au maximum", claims) def test_aucun_secret(self): for f in BASE.fiches: texte = f.titre + " " + f.corps + " " + " ".join(f.mots_cles) self.assertIsNone(re.search(r"\b\d{1,3}(\.\d{1,3}){3}\b", texte), f.id) # IPv4 self.assertIsNone(re.search(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}", texte), f.id) # UUID self.assertIsNone(re.search(r"(?i)mot de passe|password|token|secret", texte.replace( "ni de mot de passe", "")), f.id) def test_brouillon_marque(self): regles = next(f for f in BASE.fiches if f.id == "regles") self.assertIn("BROUILLON", rag.bloc_contexte([regles])) self.assertIn("pas encore validées", regles.corps) if __name__ == "__main__": unittest.main()