#!/usr/bin/env python3 """Tests für Second Brain Kern-Module.""" import sys import os import tempfile sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) try: from src.engram import Engram, Grounding, Correctness from src.store import EngramStore from src.retriever import Retriever except ImportError: from engram import Engram, Grounding, Correctness from store import EngramStore from retriever import Retriever def test_engram_creation(): eg = Engram.create("Test content", source="test", tags=["a", "b"]) assert eg.content == "Test content" assert eg.metadata["source"] == "test" assert "a" in eg.metadata["tags"] assert eg.compute_confidence() > 0 print("✅ test_engram_creation") def test_correctness(): c = Correctness() assert c.score() == 0.5 c.confirm("user") assert c.score() == 1.0 c.reject("user") assert c.score() == 0.5 print("✅ test_correctness") def test_store_crud(): with tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) as f: db_path = f.name store = EngramStore(db_path) eg = Engram.create("Store test", tags=["store"]) store.save(eg) loaded = store.get(str(eg.id)) assert loaded is not None assert loaded.content == "Store test" assert store.count() == 1 store.delete(str(eg.id)) assert store.count() == 0 store.close() os.unlink(db_path) print("✅ test_store_crud") def test_search(): with tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) as f: db_path = f.name store = EngramStore(db_path) store.save(Engram.create("Python ist eine Programmiersprache", tags=["coding"])) store.save(Engram.create("SQLite ist eine Datenbank", tags=["db"])) store.save(Engram.create("JavaScript ist eine Sprache", tags=["coding"])) ret = Retriever(store) results = ret.retrieve("Programmiersprache Datenbank") assert len(results) >= 2 results = ret.retrieve("nichtsexistenterbegriff12345") assert len(results) == 0 store.close() os.unlink(db_path) print("✅ test_search") def test_links(): eg1 = Engram.create("Parent") eg2 = Engram.create("Child") eg1.add_link(eg2) assert eg2.id in eg1.links assert eg1.id in eg2.links print("✅ test_links") def test_grounding(): eg = Engram.create("Test", grounding=Grounding.VERIFIED) assert eg.metadata["grounding"] == 4 conf = eg.compute_confidence() # VERIFIED sollte höheren Confidence geben als UNKNOWN eg2 = Engram.create("Test2", grounding=Grounding.UNKNOWN) assert eg.compute_confidence() > eg2.compute_confidence() print("✅ test_grounding") if __name__ == "__main__": test_engram_creation() test_correctness() test_store_crud() test_search() test_links() test_grounding() print("\n🎉 Alle 6 Tests bestanden!")